diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt index a40eb186a0d..cd26f630547 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt @@ -722,10 +722,13 @@ private class ConstantExpressionEvaluatorVisitor( val arguments = call.valueArguments.values.flatMap { resolveArguments(it.arguments, componentType) } - return factory.createArrayValue(arguments.map { it.toConstantValue(componentType) }, returnType) + // not evaluated arguments are not constants: function-calls, properties with custom getter... + val evaluatedArguments = arguments.filterNotNull() + + return factory.createArrayValue(evaluatedArguments.map { it.toConstantValue(componentType) }, returnType) .wrap( - usesVariableAsConstant = arguments.any { it.usesVariableAsConstant }, - usesNonConstValAsConstant = arguments.any { it.usesNonConstValAsConstant } + usesVariableAsConstant = evaluatedArguments.any { it.usesVariableAsConstant }, + usesNonConstValAsConstant = arguments.any { it == null || it.usesNonConstValAsConstant } ) } @@ -735,15 +738,12 @@ private class ConstantExpressionEvaluatorVisitor( return KClassValue(jetType).wrap() } - private fun resolveArguments(valueArguments: List, expectedType: KotlinType): List> { - val constants = arrayListOf>() + private fun resolveArguments(valueArguments: List, expectedType: KotlinType): List?> { + val constants = arrayListOf?>() for (argument in valueArguments) { val argumentExpression = argument.getArgumentExpression() if (argumentExpression != null) { - val compileTimeConstant = evaluate(argumentExpression, expectedType) - if (compileTimeConstant != null) { - constants.add(compileTimeConstant) - } + constants.add(evaluate(argumentExpression, expectedType)) } } return constants diff --git a/compiler/testData/diagnostics/tests/collectionLiterals/defaultValuesInAnnotation.kt b/compiler/testData/diagnostics/tests/collectionLiterals/defaultValuesInAnnotation.kt index 45e9190ac15..220cbc89ac2 100644 --- a/compiler/testData/diagnostics/tests/collectionLiterals/defaultValuesInAnnotation.kt +++ b/compiler/testData/diagnostics/tests/collectionLiterals/defaultValuesInAnnotation.kt @@ -8,7 +8,7 @@ annotation class Foo( annotation class Bar( val a: Array = [' '], - val b: Array = ["", ''], + val b: Array = ["", ''], val c: Array = [1] ) diff --git a/compiler/testData/diagnostics/tests/collectionLiterals/defaultValuesWithConstantsInAnnotation.kt b/compiler/testData/diagnostics/tests/collectionLiterals/defaultValuesWithConstantsInAnnotation.kt index d207fc6ff7e..ac2bc8193e8 100644 --- a/compiler/testData/diagnostics/tests/collectionLiterals/defaultValuesWithConstantsInAnnotation.kt +++ b/compiler/testData/diagnostics/tests/collectionLiterals/defaultValuesWithConstantsInAnnotation.kt @@ -15,11 +15,11 @@ fun getTwo() = TWO annotation class Bar( val a: IntArray = [TWO], val b: IntArray = [1, TWO], - val c: IntArray = [getOne(), getTwo()] + val c: IntArray = [getOne(), getTwo()] ) annotation class Baz( val a: IntArray = [null], val b: IntArray = [1, null, 2], - val c: IntArray = [this] + val c: IntArray = [this] ) diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameterMustBeConstant/array.kt b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameterMustBeConstant/array.kt index 7a56a7076ba..f7014df99f2 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameterMustBeConstant/array.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameterMustBeConstant/array.kt @@ -2,11 +2,11 @@ @Repeatable annotation class Ann(val i: IntArray) -@Ann(intArrayOf(i)) +@Ann(intArrayOf(i)) @Ann(intArrayOf(i2)) -@Ann(intArrayOf(i3)) -@Ann(intArrayOf(i, i2, i3)) -@Ann(intArrayOf(intArrayOf(i, i2, i3))) +@Ann(intArrayOf(i3)) +@Ann(intArrayOf(i, i2, i3)) +@Ann(intArrayOf(intArrayOf(i, i2, i3))) class Test var i = 1 @@ -19,6 +19,6 @@ fun foo(): Int = 1 @Repeatable annotation class AnnAnn(val i: Array) @AnnAnn(arrayOf(Ann(intArrayOf(1)))) -@AnnAnn(arrayOf(iAnn)) +@AnnAnn(arrayOf(iAnn)) class TestAnn val iAnn = Ann(intArrayOf(1)) diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameterMustBeConstant/vararg.kt b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameterMustBeConstant/vararg.kt index 498aa046ccb..661e07cc831 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameterMustBeConstant/vararg.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameterMustBeConstant/vararg.kt @@ -6,10 +6,10 @@ annotation class Ann(vararg val i: Int) @Ann(i2) @Ann(i3) @Ann(i, i2, i3) -@Ann(*intArrayOf(i)) +@Ann(*intArrayOf(i)) @Ann(*intArrayOf(i2)) -@Ann(*intArrayOf(i3)) -@Ann(*intArrayOf(i, i2, i3)) +@Ann(*intArrayOf(i3)) +@Ann(*intArrayOf(i, i2, i3)) class Test var i = 1 @@ -22,6 +22,6 @@ fun foo(): Int = 1 @Repeatable annotation class AnnAnn(vararg val i: Ann) @AnnAnn(*arrayOf(Ann(1))) -@AnnAnn(*arrayOf(iAnn)) +@AnnAnn(*arrayOf(iAnn)) class TestAnn val iAnn = Ann(1) diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/nonConstValAsArgument.kt b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/nonConstValAsArgument.kt new file mode 100644 index 00000000000..10847725111 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/nonConstValAsArgument.kt @@ -0,0 +1,34 @@ +import kotlin.reflect.KClass + +annotation class Ann( + val a: Array = arrayOf(readOnly), + val b: Array = arrayOf(withGetter), + val c: Array = arrayOf(func()), + val d: IntArray = intArrayOf(ONE, twoWithGetter), + val e: IntArray = intArrayOf(ONE + twoWithGetter), + val f: Array = arrayOf(mutable), + val g: Array = arrayOf(mutableWithGetter), + val h: Array> = arrayOf(WithLateinit.kClass) +) + +const val ONE = 1 + +val twoWithGetter + get() = 2 + +val readOnly = "" + +val withGetter + get() = "" + +fun func() = "" + +var mutable = "" + +var mutableWithGetter + get() = "" + set(x) = TODO() + +object WithLateinit { + lateinit var kClass: KClass<*> +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/nonConstValAsArgument.txt b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/nonConstValAsArgument.txt new file mode 100644 index 00000000000..3aa6a12fb05 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/nonConstValAsArgument.txt @@ -0,0 +1,32 @@ +package + +public const val ONE: kotlin.Int = 1 +public var mutable: kotlin.String +public var mutableWithGetter: kotlin.String +public val readOnly: kotlin.String = "" +public val twoWithGetter: kotlin.Int +public val withGetter: kotlin.String +public fun func(): kotlin.String + +public final annotation class Ann : kotlin.Annotation { + public constructor Ann(/*0*/ a: kotlin.Array = ..., /*1*/ b: kotlin.Array = ..., /*2*/ c: kotlin.Array = ..., /*3*/ d: kotlin.IntArray = ..., /*4*/ e: kotlin.IntArray = ..., /*5*/ f: kotlin.Array = ..., /*6*/ g: kotlin.Array = ..., /*7*/ h: kotlin.Array> = ...) + public final val a: kotlin.Array + public final val b: kotlin.Array + public final val c: kotlin.Array + public final val d: kotlin.IntArray + public final val e: kotlin.IntArray + public final val f: kotlin.Array + public final val g: kotlin.Array + public final val h: kotlin.Array> + 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 +} + +public object WithLateinit { + private constructor WithLateinit() + public final lateinit var kClass: kotlin.reflect.KClass<*> + 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/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java index f7f78c9ff21..e3386374d88 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java @@ -328,6 +328,12 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW doTest(fileName); } + @TestMetadata("nonConstValAsArgument.kt") + public void testNonConstValAsArgument() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/nonConstValAsArgument.kt"); + doTest(fileName); + } + @TestMetadata("orderWithValue.kt") public void testOrderWithValue() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/orderWithValue.kt");