fix corner case in side_effects (#4326)

fixes #4325
This commit is contained in:
Alex Lam S.L
2020-11-29 02:05:48 +00:00
committed by GitHub
parent f045e2b460
commit 9d34f8428b
5 changed files with 60 additions and 17 deletions

View File

@@ -5592,20 +5592,10 @@ merge(Compressor.prototype, {
} else if (node instanceof AST_ForIn) { } else if (node instanceof AST_ForIn) {
if (!drop_vars || !compressor.option("loops")) return; if (!drop_vars || !compressor.option("loops")) return;
if (!is_empty(node.body)) return; if (!is_empty(node.body)) return;
var sym = node.init; var sym = get_init_symbol(node);
if (sym instanceof AST_Definitions) { if (!sym) return;
sym = sym.definitions[0].name;
} else while (sym instanceof AST_PropAccess) {
sym = sym.expression.tail_node();
}
if (sym instanceof AST_Destructured) return;
var def = sym.definition(); var def = sym.definition();
if (!def) return;
if (def.id in in_use_ids) 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}"); log(sym, "Dropping unused loop variable {name}");
if (for_ins[def.id] === node) delete for_ins[def.id]; if (for_ins[def.id] === node) delete for_ins[def.id];
var body = []; var body = [];
@@ -5705,6 +5695,16 @@ merge(Compressor.prototype, {
return rhs.right; 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) { function scan_ref_scoped(node, descend, init) {
if (node instanceof AST_Assign && node.left instanceof AST_SymbolRef) { if (node instanceof AST_Assign && node.left instanceof AST_SymbolRef) {
var def = node.left.definition(); var def = node.left.definition();
@@ -5748,8 +5748,14 @@ merge(Compressor.prototype, {
} }
if (!drop_vars || !compressor.option("loops")) return; if (!drop_vars || !compressor.option("loops")) return;
if (!is_empty(node.body)) return; if (!is_empty(node.body)) return;
if (node.init instanceof AST_Destructured) return;
if (node.init.has_side_effects(compressor)) 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); node.object.walk(tw);
return true; return true;
} }
@@ -6259,6 +6265,7 @@ merge(Compressor.prototype, {
}); });
// always shallow clone to ensure stripping of negated IIFEs // always shallow clone to ensure stripping of negated IIFEs
self = self.clone(); self = self.clone();
self.expression = exp.clone();
} }
if (self instanceof AST_New) { if (self instanceof AST_New) {
var fn = exp; var fn = exp;

View File

@@ -3322,9 +3322,7 @@ issue_3506_1: {
} }
expect: { expect: {
var a = "FAIL"; var a = "FAIL";
!function(b) { a && (a = "PASS");
b && (a = "PASS");
}(a);
console.log(a); console.log(a);
} }
expect_stdout: "PASS" expect_stdout: "PASS"

View File

@@ -525,7 +525,7 @@ issue_2506: {
function f0(bar) { function f0(bar) {
(function() { (function() {
(function() { (function() {
if (false <= 0/0 & this >> 1 >= 0) if (false <= NaN & this >> 1 >= 0)
c++; c++;
})(c++); })(c++);
})(); })();

View File

@@ -1052,6 +1052,7 @@ issue_4084: {
options = { options = {
keep_fargs: "strict", keep_fargs: "strict",
loops: true, loops: true,
passes: 2,
reduce_vars: true, reduce_vars: true,
unused: true, unused: true,
} }

View File

@@ -433,3 +433,40 @@ trim_new: {
} }
expect_stdout: "PASS" 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"
}