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,105 @@
// FILE: test.kt
// WITH_RUNTIME
import kotlin.coroutines.experimental.*
// Block is allowed to be called from nested classes/lambdas (as common crossinlines)
// Are suspend calls possible inside lambda matching to the parameter
interface SuspendRunnable {
suspend fun run()
}
class Controller {
var res = "FAIL 1"
inline fun test1(crossinline runner: suspend Controller.() -> Unit) {
val l : suspend Controller.() -> Unit = { runner() }
builder(this) { l() }
}
inline fun test2(crossinline c: suspend Controller.() -> Unit) {
val sr = object: SuspendRunnable {
override suspend fun run() {
c()
}
}
builder(this) { sr.run() }
}
}
fun builder(controller: Controller, c: suspend Controller.() -> Unit) {
c.startCoroutine(controller, object: Continuation<Unit> {
override val context: CoroutineContext
get() = EmptyCoroutineContext
override fun resume(value: Unit) {
}
override fun resumeWithException(exception: Throwable) {
throw exception
}
})
}
// FILE: box.kt
suspend fun calculate() = "OK"
fun box(): String {
val controller = Controller()
controller.test1 {
res = calculate()
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 2"
controller.test1 {
test1 {
test1 {
test1 {
test1 {
test1 {
res = calculate()
}
}
}
}
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 3"
controller.test2 {
res = calculate()
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 4"
controller.test2 {
test2 {
test2 {
test2 {
test2 {
test2 {
res = calculate()
}
}
}
}
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 5"
controller.test1 {
test2 {
test1 {
test2 {
test1 {
test2 {
res = calculate()
}
}
}
}
}
}
return controller.res
}
@@ -0,0 +1,132 @@
// FILE: test.kt
// WITH_RUNTIME
import kotlin.coroutines.experimental.*
// Block is allowed to be called from nested classes/lambdas (as common crossinlines)
// Start coroutine call is possible
// Are suspend calls possible inside lambda matching to the parameter
interface SuspendRunnable {
suspend fun run()
}
val EmptyContinuation = object: Continuation<Unit> {
override val context: CoroutineContext
get() = EmptyCoroutineContext
override fun resume(value: Unit) {
}
override fun resumeWithException(exception: Throwable) {
throw exception
}
}
class Controller {
var res = "FAIL 1"
inline fun test1(noinline c: suspend Controller.() -> Unit) {
val l : suspend Controller.() -> Unit = { c() }
builder(this) { l() }
}
inline fun test2(noinline c: suspend Controller.() -> Unit) {
c.startCoroutine(this, EmptyContinuation)
}
inline fun test3(noinline c: suspend Controller.() -> Unit) {
val sr = object : SuspendRunnable {
override suspend fun run() {
c()
}
}
builder(this) { sr.run() }
}
}
fun builder(controller: Controller, c: suspend Controller.() -> Unit) {
c.startCoroutine(controller, EmptyContinuation)
}
// FILE: box.kt
suspend fun calculate() = "OK"
fun box(): String {
val controller = Controller()
controller.test1 {
res = calculate()
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 2"
controller.test2 {
res = "OK"
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 3"
controller.test3 {
res = "OK"
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 4"
controller.test1 {
test1 {
test1 {
test1 {
test1 {
test1 {
res = calculate()
}
}
}
}
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 5"
controller.test2 {
test2 {
test2 {
test2 {
test2 {
test2 {
res = calculate()
}
}
}
}
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 6"
controller.test3 {
test3 {
test3 {
test3 {
test3 {
test3 {
res = calculate()
}
}
}
}
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 7"
controller.test1 {
test2 {
test3 {
test1 {
test2 {
test3 {
res = calculate()
}
}
}
}
}
}
return controller.res
}
@@ -0,0 +1,113 @@
// FILE: test.kt
// WITH_RUNTIME
import kotlin.coroutines.experimental.*
// 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)
class Controller {
var res = "FAIL 1"
suspend inline fun test1(crossinline c: Controller.() -> Unit) {
c()
}
suspend inline fun test2(crossinline c: Controller.() -> Unit) {
val l = { c() }
l()
}
suspend inline fun test3(crossinline c: Controller.() -> Unit) {
val r = object: Runnable {
override fun run() {
c()
}
}
r.run()
}
inline fun transform(crossinline c: suspend Controller.() -> Unit) {
builder(this) { c() }
}
}
fun builder(controller : Controller, c: suspend Controller.() -> Unit) {
c.startCoroutine(controller, object: Continuation<Unit> {
override val context: CoroutineContext
get() = EmptyCoroutineContext
override fun resume(value: Unit) {
}
override fun resumeWithException(exception: Throwable) {
throw exception
}
})
}
// FILE: box.kt
import kotlin.coroutines.experimental.*
fun box() : String {
val controller = Controller()
builder(controller) {
test1 {
res = "OK"
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 2"
builder(controller) {
test2 {
res = "OK"
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 3"
builder(controller) {
test3 {
res = "OK"
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 4"
builder(controller) {
test1 {
transform {
test1 {
res = "OK"
}
}
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 5"
builder(controller) {
test2 {
transform {
test2 {
res = "OK"
}
}
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 6"
builder(controller) {
test3 {
transform {
test3 {
res = "OK"
}
}
}
}
return controller.res
}
@@ -0,0 +1,138 @@
// FILE: test.kt
// WITH_RUNTIME
import kotlin.coroutines.experimental.*
// 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)
// suspend calls possible inside lambda matching to the parameter
interface SuspendRunnable {
suspend fun run()
}
class Controller {
var res = "FAIL 1"
suspend inline fun test1(crossinline c: suspend Controller.() -> Unit) {
c()
}
suspend inline fun test2(crossinline c: suspend Controller.() -> Unit) {
val l: suspend Controller.() -> Unit = { c() }
l()
}
suspend inline fun test3(crossinline c: suspend Controller.() -> Unit) {
val sr = object : SuspendRunnable {
override suspend fun run() {
c()
}
}
sr.run()
}
}
// FILE: box.kt
import kotlin.coroutines.experimental.*
fun builder(controller: Controller, c: suspend Controller.() -> Unit) {
c.startCoroutine(controller, object: Continuation<Unit> {
override val context: CoroutineContext
get() = EmptyCoroutineContext
override fun resume(value: Unit) {
}
override fun resumeWithException(exception: Throwable) {
throw exception
}
})
}
suspend fun calculate() = "OK"
fun box() : String {
val controller = Controller()
builder(controller) {
test1 {
res = calculate()
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 2"
builder(controller) {
test2 {
res = calculate()
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 3"
builder(controller) {
test3 {
res = calculate()
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 4"
builder(controller) {
test1 {
test1 {
test1 {
test1 {
test1 {
res = calculate()
}
}
}
}
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 5"
builder(controller) {
test2 {
test2 {
test2 {
test2 {
test2 {
res = calculate()
}
}
}
}
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 6"
builder(controller) {
test3 {
test3 {
test3 {
test3 {
test3 {
res = calculate()
}
}
}
}
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 7"
builder(controller) {
test1 {
test2 {
test3 {
test1 {
test2 {
res = calculate()
}
}
}
}
}
}
return controller.res
}
@@ -0,0 +1,113 @@
// FILE: test.kt
// WITH_RUNTIME
import kotlin.coroutines.experimental.*
// 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)
class Controller {
var res = "FAIL 1"
suspend inline fun test1(noinline c: Controller.() -> Unit) {
c()
}
suspend inline fun test2(noinline c: Controller.() -> Unit) {
val l = { c() }
l()
}
suspend inline fun test3(noinline c: Controller.() -> Unit) {
val r = object: Runnable {
override fun run() {
c()
}
}
r.run()
}
inline fun transform(crossinline c: suspend Controller.() -> Unit) {
builder(this) { c() }
}
}
fun builder(controller: Controller, c: suspend Controller.() -> Unit) {
c.startCoroutine(controller, object: Continuation<Unit> {
override val context: CoroutineContext
get() = EmptyCoroutineContext
override fun resume(value: Unit) {
}
override fun resumeWithException(exception: Throwable) {
throw exception
}
})
}
// FILE: box.kt
import kotlin.coroutines.experimental.*
fun box() : String {
val controller = Controller()
builder(controller) {
test1 {
res = "OK"
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 2"
builder(controller) {
test2 {
res = "OK"
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 3"
builder(controller) {
test3 {
res = "OK"
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 4"
builder(controller) {
test1 {
transform {
test1 {
res = "OK"
}
}
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 5"
builder(controller) {
test2 {
transform {
test2 {
res = "OK"
}
}
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 6"
builder(controller) {
test3 {
transform {
test3 {
res = "OK"
}
}
}
}
return controller.res
}
@@ -0,0 +1,167 @@
// FILE: test.kt
// WITH_RUNTIME
import kotlin.coroutines.experimental.*
// Block is allowed to be called from nested classes/lambdas (as common crossinlines)
// Are suspend calls possible inside lambda matching to the parameter
// Start coroutine call is possible
// Block is allowed to be called directly inside inline function
interface SuspendRunnable {
suspend fun run()
}
object EmptyContinuation: Continuation<Unit> {
override val context: CoroutineContext
get() = EmptyCoroutineContext
override fun resume(value: Unit) {
}
override fun resumeWithException(exception: Throwable) {
throw exception
}
}
class Controller {
var res = "FAIL 1"
suspend inline fun test1(noinline c: suspend Controller.() -> Unit) {
val l : suspend Controller.() -> Unit = { c() }
l()
}
suspend inline fun test2(noinline c: suspend Controller.() -> Unit) {
c.startCoroutine(this, EmptyContinuation)
}
suspend inline fun test3(noinline c: suspend Controller.() -> Unit) {
c()
}
suspend inline fun test4(noinline c: suspend Controller.() -> Unit) {
val sr = object: SuspendRunnable {
override suspend fun run() {
c()
}
}
sr.run()
}
}
fun builder(controller: Controller, c: suspend Controller.() -> Unit) {
c.startCoroutine(controller, EmptyContinuation)
}
// FILE: box.kt
import kotlin.coroutines.experimental.*
suspend fun calculate() = "OK"
fun box(): String {
val controller = Controller()
builder(controller) {
test1 {
res = calculate()
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 2"
builder(controller) {
test2 {
res = "OK"
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 3"
builder(controller) {
test3 {
res = "OK"
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 4"
builder(controller) {
test4 {
res = "OK"
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 5"
builder(controller) {
test1 {
test1 {
test1 {
test1 {
test1 {
res = calculate()
}
}
}
}
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 6"
builder(controller) {
test2 {
test2 {
test2 {
test2 {
test2 {
res = calculate()
}
}
}
}
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 7"
builder(controller) {
test3 {
test3 {
test3 {
test3 {
test3 {
res = calculate()
}
}
}
}
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 8"
builder(controller) {
test4 {
test4 {
test4 {
test4 {
test4 {
res = calculate()
}
}
}
}
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 9"
builder(controller) {
test1 {
test2 {
test3 {
test4 {
test1 {
res = calculate()
}
}
}
}
}
}
return controller.res
}
@@ -0,0 +1,60 @@
// FILE: test.kt
// WITH_RUNTIME
// NO_CHECK_LAMBDA_INLINING
import kotlin.coroutines.experimental.*
// Block is allowed to be called inside the body of owner inline function
// suspend calls possible inside lambda matching to the parameter
class Controller {
var res = "FAIL 1"
suspend inline fun test(c: Controller.() -> Unit) {
c()
}
inline fun transform(crossinline c: suspend Controller.() -> Unit) {
builder(this) { c() }
}
}
fun builder(controller: Controller, c: suspend Controller.() -> Unit) {
c.startCoroutine(controller, object: Continuation<Unit> {
override val context: CoroutineContext
get() = EmptyCoroutineContext
override fun resume(value: Unit) {
}
override fun resumeWithException(exception: Throwable) {
throw exception
}
})
}
// FILE: box.kt
import kotlin.coroutines.experimental.*
suspend fun calculate() = "OK"
fun box() : String {
val controller = Controller()
builder(controller) {
test {
res = calculate()
}
}
if (controller.res != "OK") return controller.res
builder(controller) {
test {
transform {
test {
res = calculate()
}
}
}
}
return controller.res
}
@@ -0,0 +1,61 @@
// FILE: test.kt
// WITH_RUNTIME
// NO_CHECK_LAMBDA_INLINING
import kotlin.coroutines.experimental.*
// Block is allowed to be called inside the body of owner inline function
// suspend calls possible inside lambda matching to the parameter
class Controller {
var res = "FAIL 1"
suspend inline fun test(c: suspend Controller.() -> Unit) {
c()
}
}
// FILE: box.kt
import kotlin.coroutines.experimental.*
fun builder(controller: Controller, c: suspend Controller.() -> Unit) {
c.startCoroutine(controller, object: Continuation<Unit> {
override val context: CoroutineContext
get() = EmptyCoroutineContext
override fun resume(value: Unit) {
}
override fun resumeWithException(exception: Throwable) {
throw exception
}
})
}
suspend fun calculate() = "OK"
fun box() : String {
val controller = Controller()
builder(controller) {
test {
res = calculate()
}
}
if (controller.res != "OK") return controller.res
controller.res = "FAIL 2"
builder(controller) {
test {
test {
test {
test {
test {
res = calculate()
}
}
}
}
}
}
return controller.res
}