Support crossinline suspend lambda as parameter of inline function
Use fake continuation instead of ALOAD 0 while inlining Do not generate state machine for inner lambdas and inner objects, which capture crossinline suspend lambda. #KT-19159: Fixed
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
// FILE: inlined.kt
|
||||
// WITH_RUNTIME
|
||||
// NO_CHECK_LAMBDA_INLINING
|
||||
|
||||
object Result {
|
||||
var a: String = ""
|
||||
var b: Int = 0
|
||||
}
|
||||
|
||||
suspend inline fun inlineMe(c: suspend () -> Unit) {
|
||||
var a = ""
|
||||
var b = 0
|
||||
val r = object: Runnable {
|
||||
override fun run() {
|
||||
b++
|
||||
a += "a"
|
||||
}
|
||||
}
|
||||
r.run()
|
||||
c()
|
||||
r.run()
|
||||
Result.a = a
|
||||
Result.b = b
|
||||
}
|
||||
suspend inline fun noinlineMe(noinline c: suspend () -> Unit) {
|
||||
var a = ""
|
||||
var b = 0
|
||||
val r = object: Runnable {
|
||||
override fun run() {
|
||||
b += 2
|
||||
a += "b"
|
||||
}
|
||||
}
|
||||
r.run()
|
||||
c()
|
||||
r.run()
|
||||
Result.a += a
|
||||
Result.b += b
|
||||
}
|
||||
suspend inline fun crossinlineMe(crossinline c: suspend () -> Unit) {
|
||||
var a = ""
|
||||
var b = 0
|
||||
val r = object: Runnable {
|
||||
override fun run() {
|
||||
b += 3
|
||||
a += "c"
|
||||
}
|
||||
}
|
||||
r.run()
|
||||
c()
|
||||
r.run()
|
||||
Result.a += a
|
||||
Result.b += b
|
||||
}
|
||||
|
||||
// FILE: inlineSite.kt
|
||||
|
||||
import kotlin.coroutines.experimental.*
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(object: Continuation<Unit> {
|
||||
override val context: CoroutineContext
|
||||
get() = EmptyCoroutineContext
|
||||
|
||||
override fun resume(value: Unit) {
|
||||
}
|
||||
|
||||
override fun resumeWithException(exception: Throwable) {
|
||||
throw exception
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
suspend fun dummy() {
|
||||
val local0 = 0
|
||||
val local1 = 0
|
||||
val local2 = 0
|
||||
val local3 = 0
|
||||
val local4 = 0
|
||||
val local5 = 0
|
||||
val local6 = 0
|
||||
val local7 = 0
|
||||
val local8 = 0
|
||||
val local9 = 0
|
||||
val local10 = 0
|
||||
val local11 = 0
|
||||
val local12 = 0
|
||||
val local13 = 0
|
||||
val local14 = 0
|
||||
val local15 = 0
|
||||
val local16 = 0
|
||||
val local17 = 0
|
||||
val local18 = 0
|
||||
val local19 = 0
|
||||
val local20 = 0
|
||||
val local21 = 0
|
||||
val local22 = 0
|
||||
}
|
||||
|
||||
suspend fun inlineSite() {
|
||||
inlineMe {
|
||||
dummy()
|
||||
dummy()
|
||||
}
|
||||
if (Result.a != "aa" || Result.b != 2) throw RuntimeException("FAIL 1")
|
||||
noinlineMe {
|
||||
dummy()
|
||||
dummy()
|
||||
}
|
||||
if (Result.a != "aabb" || Result.b != 6) throw RuntimeException("FAIL 2")
|
||||
crossinlineMe {
|
||||
dummy()
|
||||
dummy()
|
||||
}
|
||||
if (Result.a != "aabbcc" || Result.b != 12) throw RuntimeException("FAIL 3")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
builder {
|
||||
inlineSite()
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user