luau-lang / luau

A fast, small, safe, gradually typed embeddable scripting language derived from Lua
https://luau.org
MIT License
4.06k stars 381 forks source link

Tables containing generic functions give type errors when unioned #972

Open funwolf7 opened 1 year ago

funwolf7 commented 1 year ago

If you union a table (whether it is an array or dictionary) containing a generic function, then it causes a type error when you attempt to convert it to anything else, even to a variable with the exact same type. Intersection works without issue.

Here is a simple repro code. I use the ? to union it with nil, but any other type causes the error. I have tested it using the latest release of luau-analyze.

type broken = {<T>(T) -> T}?
local foo: broken
local bar: broken = foo -- TypeError: Type '{(a) -> a}?' could not be converted into '{<T>(T) -> T}?'

Without the ?, this code gives no type errors.

This makes working with typechecking very difficult, as many objects I use make use of generic functions to enhance typechecking. I am unable to union any of these objects due to this bug.

vegorov-rbx commented 1 year ago

This is fixed under an experimental flag LuauInstantiateInSubtyping.

To enable it, add LUAU_FASTFLAG(LuauInstantiateInSubtyping) to your .cpp file and set FFlag::LuauInstantiateInSubtyping.value = true; once, before initializing Luau.