From 8b1e508740ee650cbc78deeb4f77319ed35300e5 Mon Sep 17 00:00:00 2001 From: Egor Kulikov Date: Fri, 3 Feb 2023 12:21:58 +0000 Subject: [PATCH] [FIR] Make check for dangling modifier lists use stubs Seventh step for ^KT-52615 Merge-request: KT-MR-8651 Merged-by: Egor Kulikov --- .../jetbrains/kotlin/fir/builder/RawFirBuilder.kt | 6 +++--- .../psi/src/org/jetbrains/kotlin/psi/KtClassBody.kt | 8 +++++++- compiler/psi/src/org/jetbrains/kotlin/psi/KtFile.kt | 13 +++++++++---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt index 1a8bb91c8f4..b357631c0e2 100644 --- a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt +++ b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt @@ -1066,7 +1066,7 @@ open class RawFirBuilder( } } - for (danglingModifierList in PsiTreeUtil.getChildrenOfTypeAsList(file, KtModifierList::class.java)) { + for (danglingModifierList in file.danglingModifierLists) { declarations += buildErrorTopLevelDeclarationForDanglingModifierList(danglingModifierList) } } @@ -1284,7 +1284,7 @@ open class RawFirBuilder( ) ) } - for (danglingModifier in PsiTreeUtil.getChildrenOfTypeAsList(classOrObject.body, KtModifierList::class.java)) { + for (danglingModifier in classOrObject.body?.danglingModifierLists ?: emptyList()) { addDeclaration( buildErrorTopLevelDeclarationForDanglingModifierList(danglingModifier).apply { containingClassAttr = currentDispatchReceiverType()?.lookupTag @@ -1383,7 +1383,7 @@ open class RawFirBuilder( ) } - for (danglingModifier in PsiTreeUtil.getChildrenOfTypeAsList(objectDeclaration.body, KtModifierList::class.java)) { + for (danglingModifier in objectDeclaration.body?.danglingModifierLists ?: emptyList()) { declarations += buildErrorTopLevelDeclarationForDanglingModifierList(danglingModifier).apply { containingClassAttr = currentDispatchReceiverType()?.lookupTag } diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtClassBody.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/KtClassBody.kt index a0c3690253a..5806d0126da 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtClassBody.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KtClassBody.kt @@ -69,5 +69,11 @@ class KtClassBody : KtElementImplStub>, KtDec * @return annotations that do not belong to any declaration due to incomplete code or syntax errors */ val danglingAnnotations: List - get() = getStubOrPsiChildrenAsList(MODIFIER_LIST).flatMap { it.annotationEntries } + get() = danglingModifierLists.flatMap { it.annotationEntries } + + /** + * @return modifier lists that do not belong to any declaration due to incomplete code or syntax errors + */ + val danglingModifierLists: List + get() = getStubOrPsiChildrenAsList(MODIFIER_LIST) } diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtFile.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/KtFile.kt index 230a5d79f04..525eb88e942 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtFile.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KtFile.kt @@ -128,18 +128,23 @@ open class KtFile(viewProvider: FileViewProvider, val isCompiled: Boolean) : get() = script != null /** - * @return annotations that do not belong to any declaration due to incomplete code or syntax errors + * @return modifier lists that do not belong to any declaration due to incomplete code or syntax errors */ - val danglingAnnotations: List + val danglingModifierLists: Array get() { val stub = stub - val danglingModifierLists = stub?.getChildrenByType( + return stub?.getChildrenByType( KtStubElementTypes.MODIFIER_LIST, KtStubElementTypes.MODIFIER_LIST.arrayFactory ) ?: findChildrenByClass(KtModifierList::class.java) - return danglingModifierLists.flatMap { obj: KtModifierList -> obj.annotationEntries } } + /** + * @return annotations that do not belong to any declaration due to incomplete code or syntax errors + */ + val danglingAnnotations: List + get() = danglingModifierLists.flatMap { obj: KtModifierList -> obj.annotationEntries } + override fun getFileType(): FileType = KotlinFileType.INSTANCE override fun toString(): String = "KtFile: $name"