diff --git a/compiler/testData/codegen/box/coroutines/controlFlow/returnFromFinally.kt b/compiler/testData/codegen/box/coroutines/controlFlow/returnFromFinally.kt index 2453c6947b1..e02800269b8 100644 --- a/compiler/testData/codegen/box/coroutines/controlFlow/returnFromFinally.kt +++ b/compiler/testData/codegen/box/coroutines/controlFlow/returnFromFinally.kt @@ -2,11 +2,6 @@ // WITH_COROUTINES import kotlin.coroutines.* - -// Does not work in JVM backend, probably due to bug. It's not clear which behaviour is right. -// TODO: fix the bug and enable for JVM backend -// TARGET_BACKEND: JS - class Controller { var result = "" @@ -25,10 +20,18 @@ fun builder(c: suspend Controller.() -> String): String { return controller.result } +fun builderUnit(c: suspend Controller.() -> Unit): String { + val controller = Controller() + c.startCoroutine(controller, handleResultContinuation { + controller.result += "return;" + }) + return controller.result +} + fun id(value: T) = value fun box(): String { - val value = builder { + var value = builder { try { if (id(23) == 23) { return@builder suspendAndLog("OK") @@ -40,7 +43,22 @@ fun box(): String { result += "afterFinally;" "shouldNotReach" } - if (value != "suspend(OK);finally;return(OK);") return "fail: $value" + if (value != "suspend(OK);finally;return(OK);") return "fail1: $value" + + value = builderUnit { + try { + if (id(23) == 23) { + suspendAndLog("OK") + return@builderUnit + } + } + finally { + result += "finally;" + } + result += "afterFinally;" + "shouldNotReach" + } + if (value != "suspend(OK);finally;return;") return "fail2: $value" return "OK" } diff --git a/compiler/testData/codegen/light-analysis/coroutines/controlFlow/returnFromFinally.txt b/compiler/testData/codegen/light-analysis/coroutines/controlFlow/returnFromFinally.txt new file mode 100644 index 00000000000..e8470034677 --- /dev/null +++ b/compiler/testData/codegen/light-analysis/coroutines/controlFlow/returnFromFinally.txt @@ -0,0 +1,30 @@ +@kotlin.Metadata +public final class Controller { + private @org.jetbrains.annotations.NotNull field result: java.lang.String + public method (): void + public final @org.jetbrains.annotations.NotNull method getResult(): java.lang.String + public final method setResult(@org.jetbrains.annotations.NotNull p0: java.lang.String): void + public final @org.jetbrains.annotations.Nullable method suspendAndLog(p0: java.lang.Object, @org.jetbrains.annotations.NotNull p1: kotlin.coroutines.Continuation): java.lang.Object +} + +@kotlin.Metadata +public final class CoroutineUtilKt { + public final static @org.jetbrains.annotations.NotNull method handleExceptionContinuation(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): kotlin.coroutines.Continuation + public final static @org.jetbrains.annotations.NotNull method handleResultContinuation(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): kotlin.coroutines.Continuation +} + +@kotlin.Metadata +public final class EmptyContinuation { + public final static field INSTANCE: EmptyContinuation + private method (): void + public method resume(@org.jetbrains.annotations.Nullable p0: java.lang.Object): void + public method resumeWithException(@org.jetbrains.annotations.NotNull p0: java.lang.Throwable): void +} + +@kotlin.Metadata +public final class ReturnFromFinallyKt { + public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String + public final static @org.jetbrains.annotations.NotNull method builder(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function2): java.lang.String + public final static @org.jetbrains.annotations.NotNull method builderUnit(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function2): java.lang.String + public final static method id(p0: java.lang.Object): java.lang.Object +} 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 3cd7e412bf7..3fa615fe50a 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 @@ -4897,6 +4897,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes 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("switchLikeWhen.kt") public void testSwitchLikeWhen() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/controlFlow/switchLikeWhen.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 2794b647799..517ab47713d 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -4897,6 +4897,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { 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("switchLikeWhen.kt") public void testSwitchLikeWhen() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/controlFlow/switchLikeWhen.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java index 22d39a4acda..c24d6787d4b 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java @@ -4897,6 +4897,12 @@ public class LightAnalysisModeCodegenTestGenerated extends AbstractLightAnalysis 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("switchLikeWhen.kt") public void testSwitchLikeWhen() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/controlFlow/switchLikeWhen.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 fff4f47ec0b..742cc0935c2 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 @@ -305,12 +305,19 @@ class CoroutineBodyTransformer(private val program: JsProgram, private val conte if (isInFinally) { val returnBlock = CoroutineBlock() jumpWithFinally(0, returnBlock) - val returnFieldRef = JsNameRef(context.returnValueFieldName, JsLiteral.THIS) - currentStatements += JsAstUtils.assignment(returnFieldRef, x.expression).makeStmt() + val returnExpression = x.expression + val returnFieldRef = if (returnExpression != null) { + val ref = JsNameRef(context.returnValueFieldName, JsLiteral.THIS) + currentStatements += JsAstUtils.assignment(ref, x.expression).makeStmt() + ref + } + else { + null + } currentStatements += jump() currentBlock = returnBlock - currentStatements += JsReturn(returnFieldRef.deepCopy()) + currentStatements += JsReturn(returnFieldRef?.deepCopy()) } else { currentStatements += x