Skip to content

Commit 5cb5aeb

Browse files
fedochetSpace Team
authored andcommitted
[AA] KT-74867 Move filtering local declarations from KotlinAnnotationsResolver into LLFirIdePredicateBasedProvider
The problem described in the KT-74867 is caused by the fact that the IDE implementation of `KotlinAnnotationsResolver` (see `IdeKotlinAnnotationsResolver`) does not filter local classes by itself. Since `LookupPredicate`s should never match local declarations, this resulted in exceptions in the IDE from lazy resolution because resolving local declarations requires resolving bodies, and that's too early for compiler plugins. It seems like moving this logic for filtering local declarations from the `KotlinAnnotationsResolver` implementations into the `LLFirIdePredicateBasedProvider` is more consistent, because this class interacts directly with `LookupPredicate` and should obey the contract. ^KT-74867 Fixed
1 parent 7ffc0e8 commit 5cb5aeb

File tree

2 files changed

+7
-13
lines changed

2 files changed

+7
-13
lines changed

analysis/analysis-api-standalone/analysis-api-standalone-base/src/org/jetbrains/kotlin/analysis/api/standalone/base/declarations/KotlinStandaloneAnnotationsResolver.kt

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,8 @@ private class KotlinStandaloneAnnotationsResolver(
3838
private val allDeclarations: List<KtDeclaration> by lazy {
3939
val result = mutableListOf<KtDeclaration>()
4040

41-
val visitor = declarationRecursiveVisitor visit@{
42-
val isLocal = when (it) {
43-
is KtClassOrObject -> it.isLocal
44-
is KtFunction -> it.isLocal
45-
is KtProperty -> it.isLocal
46-
else -> return@visit
47-
}
48-
49-
if (!isLocal) {
50-
result += it
51-
}
41+
val visitor = declarationRecursiveVisitor {
42+
result += it
5243
}
5344

5445
filesInScope.forEach { it.accept(visitor) }

analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/providers/LLFirIdePredicateBasedProvider.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ internal class LLFirIdePredicateBasedProvider(
6060

6161
return annotatedDeclarations
6262
.asSequence()
63-
.mapNotNull { it.findFirDeclaration() }
63+
.mapNotNull { it.findFirDeclarationForLookupPredicate() }
6464
.filter { matches(predicate, it) }
6565
.map { it.symbol }
6666
.toList()
6767
}
6868

69-
private fun KtElement.findFirDeclaration(): FirDeclaration? {
69+
private fun KtElement.findFirDeclarationForLookupPredicate(): FirDeclaration? {
7070
if (this !is KtDeclaration) return null
7171

7272
if (this !is KtClassLikeDeclaration &&
@@ -75,6 +75,9 @@ internal class LLFirIdePredicateBasedProvider(
7575
this !is KtProperty
7676
) return null
7777

78+
// LookupPredicates should never match local declarations, so we filter them early
79+
if (KtPsiUtil.isLocal(this)) return null
80+
7881
val moduleForFile = projectStructureProvider.getModule(this, session.ktModule)
7982
val resolutionFacadeForFile = moduleForFile.getResolutionFacade(project)
8083
return this.resolveToFirSymbol(resolutionFacadeForFile).fir

0 commit comments

Comments
 (0)