Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix corner case in reduce_vars #5893

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ Compressor.prototype.compress = function(node) {
var defined_ids = Object.create(tw.defined_ids);
var safe_ids = Object.create(tw.safe_ids);
if (!sequential) {
defined_ids.seq = {};
defined_ids.seq = Object.create(null);
safe_ids.seq = {};
}
tw.defined_ids = defined_ids;
Expand All @@ -654,18 +654,22 @@ Compressor.prototype.compress = function(node) {
}

function access(tw, def) {
tw.defined_ids[def.id] = tw.defined_ids.seq;
var seq = tw.defined_ids.seq;
tw.defined_ids[def.id] = seq;
seq[def.id] = true;
}

function assign(tw, def) {
tw.assigned_ids[def.id] = tw.defined_ids.seq;
tw.defined_ids[def.id] = false;
var seq = tw.defined_ids.seq;
tw.assigned_ids[def.id] = seq;
seq[def.id] = false;
}

function safe_to_access(tw, def) {
var seq = tw.defined_ids.seq;
var defined = tw.defined_ids[def.id];
if (defined !== seq) return false;
if (!defined[def.id]) return false;
var assigned = tw.assigned_ids[def.id];
return !assigned || assigned === seq;
}
Expand Down Expand Up @@ -1399,22 +1403,13 @@ Compressor.prototype.compress = function(node) {
}
});
if (!first) pop(tw);
var defined_ids = tw.defined_ids;
var safe_ids = tw.safe_ids;
node.body.forEach(function(branch) {
push(tw, true);
branch.walk(tw);
if (aborts(branch)) {
tw.defined_ids = defined_ids;
tw.safe_ids = safe_ids;
}
});
tw.defined_ids = defined_ids;
tw.safe_ids = safe_ids;
walk_body(node, tw);
return true;
});
def(AST_SwitchBranch, function(tw) {
push(tw, true);
walk_body(this, tw);
pop(tw);
return true;
});
def(AST_SymbolCatch, function() {
Expand Down
3 changes: 2 additions & 1 deletion test/compress/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5737,8 +5737,9 @@ issue_3929: {
(function() {
switch (f) {
default:
var abc = 0;
case 0:
0..p;
abc.p;
}
console.log(typeof f);
})();
Expand Down
33 changes: 33 additions & 0 deletions test/compress/reduce_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -8460,3 +8460,36 @@ issue_5872_3: {
}
expect_stdout: "PASS"
}

issue_5892: {
options = {
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
}
input: {
try {
var a = 42;
a.p;
if (console)
a = null;
a.q;
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect: {
try {
var a = 42;
a.p;
if (console)
a = null;
a.q;
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
}
71 changes: 71 additions & 0 deletions test/compress/switches.js
Original file line number Diff line number Diff line change
Expand Up @@ -1727,3 +1727,74 @@ issue_5890: {
}
expect_stdout: "PASS"
}

issue_5892_1: {
options = {
reduce_vars: true,
unused: true,
toplevel: true,
}
input: {
try {
switch (42) {
case null:
var a = "foo";
default:
a.p;
}
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect: {
try {
switch (42) {
case null:
var a = "foo";
default:
a.p;
}
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
}

issue_5892_2: {
options = {
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
toplevel: true,
}
input: {
try {
switch (42) {
case null:
var a = "foo";
default:
a.p;
}
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect: {
try {
switch (42) {
case null:
var a = "foo";
default:
a.p;
}
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
}