luau-lang / luau

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

Solver fails in case of narrowing known type against truthy values #1424

Open OverHash opened 1 day ago

OverHash commented 1 day ago

Using the new type solver fails on degenerate cases when type checking

For example, in Roblox, the following code causes a type error:

--!strict

local function foo(player: Player)
    if player and player.Character and player.Character:FindFirstChild("Humanoid") then
        local humanoid = player.Character:FindFirstChildWhichIsA("Humanoid")
        assert(humanoid, `Failed to find {player.Character:GetFullName()}.Humanoid`)

        if player.Character:HasTag("someTag") then
            player.Character:FindFirstChild("RagdollConstraints"):Destroy()
        end
    end
end

Hovering over player inside the innermost if statement indicates the type is inferred as

A type error of
refine<never, {
Character: *blocked-5951724*
}>
Cannot call a value of type 'blocked-5951699'

refine<never, {
    Character: *blocked-5951724*
}>
Cannot call a value of type 'blocked-5951699'
OverHash commented 1 day ago

Hope the title is somewhat reasonable to describe the case, feel free to update.