teal-language / tl

The compiler for Teal, a typed dialect of Lua
MIT License
2.04k stars 101 forks source link

Nested generic members with same identifier seem to collide #610

Closed Dieschdel closed 1 year ago

Dieschdel commented 1 year ago

The Issue seems to occur if multiple members of the same table share the same generic identifier and call each other.

Here is a minimal working example

local MyObj = {}

function MyObj.do_something<T>(array: {T}): {T}
    return array
end

function MyObj.test_fails<T>(array: {T}): {T}
    return MyObj.do_something(array)
end

The line return MyObj.do_something(array) will fail to build with the following error message

in return value: got {<invalid type>}, expected {T}

I can fix this, by renaming the generic identifier of test_fails to something other than T

For example:

function MyObj.test_works<R>(array: {R}): {R}
    return MyObj.do_something(array)
end

The issue does not occur when using global or local functions (i.e. when not defining them as a member of the same table).

Is this intended behaviour, or am I doing something wrong?

Dieschdel commented 1 year ago

Btw. I am using teal version 0.14.1 (via luarocks) if this is relevant

hishamhm commented 1 year ago

@SimonLeibfritz I'm happy to report that this is already fixed in the master branch (which you can access with luarocks install --dev tl)! The fixed behavior will land in the next stable release.

Dieschdel commented 1 year ago

Amazing! Thanks for the info, works like a charm. And sorry for not reading the patch notes beforehand ;).