From 7b0e5927cb79a290ad206e4e82661e759fde192d Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Fri, 20 Aug 2021 20:07:18 +0300 Subject: [PATCH] Report EXPERIMENTAL_ANNOTATION_ON_WRONG_TARGET without explicit @Target #KT-48349 Fixed --- ...irOldFrontendDiagnosticsTestGenerated.java | 6 +++++ ...DiagnosticsWithLightTreeTestGenerated.java | 6 +++++ ...entalMarkerDeclarationAnnotationChecker.kt | 3 ++- .../wrongTargetsWithoutExplicitTarget.fir.kt | 25 ++++++++++++++++++ .../wrongTargetsWithoutExplicitTarget.kt | 25 ++++++++++++++++++ .../wrongTargetsWithoutExplicitTarget.txt | 26 +++++++++++++++++++ .../test/runners/DiagnosticTestGenerated.java | 6 +++++ ...CompilerTestFE10TestdataTestGenerated.java | 6 +++++ 8 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/testsWithStdLib/experimental/wrongTargetsWithoutExplicitTarget.fir.kt create mode 100644 compiler/testData/diagnostics/testsWithStdLib/experimental/wrongTargetsWithoutExplicitTarget.kt create mode 100644 compiler/testData/diagnostics/testsWithStdLib/experimental/wrongTargetsWithoutExplicitTarget.txt diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index b87a7ad3d14..0e5611afbfc 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -35426,6 +35426,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti public void testWasExperimental() throws Exception { runTest("compiler/testData/diagnostics/testsWithStdLib/experimental/wasExperimental.kt"); } + + @Test + @TestMetadata("wrongTargetsWithoutExplicitTarget.kt") + public void testWrongTargetsWithoutExplicitTarget() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/experimental/wrongTargetsWithoutExplicitTarget.kt"); + } } @Nested diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java index 6700477e5a2..a41aefe3098 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java @@ -35426,6 +35426,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac public void testWasExperimental() throws Exception { runTest("compiler/testData/diagnostics/testsWithStdLib/experimental/wasExperimental.kt"); } + + @Test + @TestMetadata("wrongTargetsWithoutExplicitTarget.kt") + public void testWrongTargetsWithoutExplicitTarget() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/experimental/wrongTargetsWithoutExplicitTarget.kt"); + } } @Nested diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/ExperimentalMarkerDeclarationAnnotationChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/ExperimentalMarkerDeclarationAnnotationChecker.kt index 07dd82f7c13..239c6323cd9 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/ExperimentalMarkerDeclarationAnnotationChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/ExperimentalMarkerDeclarationAnnotationChecker.kt @@ -59,7 +59,8 @@ class ExperimentalMarkerDeclarationAnnotationChecker(private val module: ModuleD } val annotationClass = annotation.annotationClass ?: continue if (annotationClass.annotations.any { it.fqName in OptInNames.EXPERIMENTAL_FQ_NAMES }) { - val possibleTargets = AnnotationChecker.applicableTargetSet(annotationClass).orEmpty().intersect(actualTargets) + val applicableTargets = AnnotationChecker.applicableTargetSet(annotationClass) ?: KotlinTarget.DEFAULT_TARGET_SET + val possibleTargets = applicableTargets.intersect(actualTargets) val annotationUseSiteTarget = entry.useSiteTarget?.getAnnotationUseSiteTarget() if (PROPERTY_GETTER in possibleTargets || annotationUseSiteTarget == AnnotationUseSiteTarget.PROPERTY_GETTER diff --git a/compiler/testData/diagnostics/testsWithStdLib/experimental/wrongTargetsWithoutExplicitTarget.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/experimental/wrongTargetsWithoutExplicitTarget.fir.kt new file mode 100644 index 00000000000..50d416fdd63 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/experimental/wrongTargetsWithoutExplicitTarget.fir.kt @@ -0,0 +1,25 @@ +// !USE_EXPERIMENTAL: kotlin.RequiresOptIn + +@RequiresOptIn +annotation class SomeOptInMarker + +@RequiresOptIn +@Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.PROPERTY, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.LOCAL_VARIABLE) +annotation class OtherOptInMarker + +class IntWrapper( + @SomeOptInMarker + @OtherOptInMarker + val value: Int +) { + val isEven: Boolean + @SomeOptInMarker + @OtherOptInMarker + get() = (value % 2) == 0 +} + +fun foo() { + @SomeOptInMarker + @OtherOptInMarker + val value = 2 +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithStdLib/experimental/wrongTargetsWithoutExplicitTarget.kt b/compiler/testData/diagnostics/testsWithStdLib/experimental/wrongTargetsWithoutExplicitTarget.kt new file mode 100644 index 00000000000..ac141c1f408 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/experimental/wrongTargetsWithoutExplicitTarget.kt @@ -0,0 +1,25 @@ +// !USE_EXPERIMENTAL: kotlin.RequiresOptIn + +@RequiresOptIn +annotation class SomeOptInMarker + +@RequiresOptIn +@Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.PROPERTY, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.LOCAL_VARIABLE) +annotation class OtherOptInMarker + +class IntWrapper( + @SomeOptInMarker + @OtherOptInMarker + val value: Int +) { + val isEven: Boolean + @SomeOptInMarker + @OtherOptInMarker + get() = (value % 2) == 0 +} + +fun foo() { + @SomeOptInMarker + @OtherOptInMarker + val value = 2 +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithStdLib/experimental/wrongTargetsWithoutExplicitTarget.txt b/compiler/testData/diagnostics/testsWithStdLib/experimental/wrongTargetsWithoutExplicitTarget.txt new file mode 100644 index 00000000000..e8ffd97e7f7 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/experimental/wrongTargetsWithoutExplicitTarget.txt @@ -0,0 +1,26 @@ +package + +public fun foo(): kotlin.Unit + +public final class IntWrapper { + public constructor IntWrapper(/*0*/ @SomeOptInMarker @OtherOptInMarker value: kotlin.Int) + @get:SomeOptInMarker @get:OtherOptInMarker public final val isEven: kotlin.Boolean + public final val value: kotlin.Int + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +@kotlin.RequiresOptIn @kotlin.annotation.Target(allowedTargets = {AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.PROPERTY, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.LOCAL_VARIABLE}) public final annotation class OtherOptInMarker : kotlin.Annotation { + public constructor OtherOptInMarker() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +@kotlin.RequiresOptIn public final annotation class SomeOptInMarker : kotlin.Annotation { + public constructor SomeOptInMarker() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index 29f9e03d08b..6a529d9ed86 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -35522,6 +35522,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { public void testWasExperimental() throws Exception { runTest("compiler/testData/diagnostics/testsWithStdLib/experimental/wasExperimental.kt"); } + + @Test + @TestMetadata("wrongTargetsWithoutExplicitTarget.kt") + public void testWrongTargetsWithoutExplicitTarget() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/experimental/wrongTargetsWithoutExplicitTarget.kt"); + } } @Nested diff --git a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java index 14605a34861..c94f90b006f 100644 --- a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java +++ b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java @@ -35426,6 +35426,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag public void testWasExperimental() throws Exception { runTest("compiler/testData/diagnostics/testsWithStdLib/experimental/wasExperimental.kt"); } + + @Test + @TestMetadata("wrongTargetsWithoutExplicitTarget.kt") + public void testWrongTargetsWithoutExplicitTarget() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/experimental/wrongTargetsWithoutExplicitTarget.kt"); + } } @Nested