From 31d9313c51604862fc1c4addadeb3d43426d4665 Mon Sep 17 00:00:00 2001 From: Ivan Kochurkin Date: Tue, 15 Feb 2022 21:02:34 +0300 Subject: [PATCH] [FIR] Fix "Expected some types" exception, ^KT-51274 Fixed --- .../DiagnosisCompilerTestFE10TestdataTestGenerated.java | 6 ++++++ .../runners/FirOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ ...OldFrontendDiagnosticsWithLightTreeTestGenerated.java | 6 ++++++ .../jetbrains/kotlin/fir/types/ConeTypeIntersector.kt | 8 +++++--- ...eTypesOnBranchForWhenExpressionOfErroneousType.fir.kt | 9 +++++++++ ...dSomeTypesOnBranchForWhenExpressionOfErroneousType.kt | 9 +++++++++ ...SomeTypesOnBranchForWhenExpressionOfErroneousType.txt | 3 +++ .../kotlin/test/runners/DiagnosticTestGenerated.java | 6 ++++++ .../testData/diagnostics/notLinked/dfa/neg/1.fir.kt | 5 +++-- .../testData/diagnostics/notLinked/dfa/neg/1.kt | 1 + 10 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/exceptions/expectedSomeTypesOnBranchForWhenExpressionOfErroneousType.fir.kt create mode 100644 compiler/testData/diagnostics/tests/exceptions/expectedSomeTypesOnBranchForWhenExpressionOfErroneousType.kt create mode 100644 compiler/testData/diagnostics/tests/exceptions/expectedSomeTypesOnBranchForWhenExpressionOfErroneousType.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 c1cb5dac143..bd6ae48bee8 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 @@ -10292,6 +10292,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/exceptions"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true); } + @Test + @TestMetadata("expectedSomeTypesOnBranchForWhenExpressionOfErroneousType.kt") + public void testExpectedSomeTypesOnBranchForWhenExpressionOfErroneousType() throws Exception { + runTest("compiler/testData/diagnostics/tests/exceptions/expectedSomeTypesOnBranchForWhenExpressionOfErroneousType.kt"); + } + @Test @TestMetadata("kt24158.kt") public void testKt24158() 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 37c5283dc54..4c53da76a9b 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 @@ -10292,6 +10292,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/exceptions"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true); } + @Test + @TestMetadata("expectedSomeTypesOnBranchForWhenExpressionOfErroneousType.kt") + public void testExpectedSomeTypesOnBranchForWhenExpressionOfErroneousType() throws Exception { + runTest("compiler/testData/diagnostics/tests/exceptions/expectedSomeTypesOnBranchForWhenExpressionOfErroneousType.kt"); + } + @Test @TestMetadata("kt24158.kt") public void testKt24158() 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 8052b315b42..240a4d636ac 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 @@ -10292,6 +10292,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/exceptions"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true); } + @Test + @TestMetadata("expectedSomeTypesOnBranchForWhenExpressionOfErroneousType.kt") + public void testExpectedSomeTypesOnBranchForWhenExpressionOfErroneousType() throws Exception { + runTest("compiler/testData/diagnostics/tests/exceptions/expectedSomeTypesOnBranchForWhenExpressionOfErroneousType.kt"); + } + @Test @TestMetadata("kt24158.kt") public void testKt24158() throws Exception { diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypeIntersector.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypeIntersector.kt index 93946100754..20fc41b1f7e 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypeIntersector.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypeIntersector.kt @@ -102,9 +102,11 @@ object ConeTypeIntersector { val iterator = filteredTypes.iterator() while (iterator.hasNext()) { val upper = iterator.next() - val shouldFilter = filteredTypes.any { lower -> lower !== upper && predicate(lower, upper) } - - if (shouldFilter) iterator.remove() + if (filteredTypes.any { lower -> lower !== upper && predicate(lower, upper) } || + upper is ConeErrorType && filteredTypes.size > 1 + ) { + iterator.remove() + } } return filteredTypes } diff --git a/compiler/testData/diagnostics/tests/exceptions/expectedSomeTypesOnBranchForWhenExpressionOfErroneousType.fir.kt b/compiler/testData/diagnostics/tests/exceptions/expectedSomeTypesOnBranchForWhenExpressionOfErroneousType.fir.kt new file mode 100644 index 00000000000..a966f3230fd --- /dev/null +++ b/compiler/testData/diagnostics/tests/exceptions/expectedSomeTypesOnBranchForWhenExpressionOfErroneousType.fir.kt @@ -0,0 +1,9 @@ +// ISSUE: KT-51274 + +fun test() { + val x = unresolved() + val y = when (x) { + is String -> x + else -> throw Exception() + } +} diff --git a/compiler/testData/diagnostics/tests/exceptions/expectedSomeTypesOnBranchForWhenExpressionOfErroneousType.kt b/compiler/testData/diagnostics/tests/exceptions/expectedSomeTypesOnBranchForWhenExpressionOfErroneousType.kt new file mode 100644 index 00000000000..b5a863cf0b7 --- /dev/null +++ b/compiler/testData/diagnostics/tests/exceptions/expectedSomeTypesOnBranchForWhenExpressionOfErroneousType.kt @@ -0,0 +1,9 @@ +// ISSUE: KT-51274 + +fun test() { + val x = unresolved() + val y = when (x) { + is String -> x + else -> throw Exception() + } +} diff --git a/compiler/testData/diagnostics/tests/exceptions/expectedSomeTypesOnBranchForWhenExpressionOfErroneousType.txt b/compiler/testData/diagnostics/tests/exceptions/expectedSomeTypesOnBranchForWhenExpressionOfErroneousType.txt new file mode 100644 index 00000000000..93e27f34c8c --- /dev/null +++ b/compiler/testData/diagnostics/tests/exceptions/expectedSomeTypesOnBranchForWhenExpressionOfErroneousType.txt @@ -0,0 +1,3 @@ +package + +public fun test(): kotlin.Unit 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 3e645bd2f39..7f053ffe4fc 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 @@ -10298,6 +10298,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/exceptions"), Pattern.compile("^(.*)\\.kts?$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true); } + @Test + @TestMetadata("expectedSomeTypesOnBranchForWhenExpressionOfErroneousType.kt") + public void testExpectedSomeTypesOnBranchForWhenExpressionOfErroneousType() throws Exception { + runTest("compiler/testData/diagnostics/tests/exceptions/expectedSomeTypesOnBranchForWhenExpressionOfErroneousType.kt"); + } + @Test @TestMetadata("kt24158.kt") public void testKt24158() throws Exception { diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/dfa/neg/1.fir.kt b/compiler/tests-spec/testData/diagnostics/notLinked/dfa/neg/1.fir.kt index 4d3815eb1cd..847be5fc43d 100644 --- a/compiler/tests-spec/testData/diagnostics/notLinked/dfa/neg/1.fir.kt +++ b/compiler/tests-spec/testData/diagnostics/notLinked/dfa/neg/1.fir.kt @@ -184,8 +184,8 @@ fun case_13(x: otherpackage.Case13?) = if ((x == null !is Boolean) !== true) { throw Exception() } else { - x - x.equals(x) + x + x.equals(x) } // TESTCASE NUMBER: 14 @@ -196,6 +196,7 @@ class Case14 { } } +@Suppress("UNREACHABLE_CODE") fun case_14() { val a = Case14() diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/dfa/neg/1.kt b/compiler/tests-spec/testData/diagnostics/notLinked/dfa/neg/1.kt index 0385e4b3cf4..d55360b665b 100644 --- a/compiler/tests-spec/testData/diagnostics/notLinked/dfa/neg/1.kt +++ b/compiler/tests-spec/testData/diagnostics/notLinked/dfa/neg/1.kt @@ -205,6 +205,7 @@ class Case14 { } } +@Suppress("UNREACHABLE_CODE") fun case_14() { val a = Case14()