Make CHECKCAST not break TCO
So, treat CHECKCASTs as { POP, Unit } sequences. If the CHECKCAST is
between suspension point and ARETURN, put check for COROUTINE_SUSPENDED
before it and return if the suspension point is suspended.
This is safe, since if the function throws CCE, it will be thrown from
the last state in state-machine and we cannot reenter the function
during resume. So, in case of CHECKCAST throwing CCE the behavior is the
same, whether we have state-machine or not.
We do not need to disable TCO in some cases, as we do for functions,
returning Unit, since in latter case suspend function, returning Unit
might appear as returning non-Unit during resumption due to missing
{ POP, Unit } sequence, which is not executed, since the function is
tail-call. However, in case of functions, returning non-Unit there is
no such concern, since we do not POP result of suspension point, but
rather, return it after CHECKCAST.
#KT-50835 Fixed
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
// TARGET_BACKEND: JVM
|
||||
// FULL_JDK
|
||||
// WITH_STDLIB
|
||||
// WITH_COROUTINES
|
||||
// CHECK_TAIL_CALL_OPTIMIZATION
|
||||
import helpers.*
|
||||
import kotlin.coroutines.*
|
||||
import kotlin.coroutines.intrinsics.*
|
||||
|
||||
private var x = 100
|
||||
|
||||
private inline fun inlineFun(
|
||||
potentiallySuspendLambda: () -> String
|
||||
): String {
|
||||
if (x == 99) return ""
|
||||
return potentiallySuspendLambda()
|
||||
}
|
||||
|
||||
suspend fun myFunWithTailCall() = inlineFun(
|
||||
potentiallySuspendLambda = { suspendFun() }
|
||||
)
|
||||
|
||||
suspend fun suspendFun(): String = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
TailCallOptimizationChecker.saveStackTrace(x)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
builder {
|
||||
myFunWithTailCall()
|
||||
}
|
||||
TailCallOptimizationChecker.checkNoStateMachineIn("myFunWithTailCall")
|
||||
return "OK"
|
||||
}
|
||||
Vendored
-5
@@ -13,13 +13,8 @@ inline suspend fun suspendThere(v: String): String = suspendCoroutineUnintercept
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
// TODO: Somehow we still generate continuations for tail-call function, but we don't use them.
|
||||
suspend fun suspendHere(): String = suspendThere("O")
|
||||
|
||||
// There is a kind of redundant state machine generated for complexSuspend:
|
||||
// it's basically has the only suspend call just before return, but there is
|
||||
// a redundant CHECKCAST String in the run's lambda, so we have to insert the state machine.
|
||||
// TODO: Think of avoiding such redundant casts
|
||||
suspend fun complexSuspend(): String {
|
||||
return run {
|
||||
suspendThere("K")
|
||||
|
||||
Vendored
-15
@@ -14,25 +14,10 @@ final class InlineWithoutStateMachineKt$box$1 {
|
||||
public final @org.jetbrains.annotations.Nullable method invokeSuspend(@org.jetbrains.annotations.NotNull p0: java.lang.Object): java.lang.Object
|
||||
}
|
||||
|
||||
@kotlin.Metadata
|
||||
@kotlin.coroutines.jvm.internal.DebugMetadata
|
||||
final class InlineWithoutStateMachineKt$complexSuspend$1 {
|
||||
// source: 'inlineWithoutStateMachine.kt'
|
||||
enclosing method InlineWithoutStateMachineKt.complexSuspend(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||
field L$0: java.lang.Object
|
||||
field L$1: java.lang.Object
|
||||
field label: int
|
||||
synthetic field result: java.lang.Object
|
||||
inner (anonymous) class InlineWithoutStateMachineKt$complexSuspend$1
|
||||
method <init>(p0: kotlin.coroutines.Continuation): void
|
||||
public final @org.jetbrains.annotations.Nullable method invokeSuspend(@org.jetbrains.annotations.NotNull p0: java.lang.Object): java.lang.Object
|
||||
}
|
||||
|
||||
@kotlin.Metadata
|
||||
public final class InlineWithoutStateMachineKt {
|
||||
// source: 'inlineWithoutStateMachine.kt'
|
||||
inner (anonymous) class InlineWithoutStateMachineKt$box$1
|
||||
inner (anonymous) class InlineWithoutStateMachineKt$complexSuspend$1
|
||||
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
|
||||
public final static method builder(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): void
|
||||
public final static @org.jetbrains.annotations.Nullable method complexSuspend(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.Continuation): java.lang.Object
|
||||
|
||||
Vendored
-15
@@ -15,25 +15,10 @@ final class InlineWithoutStateMachineKt$box$1 {
|
||||
public final @org.jetbrains.annotations.Nullable method invokeSuspend(@org.jetbrains.annotations.NotNull p0: java.lang.Object): java.lang.Object
|
||||
}
|
||||
|
||||
@kotlin.coroutines.jvm.internal.DebugMetadata
|
||||
@kotlin.Metadata
|
||||
final class InlineWithoutStateMachineKt$complexSuspend$1 {
|
||||
// source: 'inlineWithoutStateMachine.kt'
|
||||
enclosing method InlineWithoutStateMachineKt.complexSuspend(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||
field L$0: java.lang.Object
|
||||
field L$1: java.lang.Object
|
||||
field label: int
|
||||
synthetic field result: java.lang.Object
|
||||
inner (anonymous) class InlineWithoutStateMachineKt$complexSuspend$1
|
||||
method <init>(p0: kotlin.coroutines.Continuation): void
|
||||
public final @org.jetbrains.annotations.Nullable method invokeSuspend(@org.jetbrains.annotations.NotNull p0: java.lang.Object): java.lang.Object
|
||||
}
|
||||
|
||||
@kotlin.Metadata
|
||||
public final class InlineWithoutStateMachineKt {
|
||||
// source: 'inlineWithoutStateMachine.kt'
|
||||
inner (anonymous) class InlineWithoutStateMachineKt$box$1
|
||||
inner (anonymous) class InlineWithoutStateMachineKt$complexSuspend$1
|
||||
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
|
||||
public final static method builder(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): void
|
||||
public final static @org.jetbrains.annotations.Nullable method complexSuspend(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.Continuation): java.lang.Object
|
||||
|
||||
Reference in New Issue
Block a user