[FIR] Forbid annotations on anonymous initializers and destructuring declarations

#KT-59896


Merge-request: KT-MR-13197
Merged-by: Evgeniy Zhelenskiy <Evgeniy.Zhelenskiy@jetbrains.com>
This commit is contained in:
Evgeniy.Zhelenskiy
2023-11-28 11:59:26 +00:00
committed by Space Team
parent e901629cf0
commit 5258e2044d
9 changed files with 10 additions and 40 deletions
@@ -35,7 +35,7 @@ import org.jetbrains.kotlin.resolve.checkers.OptInNames
private val defaultAnnotationTargets = KotlinTarget.DEFAULT_TARGET_SET
fun FirAnnotation.getAllowedAnnotationTargets(session: FirSession): Set<KotlinTarget> {
if (annotationTypeRef is FirErrorTypeRef) return KotlinTarget.values().toSet()
if (annotationTypeRef is FirErrorTypeRef) return KotlinTarget.ALL_TARGET_SET
val annotationClassSymbol = (this.annotationTypeRef.coneType as? ConeClassLikeType)
?.fullyExpandedType(session)?.lookupTag?.toSymbol(session) ?: return defaultAnnotationTargets
annotationClassSymbol.lazyResolveToPhase(FirResolvePhase.BODY_RESOLVE)
@@ -70,7 +70,7 @@ fun FirClassLikeSymbol<*>.getAllowedAnnotationTargets(session: FirSession): Set<
//but `JvmStubBasedFirDeserializedSymbolProvider` which works in IDE over stubs, misses classes
?: (calleeReference as? FirFromMissingDependenciesNamedReference)?.name?.asString()
?: return@mapNotNullTo null
KotlinTarget.values().firstOrNull { target -> target.name == targetName }
KotlinTarget.entries.firstOrNull { target -> target.name == targetName }
}
}
@@ -7,7 +7,6 @@ package org.jetbrains.kotlin.fir.analysis.checkers
import com.intellij.lang.LighterASTNode
import org.jetbrains.kotlin.*
import org.jetbrains.kotlin.KtFakeSourceElementKind.DesugaredComponentFunctionCall
import org.jetbrains.kotlin.builtins.StandardNames.HASHCODE_NAME
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.Modality
@@ -643,7 +642,7 @@ fun getActualTargetList(container: FirAnnotationContainer): AnnotationTargetList
when {
annotated.isLocal ->
when {
annotated.source?.kind == DesugaredComponentFunctionCall -> TargetLists.T_DESTRUCTURING_DECLARATION
annotated.name == SpecialNames.DESTRUCT -> TargetLists.T_DESTRUCTURING_DECLARATION
annotated.isCatchParameter == true -> TargetLists.T_CATCH_PARAMETER
else -> TargetLists.T_LOCAL_VARIABLE
}
@@ -694,9 +693,7 @@ fun getActualTargetList(container: FirAnnotationContainer): AnnotationTargetList
} else {
TargetLists.T_OBJECT_LITERAL
}
// TODO, KT-59819: properly implement those cases
// is KtDestructuringDeclarationEntry -> TargetLists.T_LOCAL_VARIABLE
// is KtDestructuringDeclaration -> TargetLists.T_DESTRUCTURING_DECLARATION
// TODO, KT-59819: properly implement this case
// is KtLambdaExpression -> TargetLists.T_FUNCTION_LITERAL
else -> TargetLists.EMPTY
}
@@ -1,9 +0,0 @@
annotation class Ann
data class Pair(val x: Int, val y: Int)
fun foo(): Int {
@Ann val (a, b) = Pair(12, 34)
@<!UNRESOLVED_REFERENCE!>Err<!> val (c, d) = Pair(56, 78)
return a + b + c + d
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
annotation class Ann
data class Pair(val x: Int, val y: Int)
@@ -1,14 +0,0 @@
annotation class Ann(val x: Int)
data class A(val x: Int, val y: Int)
fun bar(): Array<A> = null!!
fun foo() {
for (@Ann(1) i in 1..100) {}
for (@Ann(2) i in 1..100) {}
for (@Ann(3) (x, @Ann(4) y) in bar()) {}
for (@<!UNRESOLVED_REFERENCE!>Err<!>() (x,y) in bar()) {}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
annotation class Ann(val x: Int)
data class A(val x: Int, val y: Int)
@@ -1,7 +0,0 @@
fun test(): Any? {
@ann val (a, b) = P(1, 1)
return a + b
}
annotation class ann
data class P(val a: Int, val b: Int)
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
fun test(): Any? {
<!WRONG_ANNOTATION_TARGET!>@ann<!> val (a, b) = P(1, 1)
return a + b
@@ -71,15 +71,15 @@ enum class KotlinTarget(val description: String, val isDefault: Boolean = true)
private val map = HashMap<String, KotlinTarget>()
init {
for (target in values()) {
for (target in entries) {
map[target.name] = target
}
}
fun valueOrNull(name: String): KotlinTarget? = map[name]
val DEFAULT_TARGET_SET: Set<KotlinTarget> = values().filter { it.isDefault }.toSet()
val ALL_TARGET_SET: Set<KotlinTarget> = values().toSet()
val DEFAULT_TARGET_SET: Set<KotlinTarget> = entries.filter { it.isDefault }.toSet()
val ALL_TARGET_SET: Set<KotlinTarget> = entries.toSet()
val ANNOTATION_CLASS_LIST = listOf(ANNOTATION_CLASS, CLASS)
val LOCAL_CLASS_LIST = listOf(LOCAL_CLASS, CLASS)