Files
kotlin-fork/compiler/testData/codegen/boxInline/suspend/inlineSuspendContinuation.kt
T
Ilmir Usmanov cc06798e2c Implement unit suspend functions tail-call optimisation
Unlike previously, this optimisation works on every callee return type.
Tail-calls inside unit functions can be either
INVOKE...
ARETURN
or
INVOKE
POP
GETSTATIC kotlin/Unit.INSTANCE
ARETURN
The first pattern is already covered. The second one is a bit tricky,
since we cannot just assume than the function is tail-call, we also need
to check whether the callee returned COROUTINE_SUSPENDED marker.
Thus, resulting bytecode of function's 'epilogue' look like
DUP
INVOKESTATIC getCOROUTINE_SUSPENDED
IF_ACMPNE LN
ARETURN
LN:
POP

 #KT-28938 Fixed
2019-07-29 20:34:48 +03:00

107 lines
2.3 KiB
Kotlin
Vendored

// IGNORE_BACKEND: JVM_IR
// FILE: test.kt
// COMMON_COROUTINES_TEST
// WITH_RUNTIME
// WITH_COROUTINES
// NO_CHECK_LAMBDA_INLINING
// TARGET_BACKEND: JVM
suspend inline fun test1(c: () -> Unit) {
c()
}
suspend inline fun test2(c: suspend () -> Unit) {
c()
}
suspend inline fun test3(crossinline c: suspend() -> Unit) {
c()
}
suspend inline fun test4(crossinline c: suspend() -> Unit) {
val l : suspend () -> Unit = { c() }
l()
}
interface SuspendRunnable {
suspend fun run()
}
suspend inline fun test5(crossinline c: suspend() -> Unit) {
val sr = object : SuspendRunnable {
override suspend fun run() {
c()
}
}
sr.run()
}
// FILE: box.kt
// COMMON_COROUTINES_TEST
import COROUTINES_PACKAGE.*
import COROUTINES_PACKAGE.intrinsics.*
import helpers.*
import COROUTINES_PACKAGE.jvm.internal.*
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
var continuationChanged = true
var savedContinuation: Continuation<Unit>? = null
suspend inline fun saveContinuation() = suspendCoroutineUninterceptedOrReturn<Unit> { c ->
savedContinuation = c
Unit
}
suspend inline fun checkContinuation(continuation: Continuation<Unit>) = suspendCoroutineUninterceptedOrReturn<Unit> { c ->
continuationChanged = (continuation !== c)
Unit
}
fun box() : String {
builder {
saveContinuation()
test1 {
checkContinuation(savedContinuation!!)
}
}
if (continuationChanged) return "FAIL 1"
continuationChanged = true
builder {
saveContinuation()
test2 {
checkContinuation(savedContinuation!!)
}
}
if (continuationChanged) return "FAIL 2"
continuationChanged = true
builder {
saveContinuation()
test3 {
checkContinuation(savedContinuation!!)
}
}
if (continuationChanged) return "FAIL 3"
continuationChanged = false
builder {
saveContinuation()
test4 {
checkContinuation(savedContinuation!!)
}
}
if (!continuationChanged) return "FAIL 4"
continuationChanged = false
builder {
saveContinuation()
test5 {
checkContinuation(savedContinuation!!)
}
}
if (continuationChanged) return "FAIL 5"
return "OK"
}