From 758a4931e36a5c1d016ad0d467f800dc41c0ee30 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Mon, 5 Dec 2022 11:31:20 +0100 Subject: [PATCH] EmptyIntersectionTypeChecker: drop also incompatible supertypes check Incompatible supertypes check also don't provoke runtime problems in most situations, because this check is also bound to type argument conflict. Related to KT-54411 --- .../substitutingSuperTypes.fir.kt | 16 ------ .../substitutingSuperTypes.kt | 3 +- .../substitutingSuperTypes2.fir.kt | 18 ------- .../substitutingSuperTypes2.kt | 3 +- .../checkers/EmptyIntersectionTypeChecker.kt | 54 ------------------- .../kotlin/types/EmptyIntersectionTypeKind.kt | 1 - 6 files changed, 4 insertions(+), 91 deletions(-) delete mode 100644 compiler/testData/diagnostics/tests/inference/emptyIntersectionTypes/substitutingSuperTypes.fir.kt delete mode 100644 compiler/testData/diagnostics/tests/inference/emptyIntersectionTypes/substitutingSuperTypes2.fir.kt diff --git a/compiler/testData/diagnostics/tests/inference/emptyIntersectionTypes/substitutingSuperTypes.fir.kt b/compiler/testData/diagnostics/tests/inference/emptyIntersectionTypes/substitutingSuperTypes.fir.kt deleted file mode 100644 index 059f34839f1..00000000000 --- a/compiler/testData/diagnostics/tests/inference/emptyIntersectionTypes/substitutingSuperTypes.fir.kt +++ /dev/null @@ -1,16 +0,0 @@ -class Foo - -class Bar { - fun takeFoo(foo: Foo) {} -} - -class Out - -interface A -interface B : A -interface C : A - -fun > main() { - val foo = Foo() - Bar>().takeFoo(foo) // error in 1.3.72, no error in 1.4.31 -} diff --git a/compiler/testData/diagnostics/tests/inference/emptyIntersectionTypes/substitutingSuperTypes.kt b/compiler/testData/diagnostics/tests/inference/emptyIntersectionTypes/substitutingSuperTypes.kt index df71825011f..95f8737d787 100644 --- a/compiler/testData/diagnostics/tests/inference/emptyIntersectionTypes/substitutingSuperTypes.kt +++ b/compiler/testData/diagnostics/tests/inference/emptyIntersectionTypes/substitutingSuperTypes.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL class Foo class Bar { @@ -12,5 +13,5 @@ interface C : A fun > main() { val foo = Foo() - Bar>().takeFoo(foo) // error in 1.3.72, no error in 1.4.31 + Bar>().takeFoo(foo) // error in 1.3.72, no error in 1.4.31 } diff --git a/compiler/testData/diagnostics/tests/inference/emptyIntersectionTypes/substitutingSuperTypes2.fir.kt b/compiler/testData/diagnostics/tests/inference/emptyIntersectionTypes/substitutingSuperTypes2.fir.kt deleted file mode 100644 index 838f43b2613..00000000000 --- a/compiler/testData/diagnostics/tests/inference/emptyIntersectionTypes/substitutingSuperTypes2.fir.kt +++ /dev/null @@ -1,18 +0,0 @@ -class Foo - -class Bar { - fun takeFoo(foo: Foo) {} -} - -class Out - -interface A -interface B2 : A -interface C2 : A -interface B : B2 -interface C : C2 - -fun > main() { - val foo = Foo() - Bar>().takeFoo(foo) // error in 1.3.72, no error in 1.4.31 -} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/emptyIntersectionTypes/substitutingSuperTypes2.kt b/compiler/testData/diagnostics/tests/inference/emptyIntersectionTypes/substitutingSuperTypes2.kt index 57874a1edf1..9b9499511c6 100644 --- a/compiler/testData/diagnostics/tests/inference/emptyIntersectionTypes/substitutingSuperTypes2.kt +++ b/compiler/testData/diagnostics/tests/inference/emptyIntersectionTypes/substitutingSuperTypes2.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL class Foo class Bar { @@ -14,5 +15,5 @@ interface C : C2 fun > main() { val foo = Foo() - Bar>().takeFoo(foo) // error in 1.3.72, no error in 1.4.31 + Bar>().takeFoo(foo) // error in 1.3.72, no error in 1.4.31 } diff --git a/core/compiler.common/src/org/jetbrains/kotlin/resolve/checkers/EmptyIntersectionTypeChecker.kt b/core/compiler.common/src/org/jetbrains/kotlin/resolve/checkers/EmptyIntersectionTypeChecker.kt index 20a1ffdcec5..c48615eb34b 100644 --- a/core/compiler.common/src/org/jetbrains/kotlin/resolve/checkers/EmptyIntersectionTypeChecker.kt +++ b/core/compiler.common/src/org/jetbrains/kotlin/resolve/checkers/EmptyIntersectionTypeChecker.kt @@ -101,10 +101,6 @@ internal object EmptyIntersectionTypeChecker { return EmptyIntersectionTypeInfo(EmptyIntersectionTypeKind.MULTIPLE_CLASSES, firstType, secondType) } firstTypeConstructor.isFinalClassConstructor() || secondTypeConstructor.isFinalClassConstructor() -> { - val incompatibleSupertypes = getIncompatibleSuperTypes(firstType, secondType) - if (incompatibleSupertypes != null) { - return EmptyIntersectionTypeInfo(EmptyIntersectionTypeKind.INCOMPATIBLE_SUPERTYPES, *incompatibleSupertypes) - } // don't have incompatible supertypes so can have a common subtype only if all types are interfaces possibleEmptyIntersectionKind = EmptyIntersectionTypeInfo( EmptyIntersectionTypeKind.FINAL_CLASS_AND_INTERFACE, @@ -125,56 +121,6 @@ internal object EmptyIntersectionTypeChecker { typeCheckerState, this, otherConstructorMarker ).isNotEmpty() - private fun TypeSystemInferenceExtensionContext.getIncompatibleSuperTypes( - firstType: KotlinTypeMarker, secondType: KotlinTypeMarker - ): Array? { - @Suppress("NAME_SHADOWING") - val firstType = firstType.eraseContainingTypeParameters() - - @Suppress("NAME_SHADOWING") - val secondType = secondType.eraseContainingTypeParameters() - - // interface A - // interface B: A - // interface C: A - // => B and C have incompatible supertypes - val superTypesOfFirst = firstType.typeConstructor().supertypes() - val firstTypeSubstitutor = createSubstitutorForSuperTypes(firstType) - val superTypesOfSecond = secondType.typeConstructor().supertypes() - val secondTypeSubstitutor = createSubstitutorForSuperTypes(secondType) - - for (superTypeOfFirst in superTypesOfFirst) { - @Suppress("NAME_SHADOWING") - val superTypeOfFirst = firstTypeSubstitutor?.safeSubstitute(superTypeOfFirst) ?: superTypeOfFirst - - if (areIncompatibleSuperTypes(superTypeOfFirst, secondType)) - return arrayOf(superTypeOfFirst, secondType) - - for (superTypeOfSecond in superTypesOfSecond) { - @Suppress("NAME_SHADOWING") - val superTypeOfSecond = secondTypeSubstitutor?.safeSubstitute(superTypeOfSecond) ?: superTypeOfSecond - - if (areIncompatibleSuperTypes(firstType, superTypeOfSecond)) - return arrayOf(firstType, superTypeOfSecond) - - if (areIncompatibleSuperTypes(superTypeOfFirst, superTypeOfSecond)) - return arrayOf(superTypeOfFirst, superTypeOfSecond) - - getIncompatibleSuperTypes(superTypeOfFirst, superTypeOfSecond)?.let { return it } - } - } - - return null - } - - private fun TypeSystemInferenceExtensionContext.areIncompatibleSuperTypes( - firstType: KotlinTypeMarker, secondType: KotlinTypeMarker - ): Boolean = firstType.typeConstructor() == secondType.typeConstructor() - && !AbstractTypeChecker.equalTypes( - newTypeCheckerState(errorTypesEqualToAnything = true, stubTypesEqualToAnything = true), - firstType, secondType - ) - private fun TypeSystemInferenceExtensionContext.mayCauseEmptyIntersection(type: KotlinTypeMarker): Boolean { val typeConstructor = type.typeConstructor() diff --git a/core/compiler.common/src/org/jetbrains/kotlin/types/EmptyIntersectionTypeKind.kt b/core/compiler.common/src/org/jetbrains/kotlin/types/EmptyIntersectionTypeKind.kt index 51f671322ed..12cb6e900bc 100644 --- a/core/compiler.common/src/org/jetbrains/kotlin/types/EmptyIntersectionTypeKind.kt +++ b/core/compiler.common/src/org/jetbrains/kotlin/types/EmptyIntersectionTypeKind.kt @@ -7,6 +7,5 @@ package org.jetbrains.kotlin.types enum class EmptyIntersectionTypeKind(val description: String, val isDefinitelyEmpty: Boolean) { MULTIPLE_CLASSES("multiple incompatible classes", isDefinitelyEmpty = true), - INCOMPATIBLE_SUPERTYPES("incompatible supertypes", isDefinitelyEmpty = true), FINAL_CLASS_AND_INTERFACE("final class and interface", isDefinitelyEmpty = false) }