Restrict retention for annotations with target EXPRESSION
#KT-13762 Fixed
This commit is contained in:
@@ -229,6 +229,9 @@ public interface Errors {
|
||||
DiagnosticFactory0<KtAnnotationEntry> ANNOTATION_USED_AS_ANNOTATION_ARGUMENT = DiagnosticFactory0.create(ERROR);
|
||||
DiagnosticFactory0<KtExpression> ANNOTATION_ARGUMENT_IS_NON_CONST = DiagnosticFactory0.create(WARNING);
|
||||
|
||||
DiagnosticFactory0<PsiElement> RESTRICTED_RETENTION_FOR_EXPRESSION_ANNOTATION = DiagnosticFactory0.create(ERROR);
|
||||
DiagnosticFactory0<PsiElement> RESTRICTED_RETENTION_FOR_EXPRESSION_ANNOTATION_WARNING = DiagnosticFactory0.create(WARNING);
|
||||
|
||||
DiagnosticFactory0<KtClassOrObject> LOCAL_ANNOTATION_CLASS = DiagnosticFactory0.create(WARNING);
|
||||
DiagnosticFactory0<KtClassOrObject> LOCAL_ANNOTATION_CLASS_ERROR = DiagnosticFactory0.create(ERROR);
|
||||
|
||||
|
||||
+5
@@ -840,6 +840,11 @@ public class DefaultErrorMessages {
|
||||
MAP.put(ANNOTATION_USED_AS_ANNOTATION_ARGUMENT, "An annotation can't be used as the annotations argument");
|
||||
MAP.put(ANNOTATION_ARGUMENT_IS_NON_CONST, "An annotation argument must be a compile-time constant");
|
||||
|
||||
MAP.put(RESTRICTED_RETENTION_FOR_EXPRESSION_ANNOTATION,
|
||||
"Expression annotations with retention other than SOURCE are prohibited");
|
||||
MAP.put(RESTRICTED_RETENTION_FOR_EXPRESSION_ANNOTATION_WARNING,
|
||||
"Expression annotations with retention other than SOURCE are deprecated");
|
||||
|
||||
MAP.put(LOCAL_ANNOTATION_CLASS, "Local annotation classes are deprecated and will be unsupported in a future release");
|
||||
MAP.put(LOCAL_ANNOTATION_CLASS_ERROR, "Annotation class cannot be local");
|
||||
|
||||
|
||||
@@ -118,7 +118,8 @@ private val DEFAULT_DECLARATION_CHECKERS = listOf(
|
||||
KClassWithIncorrectTypeArgumentChecker,
|
||||
SuspendOperatorsCheckers,
|
||||
InlineClassDeclarationChecker,
|
||||
PropertiesWithBackingFieldsInsideInlineClass()
|
||||
PropertiesWithBackingFieldsInsideInlineClass(),
|
||||
AnnotationClassTargetAndRetentionChecker()
|
||||
)
|
||||
|
||||
private val DEFAULT_CALL_CHECKERS = listOf(
|
||||
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.resolve.checkers
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.config.LanguageFeature
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.annotations.KotlinRetention
|
||||
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
import org.jetbrains.kotlin.resolve.AnnotationChecker
|
||||
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.getAnnotationRetention
|
||||
|
||||
class AnnotationClassTargetAndRetentionChecker : DeclarationChecker {
|
||||
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
|
||||
if (descriptor !is ClassDescriptor) return
|
||||
if (declaration !is KtClassOrObject) return
|
||||
if (!DescriptorUtils.isAnnotationClass(descriptor)) return
|
||||
|
||||
val targets = AnnotationChecker.applicableTargetSet(descriptor) ?: return
|
||||
val retention = descriptor.getAnnotationRetention() ?: KotlinRetention.RUNTIME
|
||||
|
||||
if (targets.contains(KotlinTarget.EXPRESSION) && retention != KotlinRetention.SOURCE) {
|
||||
val retentionAnnotation = descriptor.annotations.findAnnotation(KotlinBuiltIns.FQ_NAMES.retention)
|
||||
val targetAnnotation = descriptor.annotations.findAnnotation(KotlinBuiltIns.FQ_NAMES.target)
|
||||
|
||||
val diagnostics =
|
||||
if (context.languageVersionSettings.supportsFeature(LanguageFeature.RestrictRetentionForExpressionAnnotations))
|
||||
Errors.RESTRICTED_RETENTION_FOR_EXPRESSION_ANNOTATION
|
||||
else
|
||||
Errors.RESTRICTED_RETENTION_FOR_EXPRESSION_ANNOTATION_WARNING
|
||||
context.trace.report(diagnostics.on(retentionAnnotation?.psi ?: targetAnnotation?.psi ?: declaration))
|
||||
}
|
||||
}
|
||||
|
||||
private val AnnotationDescriptor.psi: PsiElement?
|
||||
get() = DescriptorToSourceUtils.getSourceFromAnnotation(this)
|
||||
}
|
||||
Reference in New Issue
Block a user