Remove all type system-specific inheritors of TypeCheckerState

This commit is contained in:
Dmitriy Novozhilov
2021-08-25 14:20:08 +03:00
parent 3f6738c8bc
commit 7e6e0a3dd6
22 changed files with 193 additions and 173 deletions
@@ -10,7 +10,10 @@ import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemOperation
import org.jetbrains.kotlin.resolve.calls.inference.model.*
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintKind.*
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.AbstractTypeApproximator
import org.jetbrains.kotlin.types.AbstractTypeChecker
import org.jetbrains.kotlin.types.TypeApproximatorConfiguration
import org.jetbrains.kotlin.types.TypeCheckerState
import org.jetbrains.kotlin.types.model.*
import org.jetbrains.kotlin.utils.SmartList
import kotlin.math.max
@@ -214,13 +217,20 @@ class ConstraintInjector(
private fun Context.isAllowedType(type: KotlinTypeMarker) =
type.typeDepth() <= maxTypeDepthFromInitialConstraints + ALLOWED_DEPTH_DELTA_FOR_INCORPORATION
private inner class TypeCheckerStateForConstraintInjector(val c: Context, val position: IncorporationConstraintPosition) :
TypeCheckerStateForConstraintSystem(c), ConstraintIncorporator.Context, TypeSystemInferenceExtensionContext by c {
override val kotlinTypePreparator: AbstractTypePreparator
get() = baseState.kotlinTypePreparator
override val kotlinTypeRefiner: AbstractTypeRefiner
get() = baseState.kotlinTypeRefiner
private inner class TypeCheckerStateForConstraintInjector(
baseState: TypeCheckerState,
val c: Context,
val position: IncorporationConstraintPosition
) : TypeCheckerStateForConstraintSystem(
c,
baseState.kotlinTypePreparator,
baseState.kotlinTypeRefiner
), ConstraintIncorporator.Context, TypeSystemInferenceExtensionContext by c {
constructor(c: Context, position: IncorporationConstraintPosition) : this(
c.newTypeCheckerState(errorTypesEqualToAnything = true, stubTypesEqualToAnything = true),
c,
position
)
// We use `var` intentionally to avoid extra allocations as this property is quite "hot"
private var possibleNewConstraints: MutableList<Pair<TypeVariableMarker, Constraint>>? = null
@@ -248,8 +258,6 @@ class ConstraintInjector(
baseUpperType = upperType
}
val baseState: TypeCheckerState = newTypeCheckerState(isErrorTypeEqualsToAnything, isStubTypeEqualsToAnything)
fun runIsSubtypeOf(
lowerType: KotlinTypeMarker,
upperType: KotlinTypeMarker,
@@ -5,23 +5,21 @@
package org.jetbrains.kotlin.resolve.calls.inference.components
import org.jetbrains.kotlin.types.AbstractNullabilityChecker
import org.jetbrains.kotlin.types.AbstractTypeChecker
import org.jetbrains.kotlin.types.TypeCheckerState
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.model.*
abstract class TypeCheckerStateForConstraintSystem(override val typeSystemContext: TypeSystemInferenceExtensionContext) :
TypeCheckerState() {
override val allowedTypeVariable: Boolean
get() = false
override val isErrorTypeEqualsToAnything: Boolean
get() = true
override val isStubTypeEqualsToAnything: Boolean
get() = true
abstract class TypeCheckerStateForConstraintSystem(
val extensionTypeContext: TypeSystemInferenceExtensionContext,
kotlinTypePreparator: AbstractTypePreparator,
kotlinTypeRefiner: AbstractTypeRefiner
) : TypeCheckerState(
isErrorTypeEqualsToAnything = true,
isStubTypeEqualsToAnything = true,
allowedTypeVariable = false,
typeSystemContext = extensionTypeContext,
kotlinTypePreparator,
kotlinTypeRefiner
) {
abstract val isInferenceCompatibilityEnabled: Boolean
abstract fun isMyTypeVariable(type: SimpleTypeMarker): Boolean
@@ -38,7 +36,7 @@ abstract class TypeCheckerStateForConstraintSystem(override val typeSystemContex
abstract fun addEqualityConstraint(typeVariable: TypeConstructorMarker, type: KotlinTypeMarker)
override fun getLowerCapturedTypePolicy(subType: SimpleTypeMarker, superType: CapturedTypeMarker): LowerCapturedTypePolicy =
with(typeSystemContext) {
with(extensionTypeContext) {
return when {
isMyTypeVariable(subType) -> {
val projection = superType.typeConstructorProjection()
@@ -73,10 +71,10 @@ abstract class TypeCheckerStateForConstraintSystem(override val typeSystemContex
// we should strip annotation's because we have incorporation operation and they should be not affected
val mySubType =
if (hasExact) extractTypeForProjectedType(subType, out = true)
?: with(typeSystemContext) { subType.removeExactAnnotation() } else subType
?: with(extensionTypeContext) { subType.removeExactAnnotation() } else subType
val mySuperType =
if (hasExact) extractTypeForProjectedType(superType, out = false)
?: with(typeSystemContext) { superType.removeExactAnnotation() } else superType
?: with(extensionTypeContext) { superType.removeExactAnnotation() } else superType
val result = internalAddSubtypeConstraint(mySubType, mySuperType, isFromNullabilityConstraint)
if (!hasExact) return result
@@ -87,7 +85,7 @@ abstract class TypeCheckerStateForConstraintSystem(override val typeSystemContex
return (result ?: true) && (result2 ?: true)
}
private fun extractTypeForProjectedType(type: KotlinTypeMarker, out: Boolean): KotlinTypeMarker? = with(typeSystemContext) {
private fun extractTypeForProjectedType(type: KotlinTypeMarker, out: Boolean): KotlinTypeMarker? = with(extensionTypeContext) {
val typeMarker = type.asSimpleType()?.asCapturedType() ?: return null
val projection = typeMarker.typeConstructorProjection()
@@ -99,10 +97,10 @@ abstract class TypeCheckerStateForConstraintSystem(override val typeSystemContex
}
private fun KotlinTypeMarker.isTypeVariableWithExact() =
with(typeSystemContext) { hasExactAnnotation() } && anyBound(this@TypeCheckerStateForConstraintSystem::isMyTypeVariable)
with(extensionTypeContext) { hasExactAnnotation() } && anyBound(this@TypeCheckerStateForConstraintSystem::isMyTypeVariable)
private fun KotlinTypeMarker.isTypeVariableWithNoInfer() =
with(typeSystemContext) { hasNoInferAnnotation() } && anyBound(this@TypeCheckerStateForConstraintSystem::isMyTypeVariable)
with(extensionTypeContext) { hasNoInferAnnotation() } && anyBound(this@TypeCheckerStateForConstraintSystem::isMyTypeVariable)
private fun internalAddSubtypeConstraint(
subType: KotlinTypeMarker,
@@ -130,7 +128,7 @@ abstract class TypeCheckerStateForConstraintSystem(override val typeSystemContex
// extract type variable only from type like Captured(out T)
private fun extractTypeVariableForSubtype(subType: KotlinTypeMarker, superType: KotlinTypeMarker): KotlinTypeMarker? =
with(typeSystemContext) {
with(extensionTypeContext) {
val typeMarker = subType.asSimpleType()?.asCapturedType() ?: return null
@@ -198,7 +196,7 @@ abstract class TypeCheckerStateForConstraintSystem(override val typeSystemContex
typeVariable: KotlinTypeMarker,
subType: KotlinTypeMarker,
isFromNullabilityConstraint: Boolean = false
): Boolean = with(typeSystemContext) {
): Boolean = with(extensionTypeContext) {
val lowerConstraint = when (typeVariable) {
is SimpleTypeMarker ->
/*
@@ -265,7 +263,7 @@ abstract class TypeCheckerStateForConstraintSystem(override val typeSystemContex
* T? <: Foo <=> T <: Foo && Nothing? <: Foo
* T <: Foo -- leave as is
*/
private fun simplifyUpperConstraint(typeVariable: KotlinTypeMarker, superType: KotlinTypeMarker): Boolean = with(typeSystemContext) {
private fun simplifyUpperConstraint(typeVariable: KotlinTypeMarker, superType: KotlinTypeMarker): Boolean = with(extensionTypeContext) {
val typeVariableLowerBound = typeVariable.lowerBoundIfFlexible()
val simplifiedSuperType = if (typeVariableLowerBound.isDefinitelyNotNullType()) {
superType.withNullability(true)
@@ -285,7 +283,7 @@ abstract class TypeCheckerStateForConstraintSystem(override val typeSystemContex
}
private fun simplifyConstraintForPossibleIntersectionSubType(subType: KotlinTypeMarker, superType: KotlinTypeMarker): Boolean? =
with(typeSystemContext) {
with(extensionTypeContext) {
@Suppress("NAME_SHADOWING")
val subType = subType.lowerBoundIfFlexible()