luau-lang / luau

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

`index<T>` where `T` is a generic type of a function reduces to never in new solver strict mode #1524

Open nothing1649 opened 1 week ago

nothing1649 commented 1 week ago

As of version 0.651, passing a value to a function where the type of the parameter is index<T> and T is a generic type fails, reducing the parameter to never. In this repro, replacing all occurrances of T in the function set with 'a' results in the parameter being inferred correctly, therefore it should work as a generic parameter as well.

--!strict
--!optimize 2
local t: {
    read a: number,
    read b: string,
    read c: buffer
} = nil::never

local function set<T>(i:T, v:index<typeof(t), T>): () end

--note: the cast to 'a' is a workaround for an unrelated issue, see #1483 
set('a'::'a', 1) --Type 'number' could not be converted into 'never'

Expected behaviour

The second parameter is inferred as the type of the value at the index of the first parameter (in this case number), matching the behaviour of when T is replaced with 'a'.