[FIR] Correctly process invalid destructing declarations

^KTIJ-24730 fixed

Merge-request: KT-MR-10712
Merged-by: Egor Kulikov <Egor.Kulikov@jetbrains.com>
This commit is contained in:
Egor Kulikov
2023-06-27 09:54:01 +00:00
committed by Space Team
parent a9d0ff83cf
commit a7c1f53af8
12 changed files with 71 additions and 5 deletions
@@ -2189,6 +2189,12 @@ public class Fe10IdeNormalAnalysisSourceModuleReferenceResolveTestGenerated exte
runTest("analysis/analysis-api/testData/referenceResolve/withErrors/TopLevelClassVsLocalClassQualifier.kt");
}
@Test
@TestMetadata("TopLevelDestructingDeclaration.kt")
public void testTopLevelDestructingDeclaration() throws Exception {
runTest("analysis/analysis-api/testData/referenceResolve/withErrors/TopLevelDestructingDeclaration.kt");
}
@Test
@TestMetadata("TypeArgumentBeforeDot2.kt")
public void testTypeArgumentBeforeDot2() throws Exception {
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.KtFakeSourceElementKind
import org.jetbrains.kotlin.KtRealSourceElementKind
import org.jetbrains.kotlin.analysis.api.components.KtSymbolContainingDeclarationProvider
import org.jetbrains.kotlin.analysis.api.fir.KtFirAnalysisSession
import org.jetbrains.kotlin.analysis.api.fir.symbols.KtFirErrorVariableSymbol
import org.jetbrains.kotlin.analysis.api.fir.utils.firSymbol
import org.jetbrains.kotlin.analysis.api.fir.utils.getContainingKtModule
import org.jetbrains.kotlin.analysis.api.fir.utils.withSymbolAttachment
@@ -23,7 +24,9 @@ import org.jetbrains.kotlin.analysis.project.structure.KtModule
import org.jetbrains.kotlin.analysis.utils.errors.buildErrorWithAttachment
import org.jetbrains.kotlin.analysis.utils.printer.parentOfType
import org.jetbrains.kotlin.fir.analysis.checkers.getContainingClassSymbol
import org.jetbrains.kotlin.fir.diagnostics.ConeDestructuringDeclarationsOnTopLevel
import org.jetbrains.kotlin.fir.resolve.providers.firProvider
import org.jetbrains.kotlin.fir.symbols.impl.FirErrorPropertySymbol
import org.jetbrains.kotlin.psi
import org.jetbrains.kotlin.psi.*
@@ -38,6 +41,8 @@ internal class KtFirSymbolContainingDeclarationProvider(
if (symbol !is KtDeclarationSymbol) return null
if (symbol is KtSymbolWithKind && symbol.symbolKind == KtSymbolKind.TOP_LEVEL) return null
val firSymbol = symbol.firSymbol
if (firSymbol is FirErrorPropertySymbol && firSymbol.diagnostic is ConeDestructuringDeclarationsOnTopLevel) return null
fun getParentSymbolByPsi() = getContainingPsi(symbol).let { with(analysisSession) { it.getSymbol() } }
return when (symbol) {
is KtPropertyAccessorSymbol -> firSymbolBuilder.buildSymbol(symbol.firSymbol.fir.propertySymbol) as? KtDeclarationSymbol
@@ -2189,6 +2189,12 @@ public class FirIdeDependentAnalysisSourceModuleReferenceResolveTestGenerated ex
runTest("analysis/analysis-api/testData/referenceResolve/withErrors/TopLevelClassVsLocalClassQualifier.kt");
}
@Test
@TestMetadata("TopLevelDestructingDeclaration.kt")
public void testTopLevelDestructingDeclaration() throws Exception {
runTest("analysis/analysis-api/testData/referenceResolve/withErrors/TopLevelDestructingDeclaration.kt");
}
@Test
@TestMetadata("TypeArgumentBeforeDot2.kt")
public void testTypeArgumentBeforeDot2() throws Exception {
@@ -2189,6 +2189,12 @@ public class FirIdeNormalAnalysisSourceModuleReferenceResolveTestGenerated exten
runTest("analysis/analysis-api/testData/referenceResolve/withErrors/TopLevelClassVsLocalClassQualifier.kt");
}
@Test
@TestMetadata("TopLevelDestructingDeclaration.kt")
public void testTopLevelDestructingDeclaration() throws Exception {
runTest("analysis/analysis-api/testData/referenceResolve/withErrors/TopLevelDestructingDeclaration.kt");
}
@Test
@TestMetadata("TypeArgumentBeforeDot2.kt")
public void testTypeArgumentBeforeDot2() throws Exception {
@@ -2189,6 +2189,12 @@ public class FirStandaloneNormalAnalysisSourceModuleReferenceResolveTestGenerate
runTest("analysis/analysis-api/testData/referenceResolve/withErrors/TopLevelClassVsLocalClassQualifier.kt");
}
@Test
@TestMetadata("TopLevelDestructingDeclaration.kt")
public void testTopLevelDestructingDeclaration() throws Exception {
runTest("analysis/analysis-api/testData/referenceResolve/withErrors/TopLevelDestructingDeclaration.kt");
}
@Test
@TestMetadata("TypeArgumentBeforeDot2.kt")
public void testTypeArgumentBeforeDot2() throws Exception {
@@ -0,0 +1,9 @@
// UNRESOLVED_REFERENCE
fun <T : Any, Z> createTuple(a: T, b: Z&Any): Pair<T, Z&Any> {
return Pair(a, b)
}
fun main()
var (<caret>val1, val2) = createTuple<String, Int?>("a", 1)
}
@@ -25,10 +25,6 @@ class KtFe10DestructuringDeclarationEntry(
)
}
override fun resolve() = multiResolve(false).asSequence()
.map { it.element }
.firstOrNull { it is KtDestructuringDeclarationEntry }
override fun getRangeInElement() = TextRange(0, element.textLength)
override fun isReferenceToImportAlias(alias: KtImportAlias): Boolean {
@@ -21,7 +21,7 @@ abstract class KtDestructuringDeclarationReference(
override fun resolve() = multiResolve(false).asSequence()
.map { it.element }
.first { it is KtDestructuringDeclarationEntry }
.firstOrNull { it is KtDestructuringDeclarationEntry }
override val resolvesByNames: Collection<Name>
get() {
@@ -0,0 +1,7 @@
fun <T : Any, Z> createTuple(a: T, b: Z&Any): Pair<T, Z&Any> {
return Pair(a, b)
}
fun main()
var (<expr>val1</expr>, val2) = createTuple<String, Int?>("a", 1)
}
@@ -0,0 +1,13 @@
KT element: KtDestructuringDeclarationEntry
FIR element: FirErrorPropertyImpl
FIR source kind: KtRealSourceElementKind
FIR element rendered:
<ERROR PROPERTY: Destructuring declarations are only allowed for local variables/values>
FIR FILE:
FILE: [ResolvedTo(IMPORTS)] ktij24730.kt
[ResolvedTo(BODY_RESOLVE)] annotations container
public? final? [ResolvedTo(RAW_FIR)] fun <[ResolvedTo(RAW_FIR)] T : Any, [ResolvedTo(RAW_FIR)] Z> createTuple([ResolvedTo(RAW_FIR)] a: T, [ResolvedTo(RAW_FIR)] b: ZAny): Pair<T, ZAny> { LAZY_BLOCK }
public? final? [ResolvedTo(RAW_FIR)] fun main(): R|kotlin/Unit|
<ERROR PROPERTY: Destructuring declarations are only allowed for local variables/values>
@@ -507,6 +507,12 @@ public class OutOfContentRootGetOrBuildFirTestGenerated extends AbstractOutOfCon
runTest("analysis/low-level-api-fir/testdata/getOrBuildFir/declarations/ktij23263.kt");
}
@Test
@TestMetadata("ktij24730.kt")
public void testKtij24730() throws Exception {
runTest("analysis/low-level-api-fir/testdata/getOrBuildFir/declarations/ktij24730.kt");
}
@Test
@TestMetadata("objectLiteral.kt")
public void testObjectLiteral() throws Exception {
@@ -507,6 +507,12 @@ public class SourceGetOrBuildFirTestGenerated extends AbstractSourceGetOrBuildFi
runTest("analysis/low-level-api-fir/testdata/getOrBuildFir/declarations/ktij23263.kt");
}
@Test
@TestMetadata("ktij24730.kt")
public void testKtij24730() throws Exception {
runTest("analysis/low-level-api-fir/testdata/getOrBuildFir/declarations/ktij24730.kt");
}
@Test
@TestMetadata("objectLiteral.kt")
public void testObjectLiteral() throws Exception {