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) }
|
initializers.forEach { accept(it) }
|
||||||
if (isRemoved) {
|
if (isRemoved) {
|
||||||
for (initializer in initializers) {
|
for (initializer in initializers) {
|
||||||
ctx.addPrevious(JsExpressionStatement(initializer).apply { synthetic = x.synthetic })
|
ctx.addPrevious(JsExpressionStatement(accept(initializer)).apply { synthetic = x.synthetic })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -530,10 +530,7 @@ internal class TemporaryVariableElimination(private val function: JsFunction) {
|
|||||||
val (name, value) = assignment
|
val (name, value) = assignment
|
||||||
if (shouldConsiderUnused(name)) {
|
if (shouldConsiderUnused(name)) {
|
||||||
hasChanges = true
|
hasChanges = true
|
||||||
ctx.replaceMe(JsExpressionStatement(value).run {
|
ctx.replaceMe(accept(JsExpressionStatement(value)).apply { synthetic = true })
|
||||||
synthetic = true
|
|
||||||
accept(this)
|
|
||||||
})
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -552,7 +549,7 @@ internal class TemporaryVariableElimination(private val function: JsFunction) {
|
|||||||
val name = x.name
|
val name = x.name
|
||||||
if (name != null && x.qualifier == null && name in namesToSubstitute) {
|
if (name != null && x.qualifier == null && name in namesToSubstitute) {
|
||||||
val replacement = accept(definedValues[name]!!)
|
val replacement = accept(definedValues[name]!!)
|
||||||
ctx.replaceMe(replacement)
|
ctx.replaceMe(replacement.apply { synthetic = true })
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return super.visit(x, ctx)
|
return super.visit(x, ctx)
|
||||||
@@ -569,7 +566,7 @@ internal class TemporaryVariableElimination(private val function: JsFunction) {
|
|||||||
if (assignment != null) {
|
if (assignment != null) {
|
||||||
val name = assignment.first
|
val name = assignment.first
|
||||||
if (shouldConsiderUnused(name)) {
|
if (shouldConsiderUnused(name)) {
|
||||||
ctx.replaceMe(x.arg2)
|
ctx.replaceMe(accept(x.arg2).apply { synthetic = true })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
super.endVisit(x, ctx)
|
super.endVisit(x, ctx)
|
||||||
|
|||||||
+2
@@ -42,4 +42,6 @@ class TemporaryVariableEliminationTest : BasicOptimizerTest("temporary-variable"
|
|||||||
@Test fun transitiveNotConsideredTrivial() = box()
|
@Test fun transitiveNotConsideredTrivial() = box()
|
||||||
|
|
||||||
@Test fun assignmentToNonLocal() = 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