Remove all type system dependent methods from TypeCheckerState
This commit is contained in:
@@ -566,20 +566,8 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext, Ty
|
|||||||
private fun TypeConstructorMarker.unknownConstructorError(): Nothing {
|
private fun TypeConstructorMarker.unknownConstructorError(): Nothing {
|
||||||
error("Unknown type constructor: ${this::class}")
|
error("Unknown type constructor: ${this::class}")
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
class ConeTypeCheckerState(
|
override fun substitutionSupertypePolicy(type: SimpleTypeMarker): TypeCheckerState.SupertypesPolicy {
|
||||||
override val isErrorTypeEqualsToAnything: Boolean,
|
|
||||||
override val isStubTypeEqualsToAnything: Boolean,
|
|
||||||
override val typeSystemContext: ConeInferenceContext,
|
|
||||||
override val kotlinTypePreparator: ConeTypePreparator = ConeTypePreparator(typeSystemContext.session),
|
|
||||||
) : TypeCheckerState() {
|
|
||||||
override val kotlinTypeRefiner: AbstractTypeRefiner
|
|
||||||
get() = AbstractTypeRefiner.Default
|
|
||||||
|
|
||||||
val session: FirSession = typeSystemContext.session
|
|
||||||
|
|
||||||
override fun substitutionSupertypePolicy(type: SimpleTypeMarker): SupertypesPolicy = with(typeSystemContext) {
|
|
||||||
if (type.argumentsCount() == 0) return LowerIfFlexible
|
if (type.argumentsCount() == 0) return LowerIfFlexible
|
||||||
require(type is ConeKotlinType)
|
require(type is ConeKotlinType)
|
||||||
val declaration = when (type) {
|
val declaration = when (type) {
|
||||||
@@ -607,6 +595,23 @@ class ConeTypeCheckerState(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override val KotlinTypeMarker.isAllowedTypeVariable: Boolean
|
override fun KotlinTypeMarker.isTypeVariableType(): Boolean {
|
||||||
get() = this is ConeKotlinType && this is ConeTypeVariableType
|
return this is ConeTypeVariableType
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ConeTypeCheckerState(
|
||||||
|
override val isErrorTypeEqualsToAnything: Boolean,
|
||||||
|
override val isStubTypeEqualsToAnything: Boolean,
|
||||||
|
override val typeSystemContext: ConeInferenceContext,
|
||||||
|
override val kotlinTypePreparator: ConeTypePreparator = ConeTypePreparator(typeSystemContext.session),
|
||||||
|
) : TypeCheckerState() {
|
||||||
|
override val kotlinTypeRefiner: AbstractTypeRefiner
|
||||||
|
get() = AbstractTypeRefiner.Default
|
||||||
|
|
||||||
|
val session: FirSession = typeSystemContext.session
|
||||||
|
|
||||||
|
|
||||||
|
override val allowedTypeVariable: Boolean
|
||||||
|
get() = true
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -44,7 +44,7 @@ class FirCorrespondingSupertypesCache(private val session: FirSession) : FirSess
|
|||||||
if (type.typeArguments.isEmpty()) return resultTypes
|
if (type.typeArguments.isEmpty()) return resultTypes
|
||||||
|
|
||||||
val capturedType = captureType(type, typeCheckerState.typeSystemContext)
|
val capturedType = captureType(type, typeCheckerState.typeSystemContext)
|
||||||
val substitutionSupertypePolicy = typeCheckerState.substitutionSupertypePolicy(capturedType)
|
val substitutionSupertypePolicy = typeCheckerState.typeSystemContext.substitutionSupertypePolicy(capturedType)
|
||||||
return resultTypes.map {
|
return resultTypes.map {
|
||||||
substitutionSupertypePolicy.transformType(typeCheckerState, it) as ConeClassLikeType
|
substitutionSupertypePolicy.transformType(typeCheckerState, it) as ConeClassLikeType
|
||||||
}
|
}
|
||||||
@@ -97,7 +97,7 @@ class FirCorrespondingSupertypesCache(private val session: FirSession) : FirSess
|
|||||||
TypeCheckerState.SupertypesPolicy.LowerIfFlexible
|
TypeCheckerState.SupertypesPolicy.LowerIfFlexible
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
state.substitutionSupertypePolicy(captured)
|
state.typeSystemContext.substitutionSupertypePolicy(captured)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,22 +22,9 @@ open class IrTypeCheckerState(override val typeSystemContext: IrTypeSystemContex
|
|||||||
|
|
||||||
val irBuiltIns: IrBuiltIns get() = typeSystemContext.irBuiltIns
|
val irBuiltIns: IrBuiltIns get() = typeSystemContext.irBuiltIns
|
||||||
|
|
||||||
override fun substitutionSupertypePolicy(type: SimpleTypeMarker): SupertypesPolicy.DoCustomTransform {
|
|
||||||
require(type is IrSimpleType)
|
|
||||||
val parameters = extractTypeParameters((type.classifier as IrClassSymbol).owner).map { it.symbol }
|
|
||||||
val typeSubstitutor = IrTypeSubstitutor(parameters, type.arguments, irBuiltIns)
|
|
||||||
|
|
||||||
return object : SupertypesPolicy.DoCustomTransform() {
|
|
||||||
override fun transformType(state: TypeCheckerState, type: KotlinTypeMarker): SimpleTypeMarker {
|
|
||||||
require(type is IrType)
|
|
||||||
return typeSubstitutor.substitute(type) as IrSimpleType
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override val isErrorTypeEqualsToAnything get() = false
|
override val isErrorTypeEqualsToAnything get() = false
|
||||||
override val isStubTypeEqualsToAnything get() = false
|
override val isStubTypeEqualsToAnything get() = false
|
||||||
|
|
||||||
override val KotlinTypeMarker.isAllowedTypeVariable: Boolean
|
override val allowedTypeVariable: Boolean
|
||||||
get() = false
|
get() = false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -528,6 +528,23 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon
|
|||||||
override fun SimpleTypeMarker.makeSimpleTypeDefinitelyNotNullOrNotNull(): SimpleTypeMarker {
|
override fun SimpleTypeMarker.makeSimpleTypeDefinitelyNotNullOrNotNull(): SimpleTypeMarker {
|
||||||
error("makeSimpleTypeDefinitelyNotNullOrNotNull is not yet supported in IR")
|
error("makeSimpleTypeDefinitelyNotNullOrNotNull is not yet supported in IR")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun substitutionSupertypePolicy(type: SimpleTypeMarker): TypeCheckerState.SupertypesPolicy {
|
||||||
|
require(type is IrSimpleType)
|
||||||
|
val parameters = extractTypeParameters((type.classifier as IrClassSymbol).owner).map { it.symbol }
|
||||||
|
val typeSubstitutor = IrTypeSubstitutor(parameters, type.arguments, irBuiltIns)
|
||||||
|
|
||||||
|
return object : TypeCheckerState.SupertypesPolicy.DoCustomTransform() {
|
||||||
|
override fun transformType(state: TypeCheckerState, type: KotlinTypeMarker): SimpleTypeMarker {
|
||||||
|
require(type is IrType)
|
||||||
|
return typeSubstitutor.substitute(type) as IrSimpleType
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun KotlinTypeMarker.isTypeVariableType(): Boolean {
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun extractTypeParameters(parent: IrDeclarationParent): List<IrTypeParameter> {
|
fun extractTypeParameters(parent: IrDeclarationParent): List<IrTypeParameter> {
|
||||||
|
|||||||
-4
@@ -250,10 +250,6 @@ class ConstraintInjector(
|
|||||||
|
|
||||||
val baseState: TypeCheckerState = newTypeCheckerState(isErrorTypeEqualsToAnything, isStubTypeEqualsToAnything)
|
val baseState: TypeCheckerState = newTypeCheckerState(isErrorTypeEqualsToAnything, isStubTypeEqualsToAnything)
|
||||||
|
|
||||||
override fun substitutionSupertypePolicy(type: SimpleTypeMarker): SupertypesPolicy {
|
|
||||||
return baseState.substitutionSupertypePolicy(type)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun runIsSubtypeOf(
|
fun runIsSubtypeOf(
|
||||||
lowerType: KotlinTypeMarker,
|
lowerType: KotlinTypeMarker,
|
||||||
upperType: KotlinTypeMarker,
|
upperType: KotlinTypeMarker,
|
||||||
|
|||||||
+1
-1
@@ -13,7 +13,7 @@ import org.jetbrains.kotlin.types.model.*
|
|||||||
abstract class TypeCheckerStateForConstraintSystem(override val typeSystemContext: TypeSystemInferenceExtensionContext) :
|
abstract class TypeCheckerStateForConstraintSystem(override val typeSystemContext: TypeSystemInferenceExtensionContext) :
|
||||||
TypeCheckerState() {
|
TypeCheckerState() {
|
||||||
|
|
||||||
override val KotlinTypeMarker.isAllowedTypeVariable: Boolean
|
override val allowedTypeVariable: Boolean
|
||||||
get() = false
|
get() = false
|
||||||
|
|
||||||
override val isErrorTypeEqualsToAnything: Boolean
|
override val isErrorTypeEqualsToAnything: Boolean
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.types
|
package org.jetbrains.kotlin.types
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.descriptors.containerRelation
|
||||||
import org.jetbrains.kotlin.types.TypeCheckerState.LowerCapturedTypePolicy.*
|
import org.jetbrains.kotlin.types.TypeCheckerState.LowerCapturedTypePolicy.*
|
||||||
import org.jetbrains.kotlin.types.TypeCheckerState.SupertypesPolicy
|
import org.jetbrains.kotlin.types.TypeCheckerState.SupertypesPolicy
|
||||||
import org.jetbrains.kotlin.types.model.*
|
import org.jetbrains.kotlin.types.model.*
|
||||||
@@ -27,7 +28,7 @@ abstract class TypeCheckerState {
|
|||||||
|
|
||||||
abstract val typeSystemContext: TypeSystemContext
|
abstract val typeSystemContext: TypeSystemContext
|
||||||
|
|
||||||
abstract fun substitutionSupertypePolicy(type: SimpleTypeMarker): SupertypesPolicy
|
abstract val allowedTypeVariable: Boolean
|
||||||
|
|
||||||
@OptIn(TypeRefinement::class)
|
@OptIn(TypeRefinement::class)
|
||||||
fun refineType(type: KotlinTypeMarker): KotlinTypeMarker {
|
fun refineType(type: KotlinTypeMarker): KotlinTypeMarker {
|
||||||
@@ -157,10 +158,9 @@ abstract class TypeCheckerState {
|
|||||||
abstract class DoCustomTransform : SupertypesPolicy()
|
abstract class DoCustomTransform : SupertypesPolicy()
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract val KotlinTypeMarker.isAllowedTypeVariable: Boolean
|
fun isAllowedTypeVariable(type: KotlinTypeMarker): Boolean {
|
||||||
|
return allowedTypeVariable && with(typeSystemContext) { type.isTypeVariableType() }
|
||||||
@JvmName("isAllowedTypeVariableBridge")
|
}
|
||||||
fun isAllowedTypeVariable(type: KotlinTypeMarker): Boolean = type.isAllowedTypeVariable
|
|
||||||
}
|
}
|
||||||
|
|
||||||
object AbstractTypeChecker {
|
object AbstractTypeChecker {
|
||||||
@@ -596,7 +596,7 @@ object AbstractTypeChecker {
|
|||||||
SupertypesPolicy.LowerIfFlexible
|
SupertypesPolicy.LowerIfFlexible
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
state.substitutionSupertypePolicy(current)
|
state.typeSystemContext.substitutionSupertypePolicy(current)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -461,6 +461,10 @@ interface TypeSystemContext : TypeSystemOptimizationContext {
|
|||||||
fun SimpleTypeMarker.isPrimitiveType(): Boolean
|
fun SimpleTypeMarker.isPrimitiveType(): Boolean
|
||||||
|
|
||||||
fun KotlinTypeMarker.getAnnotations(): List<AnnotationMarker>
|
fun KotlinTypeMarker.getAnnotations(): List<AnnotationMarker>
|
||||||
|
|
||||||
|
fun substitutionSupertypePolicy(type: SimpleTypeMarker): TypeCheckerState.SupertypesPolicy
|
||||||
|
|
||||||
|
fun KotlinTypeMarker.isTypeVariableType(): Boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class CaptureStatus {
|
enum class CaptureStatus {
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ class OverridingUtilTypeSystemContext(
|
|||||||
return ClassicTypeCheckerState(
|
return ClassicTypeCheckerState(
|
||||||
errorTypesEqualToAnything,
|
errorTypesEqualToAnything,
|
||||||
stubTypesEqualToAnything,
|
stubTypesEqualToAnything,
|
||||||
allowedTypeVariable = true,
|
|
||||||
kotlinTypeRefiner,
|
kotlinTypeRefiner,
|
||||||
typeSystemContext = this
|
typeSystemContext = this
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ import org.jetbrains.kotlin.types.model.SimpleTypeMarker
|
|||||||
open class ClassicTypeCheckerState(
|
open class ClassicTypeCheckerState(
|
||||||
val errorTypeEqualsToAnything: Boolean,
|
val errorTypeEqualsToAnything: Boolean,
|
||||||
val stubTypeEqualsToAnything: Boolean = true,
|
val stubTypeEqualsToAnything: Boolean = true,
|
||||||
val allowedTypeVariable: Boolean = true,
|
|
||||||
override val kotlinTypeRefiner: KotlinTypeRefiner = KotlinTypeRefiner.Default,
|
override val kotlinTypeRefiner: KotlinTypeRefiner = KotlinTypeRefiner.Default,
|
||||||
override val kotlinTypePreparator: KotlinTypePreparator = KotlinTypePreparator.Default,
|
override val kotlinTypePreparator: KotlinTypePreparator = KotlinTypePreparator.Default,
|
||||||
override val typeSystemContext: ClassicTypeSystemContext = SimpleClassicTypeSystemContext
|
override val typeSystemContext: ClassicTypeSystemContext = SimpleClassicTypeSystemContext
|
||||||
@@ -34,29 +33,6 @@ open class ClassicTypeCheckerState(
|
|||||||
override val isStubTypeEqualsToAnything: Boolean
|
override val isStubTypeEqualsToAnything: Boolean
|
||||||
get() = stubTypeEqualsToAnything
|
get() = stubTypeEqualsToAnything
|
||||||
|
|
||||||
override fun substitutionSupertypePolicy(type: SimpleTypeMarker): SupertypesPolicy.DoCustomTransform {
|
override val allowedTypeVariable: Boolean
|
||||||
return typeSystemContext.classicSubstitutionSupertypePolicy(type)
|
get() = true
|
||||||
}
|
|
||||||
|
|
||||||
override val KotlinTypeMarker.isAllowedTypeVariable: Boolean get() = this is UnwrappedType && allowedTypeVariable && constructor is NewTypeVariableConstructor
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
fun ClassicTypeSystemContext.classicSubstitutionSupertypePolicy(type: SimpleTypeMarker): SupertypesPolicy.DoCustomTransform {
|
|
||||||
require(type is SimpleType, type::errorMessage)
|
|
||||||
val substitutor = TypeConstructorSubstitution.create(type).buildSubstitutor()
|
|
||||||
|
|
||||||
return object : SupertypesPolicy.DoCustomTransform() {
|
|
||||||
override fun transformType(state: TypeCheckerState, type: KotlinTypeMarker): SimpleTypeMarker {
|
|
||||||
return substitutor.safeSubstitute(
|
|
||||||
type.lowerBoundIfFlexible() as KotlinType,
|
|
||||||
Variance.INVARIANT
|
|
||||||
).asSimpleType()!!
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun Any.errorMessage(): String {
|
|
||||||
return "ClassicTypeCheckerContext couldn't handle ${this::class} $this"
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -788,6 +788,24 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy
|
|||||||
} else {
|
} else {
|
||||||
createFlexibleType(this, this.withNullability(true))
|
createFlexibleType(this, this.withNullability(true))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun substitutionSupertypePolicy(type: SimpleTypeMarker): TypeCheckerState.SupertypesPolicy {
|
||||||
|
require(type is SimpleType, type::errorMessage)
|
||||||
|
val substitutor = TypeConstructorSubstitution.create(type).buildSubstitutor()
|
||||||
|
|
||||||
|
return object : TypeCheckerState.SupertypesPolicy.DoCustomTransform() {
|
||||||
|
override fun transformType(state: TypeCheckerState, type: KotlinTypeMarker): SimpleTypeMarker {
|
||||||
|
return substitutor.safeSubstitute(
|
||||||
|
type.lowerBoundIfFlexible() as KotlinType,
|
||||||
|
Variance.INVARIANT
|
||||||
|
).asSimpleType()!!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun KotlinTypeMarker.isTypeVariableType(): Boolean {
|
||||||
|
return this is UnwrappedType && constructor is NewTypeVariableConstructor
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun TypeVariance.convertVariance(): Variance {
|
fun TypeVariance.convertVariance(): Variance {
|
||||||
|
|||||||
Reference in New Issue
Block a user