Support getValue/setValue/provideDelegate suspend functions in JVM backend

- Determine if there are non-tail calls to getValue/setValue simply
by existance of such a property
(it might be too strict, but implementing more granular check may be rather hard)

- Change in ExpressionCodegen is relevant for provideDelegate,
that in case of local variables uses OnStack as StackValue
(see the comment near these changes)

 #KT-15933 Fixed
This commit is contained in:
Denis Zharkov
2017-01-24 18:18:05 +03:00
parent 9ca9a988a6
commit 9ce3880ac6
9 changed files with 221 additions and 3 deletions
@@ -0,0 +1,104 @@
// WITH_RUNTIME
// WITH_COROUTINES
// IGNORE_BACKEND: JS
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*
import kotlin.reflect.KProperty
suspend fun suspendThere(v: String): String = suspendCoroutineOrReturn { x ->
x.resume(v)
SUSPENDED_MARKER
}
class A(val x: String) {
var isSetValueCalled = false
var isProvideDelegateCalled = false
var isMinusAssignCalled = false
var isIncCalled = false
operator suspend fun component1() = suspendThere(x + "K")
operator suspend fun getValue(thisRef: Any?, property: KProperty<*>) = suspendThere(x + "K")
operator suspend fun setValue(thisRef: Any?, property: KProperty<*>, value: String): Unit = suspendCoroutineOrReturn { x ->
if (value != "56") return@suspendCoroutineOrReturn Unit
isSetValueCalled = true
x.resume(Unit)
SUSPENDED_MARKER
}
operator suspend fun provideDelegate(host: Any?, p: Any): A = suspendCoroutineOrReturn { x ->
isProvideDelegateCalled = true
x.resume(this)
SUSPENDED_MARKER
}
operator suspend fun plus(y: String) = suspendThere(x + y)
operator suspend fun unaryPlus() = suspendThere(x + "K")
operator suspend fun inc(): A = suspendCoroutineOrReturn { x ->
isProvideDelegateCalled = true
x.resume(this)
SUSPENDED_MARKER
}
operator suspend fun minusAssign(y: String): Unit = suspendCoroutineOrReturn { x ->
if (y != "56") return@suspendCoroutineOrReturn Unit
isMinusAssignCalled = true
x.resume(Unit)
SUSPENDED_MARKER
}
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
var a = A("O")
suspend fun foo1() {
var x by a
if (x != "OK") throw RuntimeException("fail 1")
x = "56"
if (!a.isSetValueCalled || !a.isProvideDelegateCalled) throw RuntimeException("fail 2")
}
suspend fun foo2() {
val (y) = a
if (y != "OK") throw RuntimeException("fail 3")
}
suspend fun foo3() {
val y = a + "K"
if (y != "OK") throw RuntimeException("fail 4")
}
suspend fun foo4() {
val y = + a
if (y != "OK") throw RuntimeException("fail 5")
}
// TODO: KT-15930
//suspend fun foo5() {
// a -= "56"
// if (!a.isMinusAssignCalled) throw RuntimeException("fail 6")
//}
suspend fun foo6() {
var y = a++
if (y.isIncCalled) throw RuntimeException("fail 7")
}
fun box(): String {
builder {
foo1()
foo2()
foo3()
foo4()
//foo5()
foo6()
}
return "OK"
}
@@ -0,0 +1,66 @@
@kotlin.Metadata
public final class A {
private field isIncCalled: boolean
private field isMinusAssignCalled: boolean
private field isProvideDelegateCalled: boolean
private field isSetValueCalled: boolean
private final @org.jetbrains.annotations.NotNull field x: java.lang.String
public method <init>(@org.jetbrains.annotations.NotNull p0: java.lang.String): void
public final @org.jetbrains.annotations.Nullable method component1(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.Continuation): java.lang.Object
public final @org.jetbrains.annotations.Nullable method getValue(@org.jetbrains.annotations.Nullable p0: java.lang.Object, @org.jetbrains.annotations.NotNull p1: kotlin.reflect.KProperty, @org.jetbrains.annotations.NotNull p2: kotlin.coroutines.Continuation): java.lang.Object
public final @org.jetbrains.annotations.NotNull method getX(): java.lang.String
public final @org.jetbrains.annotations.Nullable method inc(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.Continuation): java.lang.Object
public final method isIncCalled(): boolean
public final method isMinusAssignCalled(): boolean
public final method isProvideDelegateCalled(): boolean
public final method isSetValueCalled(): boolean
public final @org.jetbrains.annotations.Nullable method minusAssign(@org.jetbrains.annotations.NotNull p0: java.lang.String, @org.jetbrains.annotations.NotNull p1: kotlin.coroutines.Continuation): java.lang.Object
public final @org.jetbrains.annotations.Nullable method plus(@org.jetbrains.annotations.NotNull p0: java.lang.String, @org.jetbrains.annotations.NotNull p1: kotlin.coroutines.Continuation): java.lang.Object
public final @org.jetbrains.annotations.Nullable method provideDelegate(@org.jetbrains.annotations.Nullable p0: java.lang.Object, @org.jetbrains.annotations.NotNull p1: java.lang.Object, @org.jetbrains.annotations.NotNull p2: kotlin.coroutines.Continuation): java.lang.Object
public final method setIncCalled(p0: boolean): void
public final method setMinusAssignCalled(p0: boolean): void
public final method setProvideDelegateCalled(p0: boolean): void
public final method setSetValueCalled(p0: boolean): void
public final @org.jetbrains.annotations.Nullable method setValue(@org.jetbrains.annotations.Nullable p0: java.lang.Object, @org.jetbrains.annotations.NotNull p1: kotlin.reflect.KProperty, @org.jetbrains.annotations.NotNull p2: java.lang.String, @org.jetbrains.annotations.NotNull p3: kotlin.coroutines.Continuation): java.lang.Object
public final @org.jetbrains.annotations.Nullable method unaryPlus(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.Continuation): java.lang.Object
}
@kotlin.Metadata
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
}
@kotlin.Metadata
public class EmptyContinuation {
public final static field Companion: EmptyContinuation.Companion
private final @org.jetbrains.annotations.NotNull field context: kotlin.coroutines.CoroutineContext
inner class EmptyContinuation/Companion
public @synthetic.kotlin.jvm.GeneratedByJvmOverloads method <init>(): void
public method <init>(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.CoroutineContext): void
public synthetic method <init>(p0: kotlin.coroutines.CoroutineContext, p1: int, p2: kotlin.jvm.internal.DefaultConstructorMarker): void
public @org.jetbrains.annotations.NotNull method getContext(): kotlin.coroutines.CoroutineContext
public method resume(@org.jetbrains.annotations.Nullable p0: java.lang.Object): void
public method resumeWithException(@org.jetbrains.annotations.NotNull p0: java.lang.Throwable): void
}
@kotlin.Metadata
public final static class EmptyContinuation/Companion {
inner class EmptyContinuation/Companion
private method <init>(): void
}
@kotlin.Metadata
public final class OperatorsKt {
private static @org.jetbrains.annotations.NotNull field a: A
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 foo1(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.Continuation): java.lang.Object
public final static @org.jetbrains.annotations.Nullable method foo2(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.Continuation): java.lang.Object
public final static @org.jetbrains.annotations.Nullable method foo3(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.Continuation): java.lang.Object
public final static @org.jetbrains.annotations.Nullable method foo4(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.Continuation): java.lang.Object
public final static @org.jetbrains.annotations.Nullable method foo6(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.Continuation): java.lang.Object
public final static @org.jetbrains.annotations.NotNull method getA(): A
public final static method setA(@org.jetbrains.annotations.NotNull p0: A): void
public final static @org.jetbrains.annotations.Nullable method suspendThere(@org.jetbrains.annotations.NotNull p0: java.lang.String, @org.jetbrains.annotations.NotNull p1: kotlin.coroutines.Continuation): java.lang.Object
}