[JVM IR] Support break in do-while condition.

This breaks from the loop itself which is inconsistent with
what happens for breaks in while conditions.

Also, the frontend will report that code after the loop is
unreachable, which it isn't. :-\

However, those issues are covered in
https://youtrack.jetbrains.com/issue/KT-17728, so for now
we follow the old backend to not "break" anyone. :)

Fixes KT-44412
This commit is contained in:
Mads Ager
2021-03-05 12:30:12 +01:00
committed by max-kammerer
parent d0d3b57366
commit 8588412a56
10 changed files with 85 additions and 3 deletions
@@ -7843,6 +7843,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/breakInExpr.kt");
}
@Test
@TestMetadata("breakInLoopConditions.kt")
public void testBreakInLoopConditions() throws Exception {
runTest("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/breakInLoopConditions.kt");
}
@Test
@TestMetadata("continueInDoWhile.kt")
public void testContinueInDoWhile() throws Exception {
@@ -1061,10 +1061,10 @@ class ExpressionCodegen(
mv.fakeAlwaysFalseIfeq(endLabel)
data.withBlock(LoopInfo(loop, continueLabel, endLabel)) {
loop.body?.accept(this, data)?.discard()
mv.visitLabel(continueLabel)
loop.condition.markLineNumber(true)
loop.condition.accept(this, data).coerceToBoolean().jumpIfTrue(entry)
}
mv.visitLabel(continueLabel)
loop.condition.markLineNumber(true)
loop.condition.accept(this, data).coerceToBoolean().jumpIfTrue(entry)
mv.mark(endLabel)
addInlineMarker(mv, false)
return unitValue
@@ -0,0 +1,39 @@
// See: https://youtrack.jetbrains.com/issue/KT-45319
// IGNORE_BACKEND: JVM
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JS
fun breakInDoWhileCondition(): String {
var i = 0
while (true) {
++i
var j = 0
do {
++j
} while (break)
if (j != 1) return "FAIL1"
if (i == 3) break
}
if (i != 3) return "FAIL2"
return "OK"
}
fun breakInWhileCondition(): String {
var i = 0
while (true) {
++i
var j = 0
while (break) {
j++
}
return "FAIL3"
}
if (i != 1) return "FAIL4"
return "OK"
}
fun box(): String {
val breakInDoWhileResult = breakInDoWhileCondition()
if (breakInDoWhileResult != "OK") return breakInDoWhileResult
return breakInWhileCondition()
}
@@ -7843,6 +7843,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/breakInExpr.kt");
}
@Test
@TestMetadata("breakInLoopConditions.kt")
public void testBreakInLoopConditions() throws Exception {
runTest("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/breakInLoopConditions.kt");
}
@Test
@TestMetadata("continueInDoWhile.kt")
public void testContinueInDoWhile() throws Exception {
@@ -7843,6 +7843,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/breakInExpr.kt");
}
@Test
@TestMetadata("breakInLoopConditions.kt")
public void testBreakInLoopConditions() throws Exception {
runTest("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/breakInLoopConditions.kt");
}
@Test
@TestMetadata("continueInDoWhile.kt")
public void testContinueInDoWhile() throws Exception {
@@ -6036,6 +6036,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class BreakContinueInExpressions extends AbstractLightAnalysisModeTest {
@TestMetadata("breakInLoopConditions.kt")
public void ignoreBreakInLoopConditions() throws Exception {
runTest("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/breakInLoopConditions.kt");
}
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath);
}
@@ -5322,6 +5322,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/breakInExpr.kt");
}
@TestMetadata("breakInLoopConditions.kt")
public void testBreakInLoopConditions() throws Exception {
runTest("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/breakInLoopConditions.kt");
}
@TestMetadata("continueInDoWhile.kt")
public void testContinueInDoWhile() throws Exception {
runTest("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/continueInDoWhile.kt");
@@ -4779,6 +4779,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/breakInExpr.kt");
}
@TestMetadata("breakInLoopConditions.kt")
public void testBreakInLoopConditions() throws Exception {
runTest("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/breakInLoopConditions.kt");
}
@TestMetadata("continueInDoWhile.kt")
public void testContinueInDoWhile() throws Exception {
runTest("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/continueInDoWhile.kt");
@@ -4779,6 +4779,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/breakInExpr.kt");
}
@TestMetadata("breakInLoopConditions.kt")
public void testBreakInLoopConditions() throws Exception {
runTest("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/breakInLoopConditions.kt");
}
@TestMetadata("continueInDoWhile.kt")
public void testContinueInDoWhile() throws Exception {
runTest("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/continueInDoWhile.kt");
@@ -3372,6 +3372,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/breakInExpr.kt");
}
@TestMetadata("breakInLoopConditions.kt")
public void testBreakInLoopConditions() throws Exception {
runTest("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/breakInLoopConditions.kt");
}
@TestMetadata("continueInDoWhile.kt")
public void testContinueInDoWhile() throws Exception {
runTest("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/continueInDoWhile.kt");