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:
+46
@@ -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
|
||||
}
|
||||
+34
@@ -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
|
||||
}
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
public final class CoroutineNonLocalReturnKt {
|
||||
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
|
||||
public final static method builder(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): void
|
||||
public final static @org.jetbrains.annotations.Nullable method suspendHere(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.Continuation): java.lang.Object
|
||||
}
|
||||
|
||||
|
||||
public final class CoroutineUtilKt {
|
||||
public final static @org.jetbrains.annotations.NotNull method handleExceptionContinuation(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): kotlin.coroutines.Continuation
|
||||
public final static @org.jetbrains.annotations.NotNull method handleResultContinuation(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): kotlin.coroutines.Continuation
|
||||
}
|
||||
|
||||
|
||||
public final class EmptyContinuation {
|
||||
public final static field INSTANCE: EmptyContinuation
|
||||
private method <init>(): void
|
||||
public method resume(@org.jetbrains.annotations.Nullable p0: java.lang.Object): void
|
||||
public method resumeWithException(@org.jetbrains.annotations.NotNull p0: java.lang.Throwable): void
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
public final class CoroutineReturnKt {
|
||||
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
|
||||
public final static method builder(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): void
|
||||
public final static @org.jetbrains.annotations.Nullable method suspendHere(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.Continuation): java.lang.Object
|
||||
}
|
||||
|
||||
|
||||
public final class CoroutineUtilKt {
|
||||
public final static @org.jetbrains.annotations.NotNull method handleExceptionContinuation(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): kotlin.coroutines.Continuation
|
||||
public final static @org.jetbrains.annotations.NotNull method handleResultContinuation(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): kotlin.coroutines.Continuation
|
||||
}
|
||||
|
||||
|
||||
public final class EmptyContinuation {
|
||||
public final static field INSTANCE: EmptyContinuation
|
||||
private method <init>(): void
|
||||
public method resume(@org.jetbrains.annotations.Nullable p0: java.lang.Object): void
|
||||
public method resumeWithException(@org.jetbrains.annotations.NotNull p0: java.lang.Throwable): void
|
||||
}
|
||||
Vendored
+22
@@ -0,0 +1,22 @@
|
||||
public final class CoroutineUtilKt {
|
||||
public final static @org.jetbrains.annotations.NotNull method handleExceptionContinuation(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): kotlin.coroutines.Continuation
|
||||
public final static @org.jetbrains.annotations.NotNull method handleResultContinuation(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): kotlin.coroutines.Continuation
|
||||
}
|
||||
|
||||
|
||||
public final class EmptyContinuation {
|
||||
public final static field INSTANCE: EmptyContinuation
|
||||
private method <init>(): void
|
||||
public method resume(@org.jetbrains.annotations.Nullable p0: java.lang.Object): void
|
||||
public method resumeWithException(@org.jetbrains.annotations.NotNull p0: java.lang.Throwable): void
|
||||
}
|
||||
|
||||
|
||||
public final class SuspendNonLocalReturnKt {
|
||||
private static @org.jetbrains.annotations.NotNull field result: java.lang.String
|
||||
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
|
||||
public final static method builder(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): void
|
||||
public final static @org.jetbrains.annotations.NotNull method getResult(): java.lang.String
|
||||
public final static method setResult(@org.jetbrains.annotations.NotNull p0: java.lang.String): void
|
||||
public final static @org.jetbrains.annotations.Nullable method suspendHere(p0: int, @org.jetbrains.annotations.NotNull p1: kotlin.coroutines.Continuation): java.lang.Object
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
public final class CoroutineUtilKt {
|
||||
public final static @org.jetbrains.annotations.NotNull method handleExceptionContinuation(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): kotlin.coroutines.Continuation
|
||||
public final static @org.jetbrains.annotations.NotNull method handleResultContinuation(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): kotlin.coroutines.Continuation
|
||||
}
|
||||
|
||||
|
||||
public final class EmptyContinuation {
|
||||
public final static field INSTANCE: EmptyContinuation
|
||||
private method <init>(): void
|
||||
public method resume(@org.jetbrains.annotations.Nullable p0: java.lang.Object): void
|
||||
public method resumeWithException(@org.jetbrains.annotations.NotNull p0: java.lang.Throwable): void
|
||||
}
|
||||
|
||||
|
||||
public final class SuspendReturnKt {
|
||||
private static @org.jetbrains.annotations.NotNull field result: java.lang.String
|
||||
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
|
||||
public final static method builder(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): void
|
||||
public final static @org.jetbrains.annotations.NotNull method getResult(): java.lang.String
|
||||
public final static method setResult(@org.jetbrains.annotations.NotNull p0: java.lang.String): void
|
||||
public final static @org.jetbrains.annotations.Nullable method suspendHere(p0: int, @org.jetbrains.annotations.NotNull p1: kotlin.coroutines.Continuation): java.lang.Object
|
||||
}
|
||||
Reference in New Issue
Block a user