Skip to content

Commit 7184ccd

Browse files
committed
more range narrowing
1 parent b0dd053 commit 7184ccd

2 files changed

Lines changed: 37 additions & 9 deletions

File tree

nattlua/analyzer/mutation_tracking.lua

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,28 @@ return function(META)
412412

413413
local values = {}
414414

415+
if stack[#stack].truthy and stack[#stack].truthy.Type == "range" then
416+
values[1] = stack[#stack].truthy:Copy()
417+
else
418+
for _, entry in ipairs(stack) do
419+
if entry.truthy then table.insert(values, entry.truthy) end
420+
end
421+
end
422+
423+
if #values == 0 then return end
424+
425+
if #values == 1 then return values[1] end
426+
427+
return Union(values)
428+
end
429+
430+
local function collect_falsy_values(stack)
431+
if not stack then return end
432+
433+
local values = {}
434+
415435
for _, entry in ipairs(stack) do
416-
if entry.truthy then table.insert(values, entry.truthy) end
436+
if entry.falsy then table.insert(values, entry.falsy) end
417437
end
418438

419439
if #values == 0 then return end
@@ -426,13 +446,11 @@ return function(META)
426446
function META:ApplyMutationsInIf(upvalues, tables)
427447
if upvalues then
428448
for _, data in ipairs(upvalues) do
429-
if data.stack then
430-
local obj = collect_truthy_values(data.stack)
449+
local obj = collect_truthy_values(data.stack)
431450

432-
if obj then
433-
obj:SetUpvalue(data.upvalue)
434-
self:MutateUpvalue(data.upvalue, obj, true)
435-
end
451+
if obj then
452+
obj:SetUpvalue(data.upvalue)
453+
self:MutateUpvalue(data.upvalue, obj, true)
436454
end
437455
end
438456
end
@@ -470,8 +488,8 @@ return function(META)
470488
union:SetUpvalue(data.upvalue)
471489
end
472490

473-
if data.stack[#data.stack] and data.stack[#data.stack].falsy and data.stack[#data.stack].falsy.Type == "range" then
474-
self:MutateUpvalue(data.upvalue, data.stack[#data.stack].falsy:Copy(), true)
491+
if data.stack[#data.stack] and data.stack[#data.stack].falsy and data.stack[#data.stack].falsy.Type == "range" then
492+
self:MutateUpvalue(data.upvalue, collect_falsy_values(data.stack), true)
475493
else
476494
self:MutateUpvalue(data.upvalue, union, true)
477495
end

test/tests/nattlua/analyzer/number.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,13 @@ if n > 0 then
9797
if n < 10 then attest.equal(n, _ as 1 .. 9) end
9898
end
9999
]]
100+
101+
analyze[[
102+
local n = _ as number
103+
104+
if n > 1 and n < 15 then
105+
attest.equal(n, _ as 2..14)
106+
else
107+
attest.equal(n, _ as -inf..1 | 15..inf )
108+
end
109+
]]

0 commit comments

Comments
 (0)