Skip to content

Commit afbe46c

Browse files
committed
infer by if not x or x.y then
1 parent e76e599 commit afbe46c

File tree

3 files changed

+65
-22
lines changed

3 files changed

+65
-22
lines changed

script/vm/node.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ vm.nodeCache = {}
1010
---@field [integer] vm.object
1111
local mt = {}
1212
mt.__index = mt
13+
mt.id = 0
1314
mt.type = 'vm.node'
1415
mt.optional = nil
1516
mt.lastInfer = nil
@@ -273,11 +274,16 @@ function vm.clearNodeCache()
273274
vm.nodeCache = {}
274275
end
275276

277+
local ID = 0
278+
276279
---@param a? vm.node | vm.object
277280
---@param b? vm.node | vm.object
278281
---@return vm.node
279282
function vm.createNode(a, b)
280-
local node = setmetatable({}, mt)
283+
ID = ID + 1
284+
local node = setmetatable({
285+
id = ID,
286+
}, mt)
281287
if a then
282288
node:merge(a)
283289
end

script/vm/runner.lua

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,35 +72,29 @@ function mt:_compileNarrowByFilter(filter, outStep, blockStep)
7272
self:_compileNarrowByFilter(filter[2], outStep, blockStep)
7373
end
7474
if filter.op.type == 'or' then
75-
local orRightStep = {
75+
local dummyStep = {
7676
type = 'load',
77-
tag = 'orRight',
77+
tag = 'dummy',
7878
copy = true,
7979
ref1 = outStep,
8080
pos = filter.start - 1,
8181
}
82-
local orLeftStep = {
82+
self.steps[#self.steps+1] = dummyStep
83+
self:_compileNarrowByFilter(filter[1], outStep, dummyStep)
84+
dummyStep = {
8385
type = 'load',
84-
tag = 'orLeft',
86+
tag = 'dummy',
8587
copy = true,
8688
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
89+
pos = filter.op.finish,
9790
}
98-
self:_compileNarrowByFilter(filter[2], orLeftStep, orRightStep)
91+
self.steps[#self.steps+1] = dummyStep
92+
self:_compileNarrowByFilter(filter[2], outStep, dummyStep)
9993
self.steps[#self.steps+1] = {
10094
type = 'load',
10195
tag = 'reset',
10296
ref1 = blockStep,
103-
pos = filter.finish
97+
pos = filter.finish,
10498
}
10599
end
106100
if filter.op.type == '=='
@@ -339,11 +333,7 @@ function mt:launch(callback)
339333
elseif step.type == 'save' then
340334
step.node = node
341335
elseif step.type == 'load' then
342-
if step.ref1 then
343-
step.node = step.ref1.node
344-
else
345-
step.node = node
346-
end
336+
step.node = node
347337
context = step
348338
elseif step.type == 'merge' then
349339
node:merge(step.ref2.node)

test/type_inference/init.lua

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,3 +1965,50 @@ local x
19651965
if not x or <?x?> then
19661966
end
19671967
]]
1968+
1969+
TEST 'integer?' [[
1970+
---@type integer?
1971+
local x
1972+
1973+
if not x or XXX then
1974+
print(<?x?>)
1975+
end
1976+
]]
1977+
1978+
TEST 'integer?' [[
1979+
---@type integer?
1980+
local x
1981+
1982+
if x or XXX then
1983+
print(<?x?>)
1984+
end
1985+
]]
1986+
1987+
TEST 'integer?' [[
1988+
---@type integer?
1989+
local x
1990+
1991+
if XXX or x then
1992+
print(<?x?>)
1993+
end
1994+
]]
1995+
1996+
TEST 'integer?' [[
1997+
---@type integer?
1998+
local x
1999+
2000+
if XXX or not x then
2001+
print(<?x?>)
2002+
end
2003+
]]
2004+
2005+
TEST 'integer' [[
2006+
---@type integer?
2007+
local x
2008+
2009+
if not x or XXX then
2010+
return
2011+
end
2012+
2013+
print(<?x?>)
2014+
]]

0 commit comments

Comments
 (0)