Skip to content

Commit 24abcb3

Browse files
committed
infer by goto
1 parent b204411 commit 24abcb3

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

script/parser/guide.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ local type = type
5757
---@field step parser.object
5858
---@field redundant { max: integer, passed: integer }
5959
---@field filter parser.object
60+
---@field hasGoTo? true
61+
---@field hasReturn? true
6062
---@field _root parser.object
6163

6264
---@class guide
@@ -416,6 +418,7 @@ function m.getUri(obj)
416418
return ''
417419
end
418420

421+
---@return parser.object
419422
function m.getENV(source, start)
420423
if not start then
421424
start = 1

script/parser/newparser.lua

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2963,10 +2963,7 @@ local function parseReturn()
29632963
if block.type == 'ifblock'
29642964
or block.type == 'elseifblock'
29652965
or block.type == 'else' then
2966-
if not block.returns then
2967-
block.returns = {}
2968-
end
2969-
block.returns[#block.returns+1] = rtn
2966+
block.hasReturn = true
29702967
break
29712968
end
29722969
end
@@ -3066,6 +3063,15 @@ local function parseGoTo()
30663063
break
30673064
end
30683065
end
3066+
for i = #Chunk, 1, -1 do
3067+
local chunk = Chunk[i]
3068+
if chunk.type == 'ifblock'
3069+
or chunk.type == 'elseifblock'
3070+
or chunk.type == 'elseblock' then
3071+
chunk.hasGoTo = true
3072+
break
3073+
end
3074+
end
30693075

30703076
pushActionIntoCurrentChunk(action)
30713077
return action

script/vm/runner.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ function mt:_compileBlock(block)
192192
}
193193
self.steps[#self.steps+1] = blockStep
194194
self:_compileNarrowByFilter(childBlock.filter, outStep, blockStep)
195-
if not childBlock.returns then
195+
if not childBlock.hasReturn
196+
and not childBlock.hasGoTo then
196197
finals[#finals+1] = blockStep
197198
end
198199
self.steps[#self.steps+1] = {

test/type_inference/init.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,17 @@ end
18551855
print(<?x?>)
18561856
]]
18571857

1858+
TEST 'integer' [[
1859+
---@type integer?
1860+
local x
1861+
1862+
if not x then
1863+
goto ANYWHERE
1864+
end
1865+
1866+
print(<?x?>)
1867+
]]
1868+
18581869
TEST 'integer' [=[
18591870
local x
18601871

0 commit comments

Comments
 (0)