Skip to content

Commit eca994d

Browse files
committed
resolve #1285 ignore nil in getfield
1 parent 997109e commit eca994d

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# changelog
22

33
## 3.5.0
4+
* `CHG` diagnostic: `type-check` ignores `nil` in `getfield`
45
* `CHG` diagnostic: `---@diagnostic disable: <ERR_NAME>` can suppress syntax errors
56
* `CHG` completion: `completion.callSnippet` no longer generate parameter types
67
* `CHG` hover: show `---@type {x: number, y: number}` as detail instead of `table`

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,19 @@ return function (uri, callback)
8282
valueNode = valueNode:copy():removeOptional()
8383
end
8484

85+
if value.type == 'getfield'
86+
or value.type == 'getindex' then
87+
-- 由于无法对字段进行类型收窄,
88+
-- 因此将假值移除再进行检查
89+
valueNode = valueNode:copy():setTruthy()
90+
end
91+
8592
local varNode = vm.compileNode(source)
8693
if vm.canCastType(uri, varNode, valueNode) then
8794
return
8895
end
8996

97+
-- local Cat = setmetatable({}, {__index = Animal}) 允许逆变
9098
if value.type == 'select'
9199
and value.sindex == 1
92100
and value.vararg

script/core/diagnostics/cast-local-type.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@ return function (uri, callback)
2222
return
2323
end
2424
for _, ref in ipairs(loc.ref) do
25-
if ref.type == 'setlocal' then
25+
if ref.type == 'setlocal' and ref.value then
2626
await.delay()
2727
local refNode = vm.compileNode(ref)
28+
local value = ref.value
29+
30+
if value.type == 'getfield'
31+
or value.type == 'getindex' then
32+
-- 由于无法对字段进行类型收窄,
33+
-- 因此将假值移除再进行检查
34+
refNode = refNode:copy():setTruthy()
35+
end
2836

2937
if not vm.canCastType(uri, locNode, refNode) then
3038
callback {

test/diagnostics/type-check.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,5 +664,29 @@ local a = {}
664664
local <!b!> = setmetatable({}, {__index = a})
665665
]]
666666

667+
TEST [[
668+
---@class A
669+
---@field x number?
670+
local a
671+
672+
---@class B
673+
---@field x number
674+
local b
675+
676+
b.x = a.x
677+
]]
678+
679+
TEST [[
680+
681+
---@class A
682+
---@field x number?
683+
local a
684+
685+
---@type number
686+
local t
687+
688+
t = a.x
689+
]]
690+
667691
config.remove(nil, 'Lua.diagnostics.disable', 'unused-local')
668692
config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global')

0 commit comments

Comments
 (0)