Skip to content

Commit e76e599

Browse files
committed
infer by if not x or x then
1 parent ad626d2 commit e76e599

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

script/vm/runner.lua

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,38 @@ function mt:_compileNarrowByFilter(filter, outStep, blockStep)
7171
self:_compileNarrowByFilter(filter[1], outStep, blockStep)
7272
self:_compileNarrowByFilter(filter[2], outStep, blockStep)
7373
end
74+
if filter.op.type == 'or' then
75+
local orRightStep = {
76+
type = 'load',
77+
tag = 'orRight',
78+
copy = true,
79+
ref1 = outStep,
80+
pos = filter.start - 1,
81+
}
82+
local orLeftStep = {
83+
type = 'load',
84+
tag = 'orLeft',
85+
copy = true,
86+
ref1 = outStep,
87+
pos = filter.start - 1,
88+
}
89+
self.steps[#self.steps+1] = orRightStep
90+
self.steps[#self.steps+1] = orLeftStep
91+
self:_compileNarrowByFilter(filter[1], orRightStep, orLeftStep)
92+
self.steps[#self.steps+1] = {
93+
type = 'load',
94+
tag = 'orReset',
95+
ref1 = orRightStep,
96+
pos = filter.op.start
97+
}
98+
self:_compileNarrowByFilter(filter[2], orLeftStep, orRightStep)
99+
self.steps[#self.steps+1] = {
100+
type = 'load',
101+
tag = 'reset',
102+
ref1 = blockStep,
103+
pos = filter.finish
104+
}
105+
end
74106
if filter.op.type == '=='
75107
or filter.op.type == '~=' then
76108
local loc, exp
@@ -162,7 +194,6 @@ function mt:_compileBlock(block)
162194
type = 'load',
163195
tag = 'block',
164196
copy = true,
165-
ref = outStep,
166197
pos = childBlock.start,
167198
}
168199
self.steps[#self.steps+1] = blockStep
@@ -308,8 +339,12 @@ function mt:launch(callback)
308339
elseif step.type == 'save' then
309340
step.node = node
310341
elseif step.type == 'load' then
342+
if step.ref1 then
343+
step.node = step.ref1.node
344+
else
345+
step.node = node
346+
end
311347
context = step
312-
context.node = node
313348
elseif step.type == 'merge' then
314349
node:merge(step.ref2.node)
315350
end

test/type_inference/init.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,3 +1957,11 @@ local x
19571957
if x and <?x?> then
19581958
end
19591959
]]
1960+
1961+
TEST 'integer' [[
1962+
---@type integer?
1963+
local x
1964+
1965+
if not x or <?x?> then
1966+
end
1967+
]]

0 commit comments

Comments
 (0)