KT-52592 Fix NPE from KProperty.getExtensionDelegate on property delegated to another property; make $delegate methods private

This commit is contained in:
Pavel Mikhailovskii
2022-06-01 11:44:00 +02:00
committed by teamcity
parent 315501debf
commit 3b5179686e
10 changed files with 64 additions and 11 deletions
@@ -42723,6 +42723,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/reflection/properties/getDelegate/getExtensionDelegate.kt"); runTest("compiler/testData/codegen/box/reflection/properties/getDelegate/getExtensionDelegate.kt");
} }
@Test
@TestMetadata("getExtensionDelegateForDelegatedToAnother.kt")
public void testGetExtensionDelegateForDelegatedToAnother() throws Exception {
runTest("compiler/testData/codegen/box/reflection/properties/getDelegate/getExtensionDelegateForDelegatedToAnother.kt");
}
@Test @Test
@TestMetadata("kPropertyForDelegatedProperty.kt") @TestMetadata("kPropertyForDelegatedProperty.kt")
public void testKPropertyForDelegatedProperty() throws Exception { public void testKPropertyForDelegatedProperty() throws Exception {
@@ -16,6 +16,8 @@ import org.jetbrains.kotlin.backend.jvm.ir.needsAccessor
import org.jetbrains.kotlin.backend.jvm.requiresMangling import org.jetbrains.kotlin.backend.jvm.requiresMangling
import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.DescriptorVisibility
import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
@@ -167,7 +169,7 @@ class JvmPropertiesLowering(private val backendContext: JvmBackendContext) : IrE
JvmLoweredDeclarationOrigin.SYNTHETIC_METHOD_FOR_PROPERTY_OR_TYPEALIAS_ANNOTATIONS, JvmLoweredDeclarationOrigin.SYNTHETIC_METHOD_FOR_PROPERTY_OR_TYPEALIAS_ANNOTATIONS,
// TODO: technically JVM permits having fields with same name but different type, so we could potentially // TODO: technically JVM permits having fields with same name but different type, so we could potentially
// generate two properties like that; should this be the getter's return type instead? // generate two properties like that; should this be the getter's return type instead?
isStatic = true, returnType = backendContext.irBuiltIns.unitType isStatic = true, returnType = backendContext.irBuiltIns.unitType, visibility = declaration.visibility
).apply { ).apply {
body = IrBlockBodyImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET) body = IrBlockBodyImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET)
annotations = declaration.annotations annotations = declaration.annotations
@@ -179,13 +181,14 @@ class JvmPropertiesLowering(private val backendContext: JvmBackendContext) : IrE
suffix: String, suffix: String,
origin: IrDeclarationOrigin, origin: IrDeclarationOrigin,
isStatic: Boolean, isStatic: Boolean,
returnType: IrType returnType: IrType,
visibility: DescriptorVisibility
) = irFactory.buildFun { ) = irFactory.buildFun {
name = Name.identifier(computeSyntheticMethodName(declaration, suffix)) name = Name.identifier(computeSyntheticMethodName(declaration, suffix))
modality = Modality.OPEN modality = Modality.OPEN
visibility = declaration.visibility
this.origin = origin this.origin = origin
this.returnType = returnType this.returnType = returnType
this.visibility = visibility
}.apply { }.apply {
if (!isStatic) { if (!isStatic) {
dispatchReceiverParameter = declaration.getter?.dispatchReceiverParameter?.let { dispatchReceiverParameter = declaration.getter?.dispatchReceiverParameter?.let {
@@ -207,7 +210,7 @@ class JvmPropertiesLowering(private val backendContext: JvmBackendContext) : IrE
declaration, declaration,
JvmAbi.DELEGATED_PROPERTY_NAME_SUFFIX, JvmAbi.DELEGATED_PROPERTY_NAME_SUFFIX,
IrDeclarationOrigin.PROPERTY_DELEGATE, IrDeclarationOrigin.PROPERTY_DELEGATE,
isStatic = false, returnType = irBuiltIns.anyNType isStatic = false, returnType = irBuiltIns.anyNType, visibility = DescriptorVisibilities.PRIVATE
) )
private fun JvmBackendContext.computeSyntheticMethodName(property: IrProperty, suffix: String): String { private fun JvmBackendContext.computeSyntheticMethodName(property: IrProperty, suffix: String): String {
@@ -0,0 +1,16 @@
// TARGET_BACKEND: JVM
// WITH_REFLECT
import kotlin.reflect.full.getExtensionDelegate
import kotlin.reflect.jvm.isAccessible
import kotlin.test.assertEquals
class A
val A.x: Int get() = 1
val A.y: Int by A::x
fun box(): String {
assertEquals(A::x, A::y.apply { isAccessible = true }.getExtensionDelegate())
return "OK"
}
@@ -11,11 +11,15 @@ object Delegate {
} }
val topLevel: Boolean by Delegate val topLevel: Boolean by Delegate
val delegated: Boolean by ::topLevel
val String.extension: Boolean by Delegate val String.extension: Boolean by Delegate
val String.delegated: Boolean by String::delegated
class Foo { class Foo {
val member: Boolean by Delegate val member: Boolean by Delegate
val delegated: Boolean by ::member
val String.memberExtension: Boolean by Delegate val String.memberExtension: Boolean by Delegate
val String.memberExtensionDelegated: Boolean by ::member
} }
inline fun check(block: () -> Unit) { inline fun check(block: () -> Unit) {
@@ -29,16 +33,24 @@ inline fun check(block: () -> Unit) {
fun box(): String { fun box(): String {
check { ::topLevel.getDelegate() } check { ::topLevel.getDelegate() }
check { ::delegated.getDelegate() }
check { String::extension.getDelegate("") } check { String::extension.getDelegate("") }
check { ""::extension.getDelegate() } check { ""::extension.getDelegate() }
check { String::delegated.getDelegate("") }
check { ""::delegated.getDelegate() }
val foo = Foo() val foo = Foo()
check { Foo::member.getDelegate(foo) } check { Foo::member.getDelegate(foo) }
check { foo::member.getDelegate() } check { foo::member.getDelegate() }
check { Foo::delegated.getDelegate(foo) }
check { foo::delegated.getDelegate() }
val me = Foo::class.members.single { it.name == "memberExtension" } as KProperty2<Foo, String, Boolean> val me = Foo::class.members.single { it.name == "memberExtension" } as KProperty2<Foo, String, Boolean>
check { me.getDelegate(foo, "") } check { me.getDelegate(foo, "") }
val med = Foo::class.members.single { it.name == "memberExtensionDelegated" } as KProperty2<Foo, String, Boolean>
check { med.getDelegate(foo, "") }
return "OK" return "OK"
} }
@@ -16,7 +16,7 @@ public class C {
inner (anonymous) class C$x$2$1 inner (anonymous) class C$x$2$1
public method <init>(): void public method <init>(): void
public method getX(): int public method getX(): int
public static method getY$delegate(p0: C): java.lang.Object private static method getY$delegate(p0: C): java.lang.Object
public method getY(): int public method getY(): int
} }
@@ -38,7 +38,7 @@ public final class D {
inner (anonymous) class D$x$2$1 inner (anonymous) class D$x$2$1
public method <init>(): void public method <init>(): void
public method getX(): int public method getX(): int
public static method getY$delegate(p0: D): java.lang.Object private static method getY$delegate(p0: D): java.lang.Object
public method getY(): int public method getY(): int
} }
@@ -35,7 +35,7 @@ fun local() {
// 0 private final( static)? Lkotlin/reflect/KMutableProperty[0-2]; [xyz]m?\$delegate // 0 private final( static)? Lkotlin/reflect/KMutableProperty[0-2]; [xyz]m?\$delegate
// 2 private final( static)? LC; [xyz]m?\$receiver // 2 private final( static)? LC; [xyz]m?\$receiver
// 0 LOCALVARIABLE [xyz]m? Lkotlin/reflect/KMutableProperty[0-2]; // 0 LOCALVARIABLE [xyz]m? Lkotlin/reflect/KMutableProperty[0-2];
// 12 public( static)? get[XYZ]m?\$delegate // 12 private( static)? get[XYZ]m?\$delegate
// JVM_TEMPLATES // JVM_TEMPLATES
// Not optimized, references created as classes and stored in fields: // Not optimized, references created as classes and stored in fields:
@@ -42201,6 +42201,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/reflection/properties/getDelegate/getExtensionDelegate.kt"); runTest("compiler/testData/codegen/box/reflection/properties/getDelegate/getExtensionDelegate.kt");
} }
@Test
@TestMetadata("getExtensionDelegateForDelegatedToAnother.kt")
public void testGetExtensionDelegateForDelegatedToAnother() throws Exception {
runTest("compiler/testData/codegen/box/reflection/properties/getDelegate/getExtensionDelegateForDelegatedToAnother.kt");
}
@Test @Test
@TestMetadata("kPropertyForDelegatedProperty.kt") @TestMetadata("kPropertyForDelegatedProperty.kt")
public void testKPropertyForDelegatedProperty() throws Exception { public void testKPropertyForDelegatedProperty() throws Exception {
@@ -42723,6 +42723,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/reflection/properties/getDelegate/getExtensionDelegate.kt"); runTest("compiler/testData/codegen/box/reflection/properties/getDelegate/getExtensionDelegate.kt");
} }
@Test
@TestMetadata("getExtensionDelegateForDelegatedToAnother.kt")
public void testGetExtensionDelegateForDelegatedToAnother() throws Exception {
runTest("compiler/testData/codegen/box/reflection/properties/getDelegate/getExtensionDelegateForDelegatedToAnother.kt");
}
@Test @Test
@TestMetadata("kPropertyForDelegatedProperty.kt") @TestMetadata("kPropertyForDelegatedProperty.kt")
public void testKPropertyForDelegatedProperty() throws Exception { public void testKPropertyForDelegatedProperty() throws Exception {
@@ -33942,6 +33942,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/reflection/properties/getDelegate/getExtensionDelegate.kt"); runTest("compiler/testData/codegen/box/reflection/properties/getDelegate/getExtensionDelegate.kt");
} }
@TestMetadata("getExtensionDelegateForDelegatedToAnother.kt")
public void testGetExtensionDelegateForDelegatedToAnother() throws Exception {
runTest("compiler/testData/codegen/box/reflection/properties/getDelegate/getExtensionDelegateForDelegatedToAnother.kt");
}
@TestMetadata("kPropertyForDelegatedProperty.kt") @TestMetadata("kPropertyForDelegatedProperty.kt")
public void testKPropertyForDelegatedProperty() throws Exception { public void testKPropertyForDelegatedProperty() throws Exception {
runTest("compiler/testData/codegen/box/reflection/properties/getDelegate/kPropertyForDelegatedProperty.kt"); runTest("compiler/testData/codegen/box/reflection/properties/getDelegate/kPropertyForDelegatedProperty.kt");
@@ -14,10 +14,7 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.isUnderlyingPropertyOfInlineClass import org.jetbrains.kotlin.resolve.isUnderlyingPropertyOfInlineClass
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedPropertyDescriptor import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedPropertyDescriptor
import org.jetbrains.kotlin.types.TypeUtils import org.jetbrains.kotlin.types.TypeUtils
import java.lang.reflect.Field import java.lang.reflect.*
import java.lang.reflect.Member
import java.lang.reflect.Method
import java.lang.reflect.Modifier
import kotlin.jvm.internal.CallableReference import kotlin.jvm.internal.CallableReference
import kotlin.reflect.KFunction import kotlin.reflect.KFunction
import kotlin.reflect.KMutableProperty import kotlin.reflect.KMutableProperty
@@ -25,6 +22,7 @@ import kotlin.reflect.KProperty
import kotlin.reflect.full.IllegalPropertyDelegateAccessException import kotlin.reflect.full.IllegalPropertyDelegateAccessException
import kotlin.reflect.jvm.internal.JvmPropertySignature.* import kotlin.reflect.jvm.internal.JvmPropertySignature.*
import kotlin.reflect.jvm.internal.calls.* import kotlin.reflect.jvm.internal.calls.*
import kotlin.reflect.jvm.isAccessible
internal abstract class KPropertyImpl<out V> private constructor( internal abstract class KPropertyImpl<out V> private constructor(
override val container: KDeclarationContainerImpl, override val container: KDeclarationContainerImpl,
@@ -105,6 +103,7 @@ internal abstract class KPropertyImpl<out V> private constructor(
val realReceiver1 = (if (isBound) boundReceiver else receiver1).takeIf { it !== EXTENSION_PROPERTY_DELEGATE } val realReceiver1 = (if (isBound) boundReceiver else receiver1).takeIf { it !== EXTENSION_PROPERTY_DELEGATE }
val realReceiver2 = (if (isBound) receiver1 else receiver2).takeIf { it !== EXTENSION_PROPERTY_DELEGATE } val realReceiver2 = (if (isBound) receiver1 else receiver2).takeIf { it !== EXTENSION_PROPERTY_DELEGATE }
(fieldOrMethod as? AccessibleObject)?.isAccessible = isAccessible
when (fieldOrMethod) { when (fieldOrMethod) {
null -> null null -> null
is Field -> fieldOrMethod.get(realReceiver1) is Field -> fieldOrMethod.get(realReceiver1)