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:
Ilmir Usmanov
2018-01-18 15:21:19 +03:00
parent 042ca55be7
commit 6854135077
80 changed files with 5214 additions and 144 deletions
@@ -0,0 +1,48 @@
// FILE: inlined.kt
// WITH_RUNTIME
// NO_CHECK_LAMBDA_INLINING
suspend inline fun crossinlineMe(crossinline c: suspend () -> Unit) {
val l: suspend () -> Unit = { c() }
l()
}
// FILE: inlineSite.kt
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
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
}
})
}
var i = 0;
suspend fun suspendHere() = suspendCoroutineOrReturn<Unit> {
i++
COROUTINE_SUSPENDED
}
fun box(): String {
builder {
crossinlineMe {
suspendHere()
suspendHere()
suspendHere()
suspendHere()
suspendHere()
}
}
if (i != 1) return "FAIL"
return "OK"
}
@@ -0,0 +1,53 @@
// FILE: inlined.kt
// WITH_RUNTIME
// NO_CHECK_LAMBDA_INLINING
suspend inline fun crossinlineMe(crossinline c: suspend () -> Unit) {
val l: suspend () -> Unit = {
val l1 : suspend () -> Unit = {
c()
}
l1()
}
l()
}
// FILE: inlineSite.kt
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
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
}
})
}
var i = 0;
suspend fun suspendHere() = suspendCoroutineOrReturn<Unit> {
i++
COROUTINE_SUSPENDED
}
fun box(): String {
builder {
crossinlineMe {
suspendHere()
suspendHere()
suspendHere()
suspendHere()
suspendHere()
}
}
if (i != 1) return "FAIL $i"
return "OK"
}
@@ -0,0 +1,52 @@
// FILE: inlined.kt
// WITH_RUNTIME
// NO_CHECK_LAMBDA_INLINING
import kotlin.coroutines.experimental.intrinsics.*
var i = 0;
suspend fun suspendHere() = suspendCoroutineOrReturn<Unit> {
i++
COROUTINE_SUSPENDED
}
suspend inline fun crossinlineMe(crossinline c: suspend () -> Unit) {
val l: suspend () -> Unit = {
suspendHere()
suspendHere()
suspendHere()
suspendHere()
suspendHere()
}
l()
}
// 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
}
})
}
fun box(): String {
var res = "OK"
builder {
crossinlineMe {
res = "FAIL 1"
}
}
if (i != 1) return "FAIL 2"
return res
}
@@ -0,0 +1,75 @@
// FILE: inlined.kt
// WITH_RUNTIME
// NO_CHECK_LAMBDA_INLINING
interface SuspendRunnable {
suspend fun run()
}
suspend inline fun crossinlineMe(crossinline c: suspend () -> Unit) {
val l: suspend () -> Unit = {
val sr = object: SuspendRunnable {
override suspend fun run() {
val l : suspend () -> Unit = {
val sr = object: SuspendRunnable {
override suspend fun run() {
val l : suspend () -> Unit = {
val sr = object: SuspendRunnable {
override suspend fun run() {
c()
}
}
sr.run()
}
l()
}
}
sr.run()
}
l()
}
}
sr.run()
}
l()
}
// FILE: inlineSite.kt
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
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
}
})
}
var i = 0;
suspend fun suspendHere() = suspendCoroutineOrReturn<Unit> {
i++
COROUTINE_SUSPENDED
}
fun box(): String {
builder {
crossinlineMe {
suspendHere()
suspendHere()
suspendHere()
suspendHere()
suspendHere()
}
}
if (i != 1) return "FAIL $i"
return "OK"
}
@@ -0,0 +1,75 @@
// FILE: inlined.kt
// WITH_RUNTIME
// NO_CHECK_LAMBDA_INLINING
interface SuspendRunnable {
suspend fun run()
}
suspend inline fun crossinlineMe(crossinline c: suspend () -> Unit) {
val l: suspend () -> Unit = {
c()
}
l()
}
// FILE: inlineSite.kt
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
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
}
})
}
var i = 0;
suspend fun suspendHere() = suspendCoroutineOrReturn<Unit> {
i++
COROUTINE_SUSPENDED
}
fun box(): String {
builder {
crossinlineMe {
val sr = object: SuspendRunnable {
override suspend fun run() {
val l : suspend () -> Unit = {
val sr = object: SuspendRunnable {
override suspend fun run() {
val l : suspend () -> Unit = {
val sr = object: SuspendRunnable {
override suspend fun run() {
suspendHere()
suspendHere()
suspendHere()
suspendHere()
suspendHere()
}
}
sr.run()
}
l()
}
}
sr.run()
}
l()
}
}
sr.run()
}
}
if (i != 1) return "FAIL $i"
return "OK"
}
@@ -0,0 +1,56 @@
// FILE: inlined.kt
// WITH_RUNTIME
// NO_CHECK_LAMBDA_INLINING
interface SuspendRunnable {
suspend fun run()
}
suspend inline fun crossinlineMe(crossinline c: suspend () -> Unit) {
val o = object : SuspendRunnable {
override suspend fun run() {
c()
}
}
o.run()
}
// FILE: inlineSite.kt
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
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
}
})
}
var i = 0;
suspend fun suspendHere() = suspendCoroutineOrReturn<Unit> {
i++
COROUTINE_SUSPENDED
}
fun box(): String {
builder {
crossinlineMe {
suspendHere()
suspendHere()
suspendHere()
suspendHere()
suspendHere()
}
}
if (i != 1) return "FAIL $i"
return "OK"
}
@@ -0,0 +1,61 @@
// FILE: inlined.kt
// WITH_RUNTIME
// NO_CHECK_LAMBDA_INLINING
interface SuspendRunnable {
suspend fun run()
}
suspend inline fun crossinlineMe(crossinline c: suspend () -> Unit) {
val o = object : SuspendRunnable {
override suspend fun run() {
val o1 = object: SuspendRunnable {
override suspend fun run() {
c()
}
}
o1.run()
}
}
o.run()
}
// FILE: inlineSite.kt
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
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
}
})
}
var i = 0;
suspend fun suspendHere() = suspendCoroutineOrReturn<Unit> {
i++
COROUTINE_SUSPENDED
}
fun box(): String {
builder {
crossinlineMe {
suspendHere()
suspendHere()
suspendHere()
suspendHere()
suspendHere()
}
}
if (i != 1) return "FAIL $i"
return "OK"
}
@@ -0,0 +1,60 @@
// FILE: inlined.kt
// WITH_RUNTIME
// NO_CHECK_LAMBDA_INLINING
interface SuspendRunnable {
suspend fun run1()
suspend fun run2()
}
suspend inline fun crossinlineMe(crossinline c1: suspend () -> Unit, crossinline c2: suspend () -> Unit) {
val o = object : SuspendRunnable {
override suspend fun run1() {
c1()
}
override suspend fun run2() {
c2()
}
}
o.run1()
o.run2()
}
// FILE: inlineSite.kt
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
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
}
})
}
var i = 0;
var j = 0;
suspend fun incrementI() {
i++
}
suspend fun incrementJ() {
j++
}
fun box(): String {
builder {
crossinlineMe({ incrementI() }) { incrementJ() }
}
if (i != 1) return "FAIL i $i"
if (j != 1) return "FAIL i $i"
return "OK"
}
@@ -0,0 +1,58 @@
// FILE: inlined.kt
// WITH_RUNTIME
// NO_CHECK_LAMBDA_INLINING
import kotlin.coroutines.experimental.intrinsics.*
var i = 0;
suspend fun suspendHere() = suspendCoroutineOrReturn<Unit> {
i++
COROUTINE_SUSPENDED
}
interface SuspendRunnable {
suspend fun run()
}
suspend inline fun crossinlineMe(crossinline c: suspend () -> Unit) {
val o = object : SuspendRunnable {
override suspend fun run() {
suspendHere()
suspendHere()
suspendHere()
suspendHere()
suspendHere()
}
}
o.run()
}
// 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
}
})
}
fun box(): String {
var res = "OK"
builder {
crossinlineMe {
res = "FAIL 1"
}
}
if (i != 1) return "FAIL 2"
return res
}
@@ -0,0 +1,48 @@
// FILE: inlined.kt
// WITH_RUNTIME
// NO_CHECK_LAMBDA_INLINING
suspend inline fun crossinlineMe(crossinline c: suspend () -> Unit) {
c()
c()
}
// FILE: inlineSite.kt
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
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
}
})
}
var i = 0;
suspend fun suspendHere() = suspendCoroutineOrReturn<Unit> {
i++
COROUTINE_SUSPENDED
}
fun box(): String {
builder {
crossinlineMe {
suspendHere()
suspendHere()
suspendHere()
suspendHere()
suspendHere()
}
}
if (i != 1) return "FAIL"
return "OK"
}
@@ -0,0 +1,68 @@
// FILE: inlined.kt
// WITH_RUNTIME
// NO_CHECK_LAMBDA_INLINING
suspend inline fun crossinlineMe(crossinline c: suspend () -> Unit) {
val l: suspend () -> Unit = { c() }
l()
}
suspend inline fun crossinlineMe2(crossinline c: suspend () -> Unit) {
crossinlineMe { c(); c() }
}
// FILE: inlineSite.kt
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
var result = "FAIL"
var i = 0
var finished = false
var proceed: () -> Unit = {}
suspend fun suspendHere() = suspendCoroutine<Unit> { c ->
i++
proceed = { c.resume(Unit) }
}
fun builder(c: suspend () -> Unit) {
val continuation = object: Continuation<Unit> {
override val context: CoroutineContext
get() = EmptyCoroutineContext
override fun resume(value: Unit) {
proceed = {
result = "OK"
finished = true
}
}
override fun resumeWithException(exception: Throwable) {
throw exception
}
}
c.startCoroutine(continuation)
}
fun box(): String {
builder {
crossinlineMe2 {
suspendHere()
suspendHere()
suspendHere()
suspendHere()
suspendHere()
}
}
for (counter in 0 until 10) {
if (i != counter + 1) return "Expected ${counter + 1}, got $i"
proceed()
}
if (i != 10) return "FAIL $i"
if (finished) return "resume on root continuation is called"
proceed()
if (!finished) return "resume on root continuation is not called"
return result
}