Fix IAE for wrong use-site @file annotation

#EA-100189 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2017-07-27 18:22:09 +03:00
parent 4d4c39939f
commit 8f8143d3ed
18 changed files with 208 additions and 27 deletions
@@ -246,6 +246,7 @@ public interface Errors {
DiagnosticFactory0<PsiElement> INAPPLICABLE_RECEIVER_TARGET = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> INAPPLICABLE_PARAM_TARGET = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<PsiElement, String> REDUNDANT_ANNOTATION_TARGET = DiagnosticFactory1.create(WARNING);
DiagnosticFactory0<KtAnnotationUseSiteTarget> INAPPLICABLE_FILE_TARGET = DiagnosticFactory0.create(ERROR);
// Classes and interfaces
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -112,7 +112,7 @@ fun ResolutionContext<*>.reportTypeMismatchDueToTypeProjection(
return true
}
private fun BindingTrace.reportDiagnosticOnce(diagnostic: Diagnostic) {
fun BindingTrace.reportDiagnosticOnce(diagnostic: Diagnostic) {
if (bindingContext.diagnostics.forElement(diagnostic.psiElement).any { it.factory == diagnostic.factory }) return
report(diagnostic)
@@ -159,6 +159,7 @@ public class DefaultErrorMessages {
MAP.put(INAPPLICABLE_RECEIVER_TARGET, "'@receiver:' annotations can only be applied to the receiver type of extension function or extension property declarations");
MAP.put(INAPPLICABLE_PARAM_TARGET, "'@param:' annotations could be applied only to primary constructor parameters");
MAP.put(REDUNDANT_ANNOTATION_TARGET, "Redundant annotation target ''{0}''", STRING);
MAP.put(INAPPLICABLE_FILE_TARGET, "'@file:' annotations can only be applied before package declaration");
MAP.put(ILLEGAL_SINCE_KOTLIN_VALUE, "Invalid @SinceKotlin annotation value (should be 'major.minor' or 'major.minor.patch')");
MAP.put(NEWER_VERSION_IN_SINCE_KOTLIN, "The version is greater than the specified API version {0}", STRING);
@@ -700,21 +700,16 @@ public class KotlinParsing extends AbstractKotlinParsing {
}
if (targetKeyword == null && mode.isFileAnnotationParsingMode) {
parseAnnotationTarget(mode, FILE_KEYWORD);
parseAnnotationTarget(FILE_KEYWORD);
}
else if (targetKeyword != null) {
parseAnnotationTarget(mode, targetKeyword);
parseAnnotationTarget(targetKeyword);
}
return true;
}
private void parseAnnotationTarget(AnnotationParsingMode mode, KtKeywordToken keyword) {
if (keyword == FILE_KEYWORD && !mode.isFileAnnotationParsingMode && at(keyword) && lookahead(1) == COLON) {
errorAndAdvance(AT.getValue() + keyword.getValue() + " annotations are only allowed before package declaration", 2);
return;
}
private void parseAnnotationTarget(KtKeywordToken keyword) {
String message = "Expecting \"" + keyword.getValue() + COLON.getValue() + "\" prefix for " + keyword.getValue() + " annotations";
PsiBuilder.Marker marker = mark();
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,9 +16,13 @@
package org.jetbrains.kotlin.resolve
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.diagnostics.Errors.*
import org.jetbrains.kotlin.diagnostics.reportDiagnosticOnce
import org.jetbrains.kotlin.psi.*
object AnnotationUseSiteTargetChecker {
@@ -54,14 +58,17 @@ object AnnotationUseSiteTargetChecker {
annotationEntry()?.let { report(INAPPLICABLE_TARGET_ON_PROPERTY.on(it, target.renderName)) }
}
AnnotationUseSiteTarget.CONSTRUCTOR_PARAMETER -> annotationEntry()?.let { report(INAPPLICABLE_PARAM_TARGET.on(it)) }
AnnotationUseSiteTarget.FILE -> throw IllegalArgumentException("@file annotations are not allowed here")
AnnotationUseSiteTarget.FILE -> {
annotationEntry()?.useSiteTarget?.let { reportDiagnosticOnce(INAPPLICABLE_FILE_TARGET.on(it)) }
}
}
}
}
private fun BindingTrace.checkDeclaration(annotated: KtAnnotated, descriptor: DeclarationDescriptor) {
for (annotation in annotated.annotationEntries) {
val target = annotation.useSiteTarget?.getAnnotationUseSiteTarget() ?: continue
val useSiteTarget = annotation.useSiteTarget
val target = useSiteTarget?.getAnnotationUseSiteTarget() ?: continue
when (target) {
AnnotationUseSiteTarget.FIELD -> checkIfHasBackingField(annotated, descriptor, annotation)
@@ -84,7 +91,7 @@ object AnnotationUseSiteTargetChecker {
}
}
AnnotationUseSiteTarget.SETTER_PARAMETER -> checkIfMutableProperty(annotated, annotation)
AnnotationUseSiteTarget.FILE -> throw IllegalArgumentException("@file annotations are not allowed here")
AnnotationUseSiteTarget.FILE -> reportDiagnosticOnce(INAPPLICABLE_FILE_TARGET.on(useSiteTarget))
AnnotationUseSiteTarget.RECEIVER -> report(INAPPLICABLE_RECEIVER_TARGET.on(annotation))
}
}
@@ -0,0 +1,20 @@
package bar
<!MUST_BE_INITIALIZED!><!WRONG_ANNOTATION_TARGET_WITH_USE_SITE_TARGET!>@<!INAPPLICABLE_FILE_TARGET!>file<!>:foo<!>
val prop<!>
@<!INAPPLICABLE_FILE_TARGET!>file<!>:[<!WRONG_ANNOTATION_TARGET_WITH_USE_SITE_TARGET!>bar<!> <!WRONG_ANNOTATION_TARGET_WITH_USE_SITE_TARGET!>baz<!>]
fun func() {}
@<!INAPPLICABLE_FILE_TARGET!>file<!>:[<!WRONG_ANNOTATION_TARGET_WITH_USE_SITE_TARGET!>baz<!>]
class C
<!SYNTAX!>@file:<!>
interface T
@file:[<!SYNTAX!><!>]
interface T
annotation class foo
annotation class bar
annotation class baz
@@ -0,0 +1,46 @@
package
package bar {
@file:bar.foo public val prop: [ERROR : No type, no body]
@file:bar.bar @file:bar.baz public fun func(): kotlin.Unit
@file:bar.baz public final class C {
public constructor C()
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 interface T {
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 interface T {
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 final annotation class bar : kotlin.Annotation {
public constructor bar()
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 final annotation class baz : kotlin.Annotation {
public constructor baz()
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 final annotation class foo : kotlin.Annotation {
public constructor foo()
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,8 @@
<!WRONG_ANNOTATION_TARGET_WITH_USE_SITE_TARGET!>@file:foo<!>
@foo @bar
@<!INAPPLICABLE_FILE_TARGET!>file<!>:[<!WRONG_ANNOTATION_TARGET_WITH_USE_SITE_TARGET!>baz<!>]
fun test() {}
annotation class foo
annotation class bar
annotation class baz
@@ -0,0 +1,24 @@
package
@foo @bar @file:baz public fun test(): kotlin.Unit
public final annotation class bar : kotlin.Annotation {
public constructor bar()
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 final annotation class baz : kotlin.Annotation {
public constructor baz()
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 final annotation class foo : kotlin.Annotation {
public constructor foo()
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,8 @@
<!WRONG_ANNOTATION_TARGET_WITH_USE_SITE_TARGET!>@file:foo<!>
@foo @bar
<!WRONG_ANNOTATION_TARGET_WITH_USE_SITE_TARGET!>@<!INAPPLICABLE_FILE_TARGET!>file<!>: baz<!>
fun test() {}
annotation class foo
annotation class bar
annotation class baz
@@ -0,0 +1,24 @@
package
@foo @bar @file:baz public fun test(): kotlin.Unit
public final annotation class bar : kotlin.Annotation {
public constructor bar()
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 final annotation class baz : kotlin.Annotation {
public constructor baz()
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 final annotation class foo : kotlin.Annotation {
public constructor foo()
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,10 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun test1(<!WRONG_ANNOTATION_TARGET_WITH_USE_SITE_TARGET!>@<!INAPPLICABLE_FILE_TARGET!>file<!><!SYNTAX!><!> Suppress("")<!> x: Int) {}
<!WRONG_ANNOTATION_TARGET_WITH_USE_SITE_TARGET!>@<!INAPPLICABLE_FILE_TARGET!>file<!> <!SYNTAX!>@<!>Suppress("")<!>
fun test2() {}
class OnType(x: <!WRONG_ANNOTATION_TARGET_WITH_USE_SITE_TARGET!>@file<!SYNTAX!><!> Suppress("")<!> Int)
fun <!WRONG_ANNOTATION_TARGET_WITH_USE_SITE_TARGET!>@<!INAPPLICABLE_FILE_TARGET!>file<!> : Suppress("")<!> Int.test3() {}
@@ -0,0 +1,12 @@
package
public fun test1(/*0*/ @file:kotlin.Suppress(names = {""}) x: kotlin.Int): kotlin.Unit
@file:kotlin.Suppress(names = {""}) public fun test2(): kotlin.Unit
public fun @file:kotlin.Suppress(names = {""}) kotlin.Int.test3(): kotlin.Unit
public final class OnType {
public constructor OnType(/*0*/ x: @file:kotlin.Suppress(names = {""}) 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
}
@@ -11,9 +11,9 @@ KtFile: fileAnnotationInWrongPlace.kt
MODIFIER_LIST
ANNOTATION_ENTRY
PsiElement(AT)('@')
PsiErrorElement:@file annotations are only allowed before package declaration
ANNOTATION_TARGET
PsiElement(file)('file')
PsiElement(COLON)(':')
PsiElement(COLON)(':')
CONSTRUCTOR_CALLEE
TYPE_REFERENCE
USER_TYPE
@@ -28,9 +28,9 @@ KtFile: fileAnnotationInWrongPlace.kt
MODIFIER_LIST
ANNOTATION
PsiElement(AT)('@')
PsiErrorElement:@file annotations are only allowed before package declaration
ANNOTATION_TARGET
PsiElement(file)('file')
PsiElement(COLON)(':')
PsiElement(COLON)(':')
PsiElement(LBRACKET)('[')
ANNOTATION_ENTRY
CONSTRUCTOR_CALLEE
@@ -62,9 +62,9 @@ KtFile: fileAnnotationInWrongPlace.kt
MODIFIER_LIST
ANNOTATION
PsiElement(AT)('@')
PsiErrorElement:@file annotations are only allowed before package declaration
ANNOTATION_TARGET
PsiElement(file)('file')
PsiElement(COLON)(':')
PsiElement(COLON)(':')
PsiElement(LBRACKET)('[')
ANNOTATION_ENTRY
CONSTRUCTOR_CALLEE
@@ -93,9 +93,9 @@ KtFile: fileAnnotationInWrongPlace.kt
MODIFIER_LIST
ANNOTATION
PsiElement(AT)('@')
PsiErrorElement:@file annotations are only allowed before package declaration
ANNOTATION_TARGET
PsiElement(file)('file')
PsiElement(COLON)(':')
PsiElement(COLON)(':')
PsiElement(LBRACKET)('[')
PsiErrorElement:Expecting a list of annotations
<empty list>
@@ -35,9 +35,9 @@ KtFile: withoutPackage.kt
PsiWhiteSpace('\n')
ANNOTATION
PsiElement(AT)('@')
PsiErrorElement:@file annotations are only allowed before package declaration
ANNOTATION_TARGET
PsiElement(file)('file')
PsiElement(COLON)(':')
PsiElement(COLON)(':')
PsiElement(LBRACKET)('[')
ANNOTATION_ENTRY
CONSTRUCTOR_CALLEE
@@ -35,9 +35,9 @@ KtFile: withoutPackageWithSimpleAnnotation.kt
PsiWhiteSpace('\n')
ANNOTATION_ENTRY
PsiElement(AT)('@')
PsiErrorElement:@file annotations are only allowed before package declaration
ANNOTATION_TARGET
PsiElement(file)('file')
PsiElement(COLON)(':')
PsiElement(COLON)(':')
PsiWhiteSpace(' ')
CONSTRUCTOR_CALLEE
TYPE_REFERENCE
@@ -1581,12 +1581,36 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("diagnosticFileAnnotationInWrongPlace.kt")
public void testDiagnosticFileAnnotationInWrongPlace() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/annotations/withUseSiteTarget/diagnosticFileAnnotationInWrongPlace.kt");
doTest(fileName);
}
@TestMetadata("diagnosticWithoutPackage.kt")
public void testDiagnosticWithoutPackage() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/annotations/withUseSiteTarget/diagnosticWithoutPackage.kt");
doTest(fileName);
}
@TestMetadata("diagnosticWithoutPackageWithSimpleAnnotation.kt")
public void testDiagnosticWithoutPackageWithSimpleAnnotation() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/annotations/withUseSiteTarget/diagnosticWithoutPackageWithSimpleAnnotation.kt");
doTest(fileName);
}
@TestMetadata("FieldAnnotations.kt")
public void testFieldAnnotations() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/annotations/withUseSiteTarget/FieldAnnotations.kt");
doTest(fileName);
}
@TestMetadata("fileAnnotationWithoutColon.kt")
public void testFileAnnotationWithoutColon() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/annotations/withUseSiteTarget/fileAnnotationWithoutColon.kt");
doTest(fileName);
}
@TestMetadata("FileAnnotations.kt")
public void testFileAnnotations() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/annotations/withUseSiteTarget/FileAnnotations.kt");
@@ -12,4 +12,5 @@ class Completion(@get:Ann val p1: String, @<caret>)
/*TODO: in fact is not applicable */
// EXIST: receiver
// EXIST: delegate
// EXIST: file
// NOTHING_ELSE