Annotation repetition checking with a pair of tests, some old tests changes

This commit is contained in:
Mikhail Glukhikh
2015-07-23 12:02:28 +03:00
parent 8beafe90a0
commit d6406d8d4a
19 changed files with 159 additions and 30 deletions
@@ -113,7 +113,8 @@ public interface Errors {
DiagnosticFactory1<PsiElement, JetModifierKeywordToken> REPEATED_MODIFIER = DiagnosticFactory1.create(ERROR);
DiagnosticFactory2<PsiElement, JetModifierKeywordToken, JetModifierKeywordToken> REDUNDANT_MODIFIER = DiagnosticFactory2.create(WARNING);
DiagnosticFactory0<PsiElement> INAPPLICABLE_PLATFORM_NAME = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<PsiElement, String> WRONG_ANNOTATION_TARGET = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<JetAnnotationEntry, String> WRONG_ANNOTATION_TARGET = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<JetAnnotationEntry> REPEATED_ANNOTATION = DiagnosticFactory0.create(ERROR);
// Annotations
@@ -137,6 +137,7 @@ public class DefaultErrorMessages {
MAP.put(REPEATED_MODIFIER, "Repeated ''{0}''", TO_STRING);
MAP.put(INAPPLICABLE_PLATFORM_NAME, "platformName annotation is not applicable to this declaration");
MAP.put(WRONG_ANNOTATION_TARGET, "This annotation is not applicable to target ''{0}''", TO_STRING);
MAP.put(REPEATED_ANNOTATION, "This annotation is not repeatable");
MAP.put(REDUNDANT_MODIFIER, "Modifier ''{0}'' is redundant because ''{1}'' is present", TO_STRING, TO_STRING);
MAP.put(ABSTRACT_MODIFIER_IN_TRAIT, "Modifier ''abstract'' is redundant in interface");
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.types.TypeUtils
import java.lang.annotation.ElementType
import java.util.*
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
import org.jetbrains.kotlin.resolve.constants.BooleanValue
import kotlin.annotation
public object AnnotationTargetChecker {
@@ -37,9 +38,7 @@ public object AnnotationTargetChecker {
public fun check(annotated: JetAnnotated, trace: BindingTrace, descriptor: ClassDescriptor? = null) {
if (annotated is JetTypeParameter) return // TODO: support type parameter annotations
val actualTargets = getActualTargetList(annotated, descriptor)
for (entry in annotated.getAnnotationEntries()) {
checkAnnotationEntry(entry, actualTargets, trace)
}
checkEntries(annotated.annotationEntries, actualTargets, trace)
if (annotated is JetCallableDeclaration) {
annotated.getTypeReference()?.let { check(it, trace) }
}
@@ -61,9 +60,7 @@ public object AnnotationTargetChecker {
}
public fun checkExpression(expression: JetExpression, trace: BindingTrace) {
for (entry in expression.getAnnotationEntries()) {
checkAnnotationEntry(entry, listOf(KotlinTarget.EXPRESSION), trace)
}
checkEntries(expression.getAnnotationEntries(), listOf(KotlinTarget.EXPRESSION), trace)
if (expression is JetFunctionLiteralExpression) {
for (parameter in expression.getValueParameters()) {
parameter.getTypeReference()?.let { check(it, trace) }
@@ -71,6 +68,26 @@ public object AnnotationTargetChecker {
}
}
private fun isRepeatable(classDescriptor: ClassDescriptor): Boolean {
val annotationEntryDescriptor = classDescriptor.annotations.findAnnotation(KotlinBuiltIns.FQ_NAMES.annotation) ?: return false
val repeatableArgumentValue = annotationEntryDescriptor.allValueArguments.entrySet().firstOrNull {
"repeatable" == it.key.name.asString()
}?.getValue() as? BooleanValue ?: return false
return repeatableArgumentValue.value
}
private fun checkEntries(entries: List<JetAnnotationEntry>, actualTargets: List<KotlinTarget>, trace: BindingTrace) {
val entryTypes: MutableSet<JetType> = hashSetOf()
for (entry in entries) {
checkAnnotationEntry(entry, actualTargets, trace)
val descriptor = trace.get(BindingContext.ANNOTATION, entry) ?: continue
val classDescriptor = TypeUtils.getClassDescriptor(descriptor.type) ?: continue
if (!entryTypes.add(descriptor.type) && !isRepeatable(classDescriptor)) {
trace.report(Errors.REPEATED_ANNOTATION.on(entry));
}
}
}
public fun possibleTargetSet(classDescriptor: ClassDescriptor): Set<KotlinTarget>? {
val targetEntryDescriptor = classDescriptor.getAnnotations().findAnnotation(KotlinBuiltIns.FQ_NAMES.target)
?: return null
@@ -1,3 +1,17 @@
annotation(repeatable = true) class repann
repann repann class DoubleAnnotated
annotation(retention = AnnotationRetention.SOURCE, repeatable = true) class repann1(val x: Int)
annotation(repeatable = true, retention = AnnotationRetention.SOURCE) class repann2(val f: Boolean)
target(AnnotationTarget.EXPRESSION) annotation(repeatable = true) class repexpr
repann repann class DoubleAnnotated
repann1(1) repann1(2) repann1(3) class TripleAnnotated
repann2(true) repann2(false) repann2(false) repann2(true) class FourTimesAnnotated
@repann @repann fun foo(@repann @repann x: Int): Int {
@repexpr @repexpr return x
}
@@ -1,5 +1,7 @@
package
repann() repann() internal fun foo(/*0*/ repann() repann() x: kotlin.Int): kotlin.Int
repann() repann() internal final class DoubleAnnotated {
public constructor DoubleAnnotated()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
@@ -7,9 +9,46 @@ repann() repann() internal final class DoubleAnnotated {
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
repann2(f = true) repann2(f = false) repann2(f = false) repann2(f = true) internal final class FourTimesAnnotated {
public constructor FourTimesAnnotated()
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
}
repann1(x = 1) repann1(x = 2) repann1(x = 3) internal final class TripleAnnotated {
public constructor TripleAnnotated()
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
}
kotlin.annotation.annotation(repeatable = true) internal final class repann : kotlin.Annotation {
public constructor repann()
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
}
kotlin.annotation.annotation(repeatable = true, retention = AnnotationRetention.SOURCE) internal final class repann1 : kotlin.Annotation {
public constructor repann1(/*0*/ x: kotlin.Int)
internal final val x: kotlin.Int
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
}
kotlin.annotation.annotation(repeatable = true, retention = AnnotationRetention.SOURCE) internal final class repann2 : kotlin.Annotation {
public constructor repann2(/*0*/ f: kotlin.Boolean)
internal final val f: kotlin.Boolean
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
}
kotlin.annotation.target(allowedTargets = {AnnotationTarget.EXPRESSION}) kotlin.annotation.annotation(repeatable = true) internal final class repexpr : kotlin.Annotation {
public constructor repexpr()
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
}
@@ -0,0 +1,13 @@
annotation(repeatable = false) class unrepann(val x: Int)
annotation class ann(val y: Int)
unrepann(1) <!REPEATED_ANNOTATION!>unrepann(2)<!> class DoubleAnnotated
ann(3) <!REPEATED_ANNOTATION!>ann(7)<!> <!REPEATED_ANNOTATION!>ann(42)<!> class TripleAnnotated
target(AnnotationTarget.EXPRESSION) annotation class annexpr
ann(0) <!REPEATED_ANNOTATION!>ann(1)<!> fun foo(@ann(7) <!REPEATED_ANNOTATION!>@ann(2)<!> x: Int): Int {
@annexpr <!REPEATED_ANNOTATION!>@annexpr<!> return x
}
@@ -0,0 +1,40 @@
package
ann(y = 0) ann(y = 1) internal fun foo(/*0*/ ann(y = 7) ann(y = 2) x: kotlin.Int): kotlin.Int
unrepann(x = 1) unrepann(x = 2) internal final class DoubleAnnotated {
public constructor DoubleAnnotated()
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
}
ann(y = 3) ann(y = 7) ann(y = 42) internal final class TripleAnnotated {
public constructor TripleAnnotated()
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
}
kotlin.annotation.annotation() internal final class ann : kotlin.Annotation {
public constructor ann(/*0*/ y: kotlin.Int)
internal final val y: kotlin.Int
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
}
kotlin.annotation.target(allowedTargets = {AnnotationTarget.EXPRESSION}) kotlin.annotation.annotation() internal final class annexpr : kotlin.Annotation {
public constructor annexpr()
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
}
kotlin.annotation.annotation(repeatable = false) internal final class unrepann : kotlin.Annotation {
public constructor unrepann(/*0*/ x: kotlin.Int)
internal final val x: kotlin.Int
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
}
@@ -1,5 +1,5 @@
class Test {
@`InnerAnnotation` @InnerAnnotation
@`InnerAnnotation` <!REPEATED_ANNOTATION!>@InnerAnnotation<!>
companion object : StaticClass(), <!INACCESSIBLE_OUTER_CLASS_EXPRESSION!><!MANY_CLASSES_IN_SUPERTYPE_LIST!>InnerClass<!>()<!> {
}
@@ -1,4 +1,4 @@
annotation class Ann(val i: IntArray)
annotation(repeatable = true) class Ann(val i: IntArray)
Ann(intArrayOf(<!ANNOTATION_PARAMETER_MUST_BE_CONST!>i<!>))
Ann(intArrayOf(i2))
@@ -13,7 +13,7 @@ val i3 = foo()
fun foo(): Int = 1
annotation class AnnAnn(val i: Array<Ann>)
annotation(repeatable = true) class AnnAnn(val i: Array<Ann>)
AnnAnn(arrayOf(Ann(intArrayOf(1))))
AnnAnn(arrayOf(<!ANNOTATION_PARAMETER_MUST_BE_CONST!>iAnn<!>))
class TestAnn
@@ -6,7 +6,7 @@ internal val i3: kotlin.Int
internal val iAnn: Ann
internal fun foo(): kotlin.Int
kotlin.annotation.annotation() internal final class Ann : kotlin.Annotation {
kotlin.annotation.annotation(repeatable = true) internal final class Ann : kotlin.Annotation {
public constructor Ann(/*0*/ i: kotlin.IntArray)
internal final val i: kotlin.IntArray
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
@@ -14,7 +14,7 @@ kotlin.annotation.annotation() internal final class Ann : kotlin.Annotation {
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
kotlin.annotation.annotation() internal final class AnnAnn : kotlin.Annotation {
kotlin.annotation.annotation(repeatable = true) internal final class AnnAnn : kotlin.Annotation {
public constructor AnnAnn(/*0*/ i: kotlin.Array<Ann>)
internal final val i: kotlin.Array<Ann>
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
@@ -1,4 +1,4 @@
annotation class Ann(val i: Int)
annotation(repeatable = true) class Ann(val i: Int)
annotation class AnnIA(val ia: IntArray)
annotation class AnnSA(val sa: Array<String>)
@@ -6,7 +6,7 @@ internal val ia: kotlin.IntArray
internal val sa: kotlin.Array<kotlin.String>
internal fun foo(): kotlin.Int
kotlin.annotation.annotation() internal final class Ann : kotlin.Annotation {
kotlin.annotation.annotation(repeatable = true) internal final class Ann : kotlin.Annotation {
public constructor Ann(/*0*/ i: kotlin.Int)
internal final val i: kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
@@ -1,4 +1,4 @@
annotation class Ann(vararg val i: Int)
annotation(repeatable = true) class Ann(vararg val i: Int)
Ann(<!ANNOTATION_PARAMETER_MUST_BE_CONST!>i<!>)
Ann(i2)
@@ -16,7 +16,7 @@ val i3 = foo()
fun foo(): Int = 1
annotation class AnnAnn(vararg val i: Ann)
annotation(repeatable = true) class AnnAnn(vararg val i: Ann)
AnnAnn(*arrayOf(Ann(1)))
AnnAnn(*arrayOf(<!ANNOTATION_PARAMETER_MUST_BE_CONST!>iAnn<!>))
class TestAnn
@@ -6,7 +6,7 @@ internal val i3: kotlin.Int
internal val iAnn: Ann
internal fun foo(): kotlin.Int
kotlin.annotation.annotation() internal final class Ann : kotlin.Annotation {
kotlin.annotation.annotation(repeatable = true) internal final class Ann : kotlin.Annotation {
public constructor Ann(/*0*/ vararg i: kotlin.Int /*kotlin.IntArray*/)
internal final val i: kotlin.IntArray
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
@@ -14,7 +14,7 @@ kotlin.annotation.annotation() internal final class Ann : kotlin.Annotation {
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
kotlin.annotation.annotation() internal final class AnnAnn : kotlin.Annotation {
kotlin.annotation.annotation(repeatable = true) internal final class AnnAnn : kotlin.Annotation {
public constructor AnnAnn(/*0*/ vararg i: Ann /*kotlin.Array<out Ann>*/)
internal final val i: kotlin.Array<out Ann>
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
@@ -4,14 +4,14 @@ package a.b.c
kotlin.deprecated("aaa")
ann1(kotlin.deprecated("aaa"))
a.b.c.ann1()
<!REPEATED_ANNOTATION!>a.b.c.ann1()<!>
ann2(a.b.c.ann1())
A.IAnn()
ann3(A.IAnn())
a.b.c.A.IAnn()
ann3(a.b.c.A.IAnn())
<!REPEATED_ANNOTATION!>a.b.c.A.IAnn()<!>
<!REPEATED_ANNOTATION!>ann3(a.b.c.A.IAnn())<!>
annArray(kotlin.arrayOf("a"))
fun test() = 1
@@ -1040,6 +1040,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("unrepeatable.kt")
public void testUnrepeatable() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/annotations/options/unrepeatable.kt");
doTest(fileName);
}
@TestMetadata("compiler/testData/diagnostics/tests/annotations/options/targets")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
+5 -5
View File
@@ -1,10 +1,10 @@
<error>@file:kotlin.deprecated("message")</error>
@file:suppress(<error>BAR</error>)
@file:suppress(BAZ)
<error>@file:suppress(BAZ)</error>
<error>@<error>k</error>otlin.deprecated("message")</error>
@<error>s</error>uppress(<error>BAR</error>)
@<error>s</error>uppress(BAZ)
<error><error>@<error>k</error>otlin.deprecated("message")</error></error>
<error>@<error>s</error>uppress(<error>BAR</error>)</error>
<error>@<error>s</error>uppress(BAZ)</error>
@file:myAnnotation(1, "string")
@file:boo.myAnnotation(1, <error>BAR</error>)
@@ -20,4 +20,4 @@ val BAZ = "baz"
val N = 0
target(AnnotationTarget.FILE)
annotation class myAnnotation(val i: Int, val s: String)
annotation(repeatable = true) class myAnnotation(val i: Int, val s: String)
@@ -32,6 +32,6 @@ annotation class a
target(AnnotationTarget.PROPERTY, AnnotationTarget.FUNCTION, AnnotationTarget.CLASSIFIER,
AnnotationTarget.CONSTRUCTOR, AnnotationTarget.TYPE)
annotation(repeatable = false) class b(val e: E)
annotation(repeatable = true) class b(val e: E)
enum class E { E1 E2 }
-2
View File
@@ -62,12 +62,10 @@ public fun jq(selector: String): JQuery = JQuery();
native("$")
public fun jq(selector: String, context: Element): JQuery = JQuery();
native("$")
native("$")
public fun jq(callback: () -> Unit): JQuery = JQuery();
native("$")
public fun jq(obj: JQuery): JQuery = JQuery();
native("$")
public fun jq(el: Element): JQuery = JQuery();
native("$")
native("$")
public fun jq(): JQuery = JQuery();