Skip to content

Commit f827400

Browse files
committed
check type for Enum -> Other
fix #1675
1 parent decf02a commit f827400

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* `FIX` wrong diagnostics for `pcall` and `xpcall`
55
* `FIX` duplicate fields in table hover
66
* `FIX` description disapeared for overloaded function
7+
* `FIX` [#1675]
8+
9+
[#1675]: https://github.com/sumneko/lua-language-server/issues/1675
710

811
## 3.6.0
912
`2022-11-8`

script/vm/global.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,11 @@ local compilerGlobalSwitch = util.switch()
378378
source._enums[#source._enums+1] = field
379379
local subType = vm.declareGlobal('type', name .. '.' .. field.field[1], uri)
380380
subType:addSet(uri, field)
381-
field._globalNode = subType
382381
elseif field.type == 'tableindex' then
383382
source._enums[#source._enums+1] = field
384383
if field.index.type == 'string' then
385384
local subType = vm.declareGlobal('type', name .. '.' .. field.index[1], uri)
386385
subType:addSet(uri, field)
387-
field._globalNode = subType
388386
end
389387
end
390388
end

script/vm/type.lua

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ end
5050
---@param parentName string
5151
---@param child vm.node.object
5252
---@param uri uri
53+
---@param mark table
5354
---@param errs? typecheck.err[]
5455
---@return boolean?
55-
local function checkEnum(parentName, child, uri, errs)
56+
local function checkParentEnum(parentName, child, uri, mark, errs)
5657
local parentClass = vm.getGlobal('type', parentName)
5758
if not parentClass then
5859
return nil
@@ -70,7 +71,7 @@ local function checkEnum(parentName, child, uri, errs)
7071
if child.type == 'global' then
7172
---@cast child vm.global
7273
for _, enum in ipairs(enums) do
73-
if vm.isSubType(uri, child, vm.compileNode(enum)) then
74+
if vm.isSubType(uri, child, vm.compileNode(enum), mark) then
7475
return true
7576
end
7677
end
@@ -131,6 +132,35 @@ local function checkEnum(parentName, child, uri, errs)
131132
end
132133
end
133134

135+
---@param childName string
136+
---@param parent vm.node.object
137+
---@param uri uri
138+
---@param mark table
139+
---@param errs? typecheck.err[]
140+
---@return boolean?
141+
local function checkChildEnum(childName, parent , uri, mark, errs)
142+
local childClass = vm.getGlobal('type', childName)
143+
if not childClass then
144+
return nil
145+
end
146+
local enums
147+
for _, set in ipairs(childClass:getSets(uri)) do
148+
if set.type == 'doc.enum' then
149+
enums = vm.getEnums(set)
150+
break
151+
end
152+
end
153+
if not enums then
154+
return nil
155+
end
156+
for _, enum in ipairs(enums) do
157+
if not vm.isSubType(uri, vm.compileNode(enum), parent, mark ,errs) then
158+
return false
159+
end
160+
end
161+
return true
162+
end
163+
134164
---@param parent vm.node.object
135165
---@param child vm.node.object
136166
---@param mark table
@@ -389,9 +419,14 @@ function vm.isSubType(uri, child, parent, mark, errs)
389419
return true
390420
end
391421

392-
local isEnum = checkEnum(parentName, child, uri, errs)
393-
if isEnum ~= nil then
394-
return isEnum
422+
local result = checkParentEnum(parentName, child, uri, mark, errs)
423+
if result ~= nil then
424+
return result
425+
end
426+
427+
result = checkChildEnum(childName, parent, uri, mark, errs)
428+
if result ~= nil then
429+
return result
395430
end
396431

397432
if parentName == 'table' and not guide.isBasicType(childName) then

test/diagnostics/type-check.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,20 @@ local function f(x, <!y!>)
10641064
end
10651065
]]
10661066

1067+
TEST [[
1068+
---@enum Enum
1069+
local t = {
1070+
x = 1,
1071+
y = 2,
1072+
}
1073+
1074+
---@type Enum
1075+
local y
1076+
1077+
---@type integer
1078+
local x = y
1079+
]]
1080+
10671081
config.remove(nil, 'Lua.diagnostics.disable', 'unused-local')
10681082
config.remove(nil, 'Lua.diagnostics.disable', 'unused-function')
10691083
config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global')

0 commit comments

Comments
 (0)