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

Union of generic record types as types of function argument inside a record produces an internal compiler error #845

Closed
FractalU opened this issue Oct 31, 2024 · 1 comment · Fixed by #856
Labels
bug Something isn't working

Comments

@FractalU
Copy link
Contributor

An internal compiler error is being produced when an union of generic record types is a type of a function argument whose function is inside a record definition. This can be illustrated with the following example code.

local record A<T>
	a: T
end

local record B<T>
	b: T
end

local record C<T>
	f: function<T>(A<T> | B<T>)
end

When trying to run or check this code the following error message gets produced.

lua: C:\Lua\src\tl-0.24.1\/tl.lua:7304: Internal Compiler Error: error creating fresh type vari
ables
stack traceback:
        [C]: in function 'assert'
        C:\Lua\src\tl-0.24.1\/tl.lua:7304: in function <C:\Lua\src\tl-0.24.1\/tl.lua:7296>
        (...tail calls...)
        C:\Lua\src\tl-0.24.1\/tl.lua:4681: in upvalue 'recurse_type'
        C:\Lua\src\tl-0.24.1\/tl.lua:4634: in upvalue 'recurse_type'
        C:\Lua\src\tl-0.24.1\/tl.lua:4675: in upvalue 'recurse_type'
        C:\Lua\src\tl-0.24.1\/tl.lua:4914: in local 'fn'
        C:\Lua\src\tl-0.24.1\/tl.lua:4954: in upvalue 'recurse'
        C:\Lua\src\tl-0.24.1\/tl.lua:4812: in local 'fn'
        C:\Lua\src\tl-0.24.1\/tl.lua:4954: in upvalue 'recurse'
        C:\Lua\src\tl-0.24.1\/tl.lua:4747: in local 'fn'
        C:\Lua\src\tl-0.24.1\/tl.lua:4954: in function <C:\Lua\src\tl-0.24.1\/tl.lua:4929>
        (...tail calls...)
        C:\Lua\src\tl-0.24.1\/tl.lua:13311: in function 'tl.check'
        C:\Lua\src\tl-0.24.1\/tl.lua:13447: in function 'tl.check_string'
        (...tail calls...)
        C:\Lua\src\tl-0.24.1\tl:239: in upvalue 'process_module'
        C:\Lua\src\tl-0.24.1\tl:739: in field '?'
        C:\Lua\src\tl-0.24.1\tl:977: in main chunk
        [C]: in ?

When I slightly modify this example so that the function isn't inside a record and instead a standalone function then after running or checking the modified example code a proper error message gets produced. Below the modified example.

local record A<T>
	a: T
end

local record B<T>
	b: T
end

local function f<T>(a: A<T> | B<T>)
	print(a)
end

f(nil)

And the associated error message.

1 error:
union.tl:9:24: cannot discriminate a union between multiple table types: A<T> | B<T>
----------------------------------------
1 error

Note. The f(nil) line in the modified example code is to suppress a warning.

Originally, I encountered this error with trying to modify functional.d.tl. See https://github.com/wqferr/functional/blob/master/functional.d.tl. I tried to prune the Iterator<T> record by merging duplicate function definitions into one with function argument types such as {T} | IIterator<T>. I later realized that it isn't possible because teal cannot discriminate union of multiple table types at runtime. But by loading the modified functional.d.tl file, I encountered this strange internal compiler error. I managed to reduce it to this simple example code.

@hishamhm
Copy link
Member

hishamhm commented Nov 1, 2024

Thank you for the report and especially for the minimized test case!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants