Support suspendCoroutineUninterceptedOrReturn intrinsic
KT-17336: Fixed
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
// WITH_RUNTIME
|
||||
// WITH_COROUTINES
|
||||
import helpers.*
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
suspend fun suspendHereUnintercepted(): String = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
x.resume("OK")
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
suspend fun suspendWithExceptionUnintercepted(): String = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
x.resumeWithException(RuntimeException("OK"))
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
suspend fun suspendHereIntercepted(): String = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
x.intercepted().resume("OK")
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
suspend fun suspendWithExceptionIntercepted(): String = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
x.intercepted().resumeWithException(RuntimeException("OK"))
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun builder(expectedCount: Int, c: suspend () -> String): String {
|
||||
var fromSuspension: String? = null
|
||||
var counter = 0
|
||||
|
||||
val result = try {
|
||||
c.startCoroutineUninterceptedOrReturn(object: Continuation<String> {
|
||||
override val context: CoroutineContext
|
||||
get() = ContinuationDispatcher { counter++ }
|
||||
|
||||
override fun resumeWithException(exception: Throwable) {
|
||||
fromSuspension = "Exception: " + exception.message!!
|
||||
}
|
||||
|
||||
override fun resume(value: String) {
|
||||
fromSuspension = value
|
||||
}
|
||||
})
|
||||
} catch (e: Exception) {
|
||||
"Exception: ${e.message}"
|
||||
}
|
||||
|
||||
if (counter != expectedCount) throw RuntimeException("fail 0")
|
||||
return fromSuspension!!
|
||||
}
|
||||
|
||||
class ContinuationDispatcher(val dispatcher: () -> Unit) : AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor {
|
||||
override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> = DispatchedContinuation(dispatcher, continuation)
|
||||
}
|
||||
|
||||
private class DispatchedContinuation<T>(
|
||||
val dispatcher: () -> Unit,
|
||||
val continuation: Continuation<T>
|
||||
): Continuation<T> {
|
||||
override val context: CoroutineContext = continuation.context
|
||||
|
||||
override fun resume(value: T) {
|
||||
dispatcher()
|
||||
continuation.resume(value)
|
||||
}
|
||||
|
||||
override fun resumeWithException(exception: Throwable) {
|
||||
dispatcher()
|
||||
continuation.resumeWithException(exception)
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (builder(0) { suspendHereUnintercepted() } != "OK") return "fail 2"
|
||||
if (builder(1) { suspendHereIntercepted() } != "OK") return "fail 3"
|
||||
|
||||
if (builder(0) { suspendWithExceptionUnintercepted() } != "Exception: OK") return "fail 4"
|
||||
if (builder(1) { suspendWithExceptionIntercepted() } != "Exception: OK") return "fail 5"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
compiler/testData/codegen/box/coroutines/intrinsicSemantics/suspendCoroutineUninterceptedOrReturn.kt
Vendored
+80
@@ -0,0 +1,80 @@
|
||||
// WITH_RUNTIME
|
||||
// WITH_COROUTINES
|
||||
import helpers.*
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
suspend fun suspendHere(): String = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
x.resume("OK")
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
suspend fun suspendWithException(): String = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
x.resumeWithException(RuntimeException("OK"))
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
fun builder(shouldSuspend: Boolean, c: suspend () -> String): String {
|
||||
var fromSuspension: String? = null
|
||||
var counter = 0
|
||||
|
||||
val result = try {
|
||||
c.startCoroutineUninterceptedOrReturn(object: Continuation<String> {
|
||||
override val context: CoroutineContext
|
||||
get() = ContinuationDispatcher { counter++ }
|
||||
|
||||
override fun resumeWithException(exception: Throwable) {
|
||||
fromSuspension = "Exception: " + exception.message!!
|
||||
}
|
||||
|
||||
override fun resume(value: String) {
|
||||
fromSuspension = value
|
||||
}
|
||||
})
|
||||
} catch (e: Exception) {
|
||||
"Exception: ${e.message}"
|
||||
}
|
||||
|
||||
if (counter != 0) throw RuntimeException("fail 0 $counter")
|
||||
|
||||
if (shouldSuspend) {
|
||||
if (result !== COROUTINE_SUSPENDED) throw RuntimeException("fail 1")
|
||||
if (fromSuspension == null) throw RuntimeException("fail 2")
|
||||
return fromSuspension!!
|
||||
}
|
||||
|
||||
if (result === COROUTINE_SUSPENDED) throw RuntimeException("fail 3")
|
||||
return result as String
|
||||
}
|
||||
|
||||
class ContinuationDispatcher(val dispatcher: () -> Unit) : AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor {
|
||||
override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> = DispatchedContinuation(dispatcher, continuation)
|
||||
}
|
||||
|
||||
private class DispatchedContinuation<T>(
|
||||
val dispatcher: () -> Unit,
|
||||
val continuation: Continuation<T>
|
||||
): Continuation<T> {
|
||||
override val context: CoroutineContext = continuation.context
|
||||
|
||||
override fun resume(value: T) {
|
||||
dispatcher()
|
||||
continuation.resume(value)
|
||||
}
|
||||
|
||||
override fun resumeWithException(exception: Throwable) {
|
||||
dispatcher()
|
||||
continuation.resumeWithException(exception)
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (builder(false) { "OK" } != "OK") return "fail 4"
|
||||
if (builder(true) { suspendHere() } != "OK") return "fail 5"
|
||||
|
||||
if (builder(false) { throw RuntimeException("OK") } != "Exception: OK") return "fail 6"
|
||||
if (builder(true) { suspendWithException() } != "Exception: OK") return "fail 7"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user