teal-language / tl

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

Defining a cast function from and to arbitrary types is possible #578

Open fgaz opened 1 year ago

fgaz commented 1 year ago

This code typechecks but is unsound:

local function cast<A, B>(a: A) : B
   return a
end

local n : number = cast("not a number")

https://teal-playground.netlify.app/?c=bG9jYWwgZnVuY3Rpb24gY2FzdDxBLCBCPihhOiBBKSA6IEIKICAgcmV0dXJuIGEKZW5kCgpsb2NhbCBzIDogc3RyaW5nID0gY2FzdCgxKQ%3D%3D

Found in #577

hishamhm commented 1 year ago

I suspect this is not a coding bug, but a side-effect of the combination of unbound type variables and bivariant types. This may be actually tricky to solve.