From fdc10086dafa3b1fbbb686f333b0b7b8e3bb70e5 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 3 Jan 2020 19:28:47 +0800 Subject: [PATCH] fix corner case in `reduce_vars` (#3667) fixes #3666 --- lib/compress.js | 34 +++++++++++++++++++--------------- test/compress/reduce_vars.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index f3fe8a8e..fcfe95f7 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -6797,24 +6797,28 @@ merge(Compressor.prototype, { var def = self.definition(); var fixed = self.fixed_value(); var single_use = def.single_use && !(parent instanceof AST_Call && parent.is_expr_pure(compressor)); - if (single_use && fixed instanceof AST_Lambda) { - if (def.scope !== self.scope - && (!compressor.option("reduce_funcs") || def.escaped.depth == 1 || fixed.inlined)) { - single_use = false; - } else if (recursive_ref(compressor, def)) { - single_use = false; - } else if (def.scope !== self.scope || def.orig[0] instanceof AST_SymbolFunarg) { - single_use = fixed.is_constant_expression(self.scope); - if (single_use == "f") { - var scope = self.scope; - do if (scope instanceof AST_Defun || scope instanceof AST_Function) { - scope.inlined = true; - } while (scope = scope.parent_scope); + if (single_use) { + if (fixed instanceof AST_Lambda) { + if (def.scope !== self.scope + && (!compressor.option("reduce_funcs") || def.escaped.depth == 1 || fixed.inlined)) { + single_use = false; + } else if (recursive_ref(compressor, def)) { + single_use = false; + } else if (def.scope !== self.scope || def.orig[0] instanceof AST_SymbolFunarg) { + single_use = fixed.is_constant_expression(self.scope); + if (single_use == "f") { + var scope = self.scope; + do if (scope instanceof AST_Defun || scope instanceof AST_Function) { + scope.inlined = true; + } while (scope = scope.parent_scope); + } } + if (single_use) fixed.parent_scope = self.scope; + } else if (!fixed || !fixed.is_constant_expression()) { + single_use = false; } - if (single_use) fixed.parent_scope = self.scope; } - if (single_use && fixed) { + if (single_use) { def.single_use = false; fixed._squeezed = true; fixed.single_use = true; diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index 5984042f..3331732a 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -6847,3 +6847,34 @@ issue_3631_2: { } expect_stdout: "undefined" } + +issue_3666: { + options = { + collapse_vars: true, + passes: 2, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + try { + var a = "FAIL"; + } finally { + for (;!a;) + var c = a++; + var a = "PASS", b = c = "PASS"; + } + console.log(a, b); + } + expect: { + try { + var a = "FAIL"; + } finally { + for (;!a;) + a++; + var b = a = "PASS"; + } + console.log(a, b); + } + expect_stdout: "PASS PASS" +} -- 2.34.1