JS: fix bug in temporary variable eliminator

The problem was in considering `a` as trivial in following case:

```
var a = b;
```

However, that's wrong assumption, since `b` can be temporary variable
itself which is further substituted by a non-trivial expression.
This commit is contained in:
Alexey Andreev
2017-08-24 16:18:55 +03:00
parent abb254297a
commit 989cebe79e
3 changed files with 38 additions and 1 deletions
@@ -615,7 +615,7 @@ internal class TemporaryVariableElimination(private val function: JsFunction) {
is JsNameRef -> {
val qualifier = expr.qualifier
if (expr.sideEffects == SideEffectKind.PURE && (qualifier == null || isTrivial(qualifier))) {
true
expr.name !in temporary
}
else {
val name = expr.name
@@ -4484,6 +4484,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
doTest(fileName);
}
@TestMetadata("temporaryVarNonTrivial.kt")
public void testTemporaryVarNonTrivial() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/inlineEvaluationOrder/temporaryVarNonTrivial.kt");
doTest(fileName);
}
@TestMetadata("ternaryConditional.kt")
public void testTernaryConditional() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/inlineEvaluationOrder/ternaryConditional.kt");
@@ -0,0 +1,31 @@
// EXPECTED_REACHABLE_NODES: 1002
var log = ""
fun bar(): A {
log += "foo;"
return A()
}
class A {
fun f() {
log += "f;"
}
fun g() {
log += "g;"
}
}
inline fun <T> with(x: T, a: T.() -> Unit) = x.a()
fun box(): String {
with(bar()) {
f()
g()
}
if (log != "foo;f;g;") return "fail: $log"
return "OK"
}