[LL FIR] FileStructure: support dangling modifier list in non-end position

The root cause of the exception is that we missed such an element, and
it led to unresolved declaration during iteration over file declarations

^KT-65562 Fixed
This commit is contained in:
Dmitrii Gridin
2024-02-12 16:10:36 +01:00
committed by Space Team
parent 80240198d2
commit 31852c6c05
20 changed files with 140 additions and 4 deletions
@@ -52,6 +52,18 @@ public class Fe10IdeNormalAnalysisSourceModuleCollectDiagnosticsTestGenerated ex
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/annotationWithEnumFromDuplicatedLibrary.kt");
}
@Test
@TestMetadata("danglingAnnotationInMiddle.kt")
public void testDanglingAnnotationInMiddle() throws Exception {
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/danglingAnnotationInMiddle.kt");
}
@Test
@TestMetadata("danglingAnnotationInMiddleWithComment.kt")
public void testDanglingAnnotationInMiddleWithComment() throws Exception {
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/danglingAnnotationInMiddleWithComment.kt");
}
@Test
@TestMetadata("declarationErrors.kt")
public void testDeclarationErrors() throws Exception {
@@ -52,6 +52,18 @@ public class FirIdeNormalAnalysisSourceModuleCollectDiagnosticsTestGenerated ext
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/annotationWithEnumFromDuplicatedLibrary.kt");
}
@Test
@TestMetadata("danglingAnnotationInMiddle.kt")
public void testDanglingAnnotationInMiddle() throws Exception {
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/danglingAnnotationInMiddle.kt");
}
@Test
@TestMetadata("danglingAnnotationInMiddleWithComment.kt")
public void testDanglingAnnotationInMiddleWithComment() throws Exception {
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/danglingAnnotationInMiddleWithComment.kt");
}
@Test
@TestMetadata("declarationErrors.kt")
public void testDeclarationErrors() throws Exception {
@@ -52,6 +52,18 @@ public class FirIdeNormalAnalysisSourceModuleDanglingFileCollectDiagnosticsTestG
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/annotationWithEnumFromDuplicatedLibrary.kt");
}
@Test
@TestMetadata("danglingAnnotationInMiddle.kt")
public void testDanglingAnnotationInMiddle() throws Exception {
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/danglingAnnotationInMiddle.kt");
}
@Test
@TestMetadata("danglingAnnotationInMiddleWithComment.kt")
public void testDanglingAnnotationInMiddleWithComment() throws Exception {
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/danglingAnnotationInMiddleWithComment.kt");
}
@Test
@TestMetadata("declarationErrors.kt")
public void testDeclarationErrors() throws Exception {
@@ -52,6 +52,18 @@ public class FirStandaloneNormalAnalysisSourceModuleCollectDiagnosticsTestGenera
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/annotationWithEnumFromDuplicatedLibrary.kt");
}
@Test
@TestMetadata("danglingAnnotationInMiddle.kt")
public void testDanglingAnnotationInMiddle() throws Exception {
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/danglingAnnotationInMiddle.kt");
}
@Test
@TestMetadata("danglingAnnotationInMiddleWithComment.kt")
public void testDanglingAnnotationInMiddleWithComment() throws Exception {
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/danglingAnnotationInMiddleWithComment.kt");
}
@Test
@TestMetadata("declarationErrors.kt")
public void testDeclarationErrors() throws Exception {
@@ -52,6 +52,18 @@ public class FirStandaloneNormalAnalysisSourceModuleDanglingFileCollectDiagnosti
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/annotationWithEnumFromDuplicatedLibrary.kt");
}
@Test
@TestMetadata("danglingAnnotationInMiddle.kt")
public void testDanglingAnnotationInMiddle() throws Exception {
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/danglingAnnotationInMiddle.kt");
}
@Test
@TestMetadata("danglingAnnotationInMiddleWithComment.kt")
public void testDanglingAnnotationInMiddleWithComment() throws Exception {
runTest("analysis/analysis-api/testData/components/diagnosticsProvider/diagnostics/danglingAnnotationInMiddleWithComment.kt");
}
@Test
@TestMetadata("declarationErrors.kt")
public void testDeclarationErrors() throws Exception {
@@ -0,0 +1,2 @@
@MyAnn
fin class Foo
@@ -0,0 +1,4 @@
Diagnostics from elements:
for PSI element of type KtNameReferenceExpression at (1,2-7)
UNRESOLVED_REFERENCE text ranges: [(1,6)]
PSI: KtNameReferenceExpression at (1,2-7)
@@ -0,0 +1,2 @@
@MyAnn // comment
/*comment*/ope class Foo
@@ -0,0 +1,4 @@
Diagnostics from elements:
for PSI element of type KtNameReferenceExpression at (1,2-7)
UNRESOLVED_REFERENCE text ranges: [(1,6)]
PSI: KtNameReferenceExpression at (1,2-7)
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.fir.declarations.impl.FirPrimaryConstructor
import org.jetbrains.kotlin.fir.resolve.providers.firProvider
import org.jetbrains.kotlin.fir.symbols.lazyResolveToPhase
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespaceAndComments
import org.jetbrains.kotlin.psi.psiUtil.isAncestor
import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment
import org.jetbrains.kotlin.utils.exceptions.withPsiEntry
@@ -73,7 +74,7 @@ internal class FileStructure private constructor(
container = declaration
} else {
val modifierList = PsiTreeUtil.getParentOfType(element, KtModifierList::class.java, false)
container = if (modifierList != null && modifierList.nextSibling is PsiErrorElement) {
container = if (modifierList != null && modifierList.getNextSiblingIgnoringWhitespaceAndComments() is PsiErrorElement) {
modifierList
} else {
element.containingKtFile
@@ -207,7 +208,9 @@ internal class FileStructure private constructor(
RootStructureElement(firFile, moduleComponents)
}
container is KtDeclaration -> createDeclarationStructure(container)
container is KtModifierList && container.nextSibling is PsiErrorElement -> createDanglingModifierListStructure(container)
container is KtModifierList && container.getNextSiblingIgnoringWhitespaceAndComments() is PsiErrorElement -> {
createDanglingModifierListStructure(container)
}
else -> errorWithAttachment("Invalid container ${container::class}") {
withPsiEntry("container", container)
}
@@ -0,0 +1,4 @@
annotation class MyAnn/* ClassDeclarationStructureElement */
@MyAnn/* DeclarationStructureElement */
fin class Foo/* ClassDeclarationStructureElement */
@@ -0,0 +1,14 @@
FILE: [ResolvedTo(BODY_RESOLVE)] danglingAnnotationInMiddle.kt
public final [ResolvedTo(BODY_RESOLVE)] annotation class MyAnn : R|kotlin/Annotation| {
public [ResolvedTo(BODY_RESOLVE)] constructor(): R|MyAnn| {
super<R|kotlin/Any|>()
}
}
public final [ResolvedTo(BODY_RESOLVE)] class Foo : R|kotlin/Any| {
public [ResolvedTo(BODY_RESOLVE)] constructor(): R|Foo| {
super<R|kotlin/Any|>()
}
}
[ResolvedTo(BODY_RESOLVE)] @R|MyAnn|[Types]() <DANGLING MODIFIER: Top level declaration expected>
@@ -102,6 +102,12 @@ public class FirOutOfContentRootContextCollectionTestGenerated extends AbstractF
runTest("analysis/low-level-api-fir/testData/fileStructure/danglingAnnotationClassLevel.kt");
}
@Test
@TestMetadata("danglingAnnotationInMiddle.kt")
public void testDanglingAnnotationInMiddle() throws Exception {
runTest("analysis/low-level-api-fir/testData/fileStructure/danglingAnnotationInMiddle.kt");
}
@Test
@TestMetadata("danglingAnnotationTopLevel.kt")
public void testDanglingAnnotationTopLevel() throws Exception {
@@ -102,6 +102,12 @@ public class FirSourceContextCollectionTestGenerated extends AbstractFirSourceCo
runTest("analysis/low-level-api-fir/testData/fileStructure/danglingAnnotationClassLevel.kt");
}
@Test
@TestMetadata("danglingAnnotationInMiddle.kt")
public void testDanglingAnnotationInMiddle() throws Exception {
runTest("analysis/low-level-api-fir/testData/fileStructure/danglingAnnotationInMiddle.kt");
}
@Test
@TestMetadata("danglingAnnotationTopLevel.kt")
public void testDanglingAnnotationTopLevel() throws Exception {
@@ -102,6 +102,12 @@ public class SourceDiagnosticTraversalCounterTestGenerated extends AbstractSourc
runTest("analysis/low-level-api-fir/testData/fileStructure/danglingAnnotationClassLevel.kt");
}
@Test
@TestMetadata("danglingAnnotationInMiddle.kt")
public void testDanglingAnnotationInMiddle() throws Exception {
runTest("analysis/low-level-api-fir/testData/fileStructure/danglingAnnotationInMiddle.kt");
}
@Test
@TestMetadata("danglingAnnotationTopLevel.kt")
public void testDanglingAnnotationTopLevel() throws Exception {
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.fir.declarations.FirDeclaration
import org.jetbrains.kotlin.fir.psi
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.forEachDescendantOfType
import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespaceAndComments
import org.jetbrains.kotlin.test.KotlinTestUtils
import org.jetbrains.kotlin.test.model.TestModule
import org.jetbrains.kotlin.test.services.TestServices
@@ -76,7 +77,7 @@ abstract class AbstractFileStructureTest : AbstractAnalysisApiBasedTest() {
}
PsiTreeUtil.getChildrenOfTypeAsList(mainFile, KtModifierList::class.java).forEach {
if (it.nextSibling is PsiErrorElement) {
if (it.getNextSiblingIgnoringWhitespaceAndComments() is PsiErrorElement) {
val structureElement = declarationToStructureElement[it] ?: return@forEach
val comment = structureElement.createComment()
elementToComment[it] = comment
@@ -102,6 +102,12 @@ public class OutOfContentRootFileStructureTestGenerated extends AbstractOutOfCon
runTest("analysis/low-level-api-fir/testData/fileStructure/danglingAnnotationClassLevel.kt");
}
@Test
@TestMetadata("danglingAnnotationInMiddle.kt")
public void testDanglingAnnotationInMiddle() throws Exception {
runTest("analysis/low-level-api-fir/testData/fileStructure/danglingAnnotationInMiddle.kt");
}
@Test
@TestMetadata("danglingAnnotationTopLevel.kt")
public void testDanglingAnnotationTopLevel() throws Exception {
@@ -102,6 +102,12 @@ public class SourceFileStructureTestGenerated extends AbstractSourceFileStructur
runTest("analysis/low-level-api-fir/testData/fileStructure/danglingAnnotationClassLevel.kt");
}
@Test
@TestMetadata("danglingAnnotationInMiddle.kt")
public void testDanglingAnnotationInMiddle() throws Exception {
runTest("analysis/low-level-api-fir/testData/fileStructure/danglingAnnotationInMiddle.kt");
}
@Test
@TestMetadata("danglingAnnotationTopLevel.kt")
public void testDanglingAnnotationTopLevel() throws Exception {
@@ -102,6 +102,12 @@ public class OutOfContentRootWholeFileResolvePhaseTestGenerated extends Abstract
runTest("analysis/low-level-api-fir/testData/fileStructure/danglingAnnotationClassLevel.kt");
}
@Test
@TestMetadata("danglingAnnotationInMiddle.kt")
public void testDanglingAnnotationInMiddle() throws Exception {
runTest("analysis/low-level-api-fir/testData/fileStructure/danglingAnnotationInMiddle.kt");
}
@Test
@TestMetadata("danglingAnnotationTopLevel.kt")
public void testDanglingAnnotationTopLevel() throws Exception {
@@ -102,6 +102,12 @@ public class SourceWholeFileResolvePhaseTestGenerated extends AbstractSourceWhol
runTest("analysis/low-level-api-fir/testData/fileStructure/danglingAnnotationClassLevel.kt");
}
@Test
@TestMetadata("danglingAnnotationInMiddle.kt")
public void testDanglingAnnotationInMiddle() throws Exception {
runTest("analysis/low-level-api-fir/testData/fileStructure/danglingAnnotationInMiddle.kt");
}
@Test
@TestMetadata("danglingAnnotationTopLevel.kt")
public void testDanglingAnnotationTopLevel() throws Exception {