Skip to content

Commit 0652fa5

Browse files
committed
infer by if not x then return end
1 parent 3f815c8 commit 0652fa5

File tree

4 files changed

+52
-19
lines changed

4 files changed

+52
-19
lines changed

.luarc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"discard-returns": "Any",
1111
"redundant-parameter": "Any",
1212
"missing-parameter": "Any",
13+
"need-check-nil": "Any",
1314
"redundant-value": "Any",
1415
"deprecated": "Any"
1516
},

script/parser/newparser.lua

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2947,13 +2947,25 @@ local function parseReturn()
29472947
end
29482948
pushActionIntoCurrentChunk(rtn)
29492949
for i = #Chunk, 1, -1 do
2950-
local func = Chunk[i]
2951-
if func.type == 'function'
2952-
or func.type == 'main' then
2953-
if not func.returns then
2954-
func.returns = {}
2950+
local block = Chunk[i]
2951+
if block.type == 'function'
2952+
or block.type == 'main' then
2953+
if not block.returns then
2954+
block.returns = {}
29552955
end
2956-
func.returns[#func.returns+1] = rtn
2956+
block.returns[#block.returns+1] = rtn
2957+
break
2958+
end
2959+
end
2960+
for i = #Chunk, 1, -1 do
2961+
local block = Chunk[i]
2962+
if block.type == 'ifblock'
2963+
or block.type == 'elseifblock'
2964+
or block.type == 'else' then
2965+
if not block.returns then
2966+
block.returns = {}
2967+
end
2968+
block.returns[#block.returns+1] = rtn
29572969
break
29582970
end
29592971
end

script/vm/runner.lua

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,28 @@ function mt:_compileBlock(block)
180180
order = 3,
181181
}
182182
self:_compileNarrowByFilter(childBlock.filter, childBlock[1].start - 1)
183-
local finalState = {
184-
type = 'save',
185-
pos = childBlock.finish,
186-
order = 1,
187-
}
188-
finals[#finals+1] = finalState
189-
self.steps[#self.steps+1] = finalState
190-
self.steps[#self.steps+1] = {
191-
type = 'load',
192-
ref1 = outState,
193-
pos = childBlock.finish,
194-
order = 2,
195-
}
183+
if childBlock.returns then
184+
self.steps[#self.steps+1] = {
185+
type = 'load',
186+
ref1 = outState,
187+
pos = childBlock.finish,
188+
order = 1,
189+
}
190+
else
191+
local finalState = {
192+
type = 'save',
193+
pos = childBlock.finish,
194+
order = 1,
195+
}
196+
finals[#finals+1] = finalState
197+
self.steps[#self.steps+1] = finalState
198+
self.steps[#self.steps+1] = {
199+
type = 'load',
200+
ref1 = outState,
201+
pos = childBlock.finish,
202+
order = 2,
203+
}
204+
end
196205
end
197206
end
198207
for i, final in ipairs(finals) do

test/type_inference/init.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,6 +1840,17 @@ local x
18401840
<?x?> = x or 1
18411841
]]
18421842

1843+
TEST 'integer' [[
1844+
---@type integer?
1845+
local x
1846+
1847+
if not x then
1848+
return
1849+
end
1850+
1851+
print(<?x?>)
1852+
]]
1853+
18431854
TEST 'integer' [=[
18441855
local x
18451856

0 commit comments

Comments
 (0)