JS: when removing unused temporary variable, process its RHS value. Fix KT-15325
This commit is contained in:
+4
-7
@@ -507,7 +507,7 @@ internal class TemporaryVariableElimination(private val function: JsFunction) {
|
||||
initializers.forEach { accept(it) }
|
||||
if (isRemoved) {
|
||||
for (initializer in initializers) {
|
||||
ctx.addPrevious(JsExpressionStatement(initializer).apply { synthetic = x.synthetic })
|
||||
ctx.addPrevious(JsExpressionStatement(accept(initializer)).apply { synthetic = x.synthetic })
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -530,10 +530,7 @@ internal class TemporaryVariableElimination(private val function: JsFunction) {
|
||||
val (name, value) = assignment
|
||||
if (shouldConsiderUnused(name)) {
|
||||
hasChanges = true
|
||||
ctx.replaceMe(JsExpressionStatement(value).run {
|
||||
synthetic = true
|
||||
accept(this)
|
||||
})
|
||||
ctx.replaceMe(accept(JsExpressionStatement(value)).apply { synthetic = true })
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -552,7 +549,7 @@ internal class TemporaryVariableElimination(private val function: JsFunction) {
|
||||
val name = x.name
|
||||
if (name != null && x.qualifier == null && name in namesToSubstitute) {
|
||||
val replacement = accept(definedValues[name]!!)
|
||||
ctx.replaceMe(replacement)
|
||||
ctx.replaceMe(replacement.apply { synthetic = true })
|
||||
return false
|
||||
}
|
||||
return super.visit(x, ctx)
|
||||
@@ -569,7 +566,7 @@ internal class TemporaryVariableElimination(private val function: JsFunction) {
|
||||
if (assignment != null) {
|
||||
val name = assignment.first
|
||||
if (shouldConsiderUnused(name)) {
|
||||
ctx.replaceMe(x.arg2)
|
||||
ctx.replaceMe(accept(x.arg2).apply { synthetic = true })
|
||||
}
|
||||
}
|
||||
super.endVisit(x, ctx)
|
||||
|
||||
+2
@@ -42,4 +42,6 @@ class TemporaryVariableEliminationTest : BasicOptimizerTest("temporary-variable"
|
||||
@Test fun transitiveNotConsideredTrivial() = box()
|
||||
|
||||
@Test fun assignmentToNonLocal() = box()
|
||||
|
||||
@Test fun removeUnusedAndSubstitute() = box()
|
||||
}
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
var log = "";
|
||||
|
||||
function test(a) {
|
||||
var $tmp1;
|
||||
log += $tmp1 = 1;
|
||||
return a;
|
||||
}
|
||||
|
||||
function box() {
|
||||
if (test(3) != 3) return "fail1";
|
||||
if (log != 1) return "fail2";
|
||||
return "OK"
|
||||
}
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
var log = "";
|
||||
|
||||
function test(a) {
|
||||
var $tmp1;
|
||||
log += $tmp1 = 1;
|
||||
var $tmp2 = $tmp1;
|
||||
var $tmp3;
|
||||
var $tmp4 = $tmp2;
|
||||
return a;
|
||||
}
|
||||
|
||||
function box() {
|
||||
if (test(3) != 3) return "fail1";
|
||||
if (log != 1) return "fail2";
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user