JVM IR: change generation scheme of property $delegate methods

Generate $delegate method as instance method in
PropertyReferenceDelegationLowering, and remove dispatch receiver later
in MakePropertyDelegateMethodsStatic. The method needs to be static to
be non-overridable (see delegateMethodIsNonOverridable.kt), and public
to be accessible in reflection.

Otherwise we generated incorrect IR where a static function accessed an
instance field of the containing class, which failed in multiple places
including LocalDeclarationsLowering.

 #KT-48350 Fixed
This commit is contained in:
Alexander Udalov
2021-08-21 02:38:11 +02:00
committed by Alexander Udalov
parent 50508da156
commit 04c5bbdcf8
25 changed files with 565 additions and 48 deletions
@@ -0,0 +1,14 @@
// WITH_RUNTIME
val a = 1
val b = 2
open class C {
open val x by run { ::a }
open val y by ::a
}
class D : C() {
override val x by run { ::b }
override val y by ::b
}
@@ -0,0 +1,65 @@
@kotlin.Metadata
synthetic final class C$x$2$1 {
// source: 'delegateMethodIsNonOverridable.kt'
public final static field INSTANCE: kotlin.reflect.KProperty0
static method <clinit>(): void
method <init>(): void
public @org.jetbrains.annotations.Nullable method get(): java.lang.Object
}
@kotlin.Metadata
synthetic final class C$y$2 {
// source: 'delegateMethodIsNonOverridable.kt'
public final static field INSTANCE: kotlin.reflect.KProperty0
static method <clinit>(): void
method <init>(): void
public @org.jetbrains.annotations.Nullable method get(): java.lang.Object
}
@kotlin.Metadata
public class C {
// source: 'delegateMethodIsNonOverridable.kt'
private final @org.jetbrains.annotations.NotNull field x$delegate: kotlin.reflect.KProperty0
private final @org.jetbrains.annotations.NotNull field y$delegate: kotlin.reflect.KProperty0
public method <init>(): void
public method getX(): int
public method getY(): int
}
@kotlin.Metadata
synthetic final class D$x$2$1 {
// source: 'delegateMethodIsNonOverridable.kt'
public final static field INSTANCE: kotlin.reflect.KProperty0
static method <clinit>(): void
method <init>(): void
public @org.jetbrains.annotations.Nullable method get(): java.lang.Object
}
@kotlin.Metadata
synthetic final class D$y$2 {
// source: 'delegateMethodIsNonOverridable.kt'
public final static field INSTANCE: kotlin.reflect.KProperty0
static method <clinit>(): void
method <init>(): void
public @org.jetbrains.annotations.Nullable method get(): java.lang.Object
}
@kotlin.Metadata
public final class D {
// source: 'delegateMethodIsNonOverridable.kt'
private final @org.jetbrains.annotations.NotNull field x$delegate: kotlin.reflect.KProperty0
private final @org.jetbrains.annotations.NotNull field y$delegate: kotlin.reflect.KProperty0
public method <init>(): void
public method getX(): int
public method getY(): int
}
@kotlin.Metadata
public final class DelegateMethodIsNonOverridableKt {
// source: 'delegateMethodIsNonOverridable.kt'
private final static field a: int
private final static field b: int
static method <clinit>(): void
public final static method getA(): int
public final static method getB(): int
}
@@ -0,0 +1,53 @@
@kotlin.Metadata
synthetic final class C$x$2$1 {
// source: 'delegateMethodIsNonOverridable.kt'
enclosing method C.<init>()V
public final static field INSTANCE: C$x$2$1
inner (anonymous) class C$x$2$1
static method <clinit>(): void
method <init>(): void
public @org.jetbrains.annotations.Nullable method get(): java.lang.Object
}
@kotlin.Metadata
public class C {
// source: 'delegateMethodIsNonOverridable.kt'
private final @org.jetbrains.annotations.NotNull field x$delegate: kotlin.reflect.KProperty0
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
public method getY(): int
}
@kotlin.Metadata
synthetic final class D$x$2$1 {
// source: 'delegateMethodIsNonOverridable.kt'
enclosing method D.<init>()V
public final static field INSTANCE: D$x$2$1
inner (anonymous) class D$x$2$1
static method <clinit>(): void
method <init>(): void
public @org.jetbrains.annotations.Nullable method get(): java.lang.Object
}
@kotlin.Metadata
public final class D {
// source: 'delegateMethodIsNonOverridable.kt'
private final @org.jetbrains.annotations.NotNull field x$delegate: kotlin.reflect.KProperty0
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
public method getY(): int
}
@kotlin.Metadata
public final class DelegateMethodIsNonOverridableKt {
// source: 'delegateMethodIsNonOverridable.kt'
private final static field a: int
private final static field b: int
static method <clinit>(): void
public final static method getA(): int
public final static method getB(): int
}
@@ -0,0 +1,20 @@
// WITH_RUNTIME
import kotlin.reflect.KProperty
class DP {
operator fun provideDelegate(t: Any?, kp: KProperty<*>) =
lazy { "OK" }
}
class H1 {
companion object {
val property: String by DP()
}
}
class H2 {
companion object {
val property: String by lazy { "OK" }
}
}
@@ -0,0 +1,74 @@
@kotlin.Metadata
final class DP$provideDelegate$1 {
// source: 'delegatedPropertiesInCompanionObject.kt'
enclosing method DP.provideDelegate(Ljava/lang/Object;Lkotlin/reflect/KProperty;)Lkotlin/Lazy;
public final static field INSTANCE: DP$provideDelegate$1
inner (anonymous) class DP$provideDelegate$1
static method <clinit>(): void
method <init>(): void
public synthetic bridge method invoke(): java.lang.Object
public final @org.jetbrains.annotations.NotNull method invoke(): java.lang.String
}
@kotlin.Metadata
public final class DP {
// source: 'delegatedPropertiesInCompanionObject.kt'
inner (anonymous) class DP$provideDelegate$1
public method <init>(): void
public final @org.jetbrains.annotations.NotNull method provideDelegate(@org.jetbrains.annotations.Nullable p0: java.lang.Object, @org.jetbrains.annotations.NotNull p1: kotlin.reflect.KProperty): kotlin.Lazy
}
@kotlin.Metadata
public final class H1$Companion {
// source: 'delegatedPropertiesInCompanionObject.kt'
synthetic final static field $$delegatedProperties: kotlin.reflect.KProperty[]
static method <clinit>(): void
private method <init>(): void
public synthetic method <init>(p0: kotlin.jvm.internal.DefaultConstructorMarker): void
public final @org.jetbrains.annotations.NotNull method getProperty(): java.lang.String
public final inner class H1$Companion
}
@kotlin.Metadata
public final class H1 {
// source: 'delegatedPropertiesInCompanionObject.kt'
public final static @org.jetbrains.annotations.NotNull field Companion: H1$Companion
private final static @org.jetbrains.annotations.NotNull field property$delegate: kotlin.Lazy
static method <clinit>(): void
public method <init>(): void
public synthetic final static method access$getProperty$cp(): kotlin.Lazy
public final inner class H1$Companion
}
@kotlin.Metadata
final class H2$Companion$property$2 {
// source: 'delegatedPropertiesInCompanionObject.kt'
enclosing class H2
public final static field INSTANCE: H2$Companion$property$2
inner (anonymous) class H2$Companion$property$2
static method <clinit>(): void
method <init>(): void
public synthetic bridge method invoke(): java.lang.Object
public final @org.jetbrains.annotations.NotNull method invoke(): java.lang.String
}
@kotlin.Metadata
public final class H2$Companion {
// source: 'delegatedPropertiesInCompanionObject.kt'
private method <init>(): void
public synthetic method <init>(p0: kotlin.jvm.internal.DefaultConstructorMarker): void
public final @org.jetbrains.annotations.NotNull method getProperty(): java.lang.String
public final inner class H2$Companion
}
@kotlin.Metadata
public final class H2 {
// source: 'delegatedPropertiesInCompanionObject.kt'
public final static @org.jetbrains.annotations.NotNull field Companion: H2$Companion
private final static @org.jetbrains.annotations.NotNull field property$delegate: kotlin.Lazy
inner (anonymous) class H2$Companion$property$2
static method <clinit>(): void
public method <init>(): void
public synthetic final static method access$getProperty$cp(): kotlin.Lazy
public final inner class H2$Companion
}
@@ -0,0 +1,74 @@
@kotlin.Metadata
final class DP$provideDelegate$1 {
// source: 'delegatedPropertiesInCompanionObject.kt'
enclosing method DP.provideDelegate(Ljava/lang/Object;Lkotlin/reflect/KProperty;)Lkotlin/Lazy;
public final static field INSTANCE: DP$provideDelegate$1
inner (anonymous) class DP$provideDelegate$1
static method <clinit>(): void
method <init>(): void
public synthetic bridge method invoke(): java.lang.Object
public final @org.jetbrains.annotations.NotNull method invoke(): java.lang.String
}
@kotlin.Metadata
public final class DP {
// source: 'delegatedPropertiesInCompanionObject.kt'
inner (anonymous) class DP$provideDelegate$1
public method <init>(): void
public final @org.jetbrains.annotations.NotNull method provideDelegate(@org.jetbrains.annotations.Nullable p0: java.lang.Object, @org.jetbrains.annotations.NotNull p1: kotlin.reflect.KProperty): kotlin.Lazy
}
@kotlin.Metadata
public final class H1$Companion {
// source: 'delegatedPropertiesInCompanionObject.kt'
synthetic final static field $$delegatedProperties: kotlin.reflect.KProperty[]
static method <clinit>(): void
private method <init>(): void
public synthetic method <init>(p0: kotlin.jvm.internal.DefaultConstructorMarker): void
public final @org.jetbrains.annotations.NotNull method getProperty(): java.lang.String
public final inner class H1$Companion
}
@kotlin.Metadata
public final class H1 {
// source: 'delegatedPropertiesInCompanionObject.kt'
public final static @org.jetbrains.annotations.NotNull field Companion: H1$Companion
private final static @org.jetbrains.annotations.NotNull field property$delegate: kotlin.Lazy
static method <clinit>(): void
public method <init>(): void
public synthetic final static method access$getProperty$delegate$cp(): kotlin.Lazy
public final inner class H1$Companion
}
@kotlin.Metadata
final class H2$Companion$property$2 {
// source: 'delegatedPropertiesInCompanionObject.kt'
enclosing method H2.<clinit>()V
public final static field INSTANCE: H2$Companion$property$2
inner (anonymous) class H2$Companion$property$2
static method <clinit>(): void
method <init>(): void
public synthetic bridge method invoke(): java.lang.Object
public final @org.jetbrains.annotations.NotNull method invoke(): java.lang.String
}
@kotlin.Metadata
public final class H2$Companion {
// source: 'delegatedPropertiesInCompanionObject.kt'
private method <init>(): void
public synthetic method <init>(p0: kotlin.jvm.internal.DefaultConstructorMarker): void
public final @org.jetbrains.annotations.NotNull method getProperty(): java.lang.String
public final inner class H2$Companion
}
@kotlin.Metadata
public final class H2 {
// source: 'delegatedPropertiesInCompanionObject.kt'
public final static @org.jetbrains.annotations.NotNull field Companion: H2$Companion
private final static @org.jetbrains.annotations.NotNull field property$delegate: kotlin.Lazy
inner (anonymous) class H2$Companion$property$2
static method <clinit>(): void
public method <init>(): void
public synthetic final static method access$getProperty$delegate$cp(): kotlin.Lazy
public final inner class H2$Companion
}