From d8671422b1fe9625b9670b38d6358191d91799c4 Mon Sep 17 00:00:00 2001 From: Marco Pennekamp Date: Thu, 26 Jan 2023 14:08:13 +0100 Subject: [PATCH] [FIR] KT-56212 Fix compilation exception caused by type parameter chain - `FirTypeResolverImpl.resolveSymbol` cannot simply assert that a type parameter only has a single qualifier, because code may be fed to the compiler where a type parameter is the start of a type chain (see for example the added `typeParameterChainInReturnType` test). - The fix assumes that any multi-qualifier type parameter trivially resolves to `null`, because such a chained type cannot exist. ^KT-56212 fixed ^KTIJ-24083 fixed --- ...agnosisCompilerTestFE10TestdataTestGenerated.java | 12 ++++++++++++ .../FirOldFrontendDiagnosticsTestGenerated.java | 12 ++++++++++++ ...rontendDiagnosticsWithLightTreeTestGenerated.java | 12 ++++++++++++ .../resolve/providers/impl/FirTypeResolverImpl.kt | 5 +---- .../typeParameterChainInReceiver.fir.kt | 2 ++ .../typeParameters/typeParameterChainInReceiver.kt | 2 ++ .../typeParameters/typeParameterChainInReceiver.txt | 3 +++ .../typeParameterChainInReturnType.fir.kt | 2 ++ .../typeParameters/typeParameterChainInReturnType.kt | 2 ++ .../typeParameterChainInReturnType.txt | 3 +++ .../kotlin/test/runners/DiagnosticTestGenerated.java | 12 ++++++++++++ 11 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReceiver.fir.kt create mode 100644 compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReceiver.kt create mode 100644 compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReceiver.txt create mode 100644 compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReturnType.fir.kt create mode 100644 compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReturnType.kt create mode 100644 compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReturnType.txt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java index 2ba6231b456..5a482665c72 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java @@ -33086,6 +33086,18 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/typeParameters/typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.kt"); } + @Test + @TestMetadata("typeParameterChainInReceiver.kt") + public void testTypeParameterChainInReceiver() throws Exception { + runTest("compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReceiver.kt"); + } + + @Test + @TestMetadata("typeParameterChainInReturnType.kt") + public void testTypeParameterChainInReturnType() throws Exception { + runTest("compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReturnType.kt"); + } + @Test @TestMetadata("upperBoundCannotBeArray.kt") public void testUpperBoundCannotBeArray() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index 9fe3b0900d1..26a8707e3b9 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -33182,6 +33182,18 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/typeParameters/typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.kt"); } + @Test + @TestMetadata("typeParameterChainInReceiver.kt") + public void testTypeParameterChainInReceiver() throws Exception { + runTest("compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReceiver.kt"); + } + + @Test + @TestMetadata("typeParameterChainInReturnType.kt") + public void testTypeParameterChainInReturnType() throws Exception { + runTest("compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReturnType.kt"); + } + @Test @TestMetadata("upperBoundCannotBeArray.kt") public void testUpperBoundCannotBeArray() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java index 69bda3677fb..ec85add8166 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java @@ -33086,6 +33086,18 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/typeParameters/typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.kt"); } + @Test + @TestMetadata("typeParameterChainInReceiver.kt") + public void testTypeParameterChainInReceiver() throws Exception { + runTest("compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReceiver.kt"); + } + + @Test + @TestMetadata("typeParameterChainInReturnType.kt") + public void testTypeParameterChainInReturnType() throws Exception { + runTest("compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReturnType.kt"); + } + @Test @TestMetadata("upperBoundCannotBeArray.kt") public void testUpperBoundCannotBeArray() throws Exception { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirTypeResolverImpl.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirTypeResolverImpl.kt index 697c3fafa72..cd77a4d1ab1 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirTypeResolverImpl.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirTypeResolverImpl.kt @@ -73,10 +73,7 @@ class FirTypeResolverImpl(private val session: FirSession) : FirTypeResolver() { ?: qualifierResolver.resolveEnumEntrySymbol(qualifier, symbol.classId) } } - is FirTypeParameterSymbol -> { - assert(qualifier.size == 1) - symbol - } + is FirTypeParameterSymbol -> symbol.takeIf { qualifier.size == 1 } else -> error("!") } } diff --git a/compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReceiver.fir.kt b/compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReceiver.fir.kt new file mode 100644 index 00000000000..9feb9dff699 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReceiver.fir.kt @@ -0,0 +1,2 @@ +// ISSUE: KT-56212 +fun F.X.f(): Boolean = false diff --git a/compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReceiver.kt b/compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReceiver.kt new file mode 100644 index 00000000000..c7bcfb688f7 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReceiver.kt @@ -0,0 +1,2 @@ +// ISSUE: KT-56212 +fun F.X.f(): Boolean = false diff --git a/compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReceiver.txt b/compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReceiver.txt new file mode 100644 index 00000000000..72434558827 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReceiver.txt @@ -0,0 +1,3 @@ +package + +public fun [Error type: Unresolved type for F.X].f(): kotlin.Boolean diff --git a/compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReturnType.fir.kt b/compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReturnType.fir.kt new file mode 100644 index 00000000000..1cd0086e1f5 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReturnType.fir.kt @@ -0,0 +1,2 @@ +// ISSUE: KT-56212 +fun foo(): F.X = TODO() diff --git a/compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReturnType.kt b/compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReturnType.kt new file mode 100644 index 00000000000..a553b2bc1a6 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReturnType.kt @@ -0,0 +1,2 @@ +// ISSUE: KT-56212 +fun foo(): F.X = TODO() diff --git a/compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReturnType.txt b/compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReturnType.txt new file mode 100644 index 00000000000..883bc272e53 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReturnType.txt @@ -0,0 +1,3 @@ +package + +public fun foo(): [Error type: Unresolved type for F.X] diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index 123dcf9a998..cd74d7c6629 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -33182,6 +33182,18 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/typeParameters/typeMismatchErrorHasExpectedGenericTypeArgumentInsteadOfTypeParameter.kt"); } + @Test + @TestMetadata("typeParameterChainInReceiver.kt") + public void testTypeParameterChainInReceiver() throws Exception { + runTest("compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReceiver.kt"); + } + + @Test + @TestMetadata("typeParameterChainInReturnType.kt") + public void testTypeParameterChainInReturnType() throws Exception { + runTest("compiler/testData/diagnostics/tests/typeParameters/typeParameterChainInReturnType.kt"); + } + @Test @TestMetadata("upperBoundCannotBeArray.kt") public void testUpperBoundCannotBeArray() throws Exception {