Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type identifying where clause doesn't work when concrete type is provided as a generic argument #920

Open
svermeulen opened this issue Jan 26, 2025 · 3 comments
Labels
good first issue Good for newcomers semantics Unexpected or unsound behaviors

Comments

@svermeulen
Copy link
Contributor

Example:

local interface IFoo
   get_type: function(self): string
end

local record Foo is IFoo where self:get_type() == "foo"
end

function Foo:get_type():string
   return "foo"
end

function Foo.new():Foo
   return setmetatable({}, { __index = Foo })
end

local function create_foo():IFoo
   return Foo.new()
end

local function process_foo<T>(foo:IFoo):T
   -- This works:
   -- return foo as T

   -- This does not:
   -- Does not compile due to errors:
   -- "cannot resolve a type for foo here"
   -- "foo (of type IFoo) can never be a T"
   assert(foo is T)
   return foo
end

local foo1 = create_foo()
local _foo2:Foo = process_foo(foo1)

assert(foo1 is Foo) -- this works fine
@hishamhm
Copy link
Member

foo is T is an odd construct, because, since T is an unconstrained type variable, there is no reasonable Lua-expansion for that expression that would produce a valid runtime assertion. It might be the case that we should detect that and provide a better error message.

Note that unlike, say, Rust, we do not monomorphize generic functions1, so the fact that process_foo is used with the concrete Foo type is not enough to direct code generation for that assert() line.


1 - that is, we do not generate a non-generic copy of the function for each concrete type the generic function is applied to.

@hishamhm hishamhm added the semantics Unexpected or unsound behaviors label Jan 28, 2025
@svermeulen
Copy link
Contributor Author

Oh of course, that makes perfect sense. It is an impossible ask. But yeah an error message would be nice.

@hishamhm
Copy link
Member

Marking this as a "good first issue": detecting that the rb type is a TypeVarType in the if node.op.op == "is" section and then reporting that is cannot be applied to type variables shouldn't be too hard!

@hishamhm hishamhm added the good first issue Good for newcomers label Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers semantics Unexpected or unsound behaviors
Projects
None yet
Development

No branches or pull requests

2 participants