Rename AbstractTypeCheckerContext to TypeCheckerState
This commit is contained in:
@@ -7,7 +7,7 @@ package org.jetbrains.kotlin.descriptors
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.EffectiveVisibility.Permissiveness
|
||||
import org.jetbrains.kotlin.types.AbstractTypeChecker
|
||||
import org.jetbrains.kotlin.types.AbstractTypeCheckerContext
|
||||
import org.jetbrains.kotlin.types.TypeCheckerState
|
||||
import org.jetbrains.kotlin.types.model.TypeCheckerProviderContext
|
||||
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
|
||||
|
||||
@@ -261,7 +261,7 @@ internal fun containerRelation(
|
||||
}
|
||||
}
|
||||
|
||||
private fun TypeCheckerProviderContext.createTypeCheckerContext(): AbstractTypeCheckerContext = newBaseTypeCheckerContext(
|
||||
private fun TypeCheckerProviderContext.createTypeCheckerContext(): TypeCheckerState = newTypeCheckerState(
|
||||
errorTypesEqualToAnything = false,
|
||||
stubTypesEqualToAnything = true
|
||||
)
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
|
||||
package org.jetbrains.kotlin.types
|
||||
|
||||
import org.jetbrains.kotlin.types.AbstractTypeCheckerContext.LowerCapturedTypePolicy.*
|
||||
import org.jetbrains.kotlin.types.AbstractTypeCheckerContext.SupertypesPolicy
|
||||
import org.jetbrains.kotlin.types.TypeCheckerState.LowerCapturedTypePolicy.*
|
||||
import org.jetbrains.kotlin.types.TypeCheckerState.SupertypesPolicy
|
||||
import org.jetbrains.kotlin.types.model.*
|
||||
import org.jetbrains.kotlin.utils.SmartList
|
||||
import org.jetbrains.kotlin.utils.SmartSet
|
||||
@@ -14,14 +14,14 @@ import java.util.*
|
||||
|
||||
/**
|
||||
* Context that defines how type-checker operates, stores type-checker state,
|
||||
* created by [TypeCheckerProviderContext.newBaseTypeCheckerContext] in most cases
|
||||
* created by [TypeCheckerProviderContext.newTypeCheckerState] in most cases
|
||||
*
|
||||
* Stateful and shouldn't be reused
|
||||
*
|
||||
* Once some type-checker operation is performed using a [TypeCheckerProviderContext], for example a [AbstractTypeChecker.isSubtypeOf],
|
||||
* new instance of particular [AbstractTypeCheckerContext] should be created, with properly specified type system context
|
||||
* new instance of particular [TypeCheckerState] should be created, with properly specified type system context
|
||||
*/
|
||||
abstract class AbstractTypeCheckerContext() {
|
||||
abstract class TypeCheckerState {
|
||||
|
||||
abstract val typeSystemContext: TypeSystemContext
|
||||
|
||||
@@ -43,7 +43,7 @@ abstract class AbstractTypeCheckerContext() {
|
||||
|
||||
protected var argumentsDepth = 0
|
||||
|
||||
internal inline fun <T> runWithArgumentsSettings(subArgument: KotlinTypeMarker, f: AbstractTypeCheckerContext.() -> T): T {
|
||||
internal inline fun <T> runWithArgumentsSettings(subArgument: KotlinTypeMarker, f: TypeCheckerState.() -> T): T {
|
||||
if (argumentsDepth > 100) {
|
||||
error("Arguments depth is too high. Some related argument: $subArgument")
|
||||
}
|
||||
@@ -134,21 +134,21 @@ abstract class AbstractTypeCheckerContext() {
|
||||
}
|
||||
|
||||
sealed class SupertypesPolicy {
|
||||
abstract fun transformType(context: AbstractTypeCheckerContext, type: KotlinTypeMarker): SimpleTypeMarker
|
||||
abstract fun transformType(state: TypeCheckerState, type: KotlinTypeMarker): SimpleTypeMarker
|
||||
|
||||
object None : SupertypesPolicy() {
|
||||
override fun transformType(context: AbstractTypeCheckerContext, type: KotlinTypeMarker) =
|
||||
override fun transformType(state: TypeCheckerState, type: KotlinTypeMarker) =
|
||||
throw UnsupportedOperationException("Should not be called")
|
||||
}
|
||||
|
||||
object UpperIfFlexible : SupertypesPolicy() {
|
||||
override fun transformType(context: AbstractTypeCheckerContext, type: KotlinTypeMarker) =
|
||||
with(context.typeSystemContext) { type.upperBoundIfFlexible() }
|
||||
override fun transformType(state: TypeCheckerState, type: KotlinTypeMarker) =
|
||||
with(state.typeSystemContext) { type.upperBoundIfFlexible() }
|
||||
}
|
||||
|
||||
object LowerIfFlexible : SupertypesPolicy() {
|
||||
override fun transformType(context: AbstractTypeCheckerContext, type: KotlinTypeMarker) =
|
||||
with(context.typeSystemContext) { type.lowerBoundIfFlexible() }
|
||||
override fun transformType(state: TypeCheckerState, type: KotlinTypeMarker) =
|
||||
with(state.typeSystemContext) { type.lowerBoundIfFlexible() }
|
||||
}
|
||||
|
||||
abstract class DoCustomTransform : SupertypesPolicy()
|
||||
@@ -168,7 +168,7 @@ object AbstractTypeChecker {
|
||||
context: TypeCheckerProviderContext,
|
||||
type: KotlinTypeMarker,
|
||||
stubTypesEqualToAnything: Boolean = true
|
||||
) = context.newBaseTypeCheckerContext(true, stubTypesEqualToAnything).prepareType(type)
|
||||
) = context.newTypeCheckerState(true, stubTypesEqualToAnything).prepareType(type)
|
||||
|
||||
fun isSubtypeOf(
|
||||
context: TypeCheckerProviderContext,
|
||||
@@ -176,18 +176,18 @@ object AbstractTypeChecker {
|
||||
superType: KotlinTypeMarker,
|
||||
stubTypesEqualToAnything: Boolean = true
|
||||
): Boolean {
|
||||
return isSubtypeOf(context.newBaseTypeCheckerContext(true, stubTypesEqualToAnything), subType, superType)
|
||||
return isSubtypeOf(context.newTypeCheckerState(true, stubTypesEqualToAnything), subType, superType)
|
||||
}
|
||||
|
||||
fun isSubtypeOfClass(
|
||||
context: AbstractTypeCheckerContext,
|
||||
state: TypeCheckerState,
|
||||
typeConstructor: TypeConstructorMarker,
|
||||
superConstructor: TypeConstructorMarker
|
||||
): Boolean {
|
||||
if (typeConstructor == superConstructor) return true
|
||||
with(context.typeSystemContext) {
|
||||
with(state.typeSystemContext) {
|
||||
for (superType in typeConstructor.supertypes()) {
|
||||
if (isSubtypeOfClass(context, superType.typeConstructor(), superConstructor)) {
|
||||
if (isSubtypeOfClass(state, superType.typeConstructor(), superConstructor)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -201,29 +201,29 @@ object AbstractTypeChecker {
|
||||
b: KotlinTypeMarker,
|
||||
stubTypesEqualToAnything: Boolean = true
|
||||
): Boolean {
|
||||
return equalTypes(context.newBaseTypeCheckerContext(false, stubTypesEqualToAnything), a, b)
|
||||
return equalTypes(context.newTypeCheckerState(false, stubTypesEqualToAnything), a, b)
|
||||
}
|
||||
|
||||
fun isSubtypeOf(
|
||||
context: AbstractTypeCheckerContext,
|
||||
state: TypeCheckerState,
|
||||
subType: KotlinTypeMarker,
|
||||
superType: KotlinTypeMarker,
|
||||
isFromNullabilityConstraint: Boolean = false
|
||||
): Boolean {
|
||||
if (subType === superType) return true
|
||||
|
||||
if (!context.customIsSubtypeOf(subType, superType)) return false
|
||||
if (!state.customIsSubtypeOf(subType, superType)) return false
|
||||
|
||||
return completeIsSubTypeOf(context, subType, superType, isFromNullabilityConstraint)
|
||||
return completeIsSubTypeOf(state, subType, superType, isFromNullabilityConstraint)
|
||||
}
|
||||
|
||||
fun equalTypes(context: AbstractTypeCheckerContext, a: KotlinTypeMarker, b: KotlinTypeMarker): Boolean =
|
||||
with(context.typeSystemContext) {
|
||||
fun equalTypes(state: TypeCheckerState, a: KotlinTypeMarker, b: KotlinTypeMarker): Boolean =
|
||||
with(state.typeSystemContext) {
|
||||
if (a === b) return true
|
||||
|
||||
if (isCommonDenotableType(a) && isCommonDenotableType(b)) {
|
||||
val refinedA = context.prepareType(context.refineType(a))
|
||||
val refinedB = context.prepareType(context.refineType(b))
|
||||
val refinedA = state.prepareType(state.refineType(a))
|
||||
val refinedB = state.prepareType(state.refineType(b))
|
||||
val simpleA = refinedA.lowerBoundIfFlexible()
|
||||
if (!areEqualTypeConstructors(refinedA.typeConstructor(), refinedB.typeConstructor())) return false
|
||||
if (simpleA.argumentsCount() == 0) {
|
||||
@@ -233,40 +233,40 @@ object AbstractTypeChecker {
|
||||
}
|
||||
}
|
||||
|
||||
return isSubtypeOf(context, a, b) && isSubtypeOf(context, b, a)
|
||||
return isSubtypeOf(state, a, b) && isSubtypeOf(state, b, a)
|
||||
}
|
||||
|
||||
|
||||
private fun completeIsSubTypeOf(
|
||||
context: AbstractTypeCheckerContext,
|
||||
state: TypeCheckerState,
|
||||
subType: KotlinTypeMarker,
|
||||
superType: KotlinTypeMarker,
|
||||
isFromNullabilityConstraint: Boolean
|
||||
): Boolean = with(context.typeSystemContext) {
|
||||
val preparedSubType = context.prepareType(context.refineType(subType))
|
||||
val preparedSuperType = context.prepareType(context.refineType(superType))
|
||||
): Boolean = with(state.typeSystemContext) {
|
||||
val preparedSubType = state.prepareType(state.refineType(subType))
|
||||
val preparedSuperType = state.prepareType(state.refineType(superType))
|
||||
|
||||
checkSubtypeForSpecialCases(context, preparedSubType.lowerBoundIfFlexible(), preparedSuperType.upperBoundIfFlexible())?.let {
|
||||
context.addSubtypeConstraint(preparedSubType, preparedSuperType, isFromNullabilityConstraint)
|
||||
checkSubtypeForSpecialCases(state, preparedSubType.lowerBoundIfFlexible(), preparedSuperType.upperBoundIfFlexible())?.let {
|
||||
state.addSubtypeConstraint(preparedSubType, preparedSuperType, isFromNullabilityConstraint)
|
||||
return it
|
||||
}
|
||||
|
||||
// we should add constraints with flexible types, otherwise we never get flexible type as answer in constraint system
|
||||
context.addSubtypeConstraint(preparedSubType, preparedSuperType, isFromNullabilityConstraint)?.let { return it }
|
||||
state.addSubtypeConstraint(preparedSubType, preparedSuperType, isFromNullabilityConstraint)?.let { return it }
|
||||
|
||||
return isSubtypeOfForSingleClassifierType(context, preparedSubType.lowerBoundIfFlexible(), preparedSuperType.upperBoundIfFlexible())
|
||||
return isSubtypeOfForSingleClassifierType(state, preparedSubType.lowerBoundIfFlexible(), preparedSuperType.upperBoundIfFlexible())
|
||||
}
|
||||
|
||||
private fun checkSubtypeForIntegerLiteralType(
|
||||
context: AbstractTypeCheckerContext,
|
||||
state: TypeCheckerState,
|
||||
subType: SimpleTypeMarker,
|
||||
superType: SimpleTypeMarker
|
||||
): Boolean? = with(context.typeSystemContext) {
|
||||
): Boolean? = with(state.typeSystemContext) {
|
||||
if (!subType.isIntegerLiteralType() && !superType.isIntegerLiteralType()) return null
|
||||
|
||||
fun isTypeInIntegerLiteralType(integerLiteralType: SimpleTypeMarker, type: SimpleTypeMarker, checkSupertypes: Boolean): Boolean =
|
||||
integerLiteralType.possibleIntegerTypes().any { possibleType ->
|
||||
(possibleType.typeConstructor() == type.typeConstructor()) || (checkSupertypes && isSubtypeOf(context, type, possibleType))
|
||||
(possibleType.typeConstructor() == type.typeConstructor()) || (checkSupertypes && isSubtypeOf(state, type, possibleType))
|
||||
}
|
||||
|
||||
fun isIntegerLiteralTypeInIntersectionComponents(type: SimpleTypeMarker): Boolean {
|
||||
@@ -299,12 +299,12 @@ object AbstractTypeChecker {
|
||||
return null
|
||||
}
|
||||
|
||||
private fun hasNothingSupertype(context: AbstractTypeCheckerContext, type: SimpleTypeMarker): Boolean = with(context.typeSystemContext) {
|
||||
private fun hasNothingSupertype(state: TypeCheckerState, type: SimpleTypeMarker): Boolean = with(state.typeSystemContext) {
|
||||
val typeConstructor = type.typeConstructor()
|
||||
if (typeConstructor.isClassTypeConstructor()) {
|
||||
return typeConstructor.isNothingConstructor()
|
||||
}
|
||||
return context.anySupertype(type, { it.typeConstructor().isNothingConstructor() }) {
|
||||
return state.anySupertype(type, { it.typeConstructor().isNothingConstructor() }) {
|
||||
if (it.isClassType()) {
|
||||
SupertypesPolicy.None
|
||||
} else {
|
||||
@@ -314,23 +314,23 @@ object AbstractTypeChecker {
|
||||
}
|
||||
|
||||
private fun isSubtypeOfForSingleClassifierType(
|
||||
context: AbstractTypeCheckerContext,
|
||||
state: TypeCheckerState,
|
||||
subType: SimpleTypeMarker,
|
||||
superType: SimpleTypeMarker
|
||||
): Boolean = with(context.typeSystemContext) {
|
||||
if (AbstractTypeChecker.RUN_SLOW_ASSERTIONS) {
|
||||
assert(subType.isSingleClassifierType() || subType.typeConstructor().isIntersection() || context.isAllowedTypeVariable(subType)) {
|
||||
): Boolean = with(state.typeSystemContext) {
|
||||
if (RUN_SLOW_ASSERTIONS) {
|
||||
assert(subType.isSingleClassifierType() || subType.typeConstructor().isIntersection() || state.isAllowedTypeVariable(subType)) {
|
||||
"Not singleClassifierType and not intersection subType: $subType"
|
||||
}
|
||||
assert(superType.isSingleClassifierType() || context .isAllowedTypeVariable(superType)) {
|
||||
assert(superType.isSingleClassifierType() || state .isAllowedTypeVariable(superType)) {
|
||||
"Not singleClassifierType superType: $superType"
|
||||
}
|
||||
}
|
||||
|
||||
if (!AbstractNullabilityChecker.isPossibleSubtype(context, subType, superType)) return false
|
||||
if (!AbstractNullabilityChecker.isPossibleSubtype(state, subType, superType)) return false
|
||||
|
||||
checkSubtypeForIntegerLiteralType(context, subType.lowerBoundIfFlexible(), superType.upperBoundIfFlexible())?.let {
|
||||
context.addSubtypeConstraint(subType, superType)
|
||||
checkSubtypeForIntegerLiteralType(state, subType.lowerBoundIfFlexible(), superType.upperBoundIfFlexible())?.let {
|
||||
state.addSubtypeConstraint(subType, superType)
|
||||
return it
|
||||
}
|
||||
|
||||
@@ -339,11 +339,11 @@ object AbstractTypeChecker {
|
||||
if (areEqualTypeConstructors(subType.typeConstructor(), superConstructor) && superConstructor.parametersCount() == 0) return true
|
||||
if (superType.typeConstructor().isAnyConstructor()) return true
|
||||
|
||||
val supertypesWithSameConstructor = findCorrespondingSupertypes(context, subType, superConstructor)
|
||||
.map { context.prepareType(it).asSimpleType() ?: it }
|
||||
val supertypesWithSameConstructor = findCorrespondingSupertypes(state, subType, superConstructor)
|
||||
.map { state.prepareType(it).asSimpleType() ?: it }
|
||||
when (supertypesWithSameConstructor.size) {
|
||||
0 -> return hasNothingSupertype(context, subType) // todo Nothing & Array<Number> <: Array<String>
|
||||
1 -> return context.isSubtypeForSameConstructor(supertypesWithSameConstructor.first().asArgumentList(), superType)
|
||||
0 -> return hasNothingSupertype(state, subType) // todo Nothing & Array<Number> <: Array<String>
|
||||
1 -> return state.isSubtypeForSameConstructor(supertypesWithSameConstructor.first().asArgumentList(), superType)
|
||||
|
||||
else -> { // at least 2 supertypes with same constructors. Such case is rare
|
||||
val newArguments = ArgumentList(superConstructor.parametersCount())
|
||||
@@ -361,10 +361,10 @@ object AbstractTypeChecker {
|
||||
newArguments.add(intersection)
|
||||
}
|
||||
|
||||
if (!anyNonOutParameter && context.isSubtypeForSameConstructor(newArguments, superType)) return true
|
||||
if (!anyNonOutParameter && state.isSubtypeForSameConstructor(newArguments, superType)) return true
|
||||
|
||||
// TODO: rethink this; now components order in intersection type affects semantic due to run subtyping (which can add constraints) only until the first successful candidate
|
||||
return supertypesWithSameConstructor.any { context.isSubtypeForSameConstructor(it.asArgumentList(), superType) }
|
||||
return supertypesWithSameConstructor.any { state.isSubtypeForSameConstructor(it.asArgumentList(), superType) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -388,7 +388,7 @@ object AbstractTypeChecker {
|
||||
return typeVariableConstructor.typeParameter?.hasRecursiveBounds(selfConstructor) == true
|
||||
}
|
||||
|
||||
fun AbstractTypeCheckerContext.isSubtypeForSameConstructor(
|
||||
fun TypeCheckerState.isSubtypeForSameConstructor(
|
||||
capturedSubArguments: TypeArgumentListMarker,
|
||||
superType: SimpleTypeMarker
|
||||
): Boolean = with(this.typeSystemContext) {
|
||||
@@ -474,12 +474,12 @@ object AbstractTypeChecker {
|
||||
}
|
||||
|
||||
private fun checkSubtypeForSpecialCases(
|
||||
context: AbstractTypeCheckerContext,
|
||||
state: TypeCheckerState,
|
||||
subType: SimpleTypeMarker,
|
||||
superType: SimpleTypeMarker
|
||||
): Boolean? = with(context.typeSystemContext) {
|
||||
): Boolean? = with(state.typeSystemContext) {
|
||||
if (subType.isError() || superType.isError()) {
|
||||
if (context.isErrorTypeEqualsToAnything) return true
|
||||
if (state.isErrorTypeEqualsToAnything) return true
|
||||
|
||||
if (subType.isMarkedNullable() && !superType.isMarkedNullable()) return false
|
||||
|
||||
@@ -491,10 +491,10 @@ object AbstractTypeChecker {
|
||||
}
|
||||
|
||||
if (subType.isStubTypeForBuilderInference() && superType.isStubTypeForBuilderInference())
|
||||
return isStubTypeSubtypeOfAnother(subType, superType) || context.isStubTypeEqualsToAnything
|
||||
return isStubTypeSubtypeOfAnother(subType, superType) || state.isStubTypeEqualsToAnything
|
||||
|
||||
if (subType.isStubType() || superType.isStubType())
|
||||
return context.isStubTypeEqualsToAnything
|
||||
return state.isStubTypeEqualsToAnything
|
||||
|
||||
// superType might be a definitely notNull type (see KT-42824)
|
||||
val superOriginalType = superType.asDefinitelyNotNullType()?.original() ?: superType
|
||||
@@ -508,9 +508,9 @@ object AbstractTypeChecker {
|
||||
} else {
|
||||
if (superType.isDefinitelyNotNullType()) lowerType.makeDefinitelyNotNullOrNotNull() else lowerType
|
||||
}
|
||||
when (context.getLowerCapturedTypePolicy(subType, superTypeCaptured)) {
|
||||
CHECK_ONLY_LOWER -> return isSubtypeOf(context, subType, nullableLowerType)
|
||||
CHECK_SUBTYPE_AND_LOWER -> if (isSubtypeOf(context, subType, nullableLowerType)) return true
|
||||
when (state.getLowerCapturedTypePolicy(subType, superTypeCaptured)) {
|
||||
CHECK_ONLY_LOWER -> return isSubtypeOf(state, subType, nullableLowerType)
|
||||
CHECK_SUBTYPE_AND_LOWER -> if (isSubtypeOf(state, subType, nullableLowerType)) return true
|
||||
SKIP_LOWER -> Unit
|
||||
}
|
||||
}
|
||||
@@ -519,7 +519,7 @@ object AbstractTypeChecker {
|
||||
if (superTypeConstructor.isIntersection()) {
|
||||
assert(!superType.isMarkedNullable()) { "Intersection type should not be marked nullable!: $superType" }
|
||||
|
||||
return superTypeConstructor.supertypes().all { isSubtypeOf(context, subType, it) }
|
||||
return superTypeConstructor.supertypes().all { isSubtypeOf(state, subType, it) }
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -532,7 +532,7 @@ object AbstractTypeChecker {
|
||||
if (subType is CapturedTypeMarker
|
||||
|| (subTypeConstructor.isIntersection() && subTypeConstructor.supertypes().all { it is CapturedTypeMarker })) {
|
||||
val typeParameter =
|
||||
context.typeSystemContext.getTypeParameterForArgumentInBaseIfItEqualToTarget(baseType = superType, targetType = subType)
|
||||
state.typeSystemContext.getTypeParameterForArgumentInBaseIfItEqualToTarget(baseType = superType, targetType = subType)
|
||||
if (typeParameter != null && typeParameter.hasRecursiveBounds(superType.typeConstructor())) {
|
||||
return true
|
||||
}
|
||||
@@ -561,10 +561,10 @@ object AbstractTypeChecker {
|
||||
}
|
||||
|
||||
private fun collectAllSupertypesWithGivenTypeConstructor(
|
||||
context: AbstractTypeCheckerContext,
|
||||
state: TypeCheckerState,
|
||||
subType: SimpleTypeMarker,
|
||||
superConstructor: TypeConstructorMarker
|
||||
): List<SimpleTypeMarker> = with(context.typeSystemContext) {
|
||||
): List<SimpleTypeMarker> = with(state.typeSystemContext) {
|
||||
subType.fastCorrespondingSupertypes(superConstructor)?.let {
|
||||
return it
|
||||
}
|
||||
@@ -580,7 +580,7 @@ object AbstractTypeChecker {
|
||||
|
||||
val result: MutableList<SimpleTypeMarker> = SmartList()
|
||||
|
||||
context.anySupertype(subType, { false }) {
|
||||
state.anySupertype(subType, { false }) {
|
||||
|
||||
val current = captureFromArguments(it, CaptureStatus.FOR_SUBTYPING) ?: it
|
||||
|
||||
@@ -593,7 +593,7 @@ object AbstractTypeChecker {
|
||||
SupertypesPolicy.LowerIfFlexible
|
||||
}
|
||||
else -> {
|
||||
context.substitutionSupertypePolicy(current)
|
||||
state.substitutionSupertypePolicy(current)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -602,11 +602,11 @@ object AbstractTypeChecker {
|
||||
}
|
||||
|
||||
private fun collectAndFilter(
|
||||
context: AbstractTypeCheckerContext,
|
||||
state: TypeCheckerState,
|
||||
classType: SimpleTypeMarker,
|
||||
constructor: TypeConstructorMarker
|
||||
) =
|
||||
selectOnlyPureKotlinSupertypes(context, collectAllSupertypesWithGivenTypeConstructor(context, classType, constructor))
|
||||
selectOnlyPureKotlinSupertypes(state, collectAllSupertypesWithGivenTypeConstructor(state, classType, constructor))
|
||||
|
||||
|
||||
/**
|
||||
@@ -620,9 +620,9 @@ object AbstractTypeChecker {
|
||||
* More tests: javaAndKotlinSuperType & purelyImplementedCollection folder
|
||||
*/
|
||||
private fun selectOnlyPureKotlinSupertypes(
|
||||
context: AbstractTypeCheckerContext,
|
||||
state: TypeCheckerState,
|
||||
supertypes: List<SimpleTypeMarker>
|
||||
): List<SimpleTypeMarker> = with(context.typeSystemContext) {
|
||||
): List<SimpleTypeMarker> = with(state.typeSystemContext) {
|
||||
if (supertypes.size < 2) return supertypes
|
||||
|
||||
val allPureSupertypes = supertypes.filter {
|
||||
@@ -635,22 +635,22 @@ object AbstractTypeChecker {
|
||||
// nullability was checked earlier via nullabilityChecker
|
||||
// should be used only if you really sure that it is correct
|
||||
fun findCorrespondingSupertypes(
|
||||
context: AbstractTypeCheckerContext,
|
||||
state: TypeCheckerState,
|
||||
subType: SimpleTypeMarker,
|
||||
superConstructor: TypeConstructorMarker
|
||||
): List<SimpleTypeMarker> = with(context.typeSystemContext) {
|
||||
): List<SimpleTypeMarker> = with(state.typeSystemContext) {
|
||||
if (subType.isClassType()) {
|
||||
return collectAndFilter(context, subType, superConstructor)
|
||||
return collectAndFilter(state, subType, superConstructor)
|
||||
}
|
||||
|
||||
// i.e. superType is not a classType
|
||||
if (!superConstructor.isClassTypeConstructor() && !superConstructor.isIntegerLiteralTypeConstructor()) {
|
||||
return collectAllSupertypesWithGivenTypeConstructor(context, subType, superConstructor)
|
||||
return collectAllSupertypesWithGivenTypeConstructor(state, subType, superConstructor)
|
||||
}
|
||||
|
||||
// todo add tests
|
||||
val classTypeSupertypes = SmartList<SimpleTypeMarker>()
|
||||
context.anySupertype(subType, { false }) {
|
||||
state.anySupertype(subType, { false }) {
|
||||
if (it.isClassType()) {
|
||||
classTypeSupertypes.add(it)
|
||||
SupertypesPolicy.None
|
||||
@@ -659,42 +659,42 @@ object AbstractTypeChecker {
|
||||
}
|
||||
}
|
||||
|
||||
return classTypeSupertypes.flatMap { collectAndFilter(context, it, superConstructor) }
|
||||
return classTypeSupertypes.flatMap { collectAndFilter(state, it, superConstructor) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
object AbstractNullabilityChecker {
|
||||
// this method checks only nullability
|
||||
fun isPossibleSubtype(context: AbstractTypeCheckerContext, subType: SimpleTypeMarker, superType: SimpleTypeMarker): Boolean =
|
||||
runIsPossibleSubtype(context, subType, superType)
|
||||
fun isPossibleSubtype(state: TypeCheckerState, subType: SimpleTypeMarker, superType: SimpleTypeMarker): Boolean =
|
||||
runIsPossibleSubtype(state, subType, superType)
|
||||
|
||||
fun isSubtypeOfAny(context: TypeCheckerProviderContext, type: KotlinTypeMarker): Boolean =
|
||||
AbstractNullabilityChecker.isSubtypeOfAny(
|
||||
context.newBaseTypeCheckerContext(
|
||||
context.newTypeCheckerState(
|
||||
errorTypesEqualToAnything = false,
|
||||
stubTypesEqualToAnything = true
|
||||
),
|
||||
type
|
||||
)
|
||||
|
||||
fun isSubtypeOfAny(context: AbstractTypeCheckerContext, type: KotlinTypeMarker): Boolean =
|
||||
with(context.typeSystemContext) {
|
||||
context.hasNotNullSupertype(type.lowerBoundIfFlexible(), SupertypesPolicy.LowerIfFlexible)
|
||||
fun isSubtypeOfAny(state: TypeCheckerState, type: KotlinTypeMarker): Boolean =
|
||||
with(state.typeSystemContext) {
|
||||
state.hasNotNullSupertype(type.lowerBoundIfFlexible(), SupertypesPolicy.LowerIfFlexible)
|
||||
}
|
||||
|
||||
private fun runIsPossibleSubtype(context: AbstractTypeCheckerContext, subType: SimpleTypeMarker, superType: SimpleTypeMarker): Boolean =
|
||||
with(context.typeSystemContext) {
|
||||
private fun runIsPossibleSubtype(state: TypeCheckerState, subType: SimpleTypeMarker, superType: SimpleTypeMarker): Boolean =
|
||||
with(state.typeSystemContext) {
|
||||
if (AbstractTypeChecker.RUN_SLOW_ASSERTIONS) {
|
||||
// it makes for case String? & Any <: String
|
||||
assert(
|
||||
subType.isSingleClassifierType() || subType.typeConstructor().isIntersection() || context.isAllowedTypeVariable(
|
||||
subType.isSingleClassifierType() || subType.typeConstructor().isIntersection() || state.isAllowedTypeVariable(
|
||||
subType
|
||||
)
|
||||
) {
|
||||
"Not singleClassifierType and not intersection subType: $subType"
|
||||
}
|
||||
assert(superType.isSingleClassifierType() || context.isAllowedTypeVariable(superType)) {
|
||||
assert(superType.isSingleClassifierType() || state.isAllowedTypeVariable(superType)) {
|
||||
"Not singleClassifierType superType: $superType"
|
||||
}
|
||||
}
|
||||
@@ -709,13 +709,13 @@ object AbstractNullabilityChecker {
|
||||
if (subType is CapturedTypeMarker && subType.isProjectionNotNull()) return true
|
||||
|
||||
// i.e. subType is not-nullable
|
||||
if (context.hasNotNullSupertype(subType, SupertypesPolicy.LowerIfFlexible)) return true
|
||||
if (state.hasNotNullSupertype(subType, SupertypesPolicy.LowerIfFlexible)) return true
|
||||
|
||||
// i.e. subType hasn't not-null supertype and isn't definitely not-null, but superType is definitely not-null
|
||||
if (superType.isDefinitelyNotNullType()) return false
|
||||
|
||||
// i.e subType hasn't not-null supertype, but superType has
|
||||
if (context.hasNotNullSupertype(superType, SupertypesPolicy.UpperIfFlexible)) return false
|
||||
if (state.hasNotNullSupertype(superType, SupertypesPolicy.UpperIfFlexible)) return false
|
||||
|
||||
// both superType and subType hasn't not-null supertype and are not definitely not null.
|
||||
|
||||
@@ -731,10 +731,10 @@ object AbstractNullabilityChecker {
|
||||
// classType cannot has special type in supertype list
|
||||
if (subType.isClassType()) return false
|
||||
|
||||
return hasPathByNotMarkedNullableNodes(context, subType, superType.typeConstructor())
|
||||
return hasPathByNotMarkedNullableNodes(state, subType, superType.typeConstructor())
|
||||
}
|
||||
|
||||
fun AbstractTypeCheckerContext.hasNotNullSupertype(type: SimpleTypeMarker, supertypesPolicy: SupertypesPolicy) =
|
||||
fun TypeCheckerState.hasNotNullSupertype(type: SimpleTypeMarker, supertypesPolicy: SupertypesPolicy) =
|
||||
with(typeSystemContext) {
|
||||
anySupertype(type, {
|
||||
(it.isClassType() && !it.isMarkedNullable()) || it.isDefinitelyNotNullType()
|
||||
@@ -745,24 +745,24 @@ object AbstractNullabilityChecker {
|
||||
|
||||
fun TypeCheckerProviderContext.hasPathByNotMarkedNullableNodes(start: SimpleTypeMarker, end: TypeConstructorMarker) =
|
||||
hasPathByNotMarkedNullableNodes(
|
||||
newBaseTypeCheckerContext(errorTypesEqualToAnything = false, stubTypesEqualToAnything = true), start, end
|
||||
newTypeCheckerState(errorTypesEqualToAnything = false, stubTypesEqualToAnything = true), start, end
|
||||
)
|
||||
|
||||
fun hasPathByNotMarkedNullableNodes(context: AbstractTypeCheckerContext, start: SimpleTypeMarker, end: TypeConstructorMarker) =
|
||||
with(context.typeSystemContext) {
|
||||
context.anySupertype(
|
||||
fun hasPathByNotMarkedNullableNodes(state: TypeCheckerState, start: SimpleTypeMarker, end: TypeConstructorMarker) =
|
||||
with(state.typeSystemContext) {
|
||||
state.anySupertype(
|
||||
start,
|
||||
{ isApplicableAsEndNode(context, it, end) },
|
||||
{ isApplicableAsEndNode(state, it, end) },
|
||||
{ if (it.isMarkedNullable()) SupertypesPolicy.None else SupertypesPolicy.LowerIfFlexible }
|
||||
)
|
||||
}
|
||||
|
||||
private fun isApplicableAsEndNode(context: AbstractTypeCheckerContext, type: SimpleTypeMarker, end: TypeConstructorMarker): Boolean =
|
||||
with(context.typeSystemContext) {
|
||||
private fun isApplicableAsEndNode(state: TypeCheckerState, type: SimpleTypeMarker, end: TypeConstructorMarker): Boolean =
|
||||
with(state.typeSystemContext) {
|
||||
if (type.isNothing()) return true
|
||||
if (type.isMarkedNullable()) return false
|
||||
|
||||
if (context.isStubTypeEqualsToAnything && type.isStubType()) return true
|
||||
if (state.isStubTypeEqualsToAnything && type.isStubType()) return true
|
||||
|
||||
return areEqualTypeConstructors(type.typeConstructor(), end)
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.types.model
|
||||
|
||||
import org.jetbrains.kotlin.types.AbstractTypeCheckerContext
|
||||
import org.jetbrains.kotlin.types.TypeCheckerState
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
import kotlin.contracts.ExperimentalContracts
|
||||
import kotlin.contracts.contract
|
||||
@@ -91,14 +91,14 @@ interface TypeSystemTypeFactoryContext: TypeSystemBuiltInsContext {
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory, that constructs [AbstractTypeCheckerContext], which defines type-checker behaviour
|
||||
* Factory, that constructs [TypeCheckerState], which defines type-checker behaviour
|
||||
* Implementation is recommended to be [TypeSystemContext]
|
||||
*/
|
||||
interface TypeCheckerProviderContext {
|
||||
fun newBaseTypeCheckerContext(
|
||||
fun newTypeCheckerState(
|
||||
errorTypesEqualToAnything: Boolean,
|
||||
stubTypesEqualToAnything: Boolean
|
||||
): AbstractTypeCheckerContext
|
||||
): TypeCheckerState
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,11 +107,11 @@ interface TypeCheckerProviderContext {
|
||||
interface TypeSystemCommonSuperTypesContext : TypeSystemContext, TypeSystemTypeFactoryContext, TypeCheckerProviderContext {
|
||||
|
||||
fun KotlinTypeMarker.anySuperTypeConstructor(predicate: (TypeConstructorMarker) -> Boolean) =
|
||||
newBaseTypeCheckerContext(errorTypesEqualToAnything = false, stubTypesEqualToAnything = true)
|
||||
newTypeCheckerState(errorTypesEqualToAnything = false, stubTypesEqualToAnything = true)
|
||||
.anySupertype(
|
||||
lowerBoundIfFlexible(),
|
||||
{ predicate(it.typeConstructor()) },
|
||||
{ AbstractTypeCheckerContext.SupertypesPolicy.LowerIfFlexible }
|
||||
{ TypeCheckerState.SupertypesPolicy.LowerIfFlexible }
|
||||
)
|
||||
|
||||
fun KotlinTypeMarker.canHaveUndefinedNullability(): Boolean
|
||||
|
||||
@@ -33,7 +33,6 @@ import org.jetbrains.kotlin.name.Name;
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt;
|
||||
import org.jetbrains.kotlin.types.*;
|
||||
import org.jetbrains.kotlin.types.checker.*;
|
||||
import org.jetbrains.kotlin.types.model.TypeConstructorMarker;
|
||||
import org.jetbrains.kotlin.utils.SmartSet;
|
||||
|
||||
import java.util.*;
|
||||
@@ -309,7 +308,7 @@ public class OverridingUtil {
|
||||
return OverrideCompatibilityInfo.conflict("Type parameter number mismatch");
|
||||
}
|
||||
|
||||
Pair<NewKotlinTypeCheckerImpl, ClassicTypeCheckerContext> typeChecker = createTypeChecker(superTypeParameters, subTypeParameters);
|
||||
Pair<NewKotlinTypeCheckerImpl, ClassicTypeCheckerState> typeChecker = createTypeChecker(superTypeParameters, subTypeParameters);
|
||||
|
||||
for (int i = 0; i < superTypeParameters.size(); i++) {
|
||||
if (!areTypeParametersEquivalent(
|
||||
@@ -385,7 +384,7 @@ public class OverridingUtil {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Pair<NewKotlinTypeCheckerImpl, ClassicTypeCheckerContext> createTypeChecker(
|
||||
private Pair<NewKotlinTypeCheckerImpl, ClassicTypeCheckerState> createTypeChecker(
|
||||
@NotNull List<TypeParameterDescriptor> firstParameters,
|
||||
@NotNull List<TypeParameterDescriptor> secondParameters
|
||||
) {
|
||||
@@ -393,19 +392,19 @@ public class OverridingUtil {
|
||||
"Should be the same number of type parameters: " + firstParameters + " vs " + secondParameters;
|
||||
|
||||
NewKotlinTypeCheckerImpl typeChecker = new NewKotlinTypeCheckerImpl(kotlinTypeRefiner, KotlinTypePreparator.Default.INSTANCE);
|
||||
ClassicTypeCheckerContext context = createTypeCheckerContext(firstParameters, secondParameters);
|
||||
ClassicTypeCheckerState context = createTypeCheckerContext(firstParameters, secondParameters);
|
||||
|
||||
return new Pair<NewKotlinTypeCheckerImpl, ClassicTypeCheckerContext>(typeChecker, context);
|
||||
return new Pair<NewKotlinTypeCheckerImpl, ClassicTypeCheckerState>(typeChecker, context);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private ClassicTypeCheckerContext createTypeCheckerContext(
|
||||
private ClassicTypeCheckerState createTypeCheckerContext(
|
||||
@NotNull List<TypeParameterDescriptor> firstParameters,
|
||||
@NotNull List<TypeParameterDescriptor> secondParameters
|
||||
) {
|
||||
if (firstParameters.isEmpty()) {
|
||||
return (ClassicTypeCheckerContext) new OverridingUtilTypeSystemContext(null, equalityAxioms, kotlinTypeRefiner)
|
||||
.newBaseTypeCheckerContext(true, true);
|
||||
return (ClassicTypeCheckerState) new OverridingUtilTypeSystemContext(null, equalityAxioms, kotlinTypeRefiner)
|
||||
.newTypeCheckerState(true, true);
|
||||
}
|
||||
|
||||
Map<TypeConstructor, TypeConstructor> matchingTypeConstructors = new HashMap<TypeConstructor, TypeConstructor>();
|
||||
@@ -413,8 +412,8 @@ public class OverridingUtil {
|
||||
matchingTypeConstructors.put(firstParameters.get(i).getTypeConstructor(), secondParameters.get(i).getTypeConstructor());
|
||||
}
|
||||
|
||||
return (ClassicTypeCheckerContext) new OverridingUtilTypeSystemContext(matchingTypeConstructors, equalityAxioms, kotlinTypeRefiner)
|
||||
.newBaseTypeCheckerContext(true, true);
|
||||
return (ClassicTypeCheckerState) new OverridingUtilTypeSystemContext(matchingTypeConstructors, equalityAxioms, kotlinTypeRefiner)
|
||||
.newTypeCheckerState(true, true);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -436,7 +435,7 @@ public class OverridingUtil {
|
||||
private boolean areTypesEquivalent(
|
||||
@NotNull KotlinType typeInSuper,
|
||||
@NotNull KotlinType typeInSub,
|
||||
@NotNull Pair<NewKotlinTypeCheckerImpl, ClassicTypeCheckerContext> typeChecker
|
||||
@NotNull Pair<NewKotlinTypeCheckerImpl, ClassicTypeCheckerState> typeChecker
|
||||
) {
|
||||
boolean bothErrors = KotlinTypeKt.isError(typeInSuper) && KotlinTypeKt.isError(typeInSub);
|
||||
if (bothErrors) return true;
|
||||
@@ -447,7 +446,7 @@ public class OverridingUtil {
|
||||
private boolean areTypeParametersEquivalent(
|
||||
@NotNull TypeParameterDescriptor superTypeParameter,
|
||||
@NotNull TypeParameterDescriptor subTypeParameter,
|
||||
@NotNull Pair<NewKotlinTypeCheckerImpl, ClassicTypeCheckerContext> typeChecker
|
||||
@NotNull Pair<NewKotlinTypeCheckerImpl, ClassicTypeCheckerState> typeChecker
|
||||
) {
|
||||
List<KotlinType> superBounds = superTypeParameter.getUpperBounds();
|
||||
List<KotlinType> subBounds = new ArrayList<KotlinType>(subTypeParameter.getUpperBounds());
|
||||
@@ -585,7 +584,7 @@ public class OverridingUtil {
|
||||
|
||||
if (!isVisibilityMoreSpecific(a, b)) return false;
|
||||
|
||||
Pair<NewKotlinTypeCheckerImpl, ClassicTypeCheckerContext> checker =
|
||||
Pair<NewKotlinTypeCheckerImpl, ClassicTypeCheckerState> checker =
|
||||
DEFAULT.createTypeChecker(a.getTypeParameters(), b.getTypeParameters());
|
||||
|
||||
if (a instanceof FunctionDescriptor) {
|
||||
@@ -642,7 +641,7 @@ public class OverridingUtil {
|
||||
@NotNull KotlinType aReturnType,
|
||||
@NotNull CallableDescriptor b,
|
||||
@NotNull KotlinType bReturnType,
|
||||
@NotNull Pair<NewKotlinTypeCheckerImpl, ClassicTypeCheckerContext> typeChecker
|
||||
@NotNull Pair<NewKotlinTypeCheckerImpl, ClassicTypeCheckerState> typeChecker
|
||||
) {
|
||||
return typeChecker.getFirst().isSubtypeOf(typeChecker.getSecond(), aReturnType.unwrap(), bReturnType.unwrap());
|
||||
}
|
||||
|
||||
+6
-6
@@ -5,9 +5,9 @@
|
||||
|
||||
package org.jetbrains.kotlin.resolve
|
||||
|
||||
import org.jetbrains.kotlin.types.AbstractTypeCheckerContext
|
||||
import org.jetbrains.kotlin.types.TypeCheckerState
|
||||
import org.jetbrains.kotlin.types.TypeConstructor
|
||||
import org.jetbrains.kotlin.types.checker.ClassicTypeCheckerContext
|
||||
import org.jetbrains.kotlin.types.checker.ClassicTypeCheckerState
|
||||
import org.jetbrains.kotlin.types.checker.ClassicTypeSystemContext
|
||||
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
|
||||
import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner
|
||||
@@ -25,11 +25,11 @@ class OverridingUtilTypeSystemContext(
|
||||
return super.areEqualTypeConstructors(c1, c2) || areEqualTypeConstructorsByAxioms(c1, c2)
|
||||
}
|
||||
|
||||
override fun newBaseTypeCheckerContext(
|
||||
override fun newTypeCheckerState(
|
||||
errorTypesEqualToAnything: Boolean,
|
||||
stubTypesEqualToAnything: Boolean
|
||||
): AbstractTypeCheckerContext {
|
||||
return ClassicTypeCheckerContext(
|
||||
): TypeCheckerState {
|
||||
return ClassicTypeCheckerState(
|
||||
errorTypesEqualToAnything,
|
||||
stubTypesEqualToAnything,
|
||||
allowedTypeVariable = true,
|
||||
@@ -45,4 +45,4 @@ class OverridingUtilTypeSystemContext(
|
||||
val img2 = matchingTypeConstructors[b]
|
||||
return img1 != null && img1 == b || img2 != null && img2 == a
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -21,14 +21,14 @@ import org.jetbrains.kotlin.types.model.KotlinTypeMarker
|
||||
import org.jetbrains.kotlin.types.model.SimpleTypeMarker
|
||||
import org.jetbrains.kotlin.types.refinement.TypeRefinement
|
||||
|
||||
open class ClassicTypeCheckerContext(
|
||||
open class ClassicTypeCheckerState(
|
||||
val errorTypeEqualsToAnything: Boolean,
|
||||
val stubTypeEqualsToAnything: Boolean = true,
|
||||
val allowedTypeVariable: Boolean = true,
|
||||
val kotlinTypeRefiner: KotlinTypeRefiner = KotlinTypeRefiner.Default,
|
||||
val kotlinTypePreparator: KotlinTypePreparator = KotlinTypePreparator.Default,
|
||||
override val typeSystemContext: ClassicTypeSystemContext = SimpleClassicTypeSystemContext
|
||||
) : AbstractTypeCheckerContext() {
|
||||
) : TypeCheckerState() {
|
||||
|
||||
@OptIn(TypeRefinement::class)
|
||||
override fun refineType(type: KotlinTypeMarker): KotlinTypeMarker {
|
||||
@@ -59,7 +59,7 @@ open class ClassicTypeCheckerContext(
|
||||
val substitutor = TypeConstructorSubstitution.create(type).buildSubstitutor()
|
||||
|
||||
return object : SupertypesPolicy.DoCustomTransform() {
|
||||
override fun transformType(context: AbstractTypeCheckerContext, type: KotlinTypeMarker): SimpleTypeMarker {
|
||||
override fun transformType(state: TypeCheckerState, type: KotlinTypeMarker): SimpleTypeMarker {
|
||||
return substitutor.safeSubstitute(
|
||||
type.lowerBoundIfFlexible() as KotlinType,
|
||||
Variance.INVARIANT
|
||||
@@ -388,11 +388,11 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy
|
||||
}
|
||||
|
||||
|
||||
override fun newBaseTypeCheckerContext(
|
||||
override fun newTypeCheckerState(
|
||||
errorTypesEqualToAnything: Boolean,
|
||||
stubTypesEqualToAnything: Boolean
|
||||
): AbstractTypeCheckerContext {
|
||||
return ClassicTypeCheckerContext(errorTypesEqualToAnything, stubTypesEqualToAnything, typeSystemContext = this)
|
||||
): TypeCheckerState {
|
||||
return ClassicTypeCheckerState(errorTypesEqualToAnything, stubTypesEqualToAnything, typeSystemContext = this)
|
||||
}
|
||||
|
||||
override fun nullableNothingType(): SimpleTypeMarker {
|
||||
|
||||
@@ -21,14 +21,10 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor
|
||||
import org.jetbrains.kotlin.resolve.OverridingUtil
|
||||
import org.jetbrains.kotlin.resolve.calls.inference.CapturedType
|
||||
import org.jetbrains.kotlin.resolve.calls.inference.CapturedTypeConstructorImpl
|
||||
import org.jetbrains.kotlin.resolve.constants.IntegerLiteralTypeConstructor
|
||||
import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstructor
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.AbstractNullabilityChecker.hasNotNullSupertype
|
||||
import org.jetbrains.kotlin.types.AbstractTypeCheckerContext.SupertypesPolicy
|
||||
import org.jetbrains.kotlin.types.model.CaptureStatus
|
||||
import org.jetbrains.kotlin.types.typeUtil.makeNullable
|
||||
import org.jetbrains.kotlin.types.TypeCheckerState.SupertypesPolicy
|
||||
|
||||
object SimpleClassicTypeSystemContext : ClassicTypeSystemContext
|
||||
|
||||
@@ -53,10 +49,10 @@ object StrictEqualityTypeChecker {
|
||||
|
||||
object ErrorTypesAreEqualToAnything : KotlinTypeChecker {
|
||||
override fun isSubtypeOf(subtype: KotlinType, supertype: KotlinType): Boolean =
|
||||
NewKotlinTypeChecker.Default.run { ClassicTypeCheckerContext(true).isSubtypeOf(subtype.unwrap(), supertype.unwrap()) }
|
||||
NewKotlinTypeChecker.Default.run { ClassicTypeCheckerState(true).isSubtypeOf(subtype.unwrap(), supertype.unwrap()) }
|
||||
|
||||
override fun equalTypes(a: KotlinType, b: KotlinType): Boolean =
|
||||
NewKotlinTypeChecker.Default.run { ClassicTypeCheckerContext(true).equalTypes(a.unwrap(), b.unwrap()) }
|
||||
NewKotlinTypeChecker.Default.run { ClassicTypeCheckerState(true).equalTypes(a.unwrap(), b.unwrap()) }
|
||||
}
|
||||
|
||||
interface NewKotlinTypeChecker : KotlinTypeChecker {
|
||||
@@ -77,21 +73,21 @@ class NewKotlinTypeCheckerImpl(
|
||||
override val overridingUtil: OverridingUtil = OverridingUtil.createWithTypeRefiner(kotlinTypeRefiner)
|
||||
|
||||
override fun isSubtypeOf(subtype: KotlinType, supertype: KotlinType): Boolean =
|
||||
ClassicTypeCheckerContext(
|
||||
ClassicTypeCheckerState(
|
||||
true, kotlinTypeRefiner = kotlinTypeRefiner, kotlinTypePreparator = kotlinTypePreparator
|
||||
).isSubtypeOf(subtype.unwrap(), supertype.unwrap()) // todo fix flag errorTypeEqualsToAnything
|
||||
|
||||
override fun equalTypes(a: KotlinType, b: KotlinType): Boolean =
|
||||
ClassicTypeCheckerContext(
|
||||
ClassicTypeCheckerState(
|
||||
false, kotlinTypeRefiner = kotlinTypeRefiner, kotlinTypePreparator = kotlinTypePreparator
|
||||
).equalTypes(a.unwrap(), b.unwrap())
|
||||
|
||||
fun ClassicTypeCheckerContext.equalTypes(a: UnwrappedType, b: UnwrappedType): Boolean {
|
||||
return AbstractTypeChecker.equalTypes(this as AbstractTypeCheckerContext, a, b)
|
||||
fun ClassicTypeCheckerState.equalTypes(a: UnwrappedType, b: UnwrappedType): Boolean {
|
||||
return AbstractTypeChecker.equalTypes(this as TypeCheckerState, a, b)
|
||||
}
|
||||
|
||||
fun ClassicTypeCheckerContext.isSubtypeOf(subType: UnwrappedType, superType: UnwrappedType): Boolean {
|
||||
return AbstractTypeChecker.isSubtypeOf(this as AbstractTypeCheckerContext, subType, superType)
|
||||
fun ClassicTypeCheckerState.isSubtypeOf(subType: UnwrappedType, superType: UnwrappedType): Boolean {
|
||||
return AbstractTypeChecker.isSubtypeOf(this as TypeCheckerState, subType, superType)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,18 +95,18 @@ object NullabilityChecker {
|
||||
|
||||
fun isSubtypeOfAny(type: UnwrappedType): Boolean =
|
||||
SimpleClassicTypeSystemContext
|
||||
.newBaseTypeCheckerContext(errorTypesEqualToAnything = false, stubTypesEqualToAnything = true)
|
||||
.newTypeCheckerState(errorTypesEqualToAnything = false, stubTypesEqualToAnything = true)
|
||||
.hasNotNullSupertype(type.lowerIfFlexible(), SupertypesPolicy.LowerIfFlexible)
|
||||
}
|
||||
|
||||
fun UnwrappedType.hasSupertypeWithGivenTypeConstructor(typeConstructor: TypeConstructor) =
|
||||
ClassicTypeCheckerContext(false).anySupertype(lowerIfFlexible(), {
|
||||
ClassicTypeCheckerState(false).anySupertype(lowerIfFlexible(), {
|
||||
require(it is SimpleType)
|
||||
it.constructor == typeConstructor
|
||||
}, { SupertypesPolicy.LowerIfFlexible })
|
||||
|
||||
fun UnwrappedType.anySuperTypeConstructor(predicate: (TypeConstructor) -> Boolean) =
|
||||
ClassicTypeCheckerContext(false).anySupertype(lowerIfFlexible(), {
|
||||
ClassicTypeCheckerState(false).anySupertype(lowerIfFlexible(), {
|
||||
require(it is SimpleType)
|
||||
predicate(it.constructor)
|
||||
}, { SupertypesPolicy.LowerIfFlexible })
|
||||
|
||||
Reference in New Issue
Block a user