Skip to content

Commit 722465b

Browse files
authoredJul 15, 2024··
fix corner case in inline (#5886)
fixes #5884
1 parent b6b0658 commit 722465b

File tree

4 files changed

+124
-5
lines changed

4 files changed

+124
-5
lines changed
 

‎lib/compress.js

+2
Original file line numberDiff line numberDiff line change
@@ -1653,6 +1653,7 @@ Compressor.prototype.compress = function(node) {
16531653
function reset_flags(node) {
16541654
node._squeezed = false;
16551655
node._optimized = false;
1656+
node.single_use = false;
16561657
if (node instanceof AST_BlockScope) node._var_names = undefined;
16571658
if (node instanceof AST_SymbolRef) node.fixed = undefined;
16581659
}
@@ -14178,6 +14179,7 @@ Compressor.prototype.compress = function(node) {
1417814179
var fn = call.expression;
1417914180
if (!(fn instanceof AST_LambdaExpression)) return;
1418014181
if (fn.name) return;
14182+
if (fn.single_use) return;
1418114183
if (fn.uses_arguments) return;
1418214184
if (fn.pinned()) return;
1418314185
if (is_generator(fn)) return;

‎test/compress/functions.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -8706,10 +8706,9 @@ single_use_inline_collision: {
87068706
expect: {
87078707
var a = "PASS";
87088708
(function() {
8709-
(function() {
8709+
void function() {
87108710
while (console.log(a));
8711-
return;
8712-
})();
8711+
}();
87138712
(function(a) {
87148713
a || a("FAIL");
87158714
})(console.log);

‎test/compress/hoist_vars.js

+68
Original file line numberDiff line numberDiff line change
@@ -801,3 +801,71 @@ issue_5638_4: {
801801
}
802802
expect_stdout: "foo 42"
803803
}
804+
805+
issue_5884_1: {
806+
options = {
807+
hoist_vars: true,
808+
inline: true,
809+
join_vars: true,
810+
reduce_vars: true,
811+
sequences: true,
812+
toplevel: true,
813+
unused: true,
814+
}
815+
input: {
816+
try {
817+
var f = function() {
818+
var a = [ "PASS" ];
819+
for (b in a)
820+
console.log(a[b]);
821+
};
822+
f();
823+
} finally {
824+
var b;
825+
}
826+
}
827+
expect: {
828+
var b;
829+
try {
830+
(function() {
831+
var a = [ "PASS" ];
832+
for (b in a)
833+
console.log(a[b]);
834+
})();
835+
} finally {}
836+
}
837+
expect_stdout: "PASS"
838+
}
839+
840+
issue_5884_2: {
841+
options = {
842+
hoist_vars: true,
843+
inline: true,
844+
join_vars: true,
845+
passes: 2,
846+
reduce_vars: true,
847+
sequences: true,
848+
toplevel: true,
849+
unused: true,
850+
}
851+
input: {
852+
try {
853+
var f = function() {
854+
var a = [ "PASS" ];
855+
for (b in a)
856+
console.log(a[b]);
857+
};
858+
f();
859+
} finally {
860+
var b;
861+
}
862+
}
863+
expect: {
864+
try {
865+
var a = [ "PASS" ];
866+
for (var b in a)
867+
console.log(a[b]);
868+
} finally {}
869+
}
870+
expect_stdout: "PASS"
871+
}

‎test/compress/join_vars.js

+52-2
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ inlined_assignments: {
669669
expect_stdout: "PASS"
670670
}
671671

672-
inline_for: {
672+
single_use_for: {
673673
options = {
674674
inline: true,
675675
join_vars: true,
@@ -683,16 +683,66 @@ inline_for: {
683683
};
684684
a();
685685
}
686+
expect: {
687+
(function() {
688+
for (; console.log("PASS"););
689+
})();
690+
}
691+
expect_stdout: "PASS"
692+
}
693+
694+
single_use_for_inline: {
695+
options = {
696+
inline: true,
697+
join_vars: true,
698+
passes: 2,
699+
reduce_vars: true,
700+
toplevel: true,
701+
unused: true,
702+
}
703+
input: {
704+
var a = function() {
705+
for (; console.log("PASS"););
706+
};
707+
a();
708+
}
686709
expect: {
687710
for (; console.log("PASS"););
688711
}
689712
expect_stdout: "PASS"
690713
}
691714

692-
inline_var: {
715+
single_use_var: {
716+
options = {
717+
inline: true,
718+
join_vars: true,
719+
reduce_vars: true,
720+
toplevel: true,
721+
unused: true,
722+
}
723+
input: {
724+
A = "PASS";
725+
var a = function() {
726+
var b = A;
727+
for (b in console.log(b));
728+
};
729+
a();
730+
}
731+
expect: {
732+
A = "PASS";
733+
(function() {
734+
var b = A;
735+
for (b in console.log(b));
736+
})();
737+
}
738+
expect_stdout: "PASS"
739+
}
740+
741+
single_use_var_inline: {
693742
options = {
694743
inline: true,
695744
join_vars: true,
745+
passes: 2,
696746
reduce_vars: true,
697747
toplevel: true,
698748
unused: true,

0 commit comments

Comments
 (0)
Please sign in to comment.