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 dad5ff47f5a..7083799bf8e 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 @@ -33394,6 +33394,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/typeParameters/kt46186.kt"); } + @Test + @TestMetadata("kt46186withEmptyIntersections.kt") + public void testKt46186withEmptyIntersections() throws Exception { + runTest("compiler/testData/diagnostics/tests/typeParameters/kt46186withEmptyIntersections.kt"); + } + @Test @TestMetadata("misplacedConstraints.kt") public void testMisplacedConstraints() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java index 18e82c7dac9..9c8e33d515a 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java @@ -33394,6 +33394,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir runTest("compiler/testData/diagnostics/tests/typeParameters/kt46186.kt"); } + @Test + @TestMetadata("kt46186withEmptyIntersections.kt") + public void testKt46186withEmptyIntersections() throws Exception { + runTest("compiler/testData/diagnostics/tests/typeParameters/kt46186withEmptyIntersections.kt"); + } + @Test @TestMetadata("misplacedConstraints.kt") public void testMisplacedConstraints() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java index f57a9f64f26..5ca95d6b070 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java @@ -33490,6 +33490,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia runTest("compiler/testData/diagnostics/tests/typeParameters/kt46186.kt"); } + @Test + @TestMetadata("kt46186withEmptyIntersections.kt") + public void testKt46186withEmptyIntersections() throws Exception { + runTest("compiler/testData/diagnostics/tests/typeParameters/kt46186withEmptyIntersections.kt"); + } + @Test @TestMetadata("misplacedConstraints.kt") public void testMisplacedConstraints() throws Exception { diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/ResultTypeResolver.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/ResultTypeResolver.kt index 5e5395e6219..48a216ea54e 100644 --- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/ResultTypeResolver.kt +++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/ResultTypeResolver.kt @@ -265,8 +265,9 @@ class ResultTypeResolver( } private fun Context.computeUpperType(upperConstraints: List): KotlinTypeMarker { - // TODO: Remove this after stopping support of K1 - return if (!isK2) { + return if (languageVersionSettings.supportsFeature(LanguageFeature.AllowEmptyIntersectionsInResultTypeResolver)) { + intersectTypes(upperConstraints.map { it.type }) + } else { val intersectionUpperType = intersectTypes(upperConstraints.map { it.type }) val resultIsActuallyIntersection = intersectionUpperType.typeConstructor().isIntersection() @@ -291,8 +292,6 @@ class ResultTypeResolver( if (filteredUpperConstraints.isNotEmpty()) intersectTypes(filteredUpperConstraints) else intersectionUpperType } else intersectionUpperType upperType - } else { - intersectTypes(upperConstraints.map { it.type }) } } diff --git a/compiler/testData/diagnostics/tests/typeParameters/kt46186.fir.kt b/compiler/testData/diagnostics/tests/typeParameters/kt46186.fir.kt index b6ab6e72dc9..20a9e1bba95 100644 --- a/compiler/testData/diagnostics/tests/typeParameters/kt46186.fir.kt +++ b/compiler/testData/diagnostics/tests/typeParameters/kt46186.fir.kt @@ -1,4 +1,5 @@ // !DIAGNOSTICS: -FINAL_UPPER_BOUND -CAST_NEVER_SUCCEEDS +// !LANGUAGE: -AllowEmptyIntersectionsInResultTypeResolver interface I @@ -9,13 +10,13 @@ abstract class View4 interface View5 fun findViewById1(): T = null as T -fun test1(): I = findViewById1() +fun test1(): I = findViewById1() fun findViewById2(): T = null as T fun test2(): I = findViewById2() inline fun findViewById3(): T = null as T -fun test3(): I = findViewById3() +fun test3(): I = findViewById3() inline fun findViewById4(): T = null as T fun test4(): I = findViewById4() @@ -39,17 +40,17 @@ inline fun findViewById10(): T where T: View3, T: View5 = null as T fun test10(): I = findViewById10() fun findViewById11(): T = null as T -fun test11(): View4 = findViewById11() +fun test11(): View4 = findViewById11() object Obj { fun findViewById1(): T = null as T - fun test1(): View1 = findViewById1() + fun test1(): View1 = findViewById1() fun findViewById2(): T = null as T fun test2(): View2 = findViewById2() inline fun findViewById3(): T = null as T - fun test3(): View1 = findViewById3() + fun test3(): View1 = findViewById3() inline fun findViewById4(): T = null as T fun test4(): View2 = findViewById4() @@ -67,13 +68,13 @@ object Obj { fun test8(): View4 = findViewById8() fun findViewById9(): T where T: View3, T: View5 = null as T - fun test9(): View1 = findViewById9() + fun test9(): View1 = findViewById9() inline fun findViewById10(): T where T: View3, T: View5 = null as T - fun test10(): View1 = findViewById10() + fun test10(): View1 = findViewById10() fun findViewById11(): T = null as T - fun test11(): View4 = findViewById11() + fun test11(): View4 = findViewById11() } interface A diff --git a/compiler/testData/diagnostics/tests/typeParameters/kt46186.kt b/compiler/testData/diagnostics/tests/typeParameters/kt46186.kt index 1f651e5677b..09b059945f9 100644 --- a/compiler/testData/diagnostics/tests/typeParameters/kt46186.kt +++ b/compiler/testData/diagnostics/tests/typeParameters/kt46186.kt @@ -1,4 +1,5 @@ // !DIAGNOSTICS: -FINAL_UPPER_BOUND -CAST_NEVER_SUCCEEDS +// !LANGUAGE: -AllowEmptyIntersectionsInResultTypeResolver interface I diff --git a/compiler/testData/diagnostics/tests/typeParameters/kt46186withEmptyIntersections.fir.kt b/compiler/testData/diagnostics/tests/typeParameters/kt46186withEmptyIntersections.fir.kt new file mode 100644 index 00000000000..d2268e78b1c --- /dev/null +++ b/compiler/testData/diagnostics/tests/typeParameters/kt46186withEmptyIntersections.fir.kt @@ -0,0 +1,84 @@ +// !DIAGNOSTICS: -FINAL_UPPER_BOUND -CAST_NEVER_SUCCEEDS +// !LANGUAGE: +AllowEmptyIntersectionsInResultTypeResolver + +interface I + +class View1 +open class View2 +interface View3 +abstract class View4 +interface View5 + +fun findViewById1(): T = null as T +fun test1(): I = findViewById1() + +fun findViewById2(): T = null as T +fun test2(): I = findViewById2() + +inline fun findViewById3(): T = null as T +fun test3(): I = findViewById3() + +inline fun findViewById4(): T = null as T +fun test4(): I = findViewById4() + +fun findViewById5(): T = null as T +fun test5(): I = findViewById5() + +inline fun findViewById6(): T = null as T +fun test6(): I = findViewById6() + +fun findViewById7(): T = null as T +fun test7(): I = findViewById7() + +inline fun findViewById8(): T = null as T +fun test8(): I = findViewById8() + +fun findViewById9(): T where T: View3, T: View5 = null as T +fun test9(): I = findViewById9() + +inline fun findViewById10(): T where T: View3, T: View5 = null as T +fun test10(): I = findViewById10() + +fun findViewById11(): T = null as T +fun test11(): View4 = findViewById11() + +object Obj { + fun findViewById1(): T = null as T + fun test1(): View1 = findViewById1() + + fun findViewById2(): T = null as T + fun test2(): View2 = findViewById2() + + inline fun findViewById3(): T = null as T + fun test3(): View1 = findViewById3() + + inline fun findViewById4(): T = null as T + fun test4(): View2 = findViewById4() + + fun findViewById5(): T = null as T + fun test5(): View3 = findViewById5() + + inline fun findViewById6(): T = null as T + fun test6(): View3 = findViewById6() + + fun findViewById7(): T = null as T + fun test7(): View4 = findViewById7() + + inline fun findViewById8(): T = null as T + fun test8(): View4 = findViewById8() + + fun findViewById9(): T where T: View3, T: View5 = null as T + fun test9(): View1 = findViewById9() + + inline fun findViewById10(): T where T: View3, T: View5 = null as T + fun test10(): View1 = findViewById10() + + fun findViewById11(): T = null as T + fun test11(): View4 = findViewById11() +} + +interface A +open class B { + fun f(): T where T : A, T : B = null as T + fun g(): A = f() +} diff --git a/compiler/testData/diagnostics/tests/typeParameters/kt46186withEmptyIntersections.kt b/compiler/testData/diagnostics/tests/typeParameters/kt46186withEmptyIntersections.kt new file mode 100644 index 00000000000..837a6af8d37 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typeParameters/kt46186withEmptyIntersections.kt @@ -0,0 +1,84 @@ +// !DIAGNOSTICS: -FINAL_UPPER_BOUND -CAST_NEVER_SUCCEEDS +// !LANGUAGE: +AllowEmptyIntersectionsInResultTypeResolver + +interface I + +class View1 +open class View2 +interface View3 +abstract class View4 +interface View5 + +fun findViewById1(): T = null as T +fun test1(): I = findViewById1() + +fun findViewById2(): T = null as T +fun test2(): I = findViewById2() + +inline fun findViewById3(): T = null as T +fun test3(): I = findViewById3() + +inline fun findViewById4(): T = null as T +fun test4(): I = findViewById4() + +fun findViewById5(): T = null as T +fun test5(): I = findViewById5() + +inline fun findViewById6(): T = null as T +fun test6(): I = findViewById6() + +fun findViewById7(): T = null as T +fun test7(): I = findViewById7() + +inline fun findViewById8(): T = null as T +fun test8(): I = findViewById8() + +fun findViewById9(): T where T: View3, T: View5 = null as T +fun test9(): I = findViewById9() + +inline fun findViewById10(): T where T: View3, T: View5 = null as T +fun test10(): I = findViewById10() + +fun findViewById11(): T = null as T +fun test11(): View4 = findViewById11() + +object Obj { + fun findViewById1(): T = null as T + fun test1(): View1 = findViewById1() + + fun findViewById2(): T = null as T + fun test2(): View2 = findViewById2() + + inline fun findViewById3(): T = null as T + fun test3(): View1 = findViewById3() + + inline fun findViewById4(): T = null as T + fun test4(): View2 = findViewById4() + + fun findViewById5(): T = null as T + fun test5(): View3 = findViewById5() + + inline fun findViewById6(): T = null as T + fun test6(): View3 = findViewById6() + + fun findViewById7(): T = null as T + fun test7(): View4 = findViewById7() + + inline fun findViewById8(): T = null as T + fun test8(): View4 = findViewById8() + + fun findViewById9(): T where T: View3, T: View5 = null as T + fun test9(): View1 = findViewById9() + + inline fun findViewById10(): T where T: View3, T: View5 = null as T + fun test10(): View1 = findViewById10() + + fun findViewById11(): T = null as T + fun test11(): View4 = findViewById11() +} + +interface A +open class B { + fun f(): T where T : A, T : B = null as T + fun g(): A = f() +} 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 0924959ee55..f855b393b02 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 @@ -33490,6 +33490,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/typeParameters/kt46186.kt"); } + @Test + @TestMetadata("kt46186withEmptyIntersections.kt") + public void testKt46186withEmptyIntersections() throws Exception { + runTest("compiler/testData/diagnostics/tests/typeParameters/kt46186withEmptyIntersections.kt"); + } + @Test @TestMetadata("misplacedConstraints.kt") public void testMisplacedConstraints() throws Exception { diff --git a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt index 98ddea28b21..bba4b444bdc 100644 --- a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt +++ b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt @@ -284,6 +284,7 @@ enum class LanguageFeature( // 2.0 EnhanceNullabilityOfPrimitiveArrays(KOTLIN_2_0, kind = BUG_FIX), // KT-54521 + AllowEmptyIntersectionsInResultTypeResolver(KOTLIN_2_0, kind = OTHER), // KT-51221 // End of 2.* language features --------------------------------------------------