KT-53551 KT-52213 KT-58476 Fix handling of suspend functional types with context receivers

This commit is contained in:
strangepleasures
2023-09-07 12:40:01 +00:00
committed by Space Team
parent c2ee9bb1a4
commit 88453a05f1
10 changed files with 184 additions and 1 deletions
@@ -0,0 +1,20 @@
// !LANGUAGE: +ContextReceivers
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
// WITH_COROUTINES
interface Context
class Receiver
interface Action {
context (Context)
fun run()
}
fun execute(block: suspend context(Context) Receiver.(Action) -> Unit) = Unit
fun box(): String {
execute { it.run() }
return "OK"
}
@@ -0,0 +1,35 @@
// !LANGUAGE: +ContextReceivers
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
// WITH_COROUTINES
import helpers.*
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*
fun box(): String {
withFooBar { fooBarBaz() }
return "OK"
}
fun <T> runBlocking(c: suspend () -> T): T {
var res: T? = null
c.startCoroutine(Continuation(EmptyCoroutineContext) {
res = it.getOrThrow()
})
return res!!
}
class Foo
class Bar
class Baz
context(Foo, Bar)
fun Baz.fooBarBaz() { }
fun withFooBar(block: suspend context(Foo, Bar) Baz.() -> Unit) {
val foo = Foo()
val bar = Bar()
val baz = Baz()
runBlocking { block(foo, bar, baz) }
}
@@ -0,0 +1,20 @@
// !LANGUAGE: +ContextReceivers
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
// WITH_COROUTINES
class InContext
class MyReciever {
public suspend fun MyOutput.innerFun(): Int = 123
}
class MyOutput
// 2 - Declare the caller that calls the suspended function in a context
public fun caller(block: suspend context(InContext) MyReciever.() -> Int): MyOutput = MyOutput()
fun box(): String {
val out1 = caller { MyOutput().innerFun() }
val out2 = with (InContext()) { caller { MyOutput().innerFun() } }
val out3 = caller { with (InContext()) { MyOutput().innerFun() } }
return "OK"
}