From e16d16fdb71bfebe9142aaddd3e3e254c1a2aa2c Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Mon, 11 Sep 2017 19:25:53 +0300 Subject: [PATCH] Do not report diagnostic about annotation with receiver target twice --- .../jetbrains/kotlin/diagnostics/Errors.java | 1 - .../rendering/DefaultErrorMessages.java | 1 - .../resolve/AnnotationUseSiteTargetChecker.kt | 29 +------------------ .../withUseSiteTarget/ReceiverAnnotations.kt | 12 ++++---- .../fileAnnotationWithoutColon.kt | 2 +- .../quickfix/MoveReceiverAnnotationFix.kt | 3 ++ .../kotlin/idea/quickfix/QuickFixRegistrar.kt | 2 +- .../moveReceiverAnnotation/notExtensionFun.kt | 2 +- .../moveReceiverAnnotation/notExtensionVal.kt | 2 +- 9 files changed, 14 insertions(+), 40 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java index 01bef0f8742..3621fc7d0c6 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java @@ -244,7 +244,6 @@ public interface Errors { DiagnosticFactory1 INAPPLICABLE_TARGET_PROPERTY_IMMUTABLE = DiagnosticFactory1.create(ERROR); DiagnosticFactory0 INAPPLICABLE_TARGET_PROPERTY_HAS_NO_DELEGATE = DiagnosticFactory0.create(ERROR); DiagnosticFactory0 INAPPLICABLE_TARGET_PROPERTY_HAS_NO_BACKING_FIELD = DiagnosticFactory0.create(ERROR); - DiagnosticFactory0 INAPPLICABLE_RECEIVER_TARGET = DiagnosticFactory0.create(ERROR); DiagnosticFactory0 INAPPLICABLE_PARAM_TARGET = DiagnosticFactory0.create(ERROR); DiagnosticFactory1 REDUNDANT_ANNOTATION_TARGET = DiagnosticFactory1.create(WARNING); DiagnosticFactory0 INAPPLICABLE_FILE_TARGET = DiagnosticFactory0.create(ERROR); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java index a256f2afc3f..9ea3acffe54 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -158,7 +158,6 @@ public class DefaultErrorMessages { MAP.put(INAPPLICABLE_TARGET_PROPERTY_IMMUTABLE, "''@{0}:'' annotations could be applied only to mutable properties", TO_STRING); MAP.put(INAPPLICABLE_TARGET_PROPERTY_HAS_NO_DELEGATE, "'@delegate:' annotations could be applied only to delegated properties"); MAP.put(INAPPLICABLE_TARGET_PROPERTY_HAS_NO_BACKING_FIELD, "'@field:' annotations could be applied only to properties with backing fields"); - 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"); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUseSiteTargetChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUseSiteTargetChecker.kt index df532f1037d..1eb4b6b5d71 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUseSiteTargetChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUseSiteTargetChecker.kt @@ -16,7 +16,6 @@ package org.jetbrains.kotlin.resolve -import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.ConstructorDescriptor import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.descriptors.PropertyDescriptor @@ -37,32 +36,6 @@ object AnnotationUseSiteTargetChecker { trace.checkDeclaration(parameter, parameterDescriptor) } } - - if (descriptor is CallableDescriptor) trace.checkReceiverAnnotations(descriptor) - } - - private fun BindingTrace.checkReceiverAnnotations(descriptor: CallableDescriptor) { - val extensionReceiver = descriptor.extensionReceiverParameter ?: return - for (annotationWithTarget in extensionReceiver.type.annotations.getUseSiteTargetedAnnotations()) { - val target = annotationWithTarget.target ?: continue - fun annotationEntry() = DescriptorToSourceUtils.getSourceFromAnnotation(annotationWithTarget.annotation) - - when (target) { - AnnotationUseSiteTarget.RECEIVER -> {} - AnnotationUseSiteTarget.FIELD, - AnnotationUseSiteTarget.PROPERTY_DELEGATE_FIELD, - AnnotationUseSiteTarget.PROPERTY, - AnnotationUseSiteTarget.PROPERTY_GETTER, - AnnotationUseSiteTarget.PROPERTY_SETTER, - AnnotationUseSiteTarget.SETTER_PARAMETER -> { - annotationEntry()?.let { report(INAPPLICABLE_TARGET_ON_PROPERTY.on(it, target.renderName)) } - } - AnnotationUseSiteTarget.CONSTRUCTOR_PARAMETER -> annotationEntry()?.let { report(INAPPLICABLE_PARAM_TARGET.on(it)) } - AnnotationUseSiteTarget.FILE -> { - annotationEntry()?.useSiteTarget?.let { reportDiagnosticOnce(INAPPLICABLE_FILE_TARGET.on(it)) } - } - } - } } private fun BindingTrace.checkDeclaration(annotated: KtAnnotated, descriptor: DeclarationDescriptor) { @@ -92,7 +65,7 @@ object AnnotationUseSiteTargetChecker { } AnnotationUseSiteTarget.SETTER_PARAMETER -> checkIfMutableProperty(annotated, annotation) AnnotationUseSiteTarget.FILE -> reportDiagnosticOnce(INAPPLICABLE_FILE_TARGET.on(useSiteTarget)) - AnnotationUseSiteTarget.RECEIVER -> report(INAPPLICABLE_RECEIVER_TARGET.on(annotation)) + AnnotationUseSiteTarget.RECEIVER -> {} } } } diff --git a/compiler/testData/diagnostics/tests/annotations/withUseSiteTarget/ReceiverAnnotations.kt b/compiler/testData/diagnostics/tests/annotations/withUseSiteTarget/ReceiverAnnotations.kt index db591b9c260..1fac1cfafac 100644 --- a/compiler/testData/diagnostics/tests/annotations/withUseSiteTarget/ReceiverAnnotations.kt +++ b/compiler/testData/diagnostics/tests/annotations/withUseSiteTarget/ReceiverAnnotations.kt @@ -1,17 +1,17 @@ annotation class Ann -@receiver:Ann +@receiver:Ann class SomeClass { - @receiver:Ann - constructor(@receiver:Ann a: String) + @receiver:Ann + constructor(@receiver:Ann a: String) - @receiver:Ann + @receiver:Ann protected val simpleProperty: String = "text" - @receiver:Ann + @receiver:Ann fun anotherFun() { - @receiver:Ann + @receiver:Ann val localVariable = 5 } diff --git a/compiler/testData/diagnostics/tests/annotations/withUseSiteTarget/fileAnnotationWithoutColon.kt b/compiler/testData/diagnostics/tests/annotations/withUseSiteTarget/fileAnnotationWithoutColon.kt index 600a5e6c182..18c65e57c2e 100644 --- a/compiler/testData/diagnostics/tests/annotations/withUseSiteTarget/fileAnnotationWithoutColon.kt +++ b/compiler/testData/diagnostics/tests/annotations/withUseSiteTarget/fileAnnotationWithoutColon.kt @@ -7,4 +7,4 @@ fun test2() {} class OnType(x: @file Suppress("") Int) -fun @file : Suppress("") Int.test3() {} \ No newline at end of file +fun @file : Suppress("") Int.test3() {} \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/MoveReceiverAnnotationFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/MoveReceiverAnnotationFix.kt index 674f94b9663..0369bbb7967 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/MoveReceiverAnnotationFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/MoveReceiverAnnotationFix.kt @@ -19,6 +19,7 @@ package org.jetbrains.kotlin.idea.quickfix import com.intellij.codeInsight.intention.IntentionAction import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget import org.jetbrains.kotlin.diagnostics.Diagnostic import org.jetbrains.kotlin.psi.KtAnnotationEntry import org.jetbrains.kotlin.psi.KtCallableDeclaration @@ -44,6 +45,8 @@ class MoveReceiverAnnotationFix(element: KtAnnotationEntry) : KotlinQuickFixActi override fun createAction(diagnostic: Diagnostic): IntentionAction? { val entry = diagnostic.psiElement as? KtAnnotationEntry ?: return null + if (entry.useSiteTarget?.getAnnotationUseSiteTarget() != AnnotationUseSiteTarget.RECEIVER) return null + val declaration = entry.getParentOfType(true) ?: return null if (declaration.receiverTypeReference == null) return null diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index bda9125cff0..130c8593d86 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -508,7 +508,7 @@ class QuickFixRegistrar : QuickFixContributor { RETURN_NOT_ALLOWED.registerFactory(ChangeToLabeledReturnFix) - INAPPLICABLE_RECEIVER_TARGET.registerFactory(MoveReceiverAnnotationFix) + WRONG_ANNOTATION_TARGET_WITH_USE_SITE_TARGET.registerFactory(MoveReceiverAnnotationFix) NO_CONSTRUCTOR.registerFactory(RemoveNoConstructorFix) diff --git a/idea/testData/quickfix/moveReceiverAnnotation/notExtensionFun.kt b/idea/testData/quickfix/moveReceiverAnnotation/notExtensionFun.kt index c2cee5e33f8..e238bbe2cb9 100644 --- a/idea/testData/quickfix/moveReceiverAnnotation/notExtensionFun.kt +++ b/idea/testData/quickfix/moveReceiverAnnotation/notExtensionFun.kt @@ -1,5 +1,5 @@ // "Move annotation to receiver type" "false" -// ERROR: '@receiver:' annotations can only be applied to the receiver type of extension function or extension property declarations +// ERROR: This annotation is not applicable to target 'top level function' and use site target '@receiver' // ACTION: Make internal // ACTION: Make private diff --git a/idea/testData/quickfix/moveReceiverAnnotation/notExtensionVal.kt b/idea/testData/quickfix/moveReceiverAnnotation/notExtensionVal.kt index a5acb4bc298..96608ea5cb0 100644 --- a/idea/testData/quickfix/moveReceiverAnnotation/notExtensionVal.kt +++ b/idea/testData/quickfix/moveReceiverAnnotation/notExtensionVal.kt @@ -1,5 +1,5 @@ // "Move annotation to receiver type" "false" -// ERROR: '@receiver:' annotations can only be applied to the receiver type of extension function or extension property declarations +// ERROR: This annotation is not applicable to target 'top level property without backing field or delegate' and use site target '@receiver' // ACTION: Make internal // ACTION: Make private // ACTION: Specify type explicitly