Skip to content

Commit

Permalink
check type for Enum -> Other
Browse files Browse the repository at this point in the history
fix #1675
  • Loading branch information
sumneko committed Nov 8, 2022
1 parent decf02a commit f827400
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
* `FIX` wrong diagnostics for `pcall` and `xpcall`
* `FIX` duplicate fields in table hover
* `FIX` description disapeared for overloaded function
* `FIX` [#1675]

[#1675]: https://github.com/sumneko/lua-language-server/issues/1675

## 3.6.0
`2022-11-8`
Expand Down
2 changes: 0 additions & 2 deletions script/vm/global.lua
Original file line number Diff line number Diff line change
Expand Up @@ -378,13 +378,11 @@ local compilerGlobalSwitch = util.switch()
source._enums[#source._enums+1] = field
local subType = vm.declareGlobal('type', name .. '.' .. field.field[1], uri)
subType:addSet(uri, field)
field._globalNode = subType
elseif field.type == 'tableindex' then
source._enums[#source._enums+1] = field
if field.index.type == 'string' then
local subType = vm.declareGlobal('type', name .. '.' .. field.index[1], uri)
subType:addSet(uri, field)
field._globalNode = subType
end
end
end
Expand Down
45 changes: 40 additions & 5 deletions script/vm/type.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ end
---@param parentName string
---@param child vm.node.object
---@param uri uri
---@param mark table
---@param errs? typecheck.err[]
---@return boolean?
local function checkEnum(parentName, child, uri, errs)
local function checkParentEnum(parentName, child, uri, mark, errs)
local parentClass = vm.getGlobal('type', parentName)
if not parentClass then
return nil
Expand All @@ -70,7 +71,7 @@ local function checkEnum(parentName, child, uri, errs)
if child.type == 'global' then
---@cast child vm.global
for _, enum in ipairs(enums) do
if vm.isSubType(uri, child, vm.compileNode(enum)) then
if vm.isSubType(uri, child, vm.compileNode(enum), mark) then
return true
end
end
Expand Down Expand Up @@ -131,6 +132,35 @@ local function checkEnum(parentName, child, uri, errs)
end
end

---@param childName string
---@param parent vm.node.object
---@param uri uri
---@param mark table
---@param errs? typecheck.err[]
---@return boolean?
local function checkChildEnum(childName, parent , uri, mark, errs)
local childClass = vm.getGlobal('type', childName)
if not childClass then
return nil
end
local enums
for _, set in ipairs(childClass:getSets(uri)) do
if set.type == 'doc.enum' then
enums = vm.getEnums(set)
break
end
end
if not enums then
return nil
end
for _, enum in ipairs(enums) do
if not vm.isSubType(uri, vm.compileNode(enum), parent, mark ,errs) then
return false
end
end
return true
end

---@param parent vm.node.object
---@param child vm.node.object
---@param mark table
Expand Down Expand Up @@ -389,9 +419,14 @@ function vm.isSubType(uri, child, parent, mark, errs)
return true
end

local isEnum = checkEnum(parentName, child, uri, errs)
if isEnum ~= nil then
return isEnum
local result = checkParentEnum(parentName, child, uri, mark, errs)
if result ~= nil then
return result
end

result = checkChildEnum(childName, parent, uri, mark, errs)
if result ~= nil then
return result
end

if parentName == 'table' and not guide.isBasicType(childName) then
Expand Down
14 changes: 14 additions & 0 deletions test/diagnostics/type-check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,20 @@ local function f(x, <!y!>)
end
]]

TEST [[
---@enum Enum
local t = {
x = 1,
y = 2,
}
---@type Enum
local y
---@type integer
local x = y
]]

config.remove(nil, 'Lua.diagnostics.disable', 'unused-local')
config.remove(nil, 'Lua.diagnostics.disable', 'unused-function')
config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global')
Expand Down

0 comments on commit f827400

Please sign in to comment.