From 95f2c526590a5a4acebaf246b58c07870e335b87 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Mon, 17 May 2021 16:29:36 +0300 Subject: [PATCH] FirAnnotationHelpers: use KotlinTarget instead of AnnotationTarget --- .../analysis/checkers/FirAnnotationHelpers.kt | 25 ++++++------------- .../FirAnnotationClassDeclarationChecker.kt | 3 ++- .../FirExpressionAnnotationChecker.kt | 3 ++- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirAnnotationHelpers.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirAnnotationHelpers.kt index f59b022711b..32dc8697a9f 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirAnnotationHelpers.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirAnnotationHelpers.kt @@ -6,6 +6,7 @@ package org.jetbrains.kotlin.fir.analysis.checkers import org.jetbrains.kotlin.builtins.StandardNames +import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.declarations.FirAnnotatedDeclaration import org.jetbrains.kotlin.fir.declarations.FirRegularClass @@ -35,25 +36,15 @@ fun FirRegularClass.getRetention(): AnnotationRetention { return AnnotationRetention.values().firstOrNull { it.name == retentionName } ?: AnnotationRetention.RUNTIME } -private val defaultAnnotationTargets = listOf( - AnnotationTarget.CLASS, - AnnotationTarget.PROPERTY, - AnnotationTarget.FIELD, - AnnotationTarget.LOCAL_VARIABLE, - AnnotationTarget.VALUE_PARAMETER, - AnnotationTarget.CONSTRUCTOR, - AnnotationTarget.FUNCTION, - AnnotationTarget.PROPERTY_GETTER, - AnnotationTarget.PROPERTY_SETTER, -) +private val defaultAnnotationTargets = KotlinTarget.DEFAULT_TARGET_SET -fun FirAnnotationCall.getAllowedAnnotationTargets(session: FirSession): List { - if (annotationTypeRef is FirErrorTypeRef) return AnnotationTarget.values().toList() +fun FirAnnotationCall.getAllowedAnnotationTargets(session: FirSession): Set { + if (annotationTypeRef is FirErrorTypeRef) return KotlinTarget.values().toSet() val annotationClass = (this.annotationTypeRef.coneType as? ConeClassLikeType)?.lookupTag?.toSymbol(session)?.fir as? FirRegularClass return annotationClass?.getAllowedAnnotationTargets() ?: defaultAnnotationTargets } -fun FirRegularClass.getAllowedAnnotationTargets(): List { +fun FirRegularClass.getAllowedAnnotationTargets(): Set { val targetAnnotation = getTargetAnnotation() ?: return defaultAnnotationTargets val arguments = when (val targetArgument = targetAnnotation.findSingleArgumentByName(TARGET_PARAMETER_NAME)) { is FirVarargArgumentsExpression -> targetArgument.arguments @@ -61,10 +52,10 @@ fun FirRegularClass.getAllowedAnnotationTargets(): List { else -> return defaultAnnotationTargets } - return arguments.mapNotNull { argument -> + return arguments.mapNotNullTo(mutableSetOf()) { argument -> val targetExpression = argument as? FirQualifiedAccessExpression - val targetName = (targetExpression?.calleeReference as? FirResolvedNamedReference)?.name?.asString() ?: return@mapNotNull null - AnnotationTarget.values().firstOrNull { target -> target.name == targetName } + val targetName = (targetExpression?.calleeReference as? FirResolvedNamedReference)?.name?.asString() ?: return@mapNotNullTo null + KotlinTarget.values().firstOrNull { target -> target.name == targetName } } } diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirAnnotationClassDeclarationChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirAnnotationClassDeclarationChecker.kt index 08282d38336..a64d9fdd33e 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirAnnotationClassDeclarationChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirAnnotationClassDeclarationChecker.kt @@ -9,6 +9,7 @@ import org.jetbrains.kotlin.KtNodeTypes.FUN import org.jetbrains.kotlin.KtNodeTypes.VALUE_PARAMETER import org.jetbrains.kotlin.descriptors.ClassKind.ANNOTATION_CLASS import org.jetbrains.kotlin.descriptors.ClassKind.ENUM_CLASS +import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.analysis.checkers.* import org.jetbrains.kotlin.fir.analysis.checkers.checkConstantArguments @@ -35,7 +36,7 @@ object FirAnnotationClassDeclarationChecker : FirRegularClassChecker() { } if (declaration.getRetention() != AnnotationRetention.SOURCE && - AnnotationTarget.EXPRESSION in declaration.getAllowedAnnotationTargets() + KotlinTarget.EXPRESSION in declaration.getAllowedAnnotationTargets() ) { val target = declaration.getRetentionAnnotation() ?: declaration.getTargetAnnotation() ?: declaration reporter.reportOn(target.source, FirErrors.RESTRICTED_RETENTION_FOR_EXPRESSION_ANNOTATION, context) diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirExpressionAnnotationChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirExpressionAnnotationChecker.kt index 6757df6df21..2af4959074c 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirExpressionAnnotationChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirExpressionAnnotationChecker.kt @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.fir.analysis.checkers.expression +import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget import org.jetbrains.kotlin.fir.FirFakeSourceElementKind import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext import org.jetbrains.kotlin.fir.analysis.checkers.getAllowedAnnotationTargets @@ -27,7 +28,7 @@ object FirExpressionAnnotationChecker : FirBasicExpressionChecker() { ) return for (annotation in expression.annotations) { withSuppressedDiagnostics(annotation, context) { - if (AnnotationTarget.EXPRESSION !in annotation.getAllowedAnnotationTargets(context.session)) { + if (KotlinTarget.EXPRESSION !in annotation.getAllowedAnnotationTargets(context.session)) { reporter.reportOn(annotation.source, FirErrors.WRONG_ANNOTATION_TARGET, "expression", context) } }