diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index 0fc25a7df9d..00b513aa0f0 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -12774,6 +12774,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/delegatedProperty/kt4138.kt"); } + @Test + @TestMetadata("kt45431.kt") + public void testKt45431() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/kt45431.kt"); + } + @Test @TestMetadata("kt6722.kt") public void testKt6722() throws Exception { diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ClassCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ClassCodegen.kt index d2dd80df2e6..9e595efa5df 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ClassCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ClassCodegen.kt @@ -133,7 +133,10 @@ class ClassCodegen private constructor( irClass.functions.find { it.name.asString() == "" }?.let { generateMethod(it, smap, delegatedPropertyOptimizer) } // 3. Now we have all the fields (`$$delegatedProperties` might be redundant if all reads were optimized out): for (field in irClass.fields) { - if (field !== delegatedProperties || delegatedPropertyOptimizer?.needsDelegatedProperties == true) { + if (field !== delegatedProperties || + delegatedPropertyOptimizer?.needsDelegatedProperties == true || + irClass.isCompanion + ) { generateField(field) } } @@ -326,7 +329,7 @@ class ClassCodegen private constructor( val (node, smap) = generateMethodNode(method) if (delegatedPropertyOptimizer != null) { delegatedPropertyOptimizer.transform(node) - if (method.name.asString() == "") { + if (method.name.asString() == "" && !irClass.isCompanion) { delegatedPropertyOptimizer.transformClassInitializer(node) } } diff --git a/compiler/testData/codegen/box/delegatedProperty/kt45431.kt b/compiler/testData/codegen/box/delegatedProperty/kt45431.kt new file mode 100644 index 00000000000..71e7286fed6 --- /dev/null +++ b/compiler/testData/codegen/box/delegatedProperty/kt45431.kt @@ -0,0 +1,16 @@ +// WITH_RUNTIME + +import kotlin.reflect.KProperty + +class DP { + operator fun provideDelegate(t: Any?, kp: KProperty<*>) = + lazy { "OK" } +} + +class H { + companion object { + val property: String by DP() + } +} + +fun box() = H.property diff --git a/compiler/testData/codegen/bytecodeListing/delegatedPropertiesInCompanionObject.kt b/compiler/testData/codegen/bytecodeListing/delegatedPropertiesInCompanionObject.kt new file mode 100644 index 00000000000..9bbc23bc54d --- /dev/null +++ b/compiler/testData/codegen/bytecodeListing/delegatedPropertiesInCompanionObject.kt @@ -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" } + } +} diff --git a/compiler/testData/codegen/bytecodeListing/delegatedPropertiesInCompanionObject.txt b/compiler/testData/codegen/bytecodeListing/delegatedPropertiesInCompanionObject.txt new file mode 100644 index 00000000000..9d39f7c801a --- /dev/null +++ b/compiler/testData/codegen/bytecodeListing/delegatedPropertiesInCompanionObject.txt @@ -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 (): void + method (): 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 (): 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 (): void + private method (): void + public synthetic method (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 (): void + public method (): 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 (): void + method (): 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 (): void + public synthetic method (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 (): void + public method (): void + public synthetic final static method access$getProperty$cp(): kotlin.Lazy + public final inner class H2$Companion +} diff --git a/compiler/testData/codegen/bytecodeListing/delegatedPropertiesInCompanionObject_ir.txt b/compiler/testData/codegen/bytecodeListing/delegatedPropertiesInCompanionObject_ir.txt new file mode 100644 index 00000000000..e4f0b669092 --- /dev/null +++ b/compiler/testData/codegen/bytecodeListing/delegatedPropertiesInCompanionObject_ir.txt @@ -0,0 +1,76 @@ +@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 (): void + method (): 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 (): 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 (): void + private method (): void + public synthetic method (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 (): void + public method (): 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.()V + public final static field INSTANCE: H2$Companion$property$2 + inner (anonymous) class H2$Companion$property$2 + static method (): void + method (): 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' + synthetic final static field $$delegatedProperties: kotlin.reflect.KProperty[] + static method (): void + private method (): void + public synthetic method (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 (): void + public method (): void + public synthetic final static method access$getProperty$delegate$cp(): kotlin.Lazy + public final inner class H2$Companion +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java index b97a8f4da7a..9863970bf21 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java @@ -12774,6 +12774,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/delegatedProperty/kt4138.kt"); } + @Test + @TestMetadata("kt45431.kt") + public void testKt45431() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/kt45431.kt"); + } + @Test @TestMetadata("kt6722.kt") public void testKt6722() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index b6c4355249a..515dd3d90cb 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -12774,6 +12774,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/delegatedProperty/kt4138.kt"); } + @Test + @TestMetadata("kt45431.kt") + public void testKt45431() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/kt45431.kt"); + } + @Test @TestMetadata("kt6722.kt") public void testKt6722() throws Exception { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/BytecodeListingTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/BytecodeListingTestGenerated.java index 818a87166a0..91de574ba02 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/BytecodeListingTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/BytecodeListingTestGenerated.java @@ -80,6 +80,11 @@ public class BytecodeListingTestGenerated extends AbstractBytecodeListingTest { runTest("compiler/testData/codegen/bytecodeListing/defaultImpls.kt"); } + @TestMetadata("delegatedPropertiesInCompanionObject.kt") + public void testDelegatedPropertiesInCompanionObject() throws Exception { + runTest("compiler/testData/codegen/bytecodeListing/delegatedPropertiesInCompanionObject.kt"); + } + @TestMetadata("delegationToJavaInterfaceWithWildcardType.kt") public void testDelegationToJavaInterfaceWithWildcardType() throws Exception { runTest("compiler/testData/codegen/bytecodeListing/delegationToJavaInterfaceWithWildcardType.kt"); diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 315d1dc4477..13d5b3350cd 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -10411,6 +10411,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/delegatedProperty/kt4138.kt"); } + @TestMetadata("kt45431.kt") + public void testKt45431() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/kt45431.kt"); + } + @TestMetadata("kt6722.kt") public void testKt6722() throws Exception { runTest("compiler/testData/codegen/box/delegatedProperty/kt6722.kt"); diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/ir/IrBytecodeListingTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/ir/IrBytecodeListingTestGenerated.java index 968c74442fe..2b3eef26e2c 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/ir/IrBytecodeListingTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/ir/IrBytecodeListingTestGenerated.java @@ -80,6 +80,11 @@ public class IrBytecodeListingTestGenerated extends AbstractIrBytecodeListingTes runTest("compiler/testData/codegen/bytecodeListing/defaultImpls.kt"); } + @TestMetadata("delegatedPropertiesInCompanionObject.kt") + public void testDelegatedPropertiesInCompanionObject() throws Exception { + runTest("compiler/testData/codegen/bytecodeListing/delegatedPropertiesInCompanionObject.kt"); + } + @TestMetadata("delegationToJavaInterfaceWithWildcardType.kt") public void testDelegationToJavaInterfaceWithWildcardType() throws Exception { runTest("compiler/testData/codegen/bytecodeListing/delegationToJavaInterfaceWithWildcardType.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index 648e4ca8177..6a2c595488d 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -9294,6 +9294,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/delegatedProperty/kt4138.kt"); } + @TestMetadata("kt45431.kt") + public void testKt45431() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/kt45431.kt"); + } + @TestMetadata("kt6722.kt") public void testKt6722() throws Exception { runTest("compiler/testData/codegen/box/delegatedProperty/kt6722.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index 13e8b6d6de1..533f1e5e59b 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -8751,6 +8751,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/delegatedProperty/kt4138.kt"); } + @TestMetadata("kt45431.kt") + public void testKt45431() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/kt45431.kt"); + } + @TestMetadata("kt6722.kt") public void testKt6722() throws Exception { runTest("compiler/testData/codegen/box/delegatedProperty/kt6722.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 4da07532996..aa9bc8aeec9 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -8751,6 +8751,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/delegatedProperty/kt4138.kt"); } + @TestMetadata("kt45431.kt") + public void testKt45431() throws Exception { + runTest("compiler/testData/codegen/box/delegatedProperty/kt45431.kt"); + } + @TestMetadata("kt6722.kt") public void testKt6722() throws Exception { runTest("compiler/testData/codegen/box/delegatedProperty/kt6722.kt");