From 1a3aa1bff0db293b3f5af23108a3cdc4d73b43a1 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Tue, 11 May 2021 18:05:57 +0300 Subject: [PATCH] FIR: report ANNOTATION_PARAMETER_DEFAULT_VALUE_MUST_BE_CONSTANT --- .../FirAnnotationClassDeclarationChecker.kt | 5 +++++ ...notationConstructorDefaultParameter.fir.kt | 12 ---------- .../annotationConstructorDefaultParameter.kt | 1 + .../annotationParameters/kt10136.fir.kt | 8 ------- .../annotationParameters/kt10136.kt | 1 + .../defaultValueMustBeConstant.fir.kt | 22 ------------------- .../annotations/defaultValueMustBeConstant.kt | 1 + 7 files changed, 8 insertions(+), 42 deletions(-) delete mode 100644 compiler/testData/diagnostics/tests/annotations/annotationParameterMustBeConstant/annotationConstructorDefaultParameter.fir.kt delete mode 100644 compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/kt10136.fir.kt delete mode 100644 compiler/testData/diagnostics/testsWithStdLib/annotations/defaultValueMustBeConstant.fir.kt diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirAnnotationClassDeclarationChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirAnnotationClassDeclarationChecker.kt index baafba7e1e9..76d3d3c435f 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirAnnotationClassDeclarationChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirAnnotationClassDeclarationChecker.kt @@ -10,6 +10,7 @@ import org.jetbrains.kotlin.KtNodeTypes.VALUE_PARAMETER import org.jetbrains.kotlin.descriptors.ClassKind.ANNOTATION_CLASS import org.jetbrains.kotlin.descriptors.ClassKind.ENUM_CLASS import org.jetbrains.kotlin.fir.FirSession +import org.jetbrains.kotlin.fir.analysis.checkers.checkConstantArguments import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext import org.jetbrains.kotlin.fir.analysis.diagnostics.* import org.jetbrains.kotlin.fir.declarations.* @@ -38,6 +39,10 @@ object FirAnnotationClassDeclarationChecker : FirRegularClassChecker() { } else if (source.hasVar()) { reporter.reportOn(source, FirErrors.VAR_ANNOTATION_PARAMETER, context) } + val defaultValue = parameter.defaultValue + if (defaultValue != null && checkConstantArguments(defaultValue, context.session) != null) { + reporter.reportOn(defaultValue.source, FirErrors.ANNOTATION_PARAMETER_DEFAULT_VALUE_MUST_BE_CONSTANT, context) + } val typeRef = parameter.returnTypeRef val coneType = typeRef.coneTypeSafe() diff --git a/compiler/testData/diagnostics/tests/annotations/annotationParameterMustBeConstant/annotationConstructorDefaultParameter.fir.kt b/compiler/testData/diagnostics/tests/annotations/annotationParameterMustBeConstant/annotationConstructorDefaultParameter.fir.kt deleted file mode 100644 index 2e0eb6f8ce1..00000000000 --- a/compiler/testData/diagnostics/tests/annotations/annotationParameterMustBeConstant/annotationConstructorDefaultParameter.fir.kt +++ /dev/null @@ -1,12 +0,0 @@ -const val iConst = 42 -val iVal = 42 -fun iFun() = 42 - -annotation class Ann(val x: Int) -annotation class Test1(val x: Int = 42) -annotation class Test2(val x: Int = iConst) -annotation class Test3(val x: Int = 1 + iConst + 1) -annotation class Test4(val x: Int = iVal) -annotation class Test5(val x: Int = 1 + iVal + 1) -annotation class Test6(val x: Int = iFun()) -annotation class Test7(val x: Int = 1 + iFun() + 1) diff --git a/compiler/testData/diagnostics/tests/annotations/annotationParameterMustBeConstant/annotationConstructorDefaultParameter.kt b/compiler/testData/diagnostics/tests/annotations/annotationParameterMustBeConstant/annotationConstructorDefaultParameter.kt index 5c453d953c8..a0b48fb788b 100644 --- a/compiler/testData/diagnostics/tests/annotations/annotationParameterMustBeConstant/annotationConstructorDefaultParameter.kt +++ b/compiler/testData/diagnostics/tests/annotations/annotationParameterMustBeConstant/annotationConstructorDefaultParameter.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL const val iConst = 42 val iVal = 42 fun iFun() = 42 diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/kt10136.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/kt10136.fir.kt deleted file mode 100644 index b9baf44eea1..00000000000 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/kt10136.fir.kt +++ /dev/null @@ -1,8 +0,0 @@ -annotation class A -annotation class A1(val x: Int) - -annotation class B( - val a: A = A(), - val x: Int = A1(42).x, - val aa: Array = arrayOf(A()) -) \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/kt10136.kt b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/kt10136.kt index f7bd9cb2ba5..6c4bd056639 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/kt10136.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/kt10136.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL annotation class A annotation class A1(val x: Int) diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/defaultValueMustBeConstant.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/annotations/defaultValueMustBeConstant.fir.kt deleted file mode 100644 index ad9dcbded21..00000000000 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/defaultValueMustBeConstant.fir.kt +++ /dev/null @@ -1,22 +0,0 @@ -import kotlin.reflect.KClass - -const val CONST = 1 -fun foo() = 1 -val nonConst = foo() - -annotation class ValidAnn( - val p1: Int = 1 + CONST, - val p2: String = "", - val p3: KClass<*> = String::class, - val p4: IntArray = intArrayOf(1, 2, 3), - val p5: Array = arrayOf("abc"), - val p6: Array> = arrayOf(Int::class) -) - -val nonConstKClass = String::class - -annotation class InvalidAnn( - val p1: Int = foo(), - val p2: Int = nonConst, - val p3: KClass<*> = nonConstKClass -) diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/defaultValueMustBeConstant.kt b/compiler/testData/diagnostics/testsWithStdLib/annotations/defaultValueMustBeConstant.kt index f1267f79360..8922ab754dd 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/defaultValueMustBeConstant.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/defaultValueMustBeConstant.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL import kotlin.reflect.KClass const val CONST = 1