From f05f9882aba9a270f0233354b4dc9cf7ae86d14a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Sch=C3=A4fer?= Date: Thu, 19 Aug 2021 16:38:33 +0200 Subject: [PATCH] IR: Handle annotation instantiations in constructor calls --- .../codegen/FirBlackBoxCodegenTestGenerated.java | 6 ++++++ .../lower/AnnotationImplementationTransformer.kt | 6 +++--- .../instances/nestedAnnotationInstances.kt | 14 ++++++++++++++ .../codegen/IrBlackBoxCodegenTestGenerated.java | 6 ++++++ 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/codegen/box/annotations/instances/nestedAnnotationInstances.kt 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 9d66ea64a65..f70f14f3ff2 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 @@ -454,6 +454,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT public void testMultiplatformInstantiation() throws Exception { runTest("compiler/testData/codegen/box/annotations/instances/multiplatformInstantiation.kt"); } + + @Test + @TestMetadata("nestedAnnotationInstances.kt") + public void testNestedAnnotationInstances() throws Exception { + runTest("compiler/testData/codegen/box/annotations/instances/nestedAnnotationInstances.kt"); + } } @Nested diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AnnotationImplementationTransformer.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AnnotationImplementationTransformer.kt index 249d05441bd..75900ed5085 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AnnotationImplementationTransformer.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AnnotationImplementationTransformer.kt @@ -50,9 +50,9 @@ open class AnnotationImplementationTransformer(val context: BackendContext, val internal val implementations: MutableMap = mutableMapOf() override fun visitConstructorCall(expression: IrConstructorCall): IrExpression { - val constructedClass = expression.type.classOrNull?.owner ?: return expression - if (!constructedClass.isAnnotationClass) return expression - if (constructedClass.typeParameters.isNotEmpty()) return expression // Not supported yet + val constructedClass = expression.type.classOrNull?.owner ?: return super.visitConstructorCall(expression) + if (!constructedClass.isAnnotationClass) return super.visitConstructorCall(expression) + if (constructedClass.typeParameters.isNotEmpty()) return super.visitConstructorCall(expression) // Not supported yet val implClass = implementations.getOrPut(constructedClass) { createAnnotationImplementation(constructedClass) } val ctor = implClass.constructors.single() diff --git a/compiler/testData/codegen/box/annotations/instances/nestedAnnotationInstances.kt b/compiler/testData/codegen/box/annotations/instances/nestedAnnotationInstances.kt new file mode 100644 index 00000000000..ddc3b6a4e63 --- /dev/null +++ b/compiler/testData/codegen/box/annotations/instances/nestedAnnotationInstances.kt @@ -0,0 +1,14 @@ +// IGNORE_BACKEND_FIR: JVM_IR +// TARGET_BACKEND: JVM_IR + +// WITH_RUNTIME +// !LANGUAGE: +InstantiationOfAnnotationClasses + +annotation class NestedAnnotation(val value: String) +annotation class OuterAnnotation(val nested: NestedAnnotation) +class Outer(val nested: NestedAnnotation, val outer: OuterAnnotation) + +fun box(): String { + val anno = Outer(NestedAnnotation("O"), OuterAnnotation(NestedAnnotation("K"))) + return anno.nested.value + anno.outer.nested.value +} 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 f087b7d302d..45dc7f7a2dc 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 @@ -454,6 +454,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes public void testMultiplatformInstantiation() throws Exception { runTest("compiler/testData/codegen/box/annotations/instances/multiplatformInstantiation.kt"); } + + @Test + @TestMetadata("nestedAnnotationInstances.kt") + public void testNestedAnnotationInstances() throws Exception { + runTest("compiler/testData/codegen/box/annotations/instances/nestedAnnotationInstances.kt"); + } } @Nested