Fixed a bug where multiple when entries could execute in one statement.
This commit is contained in:
@@ -91,7 +91,7 @@ public class WhenTranslator extends AbstractTranslator {
|
|||||||
return statementToExecute;
|
return statementToExecute;
|
||||||
}
|
}
|
||||||
JsExpression condition = translateConditions(entry);
|
JsExpression condition = translateConditions(entry);
|
||||||
return new JsIf(condition, statementToExecute, null);
|
return new JsIf(condition, addDummyBreak(statementToExecute), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -119,6 +119,11 @@ public class WhenTranslator extends AbstractTranslator {
|
|||||||
throw new AssertionError("Unsupported when condition " + condition.getClass());
|
throw new AssertionError("Unsupported when condition " + condition.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private JsBlock addDummyBreak(@NotNull JsStatement statement) {
|
||||||
|
return AstUtil.newBlock(statement, new JsBreak());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private JsExpression translatePatternCondition(@NotNull JetWhenConditionIsPattern condition) {
|
private JsExpression translatePatternCondition(@NotNull JetWhenConditionIsPattern condition) {
|
||||||
|
|||||||
@@ -24,4 +24,9 @@ public final class PatternMatchingTest extends IncludeLibraryTest {
|
|||||||
testFooBoxIsTrue("whenNotType.kt");
|
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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user