JS backend: fix translation when statement -- now WhenTranslator returns JsBlock instead of hackish adding statements to context block.

It fixed the compiler crashing when translating `for`/`when`/`if` with `when` statement(KT-5234) and KT-5058.

 #KT-5234 fixed
 #EA-57346 fixed
 #KT-5058 fixed
This commit is contained in:
Michael Nedzelsky
2014-06-18 12:55:40 +04:00
committed by Zalim Bashorov
parent a94f12d8fe
commit 209315baad
8 changed files with 103 additions and 2 deletions
@@ -190,4 +190,8 @@ public final class MiscTest extends AbstractExpressionTest {
public void testStringInterpolationEvaluationOrder() throws Exception {
fooBoxTest();
}
public void testKt5058() throws Exception {
checkFooBoxIsTrue("KT-5058.kt");
}
}
@@ -89,4 +89,20 @@ public final class PatternMatchingTest extends SingleFileTranslationTest {
public void testIfInWhen() throws Exception {
checkFooBoxIsOk();
}
public void testForWithOneStmWhen() throws Exception {
fooBoxTest();
}
public void testWhileWithOneStmWhen() throws Exception {
fooBoxTest();
}
public void testDoWhileWithOneStmWhen() throws Exception {
fooBoxTest();
}
public void testIfWithOneStmWhen() throws Exception {
fooBoxTest();
}
}
@@ -40,8 +40,9 @@ public final class WhenTranslator extends AbstractTranslator {
WhenTranslator translator = new WhenTranslator(expression, context);
if (BindingUtils.isStatement(context.bindingContext(), expression)) {
translator.translateAsStatement(context.dynamicContext().jsBlock().getStatements());
return null;
JsBlock jsBlock = new JsBlock();
translator.translateAsStatement(jsBlock.getStatements());
return jsBlock;
}
return translator.translateAsExpression();
@@ -0,0 +1,25 @@
package foo
fun test():Any {
val a: Any = "OK"
val f: Any =
if (true) {
when {
false -> "1"
((a as? String)?.size ?: 0 > 0) -> a
else -> "2"
}
}
else {
"3"
}
return f
}
fun box(): Boolean {
var result = test()
return result == "OK";
}
@@ -0,0 +1,15 @@
package foo
fun box(): Boolean {
var result = false
var i = 1
do
when (i) {
1 -> result = true
else -> result = false
}
while (i==0)
return result;
}
@@ -0,0 +1,13 @@
package foo
fun box(): Boolean {
var result = false
for (i in array(1))
when (i) {
1 -> result = true
else -> result = false
}
return result;
}
@@ -0,0 +1,13 @@
package foo
fun box(): Boolean {
var result = false
var i = 1
if (i==1)
when (i) {
1 -> result = true
else -> result = false
}
return result;
}
@@ -0,0 +1,14 @@
package foo
fun box(): Boolean {
var result = false
var i = 1
while(i==1)
when (i) {
1 -> { result = true; break }
else -> result = false
}
return result;
}