From 3bdd74aa30a6175f1bbf973559241f99eb563dfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 17 Oct 2022 14:54:14 +0800 Subject: fix #1637 --- changelog.md | 2 ++ meta/template/io.lua | 2 +- script/progress.lua | 2 +- script/vm/type.lua | 63 ++++++++++++++++++++++++++++------------- test/diagnostics/type-check.lua | 7 +++++ 5 files changed, 54 insertions(+), 22 deletions(-) diff --git a/changelog.md b/changelog.md index 90e535e5..9401308f 100644 --- a/changelog.md +++ b/changelog.md @@ -40,6 +40,7 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`. * `FIX` [#1599] * `FIX` [#1606] * `FIX` [#1608] +* `FIX` [#1637] [#1177]: https://github.com/sumneko/lua-language-server/issues/1177 [#1458]: https://github.com/sumneko/lua-language-server/issues/1458 @@ -53,6 +54,7 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`. [#1599]: https://github.com/sumneko/lua-language-server/issues/1599 [#1606]: https://github.com/sumneko/lua-language-server/issues/1606 [#1608]: https://github.com/sumneko/lua-language-server/issues/1608 +[#1637]: https://github.com/sumneko/lua-language-server/issues/1637 ## 3.5.6 `2022-9-16` diff --git a/meta/template/io.lua b/meta/template/io.lua index 66a77d38..d685128b 100644 --- a/meta/template/io.lua +++ b/meta/template/io.lua @@ -101,7 +101,7 @@ function io.write(...) end ---@class file* local file = {} ----@alias readmode integer +---@alias readmode integer|string ---#if VERSION >= 5.3 then ---| '"n"' # ---#DESTAIL 'readmode.n' ---| '"a"' # ---#DESTAIL 'readmode.a' diff --git a/script/progress.lua b/script/progress.lua index f1f371f5..5fffc9a3 100644 --- a/script/progress.lua +++ b/script/progress.lua @@ -145,7 +145,7 @@ function m.update() end ---创建一个进度条 ----@param uri uri +---@param uri? uri ---@param title string # 标题 ---@param delay number # 至少经过这么久之后才会显示出来 function m.create(uri, title, delay) diff --git a/script/vm/type.lua b/script/vm/type.lua index be3eff98..15504878 100644 --- a/script/vm/type.lua +++ b/script/vm/type.lua @@ -89,11 +89,27 @@ local function checkValue(parent, child) return true end +---@param name string +---@param suri uri +---@return boolean +local function isAlias(name, suri) + local global = vm.getGlobal('type', name) + if not global then + return false + end + for _, set in ipairs(global:getSets(suri)) do + if set.type == 'doc.alias' then + return true + end + end + return false +end + ---@param uri uri ---@param child vm.node|string|vm.node.object ---@param parent vm.node|string|vm.node.object ---@param mark? table ----@return boolean +---@return boolean? function vm.isSubType(uri, child, parent, mark) mark = mark or {} @@ -109,24 +125,24 @@ function vm.isSubType(uri, child, parent, mark) for n in child:eachObject() do if getNodeName(n) then hasKnownType = true - if vm.isSubType(uri, n, parent, mark) then + if vm.isSubType(uri, n, parent, mark) == true then return true end end end return not hasKnownType else - local weakNil = config.get(uri, 'Lua.type.weakNilCheck') + local weakNil = config.get(uri, 'Lua.type.weakNilCheck') for n in child:eachObject() do local nodeName = getNodeName(n) if nodeName and not (nodeName == 'nil' and weakNil) - and not vm.isSubType(uri, n, parent, mark) then + and vm.isSubType(uri, n, parent, mark) == false then return false end end if not weakNil and child:isOptional() then - if not vm.isSubType(uri, 'nil', parent, mark) then + if vm.isSubType(uri, 'nil', parent, mark) == false then return false end end @@ -134,6 +150,18 @@ function vm.isSubType(uri, child, parent, mark) end end + ---@cast child vm.node.object + local childName = getNodeName(child) + if childName == 'any' + or childName == 'unknown' then + return true + end + + if not childName + or isAlias(childName, uri) then + return nil + end + if type(parent) == 'string' then local global = vm.getGlobal('type', parent) if not global then @@ -143,7 +171,7 @@ function vm.isSubType(uri, child, parent, mark) elseif parent.type == 'vm.node' then for n in parent:eachObject() do if getNodeName(n) - and vm.isSubType(uri, child, n, mark) then + and vm.isSubType(uri, child, n, mark) == true then return true end if n.type == 'doc.generic.name' then @@ -151,27 +179,26 @@ function vm.isSubType(uri, child, parent, mark) end end if parent:isOptional() then - if vm.isSubType(uri, child, 'nil', mark) then + if vm.isSubType(uri, child, 'nil', mark) == true then return true end end return false end - ---@cast child vm.node.object ---@cast parent vm.node.object - local childName = getNodeName(child) local parentName = getNodeName(parent) - if childName == 'any' - or parentName == 'any' - or childName == 'unknown' - or parentName == 'unknown' - or not childName - or not parentName then + if parentName == 'any' + or parentName == 'unknown' then return true end + if not parentName + or isAlias(parentName, uri) then + return nil + end + if childName == parentName then if not checkValue(parent, child) then return false @@ -223,15 +250,11 @@ function vm.isSubType(uri, child, parent, mark) for _, ext in ipairs(set.extends) do if ext.type == 'doc.extends.name' and (not isBasicType or guide.isBasicType(ext[1])) - and vm.isSubType(uri, ext[1], parent, mark) then + and vm.isSubType(uri, ext[1], parent, mark) == true then return true end end end - if set.type == 'doc.alias' - or set.type == 'doc.enum' then - return true - end end end mark[childName] = nil diff --git a/test/diagnostics/type-check.lua b/test/diagnostics/type-check.lua index a938f14c..bdba855b 100644 --- a/test/diagnostics/type-check.lua +++ b/test/diagnostics/type-check.lua @@ -834,6 +834,13 @@ local function f(x) end ]] +TEST [[ +---@alias test boolean + +---@type test +local = 4 +]] + config.remove(nil, 'Lua.diagnostics.disable', 'unused-local') config.remove(nil, 'Lua.diagnostics.disable', 'unused-function') config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global') -- cgit v1.2.3