Refine returns codegen for suspend functions

This change should make the logic a bit more simple.
For all suspend functions/coroutines treat them in expression codegen
like they return boxed version of the original type.

Everything works fine then, except Unit type functions:
their bodies must be generated just like they're VOID and then load
Unit on stack manually.
This commit is contained in:
Denis Zharkov
2016-12-18 11:27:56 +03:00
parent 33ed98a0d3
commit 5dbc04abbb
13 changed files with 428 additions and 49 deletions
@@ -0,0 +1,46 @@
// WITH_RUNTIME
// WITH_COROUTINES
import kotlin.coroutines.*
suspend fun suspendHere(): String = CoroutineIntrinsics.suspendCoroutineOrReturn { x ->
x.resume("OK")
CoroutineIntrinsics.SUSPENDED
}
fun builder(c: suspend () -> Unit) {
var wasResumeCalled = false
c.startCoroutine(object : Continuation<Unit> {
override fun resume(value: Unit) {
wasResumeCalled = true
}
override fun resumeWithException(exception: Throwable) {
}
})
if (!wasResumeCalled) throw RuntimeException("fail 1")
}
fun box(): String {
var result = ""
builder {
run {
if (result == "") return@builder
}
suspendHere()
throw RuntimeException("fail 2")
}
result = "fail1"
builder {
run {
if (result == "") return@builder
}
result = suspendHere()
}
return result
}
@@ -0,0 +1,42 @@
// WITH_RUNTIME
// WITH_COROUTINES
import kotlin.coroutines.*
suspend fun suspendHere(): String = CoroutineIntrinsics.suspendCoroutineOrReturn { x ->
x.resume("OK")
CoroutineIntrinsics.SUSPENDED
}
fun builder(c: suspend () -> Unit) {
var wasResumeCalled = false
c.startCoroutine(object : Continuation<Unit> {
override fun resume(value: Unit) {
wasResumeCalled = true
}
override fun resumeWithException(exception: Throwable) {
}
})
if (!wasResumeCalled) throw RuntimeException("fail 1")
}
fun box(): String {
var result = ""
builder {
if (result == "") return@builder
suspendHere()
throw RuntimeException("fail 2")
}
result = "fail"
builder {
if (result == "") return@builder
result = suspendHere()
}
return result
}
@@ -0,0 +1,34 @@
// WITH_RUNTIME
// WITH_COROUTINES
// IGNORE_BACKEND: JS
import kotlin.coroutines.*
var result = "0"
suspend fun suspendHere(x: Int): Unit {
run {
if (x == 0) return
if (x == 1) return@suspendHere
}
result = "OK"
return CoroutineIntrinsics.suspendCoroutineOrReturn { x ->
x.resume(Unit)
CoroutineIntrinsics.SUSPENDED
}
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
fun box(): String {
builder {
if (suspendHere(0) != Unit) throw RuntimeException("fail 1")
if (suspendHere(1) != Unit) throw RuntimeException("fail 2")
if (suspendHere(2) != Unit) throw RuntimeException("fail 3")
}
return result
}
@@ -0,0 +1,27 @@
// WITH_RUNTIME
// WITH_COROUTINES
import kotlin.coroutines.*
var result = "0"
suspend fun suspendHere(x: Int): Unit {
if (x == 0) return
result = "OK"
return CoroutineIntrinsics.suspendCoroutineOrReturn { x ->
x.resume(Unit)
CoroutineIntrinsics.SUSPENDED
}
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
fun box(): String {
builder {
suspendHere(0)
suspendHere(1)
}
return result
}