Skip to content

Checkers: drop use-site targets handling from repeated annotation checks (KT-85288)#6179

Open
Mikhail Glukhikh (mglukhikh) wants to merge 5 commits into
masterfrom
rr/mg/kt85288
Open

Checkers: drop use-site targets handling from repeated annotation checks (KT-85288)#6179
Mikhail Glukhikh (mglukhikh) wants to merge 5 commits into
masterfrom
rr/mg/kt85288

Conversation

@mglukhikh

Copy link
Copy Markdown
Contributor

No description provided.

@kotlin-safemerge

kotlin-safemerge Bot commented Jun 8, 2026

Copy link
Copy Markdown

Code Owners

RuleOwnersApproval
/​compiler/​fir/​, /​compiler/​testData/​diagnostics/​
kotlin-frontend

@cypressious

for (annotation in annotations) {
val useSiteTarget = annotation.useSiteTarget ?: expression.getDefaultUseSiteTarget(annotation)
val existingTargetsForAnnotation = annotationsMap.getOrPut(annotation.annotationTypeRef.coneType) { arrayListOf() }
val coneType = annotation.annotationTypeRef.coneType

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to expand typealiases here? If yes, it might be a breaking change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably no, as the test below works, and it seems to me that we normally expand expression types beforehand. I will add the test to this MR to be more sure about correctness here.

@Target(AnnotationTarget.EXPRESSION)
@Retention(AnnotationRetention.SOURCE)
annotation class A

typealias B = A

fun main() {
    val x = @A @B 1 // This annotation is not repeatable
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JiC, the behavior might differ between regular tests and FirLightTreeDiagnosticsWithoutAliasExpansionTestGenerated.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it's failing.

}

checkDeclaredRepeatedAnnotations(declaration)
val annotations = declaration.annotations.filterNot { it in annotationsWithIncorrectTarget }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
val annotations = declaration.annotations.filterNot { it in annotationsWithIncorrectTarget }
val annotations = declaration.annotations - annotationsWithIncorrectTarget

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied manually

if (annotation.source?.kind == KtFakeSourceElementKind.FromUseSiteTarget) return
): Boolean {
if (annotation.source?.kind == KtFakeSourceElementKind.FromUseSiteTarget) return false
when (target) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be refactored to return when? Some branches look like they could be split into multiple ones with guards to make the change easier.

@mglukhikh Mikhail Glukhikh (mglukhikh) Jun 10, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will refactor it (not sure it becomes much better).

Before this commit, we handled each pair (annotation, use-site target)
separately during repeating checks. In fact, it's not needed,
as annotations are moved to different declarations during earlier checks,
so we dropped unnecessary complexity in this commit.

#KT-85288 Fixed
Before this commit, we checked all existing annotations on repetition,
including erroneous ones, forcing sometimes questionable reporting of
REPEATED_ANNOTATION and particular corner-case checks. In this commit,
we filter out all annotations with incorrect target before repetition
checks, thus allowing to drop corner cases and having less questionable
diagnostics.

Related to KT-85288
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants