Skip to content

Commit b4405b3

Browse files
authored
Merge pull request #3098 from lewis6991/fix/narrow
fix: type narrowing bugs with literal fields
2 parents 0c5df12 + 6d1da00 commit b4405b3

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* `FIX` reimplement section `luals.config` in file doc.json
1010
* `FIX` incorrect file names in file doc.json
1111
* `FIX` remove extra `./` path prefix in the check report when using `--check=.`
12+
* `FIX` Narrowing of types with literal fields: [#3056](https://github.com/LuaLS/lua-language-server/issues/3056), [#3089](https://github.com/LuaLS/lua-language-server/issues/3089)
1213
* `FIX` correct lua version of `math.ult` and `math.type`
1314
* `FIX` incorrect links for `pattern` in `string` methods
1415
* `FIX` fix type annotations for bit module

script/vm/tracer.lua

+9-4
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@ local function getNodeTypesWithLiteralField(uri, source, fieldName, literal)
268268
return
269269
end
270270

271+
-- Literal must has a value
272+
if literal[1] == nil then
273+
return
274+
end
275+
271276
local tys
272277

273278
for _, c in ipairs(vm.compileNode(loc)) do
@@ -277,10 +282,10 @@ local function getNodeTypesWithLiteralField(uri, source, fieldName, literal)
277282
for _, f in ipairs(set.fields) do
278283
if f.field[1] == fieldName then
279284
for _, t in ipairs(f.extends.types) do
280-
if t[1] == literal[1] then
281-
tys = tys or {}
282-
table.insert(tys, {set.class[1], #f.extends.types > 1})
283-
break
285+
if guide.isLiteral(t) and t[1] ~= nil and t[1] == literal[1] then
286+
tys = tys or {}
287+
table.insert(tys, { set.class[1], #f.extends.types > 1 })
288+
break
284289
end
285290
end
286291
break

test/diagnostics/need-check-nil.lua

+14
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,17 @@ end
6666
6767
x()
6868
]]
69+
70+
-- #3056
71+
TEST [[
72+
---@class A
73+
---@field b string
74+
---@field c 'string'|string1'
75+
---@field d 0|1|2
76+
77+
---@type A?
78+
local a
79+
80+
if <!a!>.b == "string1" then end
81+
if <!a!>.b == "string" then end
82+
]]

test/type_inference/common.lua

+25
Original file line numberDiff line numberDiff line change
@@ -4606,6 +4606,31 @@ local a = {}
46064606
function a:func(<?x?>) end
46074607
]]
46084608

4609+
-- #3089
4610+
TEST 'fun(x: number, y: number)' [[
4611+
---@class Person
4612+
---@field age? number
4613+
---@field foo fun(x: number, y: number)
4614+
4615+
---@param person Person
4616+
local function test(person)
4617+
if person.foo ~= nil then
4618+
local <?b?> = person.foo
4619+
end
4620+
end
4621+
]]
4622+
4623+
-- #2952
4624+
TEST 'A' [[
4625+
---@class A
4626+
---@field b {[C]:D}
4627+
local A
4628+
4629+
if A.b ~= {} then
4630+
local C = <?A?>
4631+
end
4632+
]]
4633+
46094634
TEST 'A' [[
46104635
---@class A
46114636
---@field type 'a'

0 commit comments

Comments
 (0)