luau-lang / luau

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

New type solver incorrectly infers function return type as number? #1399

Open Daw588 opened 1 month ago

Daw588 commented 1 month ago

I expect GetValidRandomNumber to return number because the while loop will never exit unless the number is returned, but the solver infers that it returns number?.

local function IsValid(x)
    return x > 50
end

local function GetValidRandomNumber() -- number?
    while true do
        local id = math.random(100)
        if IsValid(id) then
            return id
        end
    end
end
YOUWILLDIE666 commented 1 month ago

I don't think this is a bug... It's theoretically possible that the while true loop would run indefinitely and not return anything, but the probability of that happening is equivalent to the probability of the IsValid function always returning false for an infinite sequence of independent random numbers (which is lim (n → ∞) (1/2)n = 0, or speaking words, the probability of this event occurring is infinitesimally small). So the solver's infer is right?

(or it's just Luau being Luau I don't know ¯\(ツ)\/¯)