New Type Solver incorrectly handles recursive functions

The new type solver beta incorrectly handles recursive functions paired with local variables.

In the attached code example, you can see that function f calls itself recursively, taking advantage of uninitialized variable syntax.
The nested function call to f sees the type as nil-able although it is impossible for the value to be nil here.
The old type solver handles this correctly

--!strict

--Example recursive function that counts up to 3
local f; f = function(x:number):number
	x += 1
	
	if x < 3 then
		return f(x)--Type Error: Value of type '((number) -> number)?' could be nil
	end
	
	return x
end

print(f(1))-- > 3

Expected behavior

The nested call to function f should recognize the final type of the variable without the need of type-casting

Betas enabled:

  • New Type solver beta
  • Incremental typechecking
1 Like

Hi there, and thank you for the report! This is indeed a bug, I believe @MercuryBromine0 already provided you the workaround in the New Type Solver Beta thread, but just writing the following does work.

local function f(x: number): number
	x += 1
	
	if x < 3 then
		return f(x)--Type Error: Value of type '((number) -> number)?' could be nil
	end
	
	return x
end

We’ll nevertheless look at fixing this though!

1 Like