FirAnnotationHelpers: use KotlinTarget instead of AnnotationTarget

This commit is contained in:
Mikhail Glukhikh
2021-05-17 16:29:36 +03:00
parent 7675ea3c23
commit 95f2c52659
3 changed files with 12 additions and 19 deletions
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.fir.analysis.checkers package org.jetbrains.kotlin.fir.analysis.checkers
import org.jetbrains.kotlin.builtins.StandardNames import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.FirAnnotatedDeclaration import org.jetbrains.kotlin.fir.declarations.FirAnnotatedDeclaration
import org.jetbrains.kotlin.fir.declarations.FirRegularClass import org.jetbrains.kotlin.fir.declarations.FirRegularClass
@@ -35,25 +36,15 @@ fun FirRegularClass.getRetention(): AnnotationRetention {
return AnnotationRetention.values().firstOrNull { it.name == retentionName } ?: AnnotationRetention.RUNTIME return AnnotationRetention.values().firstOrNull { it.name == retentionName } ?: AnnotationRetention.RUNTIME
} }
private val defaultAnnotationTargets = listOf( private val defaultAnnotationTargets = KotlinTarget.DEFAULT_TARGET_SET
AnnotationTarget.CLASS,
AnnotationTarget.PROPERTY,
AnnotationTarget.FIELD,
AnnotationTarget.LOCAL_VARIABLE,
AnnotationTarget.VALUE_PARAMETER,
AnnotationTarget.CONSTRUCTOR,
AnnotationTarget.FUNCTION,
AnnotationTarget.PROPERTY_GETTER,
AnnotationTarget.PROPERTY_SETTER,
)
fun FirAnnotationCall.getAllowedAnnotationTargets(session: FirSession): List<AnnotationTarget> { fun FirAnnotationCall.getAllowedAnnotationTargets(session: FirSession): Set<KotlinTarget> {
if (annotationTypeRef is FirErrorTypeRef) return AnnotationTarget.values().toList() if (annotationTypeRef is FirErrorTypeRef) return KotlinTarget.values().toSet()
val annotationClass = (this.annotationTypeRef.coneType as? ConeClassLikeType)?.lookupTag?.toSymbol(session)?.fir as? FirRegularClass val annotationClass = (this.annotationTypeRef.coneType as? ConeClassLikeType)?.lookupTag?.toSymbol(session)?.fir as? FirRegularClass
return annotationClass?.getAllowedAnnotationTargets() ?: defaultAnnotationTargets return annotationClass?.getAllowedAnnotationTargets() ?: defaultAnnotationTargets
} }
fun FirRegularClass.getAllowedAnnotationTargets(): List<AnnotationTarget> { fun FirRegularClass.getAllowedAnnotationTargets(): Set<KotlinTarget> {
val targetAnnotation = getTargetAnnotation() ?: return defaultAnnotationTargets val targetAnnotation = getTargetAnnotation() ?: return defaultAnnotationTargets
val arguments = when (val targetArgument = targetAnnotation.findSingleArgumentByName(TARGET_PARAMETER_NAME)) { val arguments = when (val targetArgument = targetAnnotation.findSingleArgumentByName(TARGET_PARAMETER_NAME)) {
is FirVarargArgumentsExpression -> targetArgument.arguments is FirVarargArgumentsExpression -> targetArgument.arguments
@@ -61,10 +52,10 @@ fun FirRegularClass.getAllowedAnnotationTargets(): List<AnnotationTarget> {
else -> return defaultAnnotationTargets else -> return defaultAnnotationTargets
} }
return arguments.mapNotNull { argument -> return arguments.mapNotNullTo(mutableSetOf()) { argument ->
val targetExpression = argument as? FirQualifiedAccessExpression val targetExpression = argument as? FirQualifiedAccessExpression
val targetName = (targetExpression?.calleeReference as? FirResolvedNamedReference)?.name?.asString() ?: return@mapNotNull null val targetName = (targetExpression?.calleeReference as? FirResolvedNamedReference)?.name?.asString() ?: return@mapNotNullTo null
AnnotationTarget.values().firstOrNull { target -> target.name == targetName } KotlinTarget.values().firstOrNull { target -> target.name == targetName }
} }
} }
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.KtNodeTypes.FUN
import org.jetbrains.kotlin.KtNodeTypes.VALUE_PARAMETER import org.jetbrains.kotlin.KtNodeTypes.VALUE_PARAMETER
import org.jetbrains.kotlin.descriptors.ClassKind.ANNOTATION_CLASS import org.jetbrains.kotlin.descriptors.ClassKind.ANNOTATION_CLASS
import org.jetbrains.kotlin.descriptors.ClassKind.ENUM_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.FirSession
import org.jetbrains.kotlin.fir.analysis.checkers.* import org.jetbrains.kotlin.fir.analysis.checkers.*
import org.jetbrains.kotlin.fir.analysis.checkers.checkConstantArguments import org.jetbrains.kotlin.fir.analysis.checkers.checkConstantArguments
@@ -35,7 +36,7 @@ object FirAnnotationClassDeclarationChecker : FirRegularClassChecker() {
} }
if (declaration.getRetention() != AnnotationRetention.SOURCE && if (declaration.getRetention() != AnnotationRetention.SOURCE &&
AnnotationTarget.EXPRESSION in declaration.getAllowedAnnotationTargets() KotlinTarget.EXPRESSION in declaration.getAllowedAnnotationTargets()
) { ) {
val target = declaration.getRetentionAnnotation() ?: declaration.getTargetAnnotation() ?: declaration val target = declaration.getRetentionAnnotation() ?: declaration.getTargetAnnotation() ?: declaration
reporter.reportOn(target.source, FirErrors.RESTRICTED_RETENTION_FOR_EXPRESSION_ANNOTATION, context) reporter.reportOn(target.source, FirErrors.RESTRICTED_RETENTION_FOR_EXPRESSION_ANNOTATION, context)
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.fir.analysis.checkers.expression 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.FirFakeSourceElementKind
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
import org.jetbrains.kotlin.fir.analysis.checkers.getAllowedAnnotationTargets import org.jetbrains.kotlin.fir.analysis.checkers.getAllowedAnnotationTargets
@@ -27,7 +28,7 @@ object FirExpressionAnnotationChecker : FirBasicExpressionChecker() {
) return ) return
for (annotation in expression.annotations) { for (annotation in expression.annotations) {
withSuppressedDiagnostics(annotation, context) { 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) reporter.reportOn(annotation.source, FirErrors.WRONG_ANNOTATION_TARGET, "expression", context)
} }
} }