From 9a2307ad44004ec550d104c8205154c639be35a3 Mon Sep 17 00:00:00 2001 From: Kirill Rakhman Date: Tue, 5 Sep 2023 12:46:41 +0200 Subject: [PATCH] [FIR] Fix expression types in java annotations #KT-61518 Fixed --- ...LightTreeBlackBoxCodegenTestGenerated.java | 6 +++++ ...hIrFakeOverrideGeneratorTestGenerated.java | 6 +++++ .../FirPsiBlackBoxCodegenTestGenerated.java | 6 +++++ .../kotlin/fir/java/javaAnnotationsMapping.kt | 20 +++++++++++++---- ...aAnnotationWithDefaultValueForenumArray.kt | 22 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 +++++ .../IrBlackBoxCodegenTestGenerated.java | 6 +++++ ...kBoxCodegenWithIrInlinerTestGenerated.java | 6 +++++ .../LightAnalysisModeTestGenerated.java | 5 +++++ 9 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/codegen/box/fir/javaAnnotationWithDefaultValueForenumArray.kt diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java index 0860901f18a..eeced84c4e8 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java @@ -19029,6 +19029,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr runTest("compiler/testData/codegen/box/fir/JKEnumConstant.kt"); } + @Test + @TestMetadata("javaAnnotationWithDefaultValueForenumArray.kt") + public void testJavaAnnotationWithDefaultValueForenumArray() throws Exception { + runTest("compiler/testData/codegen/box/fir/javaAnnotationWithDefaultValueForenumArray.kt"); + } + @Test @TestMetadata("jvmFieldInLocalClass.kt") public void testJvmFieldInLocalClass() throws Exception { diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated.java index 338b0542e9e..08d62a6e09a 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated.java @@ -19029,6 +19029,12 @@ public class FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated runTest("compiler/testData/codegen/box/fir/JKEnumConstant.kt"); } + @Test + @TestMetadata("javaAnnotationWithDefaultValueForenumArray.kt") + public void testJavaAnnotationWithDefaultValueForenumArray() throws Exception { + runTest("compiler/testData/codegen/box/fir/javaAnnotationWithDefaultValueForenumArray.kt"); + } + @Test @TestMetadata("jvmFieldInLocalClass.kt") public void testJvmFieldInLocalClass() throws Exception { diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java index 6fca82c1316..95bf7c29ede 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java @@ -19029,6 +19029,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo runTest("compiler/testData/codegen/box/fir/JKEnumConstant.kt"); } + @Test + @TestMetadata("javaAnnotationWithDefaultValueForenumArray.kt") + public void testJavaAnnotationWithDefaultValueForenumArray() throws Exception { + runTest("compiler/testData/codegen/box/fir/javaAnnotationWithDefaultValueForenumArray.kt"); + } + @Test @TestMetadata("jvmFieldInLocalClass.kt") public void testJvmFieldInLocalClass() throws Exception { diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/javaAnnotationsMapping.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/javaAnnotationsMapping.kt index 43ab24cf40e..8293c086990 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/javaAnnotationsMapping.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/javaAnnotationsMapping.kt @@ -107,10 +107,14 @@ internal fun JavaAnnotationArgument.toFirExpression( ) is JavaArrayAnnotationArgument -> buildArrayLiteral { val argumentTypeRef = expectedTypeRef?.let { - coneTypeOrNull = - if (it is FirJavaTypeRef) it.toConeKotlinTypeProbablyFlexible(session, javaTypeParameterStack) else it.coneType + val type = if (it is FirJavaTypeRef) { + it.toConeKotlinTypeProbablyFlexible(session, javaTypeParameterStack) + } else { + it.coneType + } + coneTypeOrNull = type buildResolvedTypeRef { - type = it.coneTypeSafe()?.lowerBoundIfFlexible()?.arrayElementType() + this.type = type.lowerBoundIfFlexible().arrayElementType() ?: ConeErrorType(ConeSimpleDiagnostic("expected type is not array type")) } } @@ -118,7 +122,15 @@ internal fun JavaAnnotationArgument.toFirExpression( getElements().mapTo(arguments) { it.toFirExpression(session, javaTypeParameterStack, argumentTypeRef) } } } - is JavaEnumValueAnnotationArgument -> buildEnumCall(session, enumClassId, entryName) + is JavaEnumValueAnnotationArgument -> buildEnumCall( + session, + // enumClassId can be null when a java annotation uses a Kotlin enum as parameter and declares the default value using + // a static import. In this case, the parameter default initializer will not have its type set, which isn't usually an + // issue except in edge cases like KT-47702 where we do need to evaluate the default values of annotations. + // As a fallback, we use the expected type which should be the type of the enum. + classId = enumClassId ?: expectedTypeRef?.coneTypeOrNull?.lowerBoundIfFlexible()?.classId, + entryName = entryName + ) is JavaClassObjectAnnotationArgument -> buildGetClassCall { val resolvedClassTypeRef = getReferencedType().toFirResolvedTypeRef(session, javaTypeParameterStack) val resolvedTypeRef = buildResolvedTypeRef { diff --git a/compiler/testData/codegen/box/fir/javaAnnotationWithDefaultValueForenumArray.kt b/compiler/testData/codegen/box/fir/javaAnnotationWithDefaultValueForenumArray.kt new file mode 100644 index 00000000000..03d79092dd8 --- /dev/null +++ b/compiler/testData/codegen/box/fir/javaAnnotationWithDefaultValueForenumArray.kt @@ -0,0 +1,22 @@ +// IGNORE_LIGHT_ANALYSIS +// TARGET_BACKEND: JVM +// FILE: ArrayAnnEnumJava.java +package light.ann.array; + +import static light.ann.array.AnnAuxEnum.ANN_ENUM_VAL_A; +import static light.ann.array.AnnAuxEnum.ANN_ENUM_VAL_B; + +public @interface ArrayAnnEnumJava { + AnnAuxEnum[] enumValDef() default { ANN_ENUM_VAL_A, ANN_ENUM_VAL_B }; +} + +// FILE: ArrayAnnUsage.kt +package light.ann.array + +import light.ann.array.AnnAuxEnum.ANN_ENUM_VAL_A; +import light.ann.array.AnnAuxEnum.ANN_ENUM_VAL_B; + +enum class AnnAuxEnum { ANN_ENUM_VAL_A, ANN_ENUM_VAL_B } + +@ArrayAnnEnumJava(enumValDef = arrayOf(ANN_ENUM_VAL_A, ANN_ENUM_VAL_B)) +fun box() = "OK" 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 ac779c1a535..ef2d66187fd 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 @@ -18117,6 +18117,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/fir/IrBuiltIns.kt"); } + @Test + @TestMetadata("javaAnnotationWithDefaultValueForenumArray.kt") + public void testJavaAnnotationWithDefaultValueForenumArray() throws Exception { + runTest("compiler/testData/codegen/box/fir/javaAnnotationWithDefaultValueForenumArray.kt"); + } + @Test @TestMetadata("KotlinDocumentationProvider.kt") public void testKotlinDocumentationProvider() 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 13a3ad59e71..bc179e88af9 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 @@ -19029,6 +19029,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/fir/JKEnumConstant.kt"); } + @Test + @TestMetadata("javaAnnotationWithDefaultValueForenumArray.kt") + public void testJavaAnnotationWithDefaultValueForenumArray() throws Exception { + runTest("compiler/testData/codegen/box/fir/javaAnnotationWithDefaultValueForenumArray.kt"); + } + @Test @TestMetadata("jvmFieldInLocalClass.kt") public void testJvmFieldInLocalClass() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java index 02065bfdfc7..e2e96a42764 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java @@ -19029,6 +19029,12 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack runTest("compiler/testData/codegen/box/fir/JKEnumConstant.kt"); } + @Test + @TestMetadata("javaAnnotationWithDefaultValueForenumArray.kt") + public void testJavaAnnotationWithDefaultValueForenumArray() throws Exception { + runTest("compiler/testData/codegen/box/fir/javaAnnotationWithDefaultValueForenumArray.kt"); + } + @Test @TestMetadata("jvmFieldInLocalClass.kt") public void testJvmFieldInLocalClass() throws Exception { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index bb2588511bf..06bc512f64d 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -15846,6 +15846,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/fir/JKEnumConstant.kt"); } + @TestMetadata("javaAnnotationWithDefaultValueForenumArray.kt") + public void testJavaAnnotationWithDefaultValueForenumArray() throws Exception { + runTest("compiler/testData/codegen/box/fir/javaAnnotationWithDefaultValueForenumArray.kt"); + } + @TestMetadata("jvmFieldInLocalClass.kt") public void testJvmFieldInLocalClass() throws Exception { runTest("compiler/testData/codegen/box/fir/jvmFieldInLocalClass.kt");