Skip to content

Commit

Permalink
fix corner case in merge_vars (#5661)
Browse files Browse the repository at this point in the history
fixes #5660
  • Loading branch information
alexlamsl authored Sep 14, 2022
1 parent fa2511f commit e0b302d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
37 changes: 23 additions & 14 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -6560,16 +6560,19 @@ Compressor.prototype.compress = function(node) {
if (node instanceof AST_SymbolVar) {
mark(node);
} else {
references[node.definition().id] = false;
node.walk(tw);
}
} : function(node) {
var id = node.definition().id;
if (!(node instanceof AST_SymbolVar)) {
references[id] = false;
} else if (!(id in references)) {
declarations.add(id, node);
} else if (references[id]) {
references[id].push(node);
if (node instanceof AST_SymbolVar) {
var id = node.definition().id;
var refs = references[id];
if (refs) {
refs.push(node);
} else if (!(id in references)) {
declarations.add(id, node);
}
} else {
node.walk(tw);
}
}, node.name);
return true;
Expand Down Expand Up @@ -6650,8 +6653,8 @@ Compressor.prototype.compress = function(node) {
|| (head_refs.start.loop || !same_scope(def)) && !mergeable(tail_refs, head_refs)
|| compressor.option("webkit") && is_funarg(def) !== is_funarg(head.definition)
|| head.definition.const_redefs
|| !all(head_refs, function(sym) {
return sym.scope.find_variable(def.name) === def;
|| !all(head_refs.scopes, function(scope) {
return scope.find_variable(def.name) === def;
})) {
skipped.push(head);
continue;
Expand Down Expand Up @@ -6733,8 +6736,7 @@ Compressor.prototype.compress = function(node) {
var refs = references[def.id];
if (!refs) return;
if (refs.start.block !== seg.block) return references[def.id] = false;
sym.scope = find_scope(tw);
refs.push(sym);
push_ref(sym);
refs.end = seg;
if (def.id in prev) {
last[prev[def.id]] = null;
Expand All @@ -6748,8 +6750,8 @@ Compressor.prototype.compress = function(node) {
return references[def.id] = false;
} else {
var refs = declarations.get(def.id) || [];
sym.scope = find_scope(tw);
refs.push(sym);
refs.scopes = [];
push_ref(sym);
references[def.id] = refs;
if (!read) {
refs.start = seg;
Expand All @@ -6766,6 +6768,13 @@ Compressor.prototype.compress = function(node) {
index: index++,
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) {
Expand Down
35 changes: 35 additions & 0 deletions test/compress/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -2169,3 +2169,38 @@ issue_5656: {
}
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
}

0 comments on commit e0b302d

Please sign in to comment.