Files
kotlin-fork/compiler/testData/codegen/boxInline/suspend/inlineSuspendOfCrossinlineOrdinary.kt
T
Ilmir Usmanov a1448ebb37 JVM_IR: Support crossinline suspend lambdas
The main idea is the following: since we need to generate
(fake)continuations before inlining, we move IrClasses of suspend
lambdas and continuation classes of named functions into the functions.
Thus, it allows the codegen to generate them prior to inlining and
the inliner will happily transform them for us.
Because of that, lowerings which transform call-site function are likely
to change reference to lowered suspend lambdas or functions.
Hence, do not rely on references to lowered suspend lambdas or
functions, instead, rely on attributes.

Do not generate continuation for inline suspend lambdas.
Previously, inline suspend lambdas were treated like suspend functions,
thus we generated continuations for them. Now we just do not treat them
as suspend functions or lambdas during AddContinuationLowering.
We should add continuation parameter to them, however.

Do not generate secondary constructor for suspend lambdas, otherwise,
the inliner is unable to transform them (it requires only one
constructor to be present).

Generate continuation classes for suspend functions as first statement
inside the function.
This enables suspend functions in local object inside inline functions.
Since we already have attributes inside suspend named functions, we
just reuse them to generate continuation class names. This allows us
to close the gap between code generated by old back-end and the new
one.

If a suspend named function captures crossinline lambda, we should
generate a template for inliner: a copy of the function without
state-machine and a continuation constructor call. The call is needed
so the inliner transforms the continuation as well.

Refactor CoroutineTransformerMethodVisitor, so it no longer depends on
PSI.
2019-12-23 18:03:40 +01:00

109 lines
1.9 KiB
Kotlin
Vendored

// FILE: test.kt
// COMMON_COROUTINES_TEST
// WITH_RUNTIME
// WITH_COROUTINES
import COROUTINES_PACKAGE.*
import helpers.*
// Block is allowed to be called inside the body of owner inline function
// Block is allowed to be called from nested classes/lambdas (as common crossinlines)
// Needed for JS compatibility
interface Runnable {
fun run(): Unit
}
suspend inline fun test1(crossinline c: () -> Unit) {
c()
}
suspend inline fun test2(crossinline c: () -> Unit) {
val l = { c() }
l()
}
suspend inline fun test3(crossinline c: () -> Unit) {
val r = object: Runnable {
override fun run() {
c()
}
}
r.run()
}
inline fun transform(crossinline c: suspend () -> Unit) {
builder { c() }
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
// FILE: box.kt
// COMMON_COROUTINES_TEST
import COROUTINES_PACKAGE.*
import helpers.*
fun box() : String {
var res = "FAIL 1"
builder {
test1 {
res = "OK"
}
}
if (res != "OK") return res
res = "FAIL 2"
builder {
test2 {
res = "OK"
}
}
if (res != "OK") return res
res = "FAIL 3"
builder {
test3 {
res = "OK"
}
}
if (res != "OK") return res
res = "FAIL 4"
builder {
test1 {
transform {
test1 {
res = "OK"
}
}
}
}
if (res != "OK") return res
res = "FAIL 5"
builder {
test2 {
transform {
test2 {
res = "OK"
}
}
}
}
if (res != "OK") return res
res = "FAIL 6"
builder {
test3 {
transform {
test3 {
res = "OK"
}
}
}
}
return res
}