Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d185dfd

Browse files
fix: type narrowing bugs with literal fields
Fixes #3089 #2952 #3056
1 parent 64c7084 commit d185dfd

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

‎changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* `FIX` reimplement section `luals.config` in file doc.json
99
* `FIX` incorrect file names in file doc.json
1010
* `FIX` remove extra `./` path prefix in the check report when using `--check=.`
11+
* `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)
1112

1213
## 3.13.6
1314
`2025年2月6日`

‎script/vm/tracer.lua

Lines changed: 9 additions & 4 deletions
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) andt[1] ~=nilandt[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

Lines changed: 14 additions & 0 deletions
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

Lines changed: 25 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /