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

Intersections of specific function types and the top function type do not normalize correctly #1401

Open
Daw588 opened this issue Sep 12, 2024 · 1 comment
Labels
bug Something isn't working new solver This issue is specific to the new solver.

Comments

@Daw588
Copy link

Daw588 commented Sep 12, 2024

I have a Main function that will be called twice, and the second time it gets called it will print 64. However the type solver throws unclear type error.

type Func = () -> number

local FuncThatReturnsNumber: Func? = nil

local function GetFuncThatReturnsNumber(): Func
	return function()
		return 64
	end
end

local function Main()
	if FuncThatReturnsNumber then
		local position = FuncThatReturnsNumber() -- TypeError: Cannot call a value of type ((() -> number)?) & (buffer | class | function | number | string | table | thread | true) Luau(1020)
		-- local position: 'a
		print(position)
	else
		FuncThatReturnsNumber = GetFuncThatReturnsNumber()
	end
end

Main() -- Prints nothing
Main() -- Prints 64

This does not happen when the if statement is not wrapped inside a function.

type Func = () -> number

local FuncThatReturnsNumber: Func? = nil

local function GetFuncThatReturnsNumber(): Func
	return function()
		return 64
	end
end

if FuncThatReturnsNumber then
	local position = FuncThatReturnsNumber() -- number
	print(position)
else
	FuncThatReturnsNumber = GetFuncThatReturnsNumber()
end
@Daw588 Daw588 added the bug Something isn't working label Sep 12, 2024
@Daw588 Daw588 changed the title New type solver throws weird type error when code is wrapped inside a function. New type solver throws weird type error when code is wrapped inside a function Sep 12, 2024
@Daw588 Daw588 changed the title New type solver throws weird type error when code is wrapped inside a function New type solver throws unclear type error when code is wrapped inside a function Sep 13, 2024
@aatxe aatxe added the new solver This issue is specific to the new solver. label Sep 13, 2024
@aatxe
Copy link
Collaborator

aatxe commented Sep 13, 2024

The problem here is that intersections of the function type (or unions including it) with individual specific function types do not correctly simplify. So, (() -> number) | nil intersected with ~(false | nil) (which is what the refinement for a truthy test does) doesn't correctly simplify to () -> number. We might be able to fix this sooner, but this will definitely be addressed by the upcoming work on integrated e-graphs (#1285, but there's follow-up integration work that is under way).

@aatxe aatxe changed the title New type solver throws unclear type error when code is wrapped inside a function Intersections of specific function types and the top function type do not normalize correctly Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working new solver This issue is specific to the new solver.
Development

No branches or pull requests

2 participants