diff --git a/changelog.md b/changelog.md index 4961bc980..43ea92741 100644 --- a/changelog.md +++ b/changelog.md @@ -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` diff --git a/script/vm/global.lua b/script/vm/global.lua index 6f3bcb680..1bd7576dc 100644 --- a/script/vm/global.lua +++ b/script/vm/global.lua @@ -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 diff --git a/script/vm/type.lua b/script/vm/type.lua index 05b76fc3a..57a6444f0 100644 --- a/script/vm/type.lua +++ b/script/vm/type.lua @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/test/diagnostics/type-check.lua b/test/diagnostics/type-check.lua index 2a198b82e..5243ecec5 100644 --- a/test/diagnostics/type-check.lua +++ b/test/diagnostics/type-check.lua @@ -1064,6 +1064,20 @@ local function f(x, ) 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')