Skip to content

Commit 96b4e0b

Browse files
committed
resolve #1094 infer type by error
1 parent 98fe538 commit 96b4e0b

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

changelog.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88

99
if x == -- suggest `CONST.X` and `CONST.Y` here
1010
```
11+
* `CHG` infer type by `error`
12+
```lua
13+
---@type integer?
14+
local n
15+
16+
if not n then
17+
error('n is nil')
18+
end
19+
20+
print(n) -- `n` is `integer` here
21+
```
1122
* `FIX` with clients that support LSP 3.17 (VSCode), workspace diagnostics are triggered every time when opening a file.
1223
* `FIX` [#1204](https://github.com/sumneko/lua-language-server/issues/1204)
1324
* `FIX` [#1208](https://github.com/sumneko/lua-language-server/issues/1208)

script/parser/guide.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ local type = type
6565
---@field hasGoTo? true
6666
---@field hasReturn? true
6767
---@field hasBreak? true
68+
---@field hasError? true
6869
---@field _root parser.object
6970

7071
---@class guide

script/parser/newparser.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2818,6 +2818,17 @@ local function compileExpAsAction(exp)
28182818
end
28192819

28202820
if exp.type == 'call' then
2821+
if exp.node.special == 'error' then
2822+
for i = #Chunk, 1, -1 do
2823+
local block = Chunk[i]
2824+
if block.type == 'ifblock'
2825+
or block.type == 'elseifblock'
2826+
or block.type == 'else' then
2827+
block.hasError = true
2828+
break
2829+
end
2830+
end
2831+
end
28212832
return exp
28222833
end
28232834

script/vm/runner.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ function mt:_lookInto(action, topNode, outNode)
153153
local neverReturn = subBlock.hasReturn
154154
or subBlock.hasGoTo
155155
or subBlock.hasBreak
156+
or subBlock.hasError
156157
if not neverReturn then
157158
blockNodes[#blockNodes+1] = blockNode
158159
end

test/type_inference/init.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2548,3 +2548,14 @@ while <?t?> ~= nil do
25482548
print(t)
25492549
end
25502550
]]
2551+
2552+
TEST 'integer' [[
2553+
---@type integer?
2554+
local n
2555+
2556+
if not n then
2557+
error('n is nil')
2558+
end
2559+
2560+
print(<?n?>)
2561+
]]

0 commit comments

Comments
 (0)