Infer suspend flag for local delegated property accessors

Local delegated property accessors calling suspend operators getValue
or setValue should be suspend functions themselves.

KT-17605 Getter and setter of suspend delegated property are not suspend
This commit is contained in:
Dmitry Petrov
2017-04-28 15:05:39 +03:00
parent 0624854d5a
commit 6cefc0ddf3
4 changed files with 138 additions and 3 deletions
@@ -20,9 +20,7 @@ import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor
import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl
import org.jetbrains.kotlin.descriptors.impl.VariableDescriptorWithInitializerImpl
import org.jetbrains.kotlin.descriptors.impl.*
import org.jetbrains.kotlin.diagnostics.Errors.*
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtProperty
@@ -86,6 +84,8 @@ class LocalVariableResolver(
delegateExpression,
typingContext.scope,
typingContext.trace)
propertyDescriptor.getter?.updateAccessorFlagsFromResolvedCallForDelegatedProperty(typingContext.trace)
propertyDescriptor.setter?.updateAccessorFlagsFromResolvedCallForDelegatedProperty(typingContext.trace)
}
}
@@ -212,4 +212,13 @@ class LocalVariableResolver(
trace.record(BindingContext.VARIABLE, variable, variableDescriptor)
return variableDescriptor
}
private fun VariableAccessorDescriptor.updateAccessorFlagsFromResolvedCallForDelegatedProperty(trace: BindingTrace) {
if (this is FunctionDescriptorImpl) {
val resultingDescriptor = trace.bindingContext.get(BindingContext.DELEGATED_PROPERTY_RESOLVED_CALL, this)?.resultingDescriptor
if (resultingDescriptor != null) {
setSuspend(resultingDescriptor.isSuspend)
}
}
}
}
@@ -0,0 +1,27 @@
// WITH_RUNTIME
import kotlin.coroutines.experimental.intrinsics.COROUTINE_SUSPENDED
import kotlin.coroutines.experimental.intrinsics.suspendCoroutineOrReturn
import kotlin.reflect.KProperty
class A {
var z: Int = 42
operator suspend fun getValue(thisRef: Any?, property: KProperty<*>) = z
operator suspend fun setValue(thisRef: Any?, property: KProperty<*>, value: Int): Unit = suspendCoroutineOrReturn { x ->
z = value
x.resume(Unit)
COROUTINE_SUSPENDED
}
operator suspend fun provideDelegate(host: Any?, p: Any): A = suspendCoroutineOrReturn { x ->
x.resume(this)
COROUTINE_SUSPENDED
}
}
suspend fun test() {
val testVal by A()
var testVar by A()
}
@@ -0,0 +1,93 @@
FILE /localDelegatedPropertyWithSuspendOperators.kt
CLASS CLASS A
CONSTRUCTOR public constructor A()
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
INSTANCE_INITIALIZER_CALL classDescriptor='A'
PROPERTY public final var z: kotlin.Int
FIELD PROPERTY_BACKING_FIELD public final var z: kotlin.Int
EXPRESSION_BODY
CONST Int type=kotlin.Int value='42'
FUN DEFAULT_PROPERTY_ACCESSOR public final fun <get-z>(): kotlin.Int
BLOCK_BODY
RETURN type=kotlin.Nothing from='<get-z>(): Int'
GET_FIELD 'z: Int' type=kotlin.Int origin=null
receiver: GET_VAR '<receiver: A>' type=A origin=null
FUN DEFAULT_PROPERTY_ACCESSOR public final fun <set-z>(<set-?>: kotlin.Int): kotlin.Unit
BLOCK_BODY
SET_FIELD 'z: Int' type=kotlin.Unit origin=null
receiver: GET_VAR '<receiver: A>' type=A origin=null
value: GET_VAR 'value-parameter <set-?>: Int' type=kotlin.Int origin=null
FUN public final operator suspend fun getValue(thisRef: kotlin.Any?, property: kotlin.reflect.KProperty<*>): kotlin.Int
BLOCK_BODY
RETURN type=kotlin.Nothing from='getValue(Any?, KProperty<*>): Int'
CALL '<get-z>(): Int' type=kotlin.Int origin=GET_PROPERTY
$this: GET_VAR '<receiver: A>' type=A origin=null
FUN public final operator suspend fun setValue(thisRef: kotlin.Any?, property: kotlin.reflect.KProperty<*>, value: kotlin.Int): kotlin.Unit
BLOCK_BODY
RETURN type=kotlin.Nothing from='setValue(Any?, KProperty<*>, Int): Unit'
CALL 'suspendCoroutineOrReturn(crossinline (Continuation<Unit>) -> Any?): Unit' type=kotlin.Unit origin=null
<T>: Unit
block: BLOCK type=(kotlin.coroutines.experimental.Continuation<kotlin.Unit>) -> kotlin.Any origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA local final fun <anonymous>(x: kotlin.coroutines.experimental.Continuation<kotlin.Unit>): kotlin.Any
BLOCK_BODY
CALL '<set-z>(Int): Unit' type=kotlin.Unit origin=EQ
$this: GET_VAR '<receiver: A>' type=A origin=null
<set-?>: GET_VAR 'value-parameter value: Int' type=kotlin.Int origin=null
CALL 'resume(Unit): Unit' type=kotlin.Unit origin=null
$this: GET_VAR 'value-parameter x: Continuation<Unit>' type=kotlin.coroutines.experimental.Continuation<kotlin.Unit> origin=null
value: GET_OBJECT 'Unit' type=kotlin.Unit
RETURN type=kotlin.Nothing from='<anonymous>(Continuation<Unit>): Any'
CALL '<get-COROUTINE_SUSPENDED>(): Any' type=kotlin.Any origin=GET_PROPERTY
CALLABLE_REFERENCE '<anonymous>(Continuation<Unit>): Any' type=(kotlin.coroutines.experimental.Continuation<kotlin.Unit>) -> kotlin.Any origin=LAMBDA
FUN public final operator suspend fun provideDelegate(host: kotlin.Any?, p: kotlin.Any): A
BLOCK_BODY
RETURN type=kotlin.Nothing from='provideDelegate(Any?, Any): A'
CALL 'suspendCoroutineOrReturn(crossinline (Continuation<A>) -> Any?): A' type=A origin=null
<T>: A
block: BLOCK type=(kotlin.coroutines.experimental.Continuation<A>) -> kotlin.Any origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA local final fun <anonymous>(x: kotlin.coroutines.experimental.Continuation<A>): kotlin.Any
BLOCK_BODY
CALL 'resume(A): Unit' type=kotlin.Unit origin=null
$this: GET_VAR 'value-parameter x: Continuation<A>' type=kotlin.coroutines.experimental.Continuation<A> origin=null
value: GET_VAR '<receiver: A>' type=A origin=null
RETURN type=kotlin.Nothing from='<anonymous>(Continuation<A>): Any'
CALL '<get-COROUTINE_SUSPENDED>(): Any' type=kotlin.Any origin=GET_PROPERTY
CALLABLE_REFERENCE '<anonymous>(Continuation<A>): Any' type=(kotlin.coroutines.experimental.Continuation<A>) -> kotlin.Any origin=LAMBDA
FUN public suspend fun test(): kotlin.Unit
BLOCK_BODY
LOCAL_DELEGATED_PROPERTY val testVal: kotlin.Int
VAR DELEGATE val `testVal$delegate`: A
CALL 'provideDelegate(Any?, Any): A' type=A origin=null
$this: CALL 'constructor A()' type=A origin=null
host: CONST Null type=kotlin.Nothing? value='null'
p: CALLABLE_REFERENCE 'testVal: Int' type=kotlin.reflect.KProperty0<kotlin.Int> origin=PROPERTY_REFERENCE_FOR_DELEGATE
FUN DELEGATED_PROPERTY_ACCESSOR local final suspend fun <get-testVal>(): kotlin.Int
BLOCK_BODY
RETURN type=kotlin.Nothing from='<get-testVal>(): Int'
CALL 'getValue(Any?, KProperty<*>): Int' type=kotlin.Int origin=null
$this: GET_VAR '`testVal$delegate`: A' type=A origin=null
thisRef: CONST Null type=kotlin.Nothing? value='null'
property: CALLABLE_REFERENCE 'testVal: Int' type=kotlin.reflect.KProperty0<kotlin.Int> origin=PROPERTY_REFERENCE_FOR_DELEGATE
LOCAL_DELEGATED_PROPERTY var testVar: kotlin.Int
VAR DELEGATE val `testVar$delegate`: A
CALL 'provideDelegate(Any?, Any): A' type=A origin=null
$this: CALL 'constructor A()' type=A origin=null
host: CONST Null type=kotlin.Nothing? value='null'
p: CALLABLE_REFERENCE 'testVar: Int' type=kotlin.reflect.KMutableProperty0<kotlin.Int> origin=PROPERTY_REFERENCE_FOR_DELEGATE
FUN DELEGATED_PROPERTY_ACCESSOR local final suspend fun <get-testVar>(): kotlin.Int
BLOCK_BODY
RETURN type=kotlin.Nothing from='<get-testVar>(): Int'
CALL 'getValue(Any?, KProperty<*>): Int' type=kotlin.Int origin=null
$this: GET_VAR '`testVar$delegate`: A' type=A origin=null
thisRef: CONST Null type=kotlin.Nothing? value='null'
property: CALLABLE_REFERENCE 'testVar: Int' type=kotlin.reflect.KMutableProperty0<kotlin.Int> origin=PROPERTY_REFERENCE_FOR_DELEGATE
FUN DELEGATED_PROPERTY_ACCESSOR local final suspend fun <set-testVar>(value: kotlin.Int): kotlin.Int
BLOCK_BODY
RETURN type=kotlin.Nothing from='<set-testVar>(Int): Int'
TYPE_OP type=kotlin.Int origin=IMPLICIT_CAST typeOperand=kotlin.Int
CALL 'setValue(Any?, KProperty<*>, Int): Unit' type=kotlin.Unit origin=null
$this: GET_VAR '`testVar$delegate`: A' type=A origin=null
thisRef: CONST Null type=kotlin.Nothing? value='null'
property: CALLABLE_REFERENCE 'testVar: Int' type=kotlin.reflect.KMutableProperty0<kotlin.Int> origin=PROPERTY_REFERENCE_FOR_DELEGATE
value: GET_VAR 'value-parameter value: Int' type=kotlin.Int origin=null
@@ -257,6 +257,12 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase {
doTest(fileName);
}
@TestMetadata("localDelegatedPropertyWithSuspendOperators.kt")
public void testLocalDelegatedPropertyWithSuspendOperators() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/declarations/localDelegatedPropertyWithSuspendOperators.kt");
doTest(fileName);
}
@TestMetadata("packageLevelProperties.kt")
public void testPackageLevelProperties() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/declarations/packageLevelProperties.kt");