[FIR] KT-57214: Revert the move and check the annotations "if possible"
diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/declarations/FirAnnotationUtils.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/declarations/FirAnnotationUtils.kt
index 69ccce2..820c305 100644
--- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/declarations/FirAnnotationUtils.kt
+++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/declarations/FirAnnotationUtils.kt
@@ -77,9 +77,8 @@
fun FirAnnotation.useSiteTargetsFromMetaAnnotation(session: FirSession): Set<AnnotationUseSiteTarget> {
return toAnnotationClass(session)
- ?.symbol
- ?.resolvedAnnotations
- ?.find { it.toAnnotationClassId(session) == StandardClassIds.Annotations.Target }
+ ?.annotations
+ ?.find { it.toAnnotationClassIdSafe(session) == StandardClassIds.Annotations.Target }
?.findUseSiteTargets()
?: DEFAULT_USE_SITE_TARGETS
}
diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirStatusResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirStatusResolveTransformer.kt
index cb1c79f..90c3b1b 100644
--- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirStatusResolveTransformer.kt
+++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirStatusResolveTransformer.kt
@@ -5,14 +5,12 @@
package org.jetbrains.kotlin.fir.resolve.transformers
-import org.jetbrains.kotlin.KtFakeSourceElementKind
-import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.fir.FirSession
-import org.jetbrains.kotlin.fir.correspondingProperty
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.utils.isInline
import org.jetbrains.kotlin.fir.expressions.FirBlock
import org.jetbrains.kotlin.fir.expressions.FirStatement
+import org.jetbrains.kotlin.fir.render
import org.jetbrains.kotlin.fir.resolve.ScopeSession
import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.LocalClassesNavigationInfo
import org.jetbrains.kotlin.fir.scopes.FirCompositeScope
@@ -387,43 +385,7 @@
): FirStatement = whileAnalysing(session, constructor) {
constructor.transformStatus(this, statusResolver.resolveStatus(constructor, containingClass, isLocal = false))
calculateDeprecations(constructor)
- val result = transformDeclaration(constructor, data) as FirConstructor
-
- if (result.isPrimary) {
- for (valueParameter in result.valueParameters) {
- if (valueParameter.correspondingProperty != null) {
- valueParameter.removeDuplicateAnnotationsOfPrimaryConstructorElement()
- }
- }
- }
-
- result
- }
-
- /**
- * In a scenario like
- *
- * ```
- * annotation class Ann
- * class Foo(@Ann val x: String)
- * ```
- *
- * both, the primary ctor value parameter and the property `x` will be annotated with `@Ann`. This is due to the fact, that the
- * annotation needs to be resolved in order to determine its annotation targets. We remove annotations from the wrong target if they
- * don't explicitly specify the use-site target (in which case they shouldn't have been added to the element in the raw FIR).
- *
- * For value parameters, we remove the annotation if the targets don't include [AnnotationUseSiteTarget.CONSTRUCTOR_PARAMETER].
- * For properties, we remove the annotation, if the targets include [AnnotationUseSiteTarget.CONSTRUCTOR_PARAMETER].
- */
- private fun FirVariable.removeDuplicateAnnotationsOfPrimaryConstructorElement() {
- val isParameter = this is FirValueParameter
- replaceAnnotations(annotations.filter {
- it.useSiteTarget != null ||
- // equivalent to
- // CONSTRUCTOR_PARAMETER in targets && isParameter ||
- // CONSTRUCTOR_PARAMETER !in targets && !isParameter
- AnnotationUseSiteTarget.CONSTRUCTOR_PARAMETER in it.useSiteTargetsFromMetaAnnotation(session) == isParameter
- })
+ return transformDeclaration(constructor, data) as FirStatement
}
override fun transformSimpleFunction(
@@ -464,10 +426,6 @@
)
}
- if (property.source?.kind == KtFakeSourceElementKind.PropertyFromParameter) {
- property.removeDuplicateAnnotationsOfPrimaryConstructorElement()
- }
-
calculateDeprecations(property)
return property
}
diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirTypeResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirTypeResolveTransformer.kt
index 5f66810..28f8653 100644
--- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirTypeResolveTransformer.kt
+++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirTypeResolveTransformer.kt
@@ -7,7 +7,9 @@
import kotlinx.collections.immutable.toImmutableList
import org.jetbrains.kotlin.KtFakeSourceElementKind
+import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.fir.FirSession
+import org.jetbrains.kotlin.fir.correspondingProperty
import org.jetbrains.kotlin.fir.copyWithNewSourceKind
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.utils.isFromVararg
@@ -115,7 +117,17 @@
override fun transformConstructor(constructor: FirConstructor, data: Any?): FirConstructor = whileAnalysing(session, constructor) {
return withScopeCleanup {
constructor.addTypeParametersScope()
- transformDeclaration(constructor, data) as FirConstructor
+ val result = transformDeclaration(constructor, data) as FirConstructor
+
+ if (result.isPrimary) {
+ for (valueParameter in result.valueParameters) {
+ if (valueParameter.correspondingProperty != null) {
+ valueParameter.removeDuplicateAnnotationsOfPrimaryConstructorElement()
+ }
+ }
+ }
+
+ result
}
}
@@ -169,6 +181,11 @@
}
unboundCyclesInTypeParametersSupertypes(property)
+
+ if (property.source?.kind == KtFakeSourceElementKind.PropertyFromParameter) {
+ property.removeDuplicateAnnotationsOfPrimaryConstructorElement()
+ }
+
property
}
}
@@ -379,4 +396,30 @@
scopes.add(FirMemberTypeParameterScope(this))
}
}
+
+ /**
+ * In a scenario like
+ *
+ * ```
+ * annotation class Ann
+ * class Foo(@Ann val x: String)
+ * ```
+ *
+ * both, the primary ctor value parameter and the property `x` will be annotated with `@Ann`. This is due to the fact, that the
+ * annotation needs to be resolved in order to determine its annotation targets. We remove annotations from the wrong target if they
+ * don't explicitly specify the use-site target (in which case they shouldn't have been added to the element in the raw FIR).
+ *
+ * For value parameters, we remove the annotation if the targets don't include [AnnotationUseSiteTarget.CONSTRUCTOR_PARAMETER].
+ * For properties, we remove the annotation, if the targets include [AnnotationUseSiteTarget.CONSTRUCTOR_PARAMETER].
+ */
+ private fun FirVariable.removeDuplicateAnnotationsOfPrimaryConstructorElement() {
+ val isParameter = this is FirValueParameter
+ replaceAnnotations(annotations.filter {
+ it.useSiteTarget != null ||
+ // equivalent to
+ // CONSTRUCTOR_PARAMETER in targets && isParameter ||
+ // CONSTRUCTOR_PARAMETER !in targets && !isParameter
+ AnnotationUseSiteTarget.CONSTRUCTOR_PARAMETER in it.useSiteTargetsFromMetaAnnotation(session) == isParameter
+ })
+ }
}
diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/symbols/FirBasedSymbol.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/symbols/FirBasedSymbol.kt
index d3029a7..733d51a 100644
--- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/symbols/FirBasedSymbol.kt
+++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/symbols/FirBasedSymbol.kt
@@ -53,9 +53,6 @@
val resolvedCompilerAnnotationsWithClassIds: List<FirAnnotation>
get() = fir.resolvedCompilerRequiredAnnotations(this)
- val resolvedAnnotations: List<FirAnnotation>
- get() = fir.resolvedAnnotations(this)
-
val resolvedAnnotationClassIds: List<ClassId>
get() = fir.resolvedAnnotationClassIds(this)
}
@@ -114,16 +111,11 @@
}
@SymbolInternals
-fun FirAnnotationContainer.resolvedAnnotations(anchorElement: FirBasedSymbol<*>): List<FirAnnotation> {
+fun FirAnnotationContainer.resolvedAnnotationClassIds(anchorElement: FirBasedSymbol<*>): List<ClassId> {
if (annotations.isEmpty()) return emptyList()
anchorElement.lazyResolveToPhase(FirResolvePhase.TYPES)
- return annotations
-}
-
-@SymbolInternals
-fun FirAnnotationContainer.resolvedAnnotationClassIds(anchorElement: FirBasedSymbol<*>): List<ClassId> {
- return resolvedAnnotations(anchorElement).mapNotNull { (it.annotationTypeRef.coneType as? ConeClassLikeType)?.lookupTag?.classId }
+ return annotations.mapNotNull { (it.annotationTypeRef.coneType as? ConeClassLikeType)?.lookupTag?.classId }
}
@RequiresOptIn