diff --git a/compiler/testData/codegen/box/coroutines/controlFlow/throwFromCatch.kt b/compiler/testData/codegen/box/coroutines/controlFlow/throwFromCatch.kt new file mode 100644 index 00000000000..0cb279481e8 --- /dev/null +++ b/compiler/testData/codegen/box/coroutines/controlFlow/throwFromCatch.kt @@ -0,0 +1,48 @@ +// WITH_RUNTIME + +class Controller { + var result = "" + + suspend fun suspendAndLog(value: T, c: Continuation) { + result += "suspend($value);" + c.resume(value) + } + + suspend fun suspendLogAndThrow(exception: Throwable, c: Continuation) { + result += "throw(${exception.message});" + c.resumeWithException(exception) + } + + operator fun handleException(exception: Throwable, c: Continuation) { + result += "caught(${exception.message});" + } +} + +fun builder(coroutine c: Controller.() -> Continuation): String { + val controller = Controller() + c(controller).resume(Unit) + return controller.result +} + +fun box(): String { + val value = builder { + try { + try { + suspendAndLog("1") + suspendLogAndThrow(RuntimeException("exception")) + } + catch (e: RuntimeException) { + suspendAndLog("caught") + suspendLogAndThrow(RuntimeException("fromCatch")) + } + } finally { + suspendAndLog("finally") + } + suspendAndLog("ignore") + } + if (value != "suspend(1);throw(exception);suspend(caught);throw(fromCatch);suspend(finally);caught(fromCatch);") { + return "fail: $value" + } + + return "OK" +} \ No newline at end of file diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 72ab5956b22..90fb2883520 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -4716,14 +4716,8 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) public static class ControlFlow extends AbstractIrBlackBoxCodegenTest { - @TestMetadata("returnFromFinally.kt") - public void ignoredReturnFromFinally() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/controlFlow/returnFromFinally.kt"); - doTest(fileName); - } - public void testAllFilesPresentInControlFlow() throws Exception { - KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/controlFlow"), Pattern.compile("^(.+)\\.kt$"), true); + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/controlFlow"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } @TestMetadata("breakFinally.kt") @@ -4756,6 +4750,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes doTest(fileName); } + @TestMetadata("throwFromCatch.kt") + public void testThrowFromCatch() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/controlFlow/throwFromCatch.kt"); + doTest(fileName); + } + @TestMetadata("whileStatement.kt") public void testWhileStatement() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/controlFlow/whileStatement.kt"); @@ -4837,7 +4837,7 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes @RunWith(JUnit3RunnerWithInners.class) public static class MultiModule extends AbstractIrBlackBoxCodegenTest { public void testAllFilesPresentInMultiModule() throws Exception { - KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/multiModule"), Pattern.compile("^(.+)\\.kt$"), true); + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/multiModule"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } @TestMetadata("simple.kt") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index f394fbc293b..d077fd90ce4 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -4716,14 +4716,8 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) public static class ControlFlow extends AbstractBlackBoxCodegenTest { - @TestMetadata("returnFromFinally.kt") - public void ignoredReturnFromFinally() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/controlFlow/returnFromFinally.kt"); - doTest(fileName); - } - public void testAllFilesPresentInControlFlow() throws Exception { - KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/controlFlow"), Pattern.compile("^(.+)\\.kt$"), true); + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/controlFlow"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } @TestMetadata("breakFinally.kt") @@ -4756,6 +4750,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("throwFromCatch.kt") + public void testThrowFromCatch() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/controlFlow/throwFromCatch.kt"); + doTest(fileName); + } + @TestMetadata("whileStatement.kt") public void testWhileStatement() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/controlFlow/whileStatement.kt"); @@ -4837,7 +4837,7 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { @RunWith(JUnit3RunnerWithInners.class) public static class MultiModule extends AbstractBlackBoxCodegenTest { public void testAllFilesPresentInMultiModule() throws Exception { - KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/multiModule"), Pattern.compile("^(.+)\\.kt$"), true); + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/multiModule"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } @TestMetadata("simple.kt") diff --git a/js/js.inliner/src/org/jetbrains/kotlin/js/coroutine/CoroutineBodyTransformer.kt b/js/js.inliner/src/org/jetbrains/kotlin/js/coroutine/CoroutineBodyTransformer.kt index 9e1330eee96..156191ced7e 100644 --- a/js/js.inliner/src/org/jetbrains/kotlin/js/coroutine/CoroutineBodyTransformer.kt +++ b/js/js.inliner/src/org/jetbrains/kotlin/js/coroutine/CoroutineBodyTransformer.kt @@ -390,10 +390,10 @@ class CoroutineBodyTransformer(val program: JsProgram, val scope: JsScope, val t if (finallyNode != null) { currentStatements += updateFinallyPath(listOf(oldCatchBlock)) - currentStatements += exceptionState(finallyBlock) + currentStatements += if (catchNode != null) exceptionState(finallyBlock) else stateAndJump(finallyBlock) } else { - currentStatements += exceptionState(oldCatchBlock) + currentStatements += if (catchNode != null) exceptionState(oldCatchBlock) else stateAndJump(oldCatchBlock) } if (catchNode != null) { diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 782d8d3bffe..9fcecbcb0fa 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -5672,6 +5672,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { doTest(fileName); } + @TestMetadata("handleResultSuspended.kt") + public void testHandleResultSuspended() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/handleResultSuspended.kt"); + doTest(fileName); + } + @TestMetadata("inlineSuspendFunction.kt") public void testInlineSuspendFunction() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/inlineSuspendFunction.kt"); @@ -5882,6 +5888,63 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { doTest(fileName); } + @TestMetadata("compiler/testData/codegen/box/coroutines/controlFlow") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class ControlFlow extends AbstractJsCodegenBoxTest { + public void testAllFilesPresentInControlFlow() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/controlFlow"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true); + } + + @TestMetadata("breakFinally.kt") + public void testBreakFinally() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/controlFlow/breakFinally.kt"); + doTest(fileName); + } + + @TestMetadata("breakStatement.kt") + public void testBreakStatement() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/controlFlow/breakStatement.kt"); + doTest(fileName); + } + + @TestMetadata("doWhileStatement.kt") + public void testDoWhileStatement() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/controlFlow/doWhileStatement.kt"); + doTest(fileName); + } + + @TestMetadata("forStatement.kt") + public void testForStatement() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/controlFlow/forStatement.kt"); + doTest(fileName); + } + + @TestMetadata("ifStatement.kt") + public void testIfStatement() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/controlFlow/ifStatement.kt"); + doTest(fileName); + } + + @TestMetadata("returnFromFinally.kt") + public void testReturnFromFinally() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/controlFlow/returnFromFinally.kt"); + doTest(fileName); + } + + @TestMetadata("throwFromCatch.kt") + public void testThrowFromCatch() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/controlFlow/throwFromCatch.kt"); + doTest(fileName); + } + + @TestMetadata("whileStatement.kt") + public void testWhileStatement() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/controlFlow/whileStatement.kt"); + doTest(fileName); + } + } + @TestMetadata("compiler/testData/codegen/box/coroutines/intLikeVarSpilling") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -5938,6 +6001,27 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { doTest(fileName); } } + + @TestMetadata("compiler/testData/codegen/box/coroutines/multiModule") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class MultiModule extends AbstractJsCodegenBoxTest { + public void testAllFilesPresentInMultiModule() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/multiModule"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true); + } + + @TestMetadata("simple.kt") + public void testSimple() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/multiModule/simple.kt"); + doTest(fileName); + } + + @TestMetadata("suspendExtension.kt") + public void testSuspendExtension() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/multiModule/suspendExtension.kt"); + doTest(fileName); + } + } } @TestMetadata("compiler/testData/codegen/box/dataClasses")