Skip to content

Commit 25cae7d

Browse files
committed
dose not infer as nil by t.field = nil
1 parent eca994d commit 25cae7d

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* `CHG` diagnostic: `---@diagnostic disable: <ERR_NAME>` can suppress syntax errors
66
* `CHG` completion: `completion.callSnippet` no longer generate parameter types
77
* `CHG` hover: show `---@type {x: number, y: number}` as detail instead of `table`
8+
* `CHG` dose not infer as `nil` by `t.field = nil`
89
* `FIX` [#1278](https://github.com/sumneko/lua-language-server/issues/1278)
910
* `FIX` [#1288](https://github.com/sumneko/lua-language-server/issues/1288)
1011

script/core/diagnostics/assign-type-mismatch.lua

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,20 @@ return function (uri, callback)
6464
return
6565
end
6666
end
67-
--[[
68-
---@class A
69-
local mt
70-
---@type X
71-
mt._x = nil -- don't warn this
72-
]]
7367
if value.type == 'nil' then
68+
--[[
69+
---@class A
70+
local mt
71+
---@type X
72+
mt._x = nil -- don't warn this
73+
]]
7474
if hasMarkType(source) then
7575
return
7676
end
77+
if source.type == 'setfield'
78+
or source.type == 'setindex' then
79+
return
80+
end
7781
end
7882

7983
local valueNode = vm.compileNode(value)

script/vm/compiler.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ local compilerSwitch = util.switch()
13871387
if src.value then
13881388
if bindDocs(src) then
13891389
vm.setNode(source, vm.compileNode(src))
1390-
else
1390+
elseif src.value.type ~= 'nil' then
13911391
vm.setNode(source, vm.compileNode(src.value))
13921392
local node = vm.getNode(src)
13931393
if node then
@@ -1949,7 +1949,7 @@ local function compileByGlobal(source)
19491949
vm.setNode(set, globalNode, true)
19501950
end
19511951
for _, set in ipairs(global:getSets(uri)) do
1952-
if set.value then
1952+
if set.value and set.value.type ~= 'nil' then
19531953
if not hasMarkDoc or guide.isLiteral(set.value) then
19541954
globalNode:merge(vm.compileNode(set.value))
19551955
end

test/diagnostics/type-check.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,9 @@ end
570570
]]
571571

572572
TEST [[
573+
---@class A
574+
---@field x number?
573575
local t = {}
574-
t.x = 1
575-
t.x = nil
576576
577577
---@return number
578578
function F()
@@ -688,5 +688,11 @@ local t
688688
t = a.x
689689
]]
690690

691+
TEST [[
692+
local mt = {}
693+
mt.x = 1
694+
mt.x = nil
695+
]]
696+
691697
config.remove(nil, 'Lua.diagnostics.disable', 'unused-local')
692698
config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global')

test/type_inference/init.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3381,3 +3381,13 @@ TEST '{ [string]: number, [true]: string, [1]: boolean, tag: integer }' [[
33813381
---@type {[string]: number, [true]: string, [1]: boolean, tag: integer}
33823382
local <?t?>
33833383
]]
3384+
3385+
TEST 'unknown' [[
3386+
local mt = {}
3387+
mt.<?x?> = nil
3388+
]]
3389+
3390+
TEST 'unknown' [[
3391+
mt = {}
3392+
mt.<?x?> = nil
3393+
]]

0 commit comments

Comments
 (0)