From 45619ab39070447a164508ba26b6d54b8184a941 Mon Sep 17 00:00:00 2001 From: Nikolay Lunyak Date: Wed, 7 Sep 2022 12:34:26 +0000 Subject: [PATCH] [FIR] KT-53435: Fix false positive `ANNOTATION_ARGUMENT_MUST_BE_CONST` Otherwise, `FirNamedArgumentExpression` are not considered const expr despite the value. Merge-request: KT-MR-7052 Merged-by: Nikolay Lunyak --- ...nosisCompilerFirTestdataTestGenerated.java | 6 +++ ...TouchedTilContractsPhaseTestGenerated.java | 5 +++ ...nestedAnnotationConstExprArguments.fir.txt | 43 +++++++++++++++++++ .../nestedAnnotationConstExprArguments.kt | 14 ++++++ .../runners/FirDiagnosticTestGenerated.java | 6 +++ ...DiagnosticsWithLightTreeTestGenerated.java | 6 +++ .../fir/analysis/checkers/FirConstChecks.kt | 3 ++ 7 files changed, 83 insertions(+) create mode 100644 compiler/fir/analysis-tests/testData/resolve/nestedAnnotationConstExprArguments.fir.txt create mode 100644 compiler/fir/analysis-tests/testData/resolve/nestedAnnotationConstExprArguments.kt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java index bff43b51d06..bc2586ffc44 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java @@ -404,6 +404,12 @@ public class DiagnosisCompilerFirTestdataTestGenerated extends AbstractDiagnosis runTest("compiler/fir/analysis-tests/testData/resolve/localObject.kt"); } + @Test + @TestMetadata("nestedAnnotationConstExprArguments.kt") + public void testNestedAnnotationConstExprArguments() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/nestedAnnotationConstExprArguments.kt"); + } + @Test @TestMetadata("nestedClass.kt") public void testNestedClass() throws Exception { diff --git a/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java b/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java index 3499abbf556..bcabd5ec3e6 100644 --- a/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java +++ b/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java @@ -344,6 +344,11 @@ public class LazyBodyIsNotTouchedTilContractsPhaseTestGenerated extends Abstract runTest("compiler/fir/analysis-tests/testData/resolve/localObject.kt"); } + @TestMetadata("nestedAnnotationConstExprArguments.kt") + public void testNestedAnnotationConstExprArguments() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/nestedAnnotationConstExprArguments.kt"); + } + @TestMetadata("nestedClass.kt") public void testNestedClass() throws Exception { runTest("compiler/fir/analysis-tests/testData/resolve/nestedClass.kt"); diff --git a/compiler/fir/analysis-tests/testData/resolve/nestedAnnotationConstExprArguments.fir.txt b/compiler/fir/analysis-tests/testData/resolve/nestedAnnotationConstExprArguments.fir.txt new file mode 100644 index 00000000000..a7dda21c1dc --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/nestedAnnotationConstExprArguments.fir.txt @@ -0,0 +1,43 @@ +FILE: nestedAnnotationConstExprArguments.kt + public final annotation class InnerAnnotation : R|kotlin/Annotation| { + public constructor(text: R|kotlin/String|): R|InnerAnnotation| { + super() + } + + public final val text: R|kotlin/String| = R|/text| + public get(): R|kotlin/String| + + } + public final annotation class OuterAnnotation : R|kotlin/Annotation| { + public constructor(inner: R|InnerAnnotation|): R|OuterAnnotation| { + super() + } + + public final val inner: R|InnerAnnotation| = R|/inner| + public get(): R|InnerAnnotation| + + } + @R|OuterAnnotation|(inner = R|/InnerAnnotation.InnerAnnotation|(text = String(x).R|kotlin/String.plus|(String(x)))) public final class Payload : R|kotlin/Any| { + public constructor(): R|Payload| { + super() + } + + } + @R|InnerAnnotation|(text = String(x).R|kotlin/String.plus|(String(x))) public final class Payload2 : R|kotlin/Any| { + public constructor(): R|Payload2| { + super() + } + + } + @R|OuterAnnotation|(inner = R|/InnerAnnotation.InnerAnnotation|(text = String(x))) public final class Payload3 : R|kotlin/Any| { + public constructor(): R|Payload3| { + super() + } + + } + @R|OuterAnnotation|(inner = R|/InnerAnnotation.InnerAnnotation|(String(x).R|kotlin/String.plus|(String(x)))) public final class Payload4 : R|kotlin/Any| { + public constructor(): R|Payload4| { + super() + } + + } diff --git a/compiler/fir/analysis-tests/testData/resolve/nestedAnnotationConstExprArguments.kt b/compiler/fir/analysis-tests/testData/resolve/nestedAnnotationConstExprArguments.kt new file mode 100644 index 00000000000..195f5f9f0bf --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/nestedAnnotationConstExprArguments.kt @@ -0,0 +1,14 @@ +annotation class InnerAnnotation(val text: String) +annotation class OuterAnnotation(val inner: InnerAnnotation) + +@OuterAnnotation(InnerAnnotation(text = "x" + "x")) +class Payload + +@InnerAnnotation(text = "x" + "x") +class Payload2 + +@OuterAnnotation(InnerAnnotation(text = "x")) +class Payload3 + +@OuterAnnotation(InnerAnnotation("x" + "x")) +class Payload4 diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java index 106e7dc3161..8903ac12841 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java @@ -404,6 +404,12 @@ public class FirDiagnosticTestGenerated extends AbstractFirDiagnosticTest { runTest("compiler/fir/analysis-tests/testData/resolve/localObject.kt"); } + @Test + @TestMetadata("nestedAnnotationConstExprArguments.kt") + public void testNestedAnnotationConstExprArguments() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/nestedAnnotationConstExprArguments.kt"); + } + @Test @TestMetadata("nestedClass.kt") public void testNestedClass() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java index 2119daabae5..15b7de6cbef 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java @@ -404,6 +404,12 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos runTest("compiler/fir/analysis-tests/testData/resolve/localObject.kt"); } + @Test + @TestMetadata("nestedAnnotationConstExprArguments.kt") + public void testNestedAnnotationConstExprArguments() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/nestedAnnotationConstExprArguments.kt"); + } + @Test @TestMetadata("nestedClass.kt") public void testNestedClass() throws Exception { diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirConstChecks.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirConstChecks.kt index b22c0ba8959..d617d28f97f 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirConstChecks.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirConstChecks.kt @@ -34,6 +34,9 @@ internal fun checkConstantArguments( ?.classKind when { + expression is FirNamedArgumentExpression -> { + checkConstantArguments(expression.expression, session) + } expression is FirTypeOperatorCall -> if (expression.operation == FirOperation.AS) return ConstantArgumentKind.NOT_CONST expression is FirWhenExpression -> { if (!expression.isProperlyExhaustive) {