JS: fix translation of return statement surrounded by try..finally block in suspend lambda. See KT-15625
This commit is contained in:
@@ -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 <T> 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"
|
||||
}
|
||||
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
@kotlin.Metadata
|
||||
public final class Controller {
|
||||
private @org.jetbrains.annotations.NotNull field result: java.lang.String
|
||||
public method <init>(): 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 <init>(): 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
|
||||
}
|
||||
+6
@@ -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");
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user