fix corner case in merge_vars (#5661)

fixes #5660
This commit is contained in:
Alex Lam S.L
2022-09-14 16:36:54 +01:00
committed by GitHub
parent fa2511f71c
commit e0b302d651
2 changed files with 58 additions and 14 deletions

View File

@@ -6560,16 +6560,19 @@ Compressor.prototype.compress = function(node) {
if (node instanceof AST_SymbolVar) { if (node instanceof AST_SymbolVar) {
mark(node); mark(node);
} else { } else {
references[node.definition().id] = false; node.walk(tw);
} }
} : function(node) { } : function(node) {
var id = node.definition().id; if (node instanceof AST_SymbolVar) {
if (!(node instanceof AST_SymbolVar)) { var id = node.definition().id;
references[id] = false; var refs = references[id];
} else if (!(id in references)) { if (refs) {
declarations.add(id, node); refs.push(node);
} else if (references[id]) { } else if (!(id in references)) {
references[id].push(node); declarations.add(id, node);
}
} else {
node.walk(tw);
} }
}, node.name); }, node.name);
return true; return true;
@@ -6650,8 +6653,8 @@ Compressor.prototype.compress = function(node) {
|| (head_refs.start.loop || !same_scope(def)) && !mergeable(tail_refs, head_refs) || (head_refs.start.loop || !same_scope(def)) && !mergeable(tail_refs, head_refs)
|| compressor.option("webkit") && is_funarg(def) !== is_funarg(head.definition) || compressor.option("webkit") && is_funarg(def) !== is_funarg(head.definition)
|| head.definition.const_redefs || head.definition.const_redefs
|| !all(head_refs, function(sym) { || !all(head_refs.scopes, function(scope) {
return sym.scope.find_variable(def.name) === def; return scope.find_variable(def.name) === def;
})) { })) {
skipped.push(head); skipped.push(head);
continue; continue;
@@ -6733,8 +6736,7 @@ Compressor.prototype.compress = function(node) {
var refs = references[def.id]; var refs = references[def.id];
if (!refs) return; if (!refs) return;
if (refs.start.block !== seg.block) return references[def.id] = false; if (refs.start.block !== seg.block) return references[def.id] = false;
sym.scope = find_scope(tw); push_ref(sym);
refs.push(sym);
refs.end = seg; refs.end = seg;
if (def.id in prev) { if (def.id in prev) {
last[prev[def.id]] = null; last[prev[def.id]] = null;
@@ -6748,8 +6750,8 @@ Compressor.prototype.compress = function(node) {
return references[def.id] = false; return references[def.id] = false;
} else { } else {
var refs = declarations.get(def.id) || []; var refs = declarations.get(def.id) || [];
sym.scope = find_scope(tw); refs.scopes = [];
refs.push(sym); push_ref(sym);
references[def.id] = refs; references[def.id] = refs;
if (!read) { if (!read) {
refs.start = seg; refs.start = seg;
@@ -6766,6 +6768,13 @@ Compressor.prototype.compress = function(node) {
index: index++, index: index++,
definition: def, definition: def,
}); });
function push_ref(sym) {
refs.push(sym);
push_uniq(refs.scopes, sym.scope);
var scope = find_scope(tw);
if (scope !== sym.scope) push_uniq(refs.scopes, scope);
}
} }
function insert(target) { function insert(target) {

View File

@@ -2169,3 +2169,38 @@ issue_5656: {
} }
expect_stdout: true expect_stdout: true
} }
issue_5660: {
options = {
merge_vars: true,
side_effects: true,
}
input: {
function f() {
try {
a;
var b;
return b;
} catch (e) {
var a = "FAIL";
const b = null;
return a;
}
}
console.log(f());
}
expect: {
function f() {
try {
var b;
return b;
} catch (e) {
var a = "FAIL";
const b = null;
return a;
}
}
console.log(f());
}
expect_stdout: true
}