Fix type of reference to protected var

#KT-12982 Fixed
This commit is contained in:
Alexander Udalov
2016-07-21 15:42:23 +03:00
parent 6baf3ae737
commit 9ab8da2ef9
6 changed files with 86 additions and 2 deletions
@@ -527,8 +527,7 @@ class DoubleColonExpressionResolver(
is PropertyDescriptor -> {
val mutable = descriptor.isVar && run {
val setter = descriptor.setter
// TODO: support private-to-this
setter == null || Visibilities.isVisible(null, setter, scopeOwnerDescriptor)
setter == null || Visibilities.isVisible(receiverType?.let(::TransientReceiver), setter, scopeOwnerDescriptor)
}
reflectionTypes.getKPropertyType(Annotations.EMPTY, receiverType, descriptor.type, mutable)
}
@@ -0,0 +1,12 @@
class Foo {
protected var x = 0
fun getX() = Foo::x
}
fun box(): String {
val x = Foo().getX()
val foo = Foo()
x.set(foo, 42)
return if (x.get(foo) == 42) "OK" else "Fail"
}
@@ -0,0 +1,22 @@
// KT-12982 Incorrect type inference when accessing mutable protected property via reflection
import kotlin.reflect.KMutableProperty1
class Foo {
protected var x = 0
fun baz(p: KMutableProperty1<Foo, Int>) = p
fun print() = baz(Foo::x)
}
open class A {
protected fun a() {}
}
open class B : A() {
val x = C::a
val y = C()::a
}
class C : B()
@@ -0,0 +1,39 @@
package
public open class A {
public constructor A()
protected final fun a(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public open class B : A {
public constructor B()
public final val x: kotlin.reflect.KFunction1<C, kotlin.Unit>
public final val y: kotlin.reflect.KFunction0<kotlin.Unit>
protected final override /*1*/ /*fake_override*/ fun a(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class C : B {
public constructor C()
public final override /*1*/ /*fake_override*/ val x: kotlin.reflect.KFunction1<C, kotlin.Unit>
public final override /*1*/ /*fake_override*/ val y: kotlin.reflect.KFunction0<kotlin.Unit>
protected final override /*1*/ /*fake_override*/ fun a(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Foo {
public constructor Foo()
protected final var x: kotlin.Int
public final fun baz(/*0*/ p: kotlin.reflect.KMutableProperty1<Foo, kotlin.Int>): kotlin.reflect.KMutableProperty1<Foo, kotlin.Int>
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final fun print(): kotlin.reflect.KMutableProperty1<Foo, kotlin.Int>
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -2204,6 +2204,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("protectedVarFromClass.kt")
public void testProtectedVarFromClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/callableReference/property/protectedVarFromClass.kt");
doTest(fileName);
}
@TestMetadata("returnTypeDependentOnGenericProperty.kt")
public void testReturnTypeDependentOnGenericProperty() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/callableReference/property/returnTypeDependentOnGenericProperty.kt");
@@ -1950,6 +1950,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("kt12982_protectedPropertyReference.kt")
public void testKt12982_protectedPropertyReference() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/callableReference/property/kt12982_protectedPropertyReference.kt");
doTest(fileName);
}
@TestMetadata("kt6870_privatePropertyReference.kt")
public void testKt6870_privatePropertyReference() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/callableReference/property/kt6870_privatePropertyReference.kt");