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");
}
@Test
@TestMetadata("getExtensionDelegateForDelegatedToAnother.kt")
public void testGetExtensionDelegateForDelegatedToAnother() throws Exception {
runTest("compiler/testData/codegen/box/reflection/properties/getDelegate/getExtensionDelegateForDelegatedToAnother.kt");
}
@Test
@TestMetadata("kPropertyForDelegatedProperty.kt")
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.config.LanguageFeature
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.ir.IrStatement
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,
// 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?
isStatic = true, returnType = backendContext.irBuiltIns.unitType
isStatic = true, returnType = backendContext.irBuiltIns.unitType, visibility = declaration.visibility
).apply {
body = IrBlockBodyImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET)
annotations = declaration.annotations
@@ -179,13 +181,14 @@ class JvmPropertiesLowering(private val backendContext: JvmBackendContext) : IrE
suffix: String,
origin: IrDeclarationOrigin,
isStatic: Boolean,
returnType: IrType
returnType: IrType,
visibility: DescriptorVisibility
) = irFactory.buildFun {
name = Name.identifier(computeSyntheticMethodName(declaration, suffix))
modality = Modality.OPEN
visibility = declaration.visibility
this.origin = origin
this.returnType = returnType
this.visibility = visibility
}.apply {
if (!isStatic) {
dispatchReceiverParameter = declaration.getter?.dispatchReceiverParameter?.let {
@@ -207,7 +210,7 @@ class JvmPropertiesLowering(private val backendContext: JvmBackendContext) : IrE
declaration,
JvmAbi.DELEGATED_PROPERTY_NAME_SUFFIX,
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 {
@@ -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 delegated: Boolean by ::topLevel
val String.extension: Boolean by Delegate
val String.delegated: Boolean by String::delegated
class Foo {
val member: Boolean by Delegate
val delegated: Boolean by ::member
val String.memberExtension: Boolean by Delegate
val String.memberExtensionDelegated: Boolean by ::member
}
inline fun check(block: () -> Unit) {
@@ -29,16 +33,24 @@ inline fun check(block: () -> Unit) {
fun box(): String {
check { ::topLevel.getDelegate() }
check { ::delegated.getDelegate() }
check { String::extension.getDelegate("") }
check { ""::extension.getDelegate() }
check { String::delegated.getDelegate("") }
check { ""::delegated.getDelegate() }
val foo = Foo()
check { Foo::member.getDelegate(foo) }
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>
check { me.getDelegate(foo, "") }
val med = Foo::class.members.single { it.name == "memberExtensionDelegated" } as KProperty2<Foo, String, Boolean>
check { med.getDelegate(foo, "") }
return "OK"
}
@@ -16,7 +16,7 @@ public class C {
inner (anonymous) class C$x$2$1
public method <init>(): void
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
}
@@ -38,7 +38,7 @@ public final class D {
inner (anonymous) class D$x$2$1
public method <init>(): void
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
}
@@ -35,7 +35,7 @@ fun local() {
// 0 private final( static)? Lkotlin/reflect/KMutableProperty[0-2]; [xyz]m?\$delegate
// 2 private final( static)? LC; [xyz]m?\$receiver
// 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
// 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");
}
@Test
@TestMetadata("getExtensionDelegateForDelegatedToAnother.kt")
public void testGetExtensionDelegateForDelegatedToAnother() throws Exception {
runTest("compiler/testData/codegen/box/reflection/properties/getDelegate/getExtensionDelegateForDelegatedToAnother.kt");
}
@Test
@TestMetadata("kPropertyForDelegatedProperty.kt")
public void testKPropertyForDelegatedProperty() throws Exception {
@@ -42723,6 +42723,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
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
@TestMetadata("kPropertyForDelegatedProperty.kt")
public void testKPropertyForDelegatedProperty() throws Exception {
@@ -33942,6 +33942,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
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")
public void testKPropertyForDelegatedProperty() throws Exception {
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.serialization.deserialization.descriptors.DeserializedPropertyDescriptor
import org.jetbrains.kotlin.types.TypeUtils
import java.lang.reflect.Field
import java.lang.reflect.Member
import java.lang.reflect.Method
import java.lang.reflect.Modifier
import java.lang.reflect.*
import kotlin.jvm.internal.CallableReference
import kotlin.reflect.KFunction
import kotlin.reflect.KMutableProperty
@@ -25,6 +22,7 @@ import kotlin.reflect.KProperty
import kotlin.reflect.full.IllegalPropertyDelegateAccessException
import kotlin.reflect.jvm.internal.JvmPropertySignature.*
import kotlin.reflect.jvm.internal.calls.*
import kotlin.reflect.jvm.isAccessible
internal abstract class KPropertyImpl<out V> private constructor(
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 realReceiver2 = (if (isBound) receiver1 else receiver2).takeIf { it !== EXTENSION_PROPERTY_DELEGATE }
(fieldOrMethod as? AccessibleObject)?.isAccessible = isAccessible
when (fieldOrMethod) {
null -> null
is Field -> fieldOrMethod.get(realReceiver1)