Skip to content

Commit 0ee7cf6

Browse files
committed
fix analyze bug
1 parent fdf35a4 commit 0ee7cf6

4 files changed

Lines changed: 63 additions & 8 deletions

File tree

nattlua/analyzer/operators/newindex.lua

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,19 @@ return {
8585
return true
8686
end
8787
else
88-
analyzer:Error(err)
88+
if existing.Type == "symbol" and existing:IsNil() then
89+
local contract_keyval = contract:FindKeyValWide(key)
90+
91+
if contract_keyval then
92+
local ok, reason = shared.IsSubsetOf(val, contract_keyval.val)
93+
94+
if not ok then analyzer:Error(reason) end
95+
else
96+
analyzer:Error(err)
97+
end
98+
else
99+
analyzer:Error(err)
100+
end
89101
end
90102
elseif err then
91103
analyzer:Error(err)

test/helpers/profiler.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ local function get_time_function()
5656
end
5757
end
5858

59-
local function format_error(err--[[#: number]], arg--[[#: any]])--[[#: string]]
59+
local function format_error(err--[[#: number]], arg--[[#: nil | number]])--[[#: string]]
6060
local fmt = vmdef.traceerr[err]
6161

62-
if not fmt then return "unknown error: " .. err end
62+
if not fmt then return "unknown error: " .. tostring(err) end
6363

6464
if not arg then return fmt end
6565

@@ -283,7 +283,7 @@ do
283283
func--[[#: AnyFunction]],
284284
pc--[[#: number]],
285285
code--[[#: number]],
286-
reason--[[#: number | string]]
286+
reason--[[#: number]]
287287
)
288288
local trace = self._traces[id]
289289

@@ -318,7 +318,7 @@ do
318318
end
319319
end
320320

321-
local function on_trace_flush(self)
321+
local function on_trace_flush(self--[[#: TProfile]])
322322
if self._trace_count > 0 then
323323
local x, interval = self._should_warn_abort()
324324

test/tests/language_server/editor_helper.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,4 +423,4 @@ do
423423
assert(#diagnostics_calls == 0)
424424
end
425425

426-
_G.TEST = false
426+
_G.TEST = false

test/tests/nattlua/analyzer/table.lua

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,8 +661,23 @@ analyze(
661661
}
662662
t.foo["test"] = true
663663
]],
664-
"is not a subset of nil"
664+
"true is not a subset of nil"
665665
)
666+
analyze(
667+
[[
668+
local t = {} as {
669+
foo = {foo = string}
670+
}
671+
t.bar = 1
672+
]],
673+
"has no key \"bar\""
674+
)
675+
analyze([[
676+
local t = {} as {
677+
foo = {foo = string, [string] = string | nil}
678+
}
679+
t.foo["test"] = "bar"
680+
]])
666681
analyze[[
667682
local t = {} as {[1 .. inf] = number}
668683
attest.equal(#t, _ as 1 .. inf)
@@ -1071,4 +1086,32 @@ for i, v in ipairs(x) do
10711086
x[i] = nil
10721087
end
10731088
attest.equal(x, _ as {foo = 1, bar = 2, faz = 3})
1074-
]]
1089+
]]
1090+
1091+
test("repro number is not a subset of nil", function()
1092+
analyze([=[
1093+
local type TProfile = {
1094+
_strings = {[number] = string | nil},
1095+
_string_lookup = {[string] = number | nil},
1096+
_string_count = number,
1097+
_strings_flushed = number,
1098+
}
1099+
1100+
local function intern(self, s)
1101+
self = self--[[# as TProfile]]
1102+
s = s--[[# as string | nil]]
1103+
if not s then return -1 end
1104+
1105+
local sl = self._string_lookup
1106+
local idx = sl[s]
1107+
1108+
if idx then return idx end
1109+
1110+
idx = self._string_count
1111+
self._string_count = self._string_count + 1
1112+
self._strings[idx] = s
1113+
sl[s] = idx
1114+
return idx
1115+
end
1116+
]=])
1117+
end)

0 commit comments

Comments
 (0)