luau
luau copied to clipboard
[New solver] accessing a table field before assignment incorrectly infers the assigned values type
--!strict
local t = {}
local foo1 = t.foo -- incorrectly typed as <T>(T) -> T; actually nil
local abc1 = t.abc -- incorrectly typed as number; actually nil
function t.foo <T>(x: T): T return x end
t.abc = 5
local foo2 = t.foo -- correctly typed as <T>(T) -> T
local abc2 = t.abc -- correctly typed as number
This is sort of a weird situation. The behavior in the old solver is actually mostly the same here, so this isn't particularly a "new solver" bug, but there is a deviation. In the old solver, t.foo does get the type <T>(T) -> T even in the assignment for foo1, but abc1 has an error type because the index operation itself fails (since t has the type {} at that point, and so looking up abc fails). The specific behavior you're desiring here is something that could in principle be achieved now with the new solver (by type stating both of them), but the only part that is a regression here is that there is no error on the t.abc access.