[NI] Fix common supertype of types with error supertypes

Enable check for error supertypes during CST calculation in classic type system context.
Cyclic upper bound + known type parameters of superclasses may create non-error types
with error supertypes. Such types don't have common constructors with other normal types
and cause assertion errors during intersection.

^KT-36951 Fixed
This commit is contained in:
Pavel Kirpichenkov
2020-07-20 14:05:58 +03:00
parent 95cc35f22e
commit 710659324c
14 changed files with 127 additions and 5 deletions
@@ -10109,6 +10109,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/inference/commonSuperTypeOfErrorTypes.kt");
}
@TestMetadata("commonSuperTypeOfTypesWithErrorSupertypes.kt")
public void testCommonSuperTypeOfTypesWithErrorSupertypes() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/commonSuperTypeOfTypesWithErrorSupertypes.kt");
}
@TestMetadata("compatibilityResolveWhenVariableHasComplexIntersectionType.kt")
public void testCompatibilityResolveWhenVariableHasComplexIntersectionType() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/compatibilityResolveWhenVariableHasComplexIntersectionType.kt");
@@ -2910,6 +2910,11 @@ public class FirOldFrontendDiagnosticsTestWithStdlibGenerated extends AbstractFi
runTest("compiler/testData/diagnostics/testsWithStdLib/inference/kt36249.kt");
}
@TestMetadata("kt36951.kt")
public void testKt36951() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/inference/kt36951.kt");
}
@TestMetadata("kt37627.kt")
public void testKt37627() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/inference/kt37627.kt");
@@ -0,0 +1,15 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER
interface Foo<F> {
fun getSum(): F = TODO()
}
fun <S> select(vararg args: S): S = TODO()
class Bar<B : B> : Foo<B> {
val v = <!DEBUG_INFO_EXPRESSION_TYPE("B?")!>select(
getSum(),
42
)<!>
}
@@ -0,0 +1,15 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER
interface Foo<F> {
fun getSum(): F = TODO()
}
fun <S> select(vararg args: S): S = TODO()
class Bar<B : <!CYCLIC_GENERIC_UPPER_BOUND!>B<!>> : Foo<B> {
val v = <!DEBUG_INFO_EXPRESSION_TYPE("[ERROR : from type constructor([ERROR : Cyclic upper bounds])]")!>select(
<!DEBUG_INFO_LEAKING_THIS!>getSum<!>(),
42
)<!>
}
@@ -0,0 +1,19 @@
package
public fun </*0*/ S> select(/*0*/ vararg args: S /*kotlin.Array<out S>*/): S
public final class Bar</*0*/ B : [ERROR : Cyclic upper bounds]> : Foo<B> {
public constructor Bar</*0*/ B : [ERROR : Cyclic upper bounds]>()
public final val v: [ERROR : from type constructor([ERROR : Cyclic upper bounds])]
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun getSum(): B
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface Foo</*0*/ F> {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open fun getSum(): F
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,8 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER
class Base<T : T> : HashSet<T>() {
fun foo() {
super.remove("")
}
}
@@ -0,0 +1,8 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER
class Base<T : <!CYCLIC_GENERIC_UPPER_BOUND!>T<!>> : HashSet<T>() {
fun foo() {
<!SUPER_CANT_BE_EXTENSION_RECEIVER!>super<!>.<!TYPE_INFERENCE_ONLY_INPUT_TYPES_WARNING!>remove<!>("")
}
}
@@ -0,0 +1,26 @@
package
public final class Base</*0*/ T : [ERROR : Cyclic upper bounds]> : kotlin.collections.HashSet<T> /* = java.util.HashSet<T> */ {
public constructor Base</*0*/ T : [ERROR : Cyclic upper bounds]>()
invisible_fake final override /*1*/ /*fake_override*/ var map: java.util.HashMap<T!, kotlin.Any!>!
public open override /*1*/ /*fake_override*/ val size: kotlin.Int
public open override /*1*/ /*fake_override*/ fun add(/*0*/ element: T): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun addAll(/*0*/ elements: kotlin.collections.Collection<T>): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun clear(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun clone(): kotlin.Any
public open override /*1*/ /*fake_override*/ fun contains(/*0*/ element: T): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun containsAll(/*0*/ elements: kotlin.collections.Collection<T>): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final fun foo(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun isEmpty(): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun iterator(): kotlin.collections.MutableIterator<T>
invisible_fake open override /*1*/ /*fake_override*/ fun readObject(/*0*/ s: java.io.ObjectInputStream!): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun remove(/*0*/ element: T): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun removeAll(/*0*/ elements: kotlin.collections.Collection<T>): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun retainAll(/*0*/ elements: kotlin.collections.Collection<T>): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun toArray(): kotlin.Array<(out) kotlin.Any!>!
public open override /*1*/ /*fake_override*/ fun </*0*/ T : kotlin.Any!> toArray(/*0*/ a: kotlin.Array<(out) T!>!): kotlin.Array<(out) T!>!
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
invisible_fake open override /*1*/ /*fake_override*/ fun writeObject(/*0*/ s: java.io.ObjectOutputStream!): kotlin.Unit
}
@@ -10116,6 +10116,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali
runTest("compiler/testData/diagnostics/tests/inference/commonSuperTypeOfErrorTypes.kt");
}
@TestMetadata("commonSuperTypeOfTypesWithErrorSupertypes.kt")
public void testCommonSuperTypeOfTypesWithErrorSupertypes() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/commonSuperTypeOfTypesWithErrorSupertypes.kt");
}
@TestMetadata("compatibilityResolveWhenVariableHasComplexIntersectionType.kt")
public void testCompatibilityResolveWhenVariableHasComplexIntersectionType() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/compatibilityResolveWhenVariableHasComplexIntersectionType.kt");
@@ -3060,6 +3060,11 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
runTest("compiler/testData/diagnostics/testsWithStdLib/inference/kt36249.kt");
}
@TestMetadata("kt36951.kt")
public void testKt36951() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/inference/kt36951.kt");
}
@TestMetadata("kt37627.kt")
public void testKt37627() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/inference/kt37627.kt");
@@ -3060,6 +3060,11 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno
runTest("compiler/testData/diagnostics/testsWithStdLib/inference/kt36249.kt");
}
@TestMetadata("kt36951.kt")
public void testKt36951() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/inference/kt36951.kt");
}
@TestMetadata("kt37627.kt")
public void testKt37627() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/inference/kt37627.kt");
@@ -10111,6 +10111,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/inference/commonSuperTypeOfErrorTypes.kt");
}
@TestMetadata("commonSuperTypeOfTypesWithErrorSupertypes.kt")
public void testCommonSuperTypeOfTypesWithErrorSupertypes() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/commonSuperTypeOfTypesWithErrorSupertypes.kt");
}
@TestMetadata("compatibilityResolveWhenVariableHasComplexIntersectionType.kt")
public void testCompatibilityResolveWhenVariableHasComplexIntersectionType() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/compatibilityResolveWhenVariableHasComplexIntersectionType.kt");
@@ -33,7 +33,7 @@ import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSystemCommonBackendContext {
override val isErrorTypeAllowed: Boolean get() = true
override val isErrorTypeAllowed: Boolean get() = false
override fun TypeConstructorMarker.isDenotable(): Boolean {
require(this is TypeConstructor, this::errorMessage)
@@ -62,7 +62,8 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy
}
override fun TypeConstructorMarker.toErrorType(): SimpleTypeMarker {
throw IllegalStateException("Should not be called")
require(this is TypeConstructor && ErrorUtils.isError(declarationDescriptor), this::errorMessage)
return ErrorUtils.createErrorType("from type constructor(${toString()})")
}
override fun KotlinTypeMarker.isUninferredParameter(): Boolean {
@@ -529,7 +530,8 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy
}
override fun TypeConstructorMarker.isError(): Boolean {
throw IllegalStateException("Should not be called")
require(this is TypeConstructor, this::errorMessage)
return ErrorUtils.isError(declarationDescriptor)
}
override fun TypeConstructorMarker.getApproximatedIntegerLiteralType(): KotlinTypeMarker {
@@ -79,8 +79,7 @@ interface TypeCheckerProviderContext {
interface TypeSystemCommonSuperTypesContext : TypeSystemContext, TypeSystemTypeFactoryContext, TypeCheckerProviderContext {
/*
* If set in false then if there is an error type in input types list of `commonSuperType` it will be return
* That flag is needed for FIR where there are a problems with recursive class hierarchies
* If set in false then if there is an error supertype in input types list of `commonSuperType` it will be returned
*/
val isErrorTypeAllowed: Boolean