Skip to content

Commit

Permalink
fix corner case in side_effects (#4326)
Browse files Browse the repository at this point in the history
fixes #4325
  • Loading branch information
alexlamsl authored Nov 29, 2020
1 parent f045e2b commit 9d34f84
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 17 deletions.
33 changes: 20 additions & 13 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -5592,20 +5592,10 @@ merge(Compressor.prototype, {
} else if (node instanceof AST_ForIn) {
if (!drop_vars || !compressor.option("loops")) return;
if (!is_empty(node.body)) return;
var sym = node.init;
if (sym instanceof AST_Definitions) {
sym = sym.definitions[0].name;
} else while (sym instanceof AST_PropAccess) {
sym = sym.expression.tail_node();
}
if (sym instanceof AST_Destructured) return;
var sym = get_init_symbol(node);
if (!sym) return;
var def = sym.definition();
if (!def) return;
if (def.id in in_use_ids) return;
if (def.scope !== self) {
var d = self.find_variable(sym);
if ((d && d.redefined() || d) === def) return;
}
log(sym, "Dropping unused loop variable {name}");
if (for_ins[def.id] === node) delete for_ins[def.id];
var body = [];
Expand Down Expand Up @@ -5705,6 +5695,16 @@ merge(Compressor.prototype, {
return rhs.right;
}

function get_init_symbol(for_in) {
var init = for_in.init;
if (init instanceof AST_Definitions) {
init = init.definitions[0].name;
return init instanceof AST_SymbolDeclaration && init;
}
while (init instanceof AST_PropAccess) init = init.expression.tail_node();
if (init instanceof AST_SymbolRef) return init;
}

function scan_ref_scoped(node, descend, init) {
if (node instanceof AST_Assign && node.left instanceof AST_SymbolRef) {
var def = node.left.definition();
Expand Down Expand Up @@ -5748,8 +5748,14 @@ merge(Compressor.prototype, {
}
if (!drop_vars || !compressor.option("loops")) return;
if (!is_empty(node.body)) return;
if (node.init instanceof AST_Destructured) return;
if (node.init.has_side_effects(compressor)) return;
var sym = get_init_symbol(node);
if (!sym) return;
var def = sym.definition();
if (def.scope !== self) {
var d = self.find_variable(sym);
if ((d && d.redefined() || d) === def) return;
}
node.object.walk(tw);
return true;
}
Expand Down Expand Up @@ -6259,6 +6265,7 @@ merge(Compressor.prototype, {
});
// always shallow clone to ensure stripping of negated IIFEs
self = self.clone();
self.expression = exp.clone();
}
if (self instanceof AST_New) {
var fn = exp;
Expand Down
4 changes: 1 addition & 3 deletions test/compress/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3322,9 +3322,7 @@ issue_3506_1: {
}
expect: {
var a = "FAIL";
!function(b) {
b && (a = "PASS");
}(a);
a && (a = "PASS");
console.log(a);
}
expect_stdout: "PASS"
Expand Down
2 changes: 1 addition & 1 deletion test/compress/keep_fargs.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ issue_2506: {
function f0(bar) {
(function() {
(function() {
if (false <= 0/0 & this >> 1 >= 0)
if (false <= NaN & this >> 1 >= 0)
c++;
})(c++);
})();
Expand Down
1 change: 1 addition & 0 deletions test/compress/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,7 @@ issue_4084: {
options = {
keep_fargs: "strict",
loops: true,
passes: 2,
reduce_vars: true,
unused: true,
}
Expand Down
37 changes: 37 additions & 0 deletions test/compress/side_effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,40 @@ trim_new: {
}
expect_stdout: "PASS"
}

issue_4325: {
options = {
keep_fargs: "strict",
passes: 2,
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
unused: true,
}
input: {
(function f() {
(function(b, c) {
try {
c.p = 0;
} catch (e) {
console.log("PASS");
return b;
}
c;
})(f++);
})();
}
expect: {
(function() {
(function() {
try {
(void 0).p = 0;
} catch (e) {
console.log("PASS");
return;
}
})();
})();
}
expect_stdout: "PASS"
}

0 comments on commit 9d34f84

Please sign in to comment.