diff options
-rw-r--r-- | changelog.md | 1 | ||||
-rw-r--r-- | script/core/diagnostics/assign-type-mismatch.lua | 16 | ||||
-rw-r--r-- | script/vm/compiler.lua | 4 | ||||
-rw-r--r-- | test/diagnostics/type-check.lua | 10 | ||||
-rw-r--r-- | test/type_inference/init.lua | 10 |
5 files changed, 31 insertions, 10 deletions
diff --git a/changelog.md b/changelog.md index b462cf58..9c35eeba 100644 --- a/changelog.md +++ b/changelog.md @@ -5,6 +5,7 @@ * `CHG` diagnostic: `---@diagnostic disable: <ERR_NAME>` can suppress syntax errors * `CHG` completion: `completion.callSnippet` no longer generate parameter types * `CHG` hover: show `---@type {x: number, y: number}` as detail instead of `table` +* `CHG` dose not infer as `nil` by `t.field = nil` * `FIX` [#1278](https://github.com/sumneko/lua-language-server/issues/1278) * `FIX` [#1288](https://github.com/sumneko/lua-language-server/issues/1288) diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index 6809760c..0cccc7ee 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -64,16 +64,20 @@ return function (uri, callback) return end end - --[[ - ---@class A - local mt - ---@type X - mt._x = nil -- don't warn this - ]] if value.type == 'nil' then + --[[ + ---@class A + local mt + ---@type X + mt._x = nil -- don't warn this + ]] if hasMarkType(source) then return end + if source.type == 'setfield' + or source.type == 'setindex' then + return + end end local valueNode = vm.compileNode(value) diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index 67ca3b41..12cf102b 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -1387,7 +1387,7 @@ local compilerSwitch = util.switch() if src.value then if bindDocs(src) then vm.setNode(source, vm.compileNode(src)) - else + elseif src.value.type ~= 'nil' then vm.setNode(source, vm.compileNode(src.value)) local node = vm.getNode(src) if node then @@ -1949,7 +1949,7 @@ local function compileByGlobal(source) vm.setNode(set, globalNode, true) end for _, set in ipairs(global:getSets(uri)) do - if set.value then + if set.value and set.value.type ~= 'nil' then if not hasMarkDoc or guide.isLiteral(set.value) then globalNode:merge(vm.compileNode(set.value)) end diff --git a/test/diagnostics/type-check.lua b/test/diagnostics/type-check.lua index cc60a969..082d0b41 100644 --- a/test/diagnostics/type-check.lua +++ b/test/diagnostics/type-check.lua @@ -570,9 +570,9 @@ end ]] TEST [[ +---@class A +---@field x number? local t = {} -t.x = 1 -t.x = nil ---@return number function F() @@ -688,5 +688,11 @@ local t t = a.x ]] +TEST [[ +local mt = {} +mt.x = 1 +mt.x = nil +]] + config.remove(nil, 'Lua.diagnostics.disable', 'unused-local') config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global') diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua index 8e9e134f..bae14650 100644 --- a/test/type_inference/init.lua +++ b/test/type_inference/init.lua @@ -3381,3 +3381,13 @@ TEST '{ [string]: number, [true]: string, [1]: boolean, tag: integer }' [[ ---@type {[string]: number, [true]: string, [1]: boolean, tag: integer} local <?t?> ]] + +TEST 'unknown' [[ +local mt = {} +mt.<?x?> = nil +]] + +TEST 'unknown' [[ +mt = {} +mt.<?x?> = nil +]] |