Skip to content

Commit fac0aa2

Browse files
authoredJul 26, 2024··
fix corner case in reduce_vars (#5893)
fixes #5892
1 parent 1247576 commit fac0aa2

File tree

4 files changed

+117
-17
lines changed

4 files changed

+117
-17
lines changed
 

‎lib/compress.js

+11-16
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ Compressor.prototype.compress = function(node) {
641641
var defined_ids = Object.create(tw.defined_ids);
642642
var safe_ids = Object.create(tw.safe_ids);
643643
if (!sequential) {
644-
defined_ids.seq = {};
644+
defined_ids.seq = Object.create(null);
645645
safe_ids.seq = {};
646646
}
647647
tw.defined_ids = defined_ids;
@@ -654,18 +654,22 @@ Compressor.prototype.compress = function(node) {
654654
}
655655

656656
function access(tw, def) {
657-
tw.defined_ids[def.id] = tw.defined_ids.seq;
657+
var seq = tw.defined_ids.seq;
658+
tw.defined_ids[def.id] = seq;
659+
seq[def.id] = true;
658660
}
659661

660662
function assign(tw, def) {
661-
tw.assigned_ids[def.id] = tw.defined_ids.seq;
662-
tw.defined_ids[def.id] = false;
663+
var seq = tw.defined_ids.seq;
664+
tw.assigned_ids[def.id] = seq;
665+
seq[def.id] = false;
663666
}
664667

665668
function safe_to_access(tw, def) {
666669
var seq = tw.defined_ids.seq;
667670
var defined = tw.defined_ids[def.id];
668671
if (defined !== seq) return false;
672+
if (!defined[def.id]) return false;
669673
var assigned = tw.assigned_ids[def.id];
670674
return !assigned || assigned === seq;
671675
}
@@ -1399,22 +1403,13 @@ Compressor.prototype.compress = function(node) {
13991403
}
14001404
});
14011405
if (!first) pop(tw);
1402-
var defined_ids = tw.defined_ids;
1403-
var safe_ids = tw.safe_ids;
1404-
node.body.forEach(function(branch) {
1405-
push(tw, true);
1406-
branch.walk(tw);
1407-
if (aborts(branch)) {
1408-
tw.defined_ids = defined_ids;
1409-
tw.safe_ids = safe_ids;
1410-
}
1411-
});
1412-
tw.defined_ids = defined_ids;
1413-
tw.safe_ids = safe_ids;
1406+
walk_body(node, tw);
14141407
return true;
14151408
});
14161409
def(AST_SwitchBranch, function(tw) {
1410+
push(tw, true);
14171411
walk_body(this, tw);
1412+
pop(tw);
14181413
return true;
14191414
});
14201415
def(AST_SymbolCatch, function() {

‎test/compress/functions.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5737,8 +5737,9 @@ issue_3929: {
57375737
(function() {
57385738
switch (f) {
57395739
default:
5740+
var abc = 0;
57405741
case 0:
5741-
0..p;
5742+
abc.p;
57425743
}
57435744
console.log(typeof f);
57445745
})();

‎test/compress/reduce_vars.js

+33
Original file line numberDiff line numberDiff line change
@@ -8460,3 +8460,36 @@ issue_5872_3: {
84608460
}
84618461
expect_stdout: "PASS"
84628462
}
8463+
8464+
issue_5892: {
8465+
options = {
8466+
pure_getters: "strict",
8467+
reduce_vars: true,
8468+
side_effects: true,
8469+
}
8470+
input: {
8471+
try {
8472+
var a = 42;
8473+
a.p;
8474+
if (console)
8475+
a = null;
8476+
a.q;
8477+
console.log("FAIL");
8478+
} catch (e) {
8479+
console.log("PASS");
8480+
}
8481+
}
8482+
expect: {
8483+
try {
8484+
var a = 42;
8485+
a.p;
8486+
if (console)
8487+
a = null;
8488+
a.q;
8489+
console.log("FAIL");
8490+
} catch (e) {
8491+
console.log("PASS");
8492+
}
8493+
}
8494+
expect_stdout: "PASS"
8495+
}

‎test/compress/switches.js

+71
Original file line numberDiff line numberDiff line change
@@ -1727,3 +1727,74 @@ issue_5890: {
17271727
}
17281728
expect_stdout: "PASS"
17291729
}
1730+
1731+
issue_5892_1: {
1732+
options = {
1733+
reduce_vars: true,
1734+
unused: true,
1735+
toplevel: true,
1736+
}
1737+
input: {
1738+
try {
1739+
switch (42) {
1740+
case null:
1741+
var a = "foo";
1742+
default:
1743+
a.p;
1744+
}
1745+
console.log("FAIL");
1746+
} catch (e) {
1747+
console.log("PASS");
1748+
}
1749+
}
1750+
expect: {
1751+
try {
1752+
switch (42) {
1753+
case null:
1754+
var a = "foo";
1755+
default:
1756+
a.p;
1757+
}
1758+
console.log("FAIL");
1759+
} catch (e) {
1760+
console.log("PASS");
1761+
}
1762+
}
1763+
expect_stdout: "PASS"
1764+
}
1765+
1766+
issue_5892_2: {
1767+
options = {
1768+
pure_getters: "strict",
1769+
reduce_vars: true,
1770+
side_effects: true,
1771+
toplevel: true,
1772+
}
1773+
input: {
1774+
try {
1775+
switch (42) {
1776+
case null:
1777+
var a = "foo";
1778+
default:
1779+
a.p;
1780+
}
1781+
console.log("FAIL");
1782+
} catch (e) {
1783+
console.log("PASS");
1784+
}
1785+
}
1786+
expect: {
1787+
try {
1788+
switch (42) {
1789+
case null:
1790+
var a = "foo";
1791+
default:
1792+
a.p;
1793+
}
1794+
console.log("FAIL");
1795+
} catch (e) {
1796+
console.log("PASS");
1797+
}
1798+
}
1799+
expect_stdout: "PASS"
1800+
}

0 commit comments

Comments
 (0)
Please sign in to comment.