From d5f6674d2d5b4f3e42d01343e70cf3a1e4d64d34 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Thu, 21 Oct 2021 17:55:46 +0300 Subject: [PATCH] JVM_IR KT-49335 run RepeatedAnnotationLowering per module Otherwise, we drop annotation constructors in AnnotationLowering, which breaks RAL in case of annotation container declared in different file. --- .../FirBlackBoxCodegenTestGenerated.java | 6 +++ .../jetbrains/kotlin/backend/jvm/JvmLower.kt | 2 +- .../jvm/lower/RepeatedAnnotationLowering.kt | 4 +- .../annotations/repeatable/kt49335.kt | 38 +++++++++++++++++++ .../IrBlackBoxCodegenTestGenerated.java | 6 +++ 5 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/codegen/box/reflection/annotations/repeatable/kt49335.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 3c56ac37c8b..c93b9a86f3c 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 @@ -35970,6 +35970,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/reflection/annotations/repeatable/kotlinAnnotation.kt"); } + @Test + @TestMetadata("kt49335.kt") + public void testKt49335() throws Exception { + runTest("compiler/testData/codegen/box/reflection/annotations/repeatable/kt49335.kt"); + } + @Test @TestMetadata("nonRepeatedAnnotationWithItsContainer.kt") public void testNonRepeatedAnnotationWithItsContainer() throws Exception { diff --git a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt index e3734c2f4c8..657abc94d0d 100644 --- a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt +++ b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt @@ -324,7 +324,6 @@ private val jvmFilePhases = listOf( inventNamesForLocalClassesPhase, kCallableNamePropertyPhase, - repeatedAnnotationPhase, annotationPhase, annotationImplementationPhase, polymorphicSignaturePhase, @@ -437,6 +436,7 @@ private val jvmLoweringPhases = NamedCompilerPhase( scriptsToClassesPhase then fileClassPhase then jvmStaticInObjectPhase then + repeatedAnnotationPhase then performByIrFile(lower = jvmFilePhases) then generateMultifileFacadesPhase then resolveInlineCallsPhase then diff --git a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/RepeatedAnnotationLowering.kt b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/RepeatedAnnotationLowering.kt index c3ea5e11b41..9ef8e38c1b5 100644 --- a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/RepeatedAnnotationLowering.kt +++ b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/RepeatedAnnotationLowering.kt @@ -6,7 +6,7 @@ package org.jetbrains.kotlin.backend.jvm.lower import org.jetbrains.kotlin.backend.common.FileLoweringPass -import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase +import org.jetbrains.kotlin.backend.common.phaser.makeIrModulePhase import org.jetbrains.kotlin.backend.jvm.JvmBackendContext import org.jetbrains.kotlin.backend.jvm.codegen.AnnotationCodegen.Companion.annotationClass import org.jetbrains.kotlin.builtins.StandardNames @@ -31,7 +31,7 @@ import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid import org.jetbrains.kotlin.ir.visitors.acceptVoid import org.jetbrains.kotlin.load.java.JvmAnnotationNames -internal val repeatedAnnotationPhase = makeIrFilePhase( +internal val repeatedAnnotationPhase = makeIrModulePhase( ::RepeatedAnnotationLowering, name = "RepeatedAnnotation", description = "Enclose repeated annotations in a container annotation, generating a container class if needed" diff --git a/compiler/testData/codegen/box/reflection/annotations/repeatable/kt49335.kt b/compiler/testData/codegen/box/reflection/annotations/repeatable/kt49335.kt new file mode 100644 index 00000000000..96445dd1f2e --- /dev/null +++ b/compiler/testData/codegen/box/reflection/annotations/repeatable/kt49335.kt @@ -0,0 +1,38 @@ +// !LANGUAGE: +RepeatableAnnotations +// !OPT_IN: kotlin.ExperimentalStdlibApi +// TARGET_BACKEND: JVM_IR +// JVM_TARGET: 1.8 +// FULL_JDK +// WITH_REFLECT + +// Android doesn't have @Repeatable, so findAnnotations can't unpack repeatable annotations. +// IGNORE_BACKEND: ANDROID + +// FILE: A.kt +@java.lang.annotation.Repeatable(A.Container::class) +annotation class A(val value: String) { + annotation class Container(val value: Array) +} + +// FILE: kt49335.kt +import kotlin.reflect.full.findAnnotation +import kotlin.reflect.full.findAnnotations +import kotlin.reflect.full.hasAnnotation + +@A("O") +@A("") +@A("K") +fun f() {} + +fun box(): String { + val element = ::f + if (element.hasAnnotation()) return "Fail hasAnnotation $element" + val find = element.findAnnotation() + if (find != null) return "Fail findAnnotation $element: $find" + + val all = (element.annotations.single() as A.Container).value.asList() + val findAll = element.findAnnotations() + if (all != findAll) throw AssertionError("Fail findAnnotations $element: $all != $findAll") + + return all.fold("") { acc, it -> acc + it.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 4a5a907139e..90b0bc6b3a2 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 @@ -35970,6 +35970,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/reflection/annotations/repeatable/kotlinAnnotation.kt"); } + @Test + @TestMetadata("kt49335.kt") + public void testKt49335() throws Exception { + runTest("compiler/testData/codegen/box/reflection/annotations/repeatable/kt49335.kt"); + } + @Test @TestMetadata("nonRepeatedAnnotationWithItsContainer.kt") public void testNonRepeatedAnnotationWithItsContainer() throws Exception {