Skip to content

Commit e5f07d0

Browse files
committed
don't check t.field = nil
1 parent 1149fe6 commit e5f07d0

File tree

5 files changed

+60
-4
lines changed

5 files changed

+60
-4
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ local checkTypes = {
1515
'tableindex'
1616
}
1717

18+
---@param source parser.object
19+
---@return boolean
20+
local function hasMarkType(source)
21+
if not source.bindDocs then
22+
return false
23+
end
24+
for _, doc in ipairs(source.bindDocs) do
25+
if doc.type == 'doc.type'
26+
or doc.type == 'doc.class' then
27+
return true
28+
end
29+
end
30+
return false
31+
end
32+
1833
---@async
1934
return function (uri, callback)
2035
local state = files.getState(uri)
@@ -35,11 +50,24 @@ return function (uri, callback)
3550
return
3651
end
3752
end
53+
--[[
54+
---@class A
55+
local mt
56+
---@type X
57+
mt._x = nil -- don't warn this
58+
]]
59+
if value.type == 'nil' then
60+
if hasMarkType(source) then
61+
return
62+
end
63+
end
64+
3865
local valueNode = vm.compileNode(value)
3966
if source.type == 'setindex' then
4067
-- boolean[1] = nil
4168
valueNode = valueNode:copy():removeOptional()
4269
end
70+
4371
local varNode = vm.compileNode(source)
4472
if vm.canCastType(uri, varNode, valueNode) then
4573
return

script/parser/guide.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ local type = type
1414
---@field locals parser.object[]
1515
---@field returns? parser.object[]
1616
---@field exps parser.object[]
17-
---@field keys parser.object[]
17+
---@field keys parser.object
1818
---@field uri uri
1919
---@field start integer
2020
---@field finish integer

script/parser/luadoc.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ Symbol <- ({} {
143143
---@field originalComment parser.object
144144
---@field as? parser.object
145145
---@field touch? integer
146+
---@field module? string
146147

147148
local function trim(str)
148149
return str:match '^%s*(%S+)%s*$'

script/vm/compiler.lua

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ local vm = require 'vm.vm'
1414
---@field func parser.object
1515

1616
-- 该函数有副作用,会给source绑定node!
17+
---@param source parser.object
18+
---@return boolean
1719
local function bindDocs(source)
20+
local docs = source.bindDocs
21+
if not docs then
22+
return false
23+
end
1824
local isParam = source.parent.type == 'funcargs'
1925
or (source.parent.type == 'in' and source.finish <= source.parent.keys.finish)
20-
local docs = source.bindDocs
2126
for i = #docs, 1, -1 do
2227
local doc = docs[i]
2328
if doc.type == 'doc.type' then
@@ -47,6 +52,9 @@ local function bindDocs(source)
4752
end
4853
if doc.type == 'doc.module' then
4954
local name = doc.module
55+
if not name then
56+
return true
57+
end
5058
local uri = rpath.findUrisByRequirePath(guide.getUri(source), name)[1]
5159
if not uri then
5260
return true
@@ -1380,9 +1388,18 @@ local compilerSwitch = util.switch()
13801388
else
13811389
---@cast key string
13821390
vm.compileByParentNode(source.node, key, false, function (src)
1383-
vm.setNode(source, vm.compileNode(src))
13841391
if src.value then
1385-
vm.setNode(source, vm.compileNode(src.value))
1392+
if bindDocs(src) then
1393+
vm.setNode(source, vm.compileNode(src))
1394+
else
1395+
vm.setNode(source, vm.compileNode(src.value))
1396+
local node = vm.getNode(src)
1397+
if node then
1398+
vm.setNode(source, node)
1399+
end
1400+
end
1401+
else
1402+
vm.setNode(source, vm.compileNode(src))
13861403
end
13871404
end)
13881405
end

test/diagnostics/type-check.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,5 +599,15 @@ function F()
599599
end
600600
]]
601601

602+
TEST [[
603+
---@class X
604+
605+
---@class A
606+
local mt = G
607+
608+
---@type X
609+
mt._x = nil
610+
]]
611+
602612
config.remove(nil, 'Lua.diagnostics.disable', 'unused-local')
603613
config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global')

0 commit comments

Comments
 (0)