Skip to content

Commit 513995f

Browse files
authored
fix corner case in inline & reduce_vars (#5579)
1 parent db6fd6d commit 513995f

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

Diff for: lib/compress.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -8387,9 +8387,8 @@ Compressor.prototype.compress = function(node) {
83878387
var scopes = [ this ];
83888388
if (orig instanceof AST_SymbolDeclaration) orig.definition().references.forEach(function(ref) {
83898389
var s = ref.scope;
8390-
if (member(s, scopes)) return;
83918390
do {
8392-
push_uniq(scopes, s);
8391+
if (!push_uniq(scopes, s)) return;
83938392
s = s.parent_scope;
83948393
} while (s && s !== this);
83958394
});
@@ -12251,12 +12250,19 @@ Compressor.prototype.compress = function(node) {
1225112250
if (fixed instanceof AST_DefClass) fixed = to_class_expr(fixed);
1225212251
if (fixed instanceof AST_LambdaDefinition) fixed = to_func_expr(fixed);
1225312252
if (is_lambda(fixed)) {
12254-
var scope = self.scope.resolve();
12253+
var scopes = [];
12254+
var scope = self.scope;
12255+
do {
12256+
scopes.push(scope);
12257+
if (scope === def.scope) break;
12258+
} while (scope = scope.parent_scope);
1225512259
fixed.enclosed.forEach(function(def) {
1225612260
if (fixed.variables.has(def.name)) return;
12257-
if (scope.var_names().has(def.name)) return;
12258-
scope.enclosed.push(def);
12259-
scope.var_names().set(def.name, true);
12261+
for (var i = 0; i < scopes.length; i++) {
12262+
var scope = scopes[i];
12263+
if (!push_uniq(scope.enclosed, def)) return;
12264+
scope.var_names().set(def.name, true);
12265+
}
1226012266
});
1226112267
}
1226212268
var value;

Diff for: lib/scope.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
286286
// ensure mangling works if `catch` reuses a scope variable
287287
var redef = def.redefined();
288288
if (redef) for (var s = node.scope; s; s = s.parent_scope) {
289-
push_uniq(s.enclosed, redef);
289+
if (!push_uniq(s.enclosed, redef)) break;
290290
if (s === redef.scope) break;
291291
}
292292
} else if (node instanceof AST_SymbolConst) {
@@ -480,7 +480,7 @@ AST_Lambda.DEFMETHOD("init_vars", function(parent_scope) {
480480
AST_Symbol.DEFMETHOD("mark_enclosed", function(options) {
481481
var def = this.definition();
482482
for (var s = this.scope; s; s = s.parent_scope) {
483-
push_uniq(s.enclosed, def);
483+
if (!push_uniq(s.enclosed, def)) break;
484484
if (!options) {
485485
s._var_names = undefined;
486486
} else {

Diff for: test/compress/functions.js

+39
Original file line numberDiff line numberDiff line change
@@ -8646,3 +8646,42 @@ module_inline: {
86468646
}
86478647
expect_stdout: "true"
86488648
}
8649+
8650+
single_use_inline_collision: {
8651+
options = {
8652+
inline: true,
8653+
reduce_funcs: true,
8654+
reduce_vars: true,
8655+
unused: true,
8656+
}
8657+
input: {
8658+
var a = "PASS";
8659+
(function() {
8660+
var f = function() {
8661+
while (console.log(a));
8662+
};
8663+
(function() {
8664+
(function() {
8665+
f();
8666+
})();
8667+
(function(a) {
8668+
a || a("FAIL");
8669+
})(console.log);
8670+
})();
8671+
})();
8672+
}
8673+
expect: {
8674+
var a = "PASS";
8675+
(function() {
8676+
(function() {
8677+
while (console.log(a));
8678+
return;
8679+
})();
8680+
(function(a) {
8681+
a || a("FAIL");
8682+
})(console.log);
8683+
return;
8684+
})();
8685+
}
8686+
expect_stdout: "PASS"
8687+
}

Diff for: test/reduce.js

+5
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
160160
return expr instanceof U.AST_Spread ? expr.expression : expr;
161161
}
162162
}
163+
else if (node instanceof U.AST_Await) {
164+
node.start._permute++;
165+
CHANGED = true;
166+
return node.expression;
167+
}
163168
else if (node instanceof U.AST_Binary) {
164169
var permute = ((node.start._permute += step) * steps | 0) % 4;
165170
var expr = [

0 commit comments

Comments
 (0)