luau icon indicating copy to clipboard operation
luau copied to clipboard

New solver: Branching function definitions produce unnecessary unions

Open ishtar112 opened this issue 9 months ago • 1 comments

--!strict
local foo = if true then
	function( x )
		return x
	end
	else
	function( x )
		return x
	end

foo() -- Type error: Cannot call of a value of the union type... We are unable to return the appropriate result type for such a call

The issue here is that foo resolves as a 'conflicting' union when all branches' types are identical. This issue of course only occurs in strict mode. It happens on both the new and old solvers.

I'm not sure if this needs to be pointed out too, but I'd rather be safe than sorry: In the above snippet where both functions are ( x: any ) -> any, foo resolves specifically to (<a>( x: a ) -> a) | (<b>( x: b ) -> b), which, I guess what foo resolves as makes sense to be upset about, but, I would like to assume that that's not how it should resolve, though I admittedly don't know enough about how the solver handles branching to be confident in my assumption.

Regardless, annotating the branch functions, even if with identical annotations, produces the same issue. The actual code that caused me to encounter this issue is something like so:

local foo = if true then
	function( x: number ): number
		return x
	end
	else
	function( x: number ): number
		return x
	end

Obviously, foo here does not resolve to (<a>...) | (<b>...), but rather (( x: number ) -> number) | (( x: number ) -> number), which I guess is a bit more 'obvious' of a problem.

ishtar112 avatar Mar 05 '25 21:03 ishtar112