luau-lang / luau

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

new solver has problems with generic typing #1442

Open idk-i-reportbugs opened 3 weeks ago

idk-i-reportbugs commented 3 weeks ago

this code raises 2 errors:

local function lerp<T>(a: T, b: T, t: number): T
    if type(a) ~= type(b) or (type(a) ~= "number" and type(a) ~= "vector") then
        warn(`Unable to cast {typeof(a)} to number or vector`)
        return a
    end
    return a + (b - a) * t
end

Error 1: line 2 gives the error: Type function instance union<*blocked-203614*, nil> is uninhabited entire line was highlighted, except if and then

Error 2: normally, line 6 should detect that generic type T should either be number, Vector3 or Vector2 (because of the ifs at line 2-5) however, that doesn't happen. Instead, I get an error at line 6: Type function instance intersect<T, number> is uninhabited


Moreover, it seems like typechecking has problems with for i, v in table do, where pairs, ipairs or next is omitted Error message: Cannot iterate over a table without indexer the table:

type RigAnimationAxisTable<T> = {
    RootJoint: T,
    Neck: T,
    ["Right Shoulder"]: T,
    ["Left Shoulder"]: T,
    ["Right Hip"]: T,
    ["Left Hip"]: T,
}

local joint_names: RigAnimationAxisTable<true> = {
    RootJoint = true,
    Neck = true,
    ["Right Shoulder"] = true,
    ["Left Shoulder"] = true,
    ["Right Hip"] = true,
    ["Left Hip"] = true,
}
AshleyFlow commented 3 weeks ago

I've found a similar issue with generics... intersections?...

local function foo<T>(bar: number & T)
    assert(typeof(bar) == "number")

    typeof(bar) -- TypeError: Type function instance intersect<T & number, number> is uninhabited
end

after the assertion bar turns into intersect<T & number, number> which causes the type error