Introduce language feature for array literals in annotations

This commit is contained in:
Mikhail Zarechenskiy
2017-03-21 02:49:03 +03:00
parent 0f1acab40d
commit bfe2ddf7c1
15 changed files with 79 additions and 4 deletions
@@ -1517,6 +1517,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
public KotlinTypeInfo visitCollectionLiteralExpression(
@NotNull KtCollectionLiteralExpression expression, ExpressionTypingContext context
) {
checkSupportsArrayLiterals(expression, context);
return resolveCollectionLiteralSpecialMethod(expression, context);
}
@@ -1781,6 +1782,20 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
return resultTypeInfo.replaceType(functionResults.getResultingDescriptor().getReturnType());
}
private void checkSupportsArrayLiterals(KtCollectionLiteralExpression expression, ExpressionTypingContext context) {
if (isInsideAnnotationEntryOrClass(expression) &&
!components.languageVersionSettings.supportsFeature(LanguageFeature.ArrayLiteralsInAnnotations)) {
context.trace.report(UNSUPPORTED_FEATURE.on(
expression, TuplesKt.to(LanguageFeature.ArrayLiteralsInAnnotations, components.languageVersionSettings)));
}
}
private static boolean isInsideAnnotationEntryOrClass(KtCollectionLiteralExpression expression) {
//noinspection unchecked
PsiElement parent = PsiTreeUtil.getParentOfType(expression, KtAnnotationEntry.class, KtClass.class);
return parent instanceof KtAnnotationEntry || (parent instanceof KtClass && ((KtClass) parent).isAnnotation());
}
private KotlinTypeInfo resolveCollectionLiteralSpecialMethod(
@NotNull KtCollectionLiteralExpression collectionLiteralExpression,
@NotNull ExpressionTypingContext context
@@ -15,14 +15,18 @@ fun check(b: Boolean, message: String) {
if (!b) throw RuntimeException(message)
}
@Suppress("UNSUPPORTED_FEATURE")
annotation class Foo(val a: FloatArray = [], val b: Array<String> = [], val c: Array<KClass<*>> = [])
@Suppress("UNSUPPORTED_FEATURE")
@Foo(a = [1f, 2f, 1 / 0f])
fun test1() {}
@Suppress("UNSUPPORTED_FEATURE")
@Foo(b = ["Hello", ", ", "Kot" + "lin"])
fun test2() {}
@Suppress("UNSUPPORTED_FEATURE")
@Foo(c = [Int::class, Array<Short>::class, Foo::class])
fun test3() {}
@@ -14,6 +14,7 @@ fun check(b: Boolean, message: String) {
if (!b) throw RuntimeException(message)
}
@Suppress("UNSUPPORTED_FEATURE")
annotation class Foo(val a: IntArray = [], val b: Array<String> = [])
const val ONE_INT = 1
@@ -21,6 +22,7 @@ const val ONE_FLOAT = 1f
const val HELLO = "hello"
const val C_CHAR = 'c'
@Suppress("UNSUPPORTED_FEATURE")
@Foo(
a = [ONE_INT, ONE_INT + ONE_FLOAT.toInt(), ONE_INT + 10, (ONE_INT % 1.0).toInt()],
b = [HELLO, HELLO + C_CHAR, HELLO + ", Kotlin", C_CHAR.toString() + C_CHAR])
@@ -15,6 +15,7 @@ fun check(b: Boolean, message: String) {
if (!b) throw RuntimeException(message)
}
@Suppress("UNSUPPORTED_FEATURE")
annotation class Foo(
val a: IntArray = [],
val b: IntArray = [1, 2, 3],
@@ -3,13 +3,14 @@ public final class CollectionLiteralsInArgumentPositionKt {
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
public final static method check(p0: boolean, @org.jetbrains.annotations.NotNull p1: java.lang.String): void
private final static method test(p0: kotlin.reflect.KFunction, p1: kotlin.jvm.functions.Function1): void
public final static @Foo method test1(): void
public final static @Foo method test2(): void
public final static @Foo method test3(): void
public final static @Foo @kotlin.Suppress method test1(): void
public final static @Foo @kotlin.Suppress method test2(): void
public final static @Foo @kotlin.Suppress method test3(): void
}
@java.lang.annotation.Retention
@kotlin.Metadata
@kotlin.Suppress
public annotation class Foo {
public abstract method a(): float[]
public abstract method b(): java.lang.String[]
@@ -7,11 +7,12 @@ public final class CollectionLiteralsWithConstantsKt {
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
public final static method check(p0: boolean, @org.jetbrains.annotations.NotNull p1: java.lang.String): void
private final static method test(p0: kotlin.reflect.KFunction, p1: kotlin.jvm.functions.Function1): void
public final static @Foo method test1(): void
public final static @Foo @kotlin.Suppress method test1(): void
}
@java.lang.annotation.Retention
@kotlin.Metadata
@kotlin.Suppress
public annotation class Foo {
public abstract method a(): int[]
public abstract method b(): java.lang.String[]
@@ -8,6 +8,7 @@ public final class DefaultAnnotationParameterValuesKt {
@java.lang.annotation.Retention
@kotlin.Metadata
@kotlin.Suppress
public annotation class Foo {
public abstract method a(): int[]
public abstract method b(): int[]
@@ -1,3 +1,5 @@
// !LANGUAGE: +ArrayLiteralsInAnnotations
annotation class Foo(val a: IntArray, val b: Array<String>, val c: FloatArray)
@Foo([1], ["/"], [1f])
@@ -1,3 +1,5 @@
// !LANGUAGE: +ArrayLiteralsInAnnotations
import kotlin.reflect.KClass
annotation class Foo(val a: Array<KClass<*>> = [])
@@ -1,3 +1,5 @@
// !LANGUAGE: +ArrayLiteralsInAnnotations
annotation class Foo(
val a: Array<String> = ["/"],
val b: Array<String> = [],
@@ -1,3 +1,5 @@
// !LANGUAGE: +ArrayLiteralsInAnnotations
const val ONE = 1
annotation class Foo(
@@ -0,0 +1,18 @@
annotation class Foo(
val a: IntArray = <!UNSUPPORTED_FEATURE!>[]<!>,
val b: FloatArray = <!UNSUPPORTED_FEATURE!>[1f, 2f]<!>,
val c: Array<String> = <!UNSUPPORTED_FEATURE!>["/"]<!>
)
@Foo
fun test1() {}
@Foo(a = <!UNSUPPORTED_FEATURE!>[1, 2]<!>, c = <!UNSUPPORTED_FEATURE!>["a"]<!>)
fun test2() {}
@Foo(<!UNSUPPORTED_FEATURE!>[1]<!>, <!UNSUPPORTED_FEATURE!>[3f]<!>, <!UNSUPPORTED_FEATURE!>["a"]<!>)
fun test3() {}
fun test4() {
[1, 2]
}
@@ -0,0 +1,16 @@
package
@Foo public fun test1(): kotlin.Unit
@Foo(a = {1, 2}, c = {"a"}) public fun test2(): kotlin.Unit
@Foo(a = {1}, b = {3.0.toFloat()}, c = {"a"}) public fun test3(): kotlin.Unit
public fun test4(): kotlin.Unit
public final annotation class Foo : kotlin.Annotation {
public constructor Foo(/*0*/ a: kotlin.IntArray = ..., /*1*/ b: kotlin.FloatArray = ..., /*2*/ c: kotlin.Array<kotlin.String> = ...)
public final val a: kotlin.IntArray
public final val b: kotlin.FloatArray
public final val c: kotlin.Array<kotlin.String>
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
}
@@ -3375,6 +3375,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/collectionLiterals/defaultValuesWithConstantsInAnnotation.kt");
doTest(fileName);
}
@TestMetadata("noArrayLiteralsInAnnotationsFeature.kt")
public void testNoArrayLiteralsInAnnotationsFeature() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/collectionLiterals/noArrayLiteralsInAnnotationsFeature.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/diagnostics/tests/constructorConsistency")
@@ -54,6 +54,8 @@ enum class LanguageFeature(
MultiPlatformProjects(sinceVersion = null, defaultState = State.DISABLED),
ArrayLiteralsInAnnotations(sinceVersion = null),
;
val presentableName: String