Fixed a bug where multiple when entries could execute in one statement.

This commit is contained in:
Pavel Talanov
2011-11-17 18:05:47 +04:00
parent 5841049683
commit 98bcfe7847
3 changed files with 26 additions and 1 deletions
@@ -91,7 +91,7 @@ public class WhenTranslator extends AbstractTranslator {
return statementToExecute;
}
JsExpression condition = translateConditions(entry);
return new JsIf(condition, statementToExecute, null);
return new JsIf(condition, addDummyBreak(statementToExecute), null);
}
@NotNull
@@ -119,6 +119,11 @@ public class WhenTranslator extends AbstractTranslator {
throw new AssertionError("Unsupported when condition " + condition.getClass());
}
@NotNull
private JsBlock addDummyBreak(@NotNull JsStatement statement) {
return AstUtil.newBlock(statement, new JsBreak());
}
@NotNull
private JsExpression translatePatternCondition(@NotNull JetWhenConditionIsPattern condition) {
@@ -24,4 +24,9 @@ public final class PatternMatchingTest extends IncludeLibraryTest {
testFooBoxIsTrue("whenNotType.kt");
}
@Test
public void whenExecutesOnlyOnce() throws Exception {
testFooBoxIsTrue("whenExecutesOnlyOnce.kt");
}
}
@@ -0,0 +1,15 @@
namespace foo
class A() {
}
fun box() : Boolean {
var a = 0
when(A()) {
is A => a++;
is A => a++;
else => a++;
}
return (a == 1)
}