From 4b0d634a27ae188c98f839736bad1b8514952467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 10 Feb 2023 16:01:17 +0800 Subject: [PATCH] fix #1889 --- changelog.md | 2 ++ script/config/template.lua | 1 + script/core/diagnostics/missing-return.lua | 2 +- script/core/diagnostics/unreachable-code.lua | 2 +- script/parser/compile.lua | 6 ++++-- script/parser/guide.lua | 2 +- script/vm/tracer.lua | 2 +- test/type_inference/init.lua | 11 +++++++++++ 8 files changed, 22 insertions(+), 6 deletions(-) diff --git a/changelog.md b/changelog.md index 321e40b88..a80ed3a9f 100644 --- a/changelog.md +++ b/changelog.md @@ -4,9 +4,11 @@ * `CHG` completion: don't show loading process * `FIX` [#1886] * `FIX` [#1895] +* `FIX` [#1889] [#1886]: https://github.com/LuaLS/lua-language-server/issues/1886 [#1895]: https://github.com/LuaLS/lua-language-server/issues/1895 +[#1889]: https://github.com/LuaLS/lua-language-server/issues/1889 ## 3.6.10 `2023-2-7` diff --git a/script/config/template.lua b/script/config/template.lua index e93ffa080..7a4d3f1b6 100644 --- a/script/config/template.lua +++ b/script/config/template.lua @@ -210,6 +210,7 @@ local template = { 'assert', 'error', 'type', + 'os.exit', } ), ['Lua.runtime.meta'] = Type.String >> '${version} ${language} ${encoding}', diff --git a/script/core/diagnostics/missing-return.lua b/script/core/diagnostics/missing-return.lua index 7333e5e3b..2b5c90d23 100644 --- a/script/core/diagnostics/missing-return.lua +++ b/script/core/diagnostics/missing-return.lua @@ -7,7 +7,7 @@ local await = require 'await' ---@param block parser.object ---@return boolean local function hasReturn(block) - if block.hasReturn or block.hasError then + if block.hasReturn or block.hasExit then return true end if block.type == 'if' then diff --git a/script/core/diagnostics/unreachable-code.lua b/script/core/diagnostics/unreachable-code.lua index 4f0a38b72..cbffe4db4 100644 --- a/script/core/diagnostics/unreachable-code.lua +++ b/script/core/diagnostics/unreachable-code.lua @@ -23,7 +23,7 @@ end ---@param block parser.object ---@return boolean local function hasReturn(block) - if block.hasReturn or block.hasError then + if block.hasReturn or block.hasExit then return true end if block.type == 'if' then diff --git a/script/parser/compile.lua b/script/parser/compile.lua index 7cc6f36b2..917a68a47 100644 --- a/script/parser/compile.lua +++ b/script/parser/compile.lua @@ -118,6 +118,7 @@ local Specials = { ['assert'] = true, ['error'] = true, ['type'] = true, + ['os.exit'] = true, } local UnarySymbol = { @@ -2899,14 +2900,15 @@ local function compileExpAsAction(exp) end if exp.type == 'call' then - if exp.node.special == 'error' then + if exp.node.special == 'error' + or exp.node.special == 'os.exit' then for i = #Chunk, 1, -1 do local block = Chunk[i] if block.type == 'ifblock' or block.type == 'elseifblock' or block.type == 'elseblock' or block.type == 'function' then - block.hasError = true + block.hasExit = true break end end diff --git a/script/parser/guide.lua b/script/parser/guide.lua index 0b6de77d6..ec5746c13 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -72,7 +72,7 @@ local type = type ---@field hasGoTo? true ---@field hasReturn? true ---@field hasBreak? true ----@field hasError? true +---@field hasExit? true ---@field [integer] parser.object|any ---@field package _root parser.object diff --git a/script/vm/tracer.lua b/script/vm/tracer.lua index 6ce57d5c9..a13d00e13 100644 --- a/script/vm/tracer.lua +++ b/script/vm/tracer.lua @@ -369,7 +369,7 @@ local lookIntoChild = util.switch() local neverReturn = subBlock.hasReturn or subBlock.hasGoTo or subBlock.hasBreak - or subBlock.hasError + or subBlock.hasExit if neverReturn then mergedNode = true else diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua index 5a9e1796b..4e3c9d5bb 100644 --- a/test/type_inference/init.lua +++ b/test/type_inference/init.lua @@ -2620,6 +2620,17 @@ end print() ]] +TEST 'integer' [[ +---@type integer? +local n + +if not n then + os.exit() +end + +print() +]] + TEST 'table' [[ ---@type table? local n