teal-language / tl

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

var: X is not a X #527

Closed shviller closed 2 years ago

shviller commented 2 years ago
local record M
  type Type1 = number
  type Type2 = Type1
end

local function map<E>(arr: {E}): {E: number}
end

local arr: {M.Type2} = {}
local var: {M.Type2: number} = map(arr)

results in a rather surreal error message:

in local declaration: var: M.Type2 is not a M.Type2

To reproduce, the type that ends up "not itself" has to be nested, and it needs to be an alias for another user-defined type. That is, using M.Type1 works, moving both Type1 and Type2 out of M works, but moving only Type1 and keeping Type2 inside M does not. The type of map also matters: making it non-generic (that is, function({M.Type2}): {M.Type2: number} works, making it return an array (function({E}): {E}) also works.

hishamhm commented 2 years ago

Thank you for the report and for the detailed reproduction steps!

One more question: which Teal version was this on? Does this happen also with the latest code in master?

lenscas commented 2 years ago

Just putt he code in the teal playground and it seems to happen there

https://teal-playground.netlify.app/?c=bG9jYWwgcmVjb3JkIE0KICB0eXBlIFR5cGUxID0gbnVtYmVyCiAgdHlwZSBUeXBlMiA9IFR5cGUxCmVuZAoKbG9jYWwgZnVuY3Rpb24gbWFwPEU%2BKGFycjoge0V9KToge0U6IG51bWJlcn0KZW5kCgpsb2NhbCBhcnI6IHtNLlR5cGUyfSA9IHt9CmxvY2FsIHZhcjoge00uVHlwZTI6IG51bWJlcn0gPSBtYXAoYXJyKQo%3D

hishamhm commented 2 years ago

@lenscas hah, smart way to quickly test on master, thanks! I was on my phone and I hadn't thought of that :)

hishamhm commented 2 years ago

I believe I fixed it!