summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--changelog.md11
-rw-r--r--script/parser/guide.lua1
-rw-r--r--script/parser/newparser.lua11
-rw-r--r--script/vm/runner.lua1
-rw-r--r--test/type_inference/init.lua11
5 files changed, 35 insertions, 0 deletions
diff --git a/changelog.md b/changelog.md
index f2242e72..3b2e2dc0 100644
--- a/changelog.md
+++ b/changelog.md
@@ -8,6 +8,17 @@
if x == -- suggest `CONST.X` and `CONST.Y` here
```
+* `CHG` infer type by `error`
+ ```lua
+ ---@type integer?
+ local n
+
+ if not n then
+ error('n is nil')
+ end
+
+ print(n) -- `n` is `integer` here
+ ```
* `FIX` with clients that support LSP 3.17 (VSCode), workspace diagnostics are triggered every time when opening a file.
* `FIX` [#1204](https://github.com/sumneko/lua-language-server/issues/1204)
* `FIX` [#1208](https://github.com/sumneko/lua-language-server/issues/1208)
diff --git a/script/parser/guide.lua b/script/parser/guide.lua
index 83321bac..2894f673 100644
--- a/script/parser/guide.lua
+++ b/script/parser/guide.lua
@@ -65,6 +65,7 @@ local type = type
---@field hasGoTo? true
---@field hasReturn? true
---@field hasBreak? true
+---@field hasError? true
---@field _root parser.object
---@class guide
diff --git a/script/parser/newparser.lua b/script/parser/newparser.lua
index fa728c8b..c7e9256a 100644
--- a/script/parser/newparser.lua
+++ b/script/parser/newparser.lua
@@ -2818,6 +2818,17 @@ local function compileExpAsAction(exp)
end
if exp.type == 'call' then
+ if exp.node.special == 'error' then
+ for i = #Chunk, 1, -1 do
+ local block = Chunk[i]
+ if block.type == 'ifblock'
+ or block.type == 'elseifblock'
+ or block.type == 'else' then
+ block.hasError = true
+ break
+ end
+ end
+ end
return exp
end
diff --git a/script/vm/runner.lua b/script/vm/runner.lua
index e7851b49..5d561677 100644
--- a/script/vm/runner.lua
+++ b/script/vm/runner.lua
@@ -153,6 +153,7 @@ function mt:_lookInto(action, topNode, outNode)
local neverReturn = subBlock.hasReturn
or subBlock.hasGoTo
or subBlock.hasBreak
+ or subBlock.hasError
if not neverReturn then
blockNodes[#blockNodes+1] = blockNode
end
diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua
index 0a19082a..5ed572dd 100644
--- a/test/type_inference/init.lua
+++ b/test/type_inference/init.lua
@@ -2548,3 +2548,14 @@ while <?t?> ~= nil do
print(t)
end
]]
+
+TEST 'integer' [[
+---@type integer?
+local n
+
+if not n then
+ error('n is nil')
+end
+
+print(<?n?>)
+]]