Minor refactoring in NewKotlinTypeChecker
This commit is contained in:
@@ -88,11 +88,16 @@ object NewKotlinTypeChecker : KotlinTypeChecker {
|
|||||||
return isSubtypeOf(a, b) && isSubtypeOf(b, a)
|
return isSubtypeOf(a, b) && isSubtypeOf(b, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun TypeCheckerContext.isSubtypeOf(subType: UnwrappedType, superType: UnwrappedType) =
|
fun TypeCheckerContext.isSubtypeOf(subType: UnwrappedType, superType: UnwrappedType): Boolean {
|
||||||
isSubtypeOf(subType.lowerIfFlexible(), superType.upperIfFlexible())
|
val newSubType = transformToNewType(subType)
|
||||||
|
val newSuperType = transformToNewType(superType)
|
||||||
|
|
||||||
fun TypeCheckerContext.isSubtypeOf(subType: SimpleType, superType: SimpleType): Boolean {
|
checkSubtypeForSpecialCases(newSubType.lowerIfFlexible(), newSuperType.upperIfFlexible())?.let { return it }
|
||||||
return isSubtypeOfForNewTypes(transformToNewType(subType), transformToNewType(superType))
|
|
||||||
|
// we should add constraints with flexible types, otherwise we never get flexible type as answer in constraint system
|
||||||
|
addSubtypeConstraint(newSubType, newSuperType)?.let { return it }
|
||||||
|
|
||||||
|
return isSubtypeOfForSingleClassifierType(newSubType.lowerIfFlexible(), newSuperType.upperIfFlexible())
|
||||||
}
|
}
|
||||||
|
|
||||||
fun transformToNewType(type: SimpleType): SimpleType {
|
fun transformToNewType(type: SimpleType): SimpleType {
|
||||||
@@ -137,9 +142,7 @@ object NewKotlinTypeChecker : KotlinTypeChecker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun TypeCheckerContext.isSubtypeOfForNewTypes(subType: SimpleType, superType: SimpleType): Boolean {
|
private fun TypeCheckerContext.checkSubtypeForSpecialCases(subType: SimpleType, superType: SimpleType): Boolean? {
|
||||||
if (isSubtypeByExternalRule(subType, superType)) return true // todo: do not call this for T? <: String?
|
|
||||||
|
|
||||||
if (subType.isError || superType.isError) {
|
if (subType.isError || superType.isError) {
|
||||||
if (errorTypeEqualsToAnything) return true
|
if (errorTypeEqualsToAnything) return true
|
||||||
|
|
||||||
@@ -155,7 +158,7 @@ object NewKotlinTypeChecker : KotlinTypeChecker {
|
|||||||
return it.supertypes.all { isSubtypeOf(subType, it.unwrap()) }
|
return it.supertypes.all { isSubtypeOf(subType, it.unwrap()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
return isSubtypeOfForSingleClassifierType(subType, superType)
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun TypeCheckerContext.hasNothingSupertype(type: SimpleType) = // todo add tests
|
private fun TypeCheckerContext.hasNothingSupertype(type: SimpleType) = // todo add tests
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ open class TypeCheckerContext(val errorTypeEqualsToAnything: Boolean) {
|
|||||||
private var supertypesDeque: ArrayDeque<SimpleType>? = null
|
private var supertypesDeque: ArrayDeque<SimpleType>? = null
|
||||||
private var supertypesSet: MutableSet<SimpleType>? = null
|
private var supertypesSet: MutableSet<SimpleType>? = null
|
||||||
|
|
||||||
open fun isSubtypeByExternalRule(subType: SimpleType, superType: SimpleType) = false
|
open fun addSubtypeConstraint(subType: UnwrappedType, superType: UnwrappedType): Boolean? = null
|
||||||
|
|
||||||
inline fun <T> runWithArgumentsSettings(subArgument: UnwrappedType, f: TypeCheckerContext.() -> T): T {
|
inline fun <T> runWithArgumentsSettings(subArgument: UnwrappedType, f: TypeCheckerContext.() -> T): T {
|
||||||
if (argumentsDepth > 100) {
|
if (argumentsDepth > 100) {
|
||||||
|
|||||||
Reference in New Issue
Block a user