From a2ea518b1ae3c3ab9ff5c1f933dfed4b10361b47 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Tue, 21 Mar 2017 03:11:54 +0300 Subject: [PATCH] Allow to use collection literals only in annotations --- .../expressions/BasicExpressionTypingVisitor.java | 4 ++++ .../collectionLiterals/basicCollectionLiterals.kt | 2 +- .../collectionLiteralsAsPrimitiveArrays.kt | 2 +- .../noArrayLiteralsInAnnotationsFeature.kt | 2 +- .../collectionLiterals/noCollectionLiterals.kt | 15 +++++++++++++++ .../collectionLiterals/noCollectionLiterals.txt | 12 ++++++++++++ .../kotlin/checkers/DiagnosticsTestGenerated.java | 6 ++++++ 7 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/collectionLiterals/noCollectionLiterals.kt create mode 100644 compiler/testData/diagnostics/tests/collectionLiterals/noCollectionLiterals.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java index 336eeb17f16..a0e90647482 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java @@ -1517,6 +1517,10 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { public KotlinTypeInfo visitCollectionLiteralExpression( @NotNull KtCollectionLiteralExpression expression, ExpressionTypingContext context ) { + if (!isInsideAnnotationEntryOrClass(expression)) { + context.trace.report(UNSUPPORTED.on(expression, "Collection literals outside of annotations")); + } + checkSupportsArrayLiterals(expression, context); return resolveCollectionLiteralSpecialMethod(expression, context); } diff --git a/compiler/testData/diagnostics/tests/collectionLiterals/basicCollectionLiterals.kt b/compiler/testData/diagnostics/tests/collectionLiterals/basicCollectionLiterals.kt index 977bc8ab79a..ccf2274f8f9 100644 --- a/compiler/testData/diagnostics/tests/collectionLiterals/basicCollectionLiterals.kt +++ b/compiler/testData/diagnostics/tests/collectionLiterals/basicCollectionLiterals.kt @@ -1,5 +1,5 @@ // !CHECK_TYPE -// !DIAGNOSTICS: -UNUSED_VARIABLE +// !DIAGNOSTICS: -UNUSED_VARIABLE, -UNSUPPORTED fun test() { val a = [] diff --git a/compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsAsPrimitiveArrays.kt b/compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsAsPrimitiveArrays.kt index 08ae16f6b19..8c1810a193b 100644 --- a/compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsAsPrimitiveArrays.kt +++ b/compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsAsPrimitiveArrays.kt @@ -1,4 +1,4 @@ -// !DIAGNOSTICS: -UNUSED_VARIABLE +// !DIAGNOSTICS: -UNUSED_VARIABLE, -UNSUPPORTED fun basicTypes() { val a: IntArray = [1] diff --git a/compiler/testData/diagnostics/tests/collectionLiterals/noArrayLiteralsInAnnotationsFeature.kt b/compiler/testData/diagnostics/tests/collectionLiterals/noArrayLiteralsInAnnotationsFeature.kt index 390b9a1d4e9..a6c56700e82 100644 --- a/compiler/testData/diagnostics/tests/collectionLiterals/noArrayLiteralsInAnnotationsFeature.kt +++ b/compiler/testData/diagnostics/tests/collectionLiterals/noArrayLiteralsInAnnotationsFeature.kt @@ -14,5 +14,5 @@ fun test2() {} fun test3() {} fun test4() { - [1, 2] + [1, 2] } diff --git a/compiler/testData/diagnostics/tests/collectionLiterals/noCollectionLiterals.kt b/compiler/testData/diagnostics/tests/collectionLiterals/noCollectionLiterals.kt new file mode 100644 index 00000000000..11ef6b1c999 --- /dev/null +++ b/compiler/testData/diagnostics/tests/collectionLiterals/noCollectionLiterals.kt @@ -0,0 +1,15 @@ +fun test(): Array { + [1, 2] + [1, 2][0] + [1, 2].get(0) + + foo([""]) + + val p = [1, 2] + [3, 4] + + return [1, 2] +} + +fun foo(a: Array = [""]) {} + +class A(val a: Array = []) \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/collectionLiterals/noCollectionLiterals.txt b/compiler/testData/diagnostics/tests/collectionLiterals/noCollectionLiterals.txt new file mode 100644 index 00000000000..c5855e8a9cc --- /dev/null +++ b/compiler/testData/diagnostics/tests/collectionLiterals/noCollectionLiterals.txt @@ -0,0 +1,12 @@ +package + +public fun foo(/*0*/ a: kotlin.Array = ...): kotlin.Unit +public fun test(): kotlin.Array + +public final class A { + public constructor A(/*0*/ a: kotlin.Array = ...) + public final val a: 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 +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 97c836ecf34..202ab8e94fd 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -3381,6 +3381,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/collectionLiterals/noArrayLiteralsInAnnotationsFeature.kt"); doTest(fileName); } + + @TestMetadata("noCollectionLiterals.kt") + public void testNoCollectionLiterals() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/collectionLiterals/noCollectionLiterals.kt"); + doTest(fileName); + } } @TestMetadata("compiler/testData/diagnostics/tests/constructorConsistency")