From 79eee6de4f34eaa1115ed84b182db01028c8c21e Mon Sep 17 00:00:00 2001 From: Mads Ager Date: Fri, 2 Feb 2024 12:11:03 +0100 Subject: [PATCH] [FIR] Do not enhance nullability for annotation interface field types. Java annotation interface fields are constant declarations that can have any Java type and are not restricted in the same way that method return types are for annotation interfaces. #KT-65482 Fixed --- ...LFirBlackBoxCodegenBasedTestGenerated.java | 6 ++++ ...rsedBlackBoxCodegenBasedTestGenerated.java | 6 ++++ ...LightTreeBlackBoxCodegenTestGenerated.java | 6 ++++ ...hIrFakeOverrideGeneratorTestGenerated.java | 6 ++++ .../FirPsiBlackBoxCodegenTestGenerated.java | 6 ++++ .../java/enhancement/SignatureEnhancement.kt | 15 ++++++++-- .../codegen/box/javaInterop/kt65482.kt | 28 +++++++++++++++++++ .../JvmAbiConsistencyTestBoxGenerated.java | 6 ++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 ++++ .../IrBlackBoxCodegenTestGenerated.java | 6 ++++ ...kBoxCodegenWithIrInlinerTestGenerated.java | 6 ++++ .../LightAnalysisModeTestGenerated.java | 5 ++++ 12 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/codegen/box/javaInterop/kt65482.kt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirBlackBoxCodegenBasedTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirBlackBoxCodegenBasedTestGenerated.java index 7da695bfb22..ff4b1cc0a1c 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirBlackBoxCodegenBasedTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirBlackBoxCodegenBasedTestGenerated.java @@ -31262,6 +31262,12 @@ public class LLFirBlackBoxCodegenBasedTestGenerated extends AbstractLLFirBlackBo runTest("compiler/testData/codegen/box/javaInterop/kt48590.kt"); } + @Test + @TestMetadata("kt65482.kt") + public void testKt65482() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/kt65482.kt"); + } + @Test @TestMetadata("lambdaInstanceOf.kt") public void testLambdaInstanceOf() throws Exception { diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirReversedBlackBoxCodegenBasedTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirReversedBlackBoxCodegenBasedTestGenerated.java index 157cfc34d54..2229be75c0c 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirReversedBlackBoxCodegenBasedTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirReversedBlackBoxCodegenBasedTestGenerated.java @@ -31262,6 +31262,12 @@ public class LLFirReversedBlackBoxCodegenBasedTestGenerated extends AbstractLLFi runTest("compiler/testData/codegen/box/javaInterop/kt48590.kt"); } + @Test + @TestMetadata("kt65482.kt") + public void testKt65482() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/kt65482.kt"); + } + @Test @TestMetadata("lambdaInstanceOf.kt") public void testLambdaInstanceOf() throws Exception { 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 8c8c0664440..463b57fd510 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 @@ -30931,6 +30931,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr runTest("compiler/testData/codegen/box/javaInterop/kt48590.kt"); } + @Test + @TestMetadata("kt65482.kt") + public void testKt65482() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/kt65482.kt"); + } + @Test @TestMetadata("lambdaInstanceOf.kt") public void testLambdaInstanceOf() 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 50d9a0bba84..10131560576 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 @@ -30931,6 +30931,12 @@ public class FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated runTest("compiler/testData/codegen/box/javaInterop/kt48590.kt"); } + @Test + @TestMetadata("kt65482.kt") + public void testKt65482() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/kt65482.kt"); + } + @Test @TestMetadata("lambdaInstanceOf.kt") public void testLambdaInstanceOf() 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 f5ef42ba223..f83707715f2 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 @@ -30931,6 +30931,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo runTest("compiler/testData/codegen/box/javaInterop/kt48590.kt"); } + @Test + @TestMetadata("kt65482.kt") + public void testKt65482() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/kt65482.kt"); + } + @Test @TestMetadata("lambdaInstanceOf.kt") public void testLambdaInstanceOf() throws Exception { diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/SignatureEnhancement.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/SignatureEnhancement.kt index 152f14b28fd..c001d69785d 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/SignatureEnhancement.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/SignatureEnhancement.kt @@ -632,10 +632,19 @@ class FirSignatureEnhancement( defaultQualifiers: JavaTypeQualifiersByElementType?, predefinedEnhancementInfo: PredefinedFunctionEnhancementInfo? ): FirResolvedTypeRef { - val containerApplicabilityType = if (owner is FirJavaField) + val containerApplicabilityType = if (owner is FirJavaField) { AnnotationQualifierApplicabilityType.FIELD - else + } else { AnnotationQualifierApplicabilityType.METHOD_RETURN_TYPE + } + val forAnnotationMember = if (owner is FirJavaField) { + // Fields in annotation interfaces are constant declarations that can have any Java type. + // For example: public @interface MyApi { Integer field = -1; } + // Therefore, for such annotation interface fields, we use default Java type enhancement. + false + } else { + this.owner.classKind == ClassKind.ANNOTATION_CLASS + } return owner.enhance( overriddenMembers, owner, @@ -644,7 +653,7 @@ class FirSignatureEnhancement( containerApplicabilityType, TypeInSignature.Return, predefinedEnhancementInfo?.returnTypeInfo, - forAnnotationMember = this.owner.classKind == ClassKind.ANNOTATION_CLASS + forAnnotationMember = forAnnotationMember ) } diff --git a/compiler/testData/codegen/box/javaInterop/kt65482.kt b/compiler/testData/codegen/box/javaInterop/kt65482.kt new file mode 100644 index 00000000000..be26417fe55 --- /dev/null +++ b/compiler/testData/codegen/box/javaInterop/kt65482.kt @@ -0,0 +1,28 @@ +// WITH_STDLIB +// TARGET_BACKEND: JVM + +// FILE: MyApi.java +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.CLASS) +@Target({ + ElementType.FIELD, + ElementType.METHOD, + ElementType.TYPE, + ElementType.CONSTRUCTOR, + ElementType.PACKAGE +}) +public @interface MyApi { + Integer LATEST = -1; + long value(); +} + +// FILE: main.kt + +fun box(): String { + if (-1 == MyApi.LATEST) return "OK" + return "NOT OK" +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/JvmAbiConsistencyTestBoxGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/JvmAbiConsistencyTestBoxGenerated.java index 6e3043dc2f5..0783cb5845e 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/JvmAbiConsistencyTestBoxGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/JvmAbiConsistencyTestBoxGenerated.java @@ -30931,6 +30931,12 @@ public class JvmAbiConsistencyTestBoxGenerated extends AbstractJvmAbiConsistency runTest("compiler/testData/codegen/box/javaInterop/kt48590.kt"); } + @Test + @TestMetadata("kt65482.kt") + public void testKt65482() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/kt65482.kt"); + } + @Test @TestMetadata("lambdaInstanceOf.kt") public void testLambdaInstanceOf() throws Exception { 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 7aa104eb522..69220e5f5ad 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 @@ -29119,6 +29119,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/javaInterop/kt48590.kt"); } + @Test + @TestMetadata("kt65482.kt") + public void testKt65482() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/kt65482.kt"); + } + @Test @TestMetadata("lambdaInstanceOf.kt") public void testLambdaInstanceOf() 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 3f940237837..aacdd1d53a7 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 @@ -30931,6 +30931,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/javaInterop/kt48590.kt"); } + @Test + @TestMetadata("kt65482.kt") + public void testKt65482() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/kt65482.kt"); + } + @Test @TestMetadata("lambdaInstanceOf.kt") public void testLambdaInstanceOf() 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 4c5b3ec59b9..638f84c0581 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 @@ -30931,6 +30931,12 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack runTest("compiler/testData/codegen/box/javaInterop/kt48590.kt"); } + @Test + @TestMetadata("kt65482.kt") + public void testKt65482() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/kt65482.kt"); + } + @Test @TestMetadata("lambdaInstanceOf.kt") public void testLambdaInstanceOf() 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 a47c5f26cbc..3c825f84edf 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -26207,6 +26207,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/javaInterop/kt48590.kt"); } + @TestMetadata("kt65482.kt") + public void testKt65482() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/kt65482.kt"); + } + @TestMetadata("lambdaInstanceOf.kt") public void testLambdaInstanceOf() throws Exception { runTest("compiler/testData/codegen/box/javaInterop/lambdaInstanceOf.kt");