summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-11-08 17:40:52 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-11-08 17:40:52 +0800
commitf82740088014a0dcd29dcefd29ef7e019b71a772 (patch)
treebe1b4ceb7b86a353a05b0d307d9ceb1cd8de68cb
parentdecf02ad1f708f9e2eb7d50d9765b22ae21f0425 (diff)
downloadlua-language-server-f82740088014a0dcd29dcefd29ef7e019b71a772.zip
check type for `Enum -> Other`
fix #1675
-rw-r--r--changelog.md3
-rw-r--r--script/vm/global.lua2
-rw-r--r--script/vm/type.lua45
-rw-r--r--test/diagnostics/type-check.lua14
4 files changed, 57 insertions, 7 deletions
diff --git a/changelog.md b/changelog.md
index 4961bc98..43ea9274 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 6f3bcb68..1bd7576d 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 05b76fc3..57a6444f 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 2a198b82..5243ecec 100644
--- a/test/diagnostics/type-check.lua
+++ b/test/diagnostics/type-check.lua
@@ -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')