Decouple TypeCheckerContext and TypeSystemContext
This commit is contained in:
committed by
TeamCityServer
parent
53a7dc1126
commit
3909e3c54c
@@ -31,7 +31,7 @@ import org.jetbrains.kotlin.fir.scopes.processOverriddenFunctions
|
||||
import org.jetbrains.kotlin.fir.scopes.unsubstitutedScope
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol
|
||||
import org.jetbrains.kotlin.fir.typeCheckerContext
|
||||
import org.jetbrains.kotlin.fir.typeContext
|
||||
import org.jetbrains.kotlin.fir.types.*
|
||||
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
@@ -317,4 +317,4 @@ val FirFunctionCall.isIterator
|
||||
internal fun throwableClassLikeType(session: FirSession) = session.builtinTypes.throwableType.type
|
||||
|
||||
fun ConeKotlinType.isSubtypeOfThrowable(session: FirSession) =
|
||||
throwableClassLikeType(session).isSupertypeOf(session.typeCheckerContext, this.fullyExpandedType(session))
|
||||
throwableClassLikeType(session).isSupertypeOf(session.typeContext, this.fullyExpandedType(session))
|
||||
|
||||
+12
-18
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol
|
||||
import org.jetbrains.kotlin.fir.typeContext
|
||||
import org.jetbrains.kotlin.fir.types.*
|
||||
import org.jetbrains.kotlin.types.AbstractTypeChecker
|
||||
import org.jetbrains.kotlin.types.AbstractTypeCheckerContext
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.min
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
|
||||
|
||||
@@ -63,18 +62,13 @@ object FirUpperBoundViolatedChecker : FirQualifiedAccessChecker() {
|
||||
parameterPairs.mapValues { it.value.coneType }
|
||||
)
|
||||
|
||||
val typeCheckerContext = context.session.typeContext.newBaseTypeCheckerContext(
|
||||
errorTypesEqualToAnything = false,
|
||||
stubTypesEqualToAnything = false
|
||||
)
|
||||
|
||||
parameterPairs.forEach { (proto, actual) ->
|
||||
if (actual.source == null) {
|
||||
// inferred types don't report INAPPLICABLE_CANDIDATE for type aliases!
|
||||
return@forEach
|
||||
}
|
||||
|
||||
if (!satisfiesBounds(proto, actual.coneType, substitutor, typeCheckerContext)) {
|
||||
if (!satisfiesBounds(proto, actual.coneType, substitutor, context.session.typeContext)) {
|
||||
reporter.reportOn(actual.source, proto, actual.coneType, context)
|
||||
return
|
||||
}
|
||||
@@ -82,7 +76,7 @@ object FirUpperBoundViolatedChecker : FirQualifiedAccessChecker() {
|
||||
// we must analyze nested things like
|
||||
// S<S<K, L>, T<K, L>>()
|
||||
actual.coneType.safeAs<ConeClassLikeType>()?.let {
|
||||
val errorOccurred = analyzeTypeParameters(it, context, reporter, typeCheckerContext, actual.source)
|
||||
val errorOccurred = analyzeTypeParameters(it, context, reporter, context.session.typeContext, actual.source)
|
||||
|
||||
if (errorOccurred) {
|
||||
return
|
||||
@@ -99,14 +93,14 @@ object FirUpperBoundViolatedChecker : FirQualifiedAccessChecker() {
|
||||
// typealias A<G> = B<List<G>>
|
||||
// val a = A<Int>()
|
||||
when (calleeFir) {
|
||||
is FirConstructor -> analyzeConstructorCall(expression, substitutor, typeCheckerContext, reporter, context)
|
||||
is FirConstructor -> analyzeConstructorCall(expression, substitutor, context.session.typeContext, reporter, context)
|
||||
}
|
||||
}
|
||||
|
||||
private fun analyzeConstructorCall(
|
||||
functionCall: FirQualifiedAccessExpression,
|
||||
callSiteSubstitutor: ConeSubstitutor,
|
||||
typeCheckerContext: AbstractTypeCheckerContext,
|
||||
typeSystemContext: ConeTypeContext,
|
||||
reporter: DiagnosticReporter,
|
||||
context: CheckerContext
|
||||
) {
|
||||
@@ -157,7 +151,7 @@ object FirUpperBoundViolatedChecker : FirQualifiedAccessChecker() {
|
||||
|
||||
constructorsParameterPairs.forEach { (proto, actual) ->
|
||||
// just in case
|
||||
var intersection = typeCheckerContext.intersectTypes(
|
||||
var intersection = typeSystemContext.intersectTypes(
|
||||
proto.fir.bounds.map { it.coneType }
|
||||
).safeAs<ConeKotlinType>() ?: return@forEach
|
||||
|
||||
@@ -167,7 +161,7 @@ object FirUpperBoundViolatedChecker : FirQualifiedAccessChecker() {
|
||||
// substitute Int for G from
|
||||
// the example above
|
||||
val target = callSiteSubstitutor.substituteOrSelf(actual)
|
||||
val satisfiesBounds = AbstractTypeChecker.isSubtypeOf(typeCheckerContext, target, intersection)
|
||||
val satisfiesBounds = AbstractTypeChecker.isSubtypeOf(typeSystemContext, target, intersection)
|
||||
|
||||
if (!satisfiesBounds) {
|
||||
reporter.reportOn(functionCall.source, proto, actual, context)
|
||||
@@ -187,7 +181,7 @@ object FirUpperBoundViolatedChecker : FirQualifiedAccessChecker() {
|
||||
type: ConeClassLikeType,
|
||||
context: CheckerContext,
|
||||
reporter: DiagnosticReporter,
|
||||
typeCheckerContext: AbstractTypeCheckerContext,
|
||||
typeSystemContext: ConeTypeContext,
|
||||
reportTarget: FirSourceElement?
|
||||
): Boolean {
|
||||
val prototypeClass = type.lookupTag.toSymbol(context.session)
|
||||
@@ -218,13 +212,13 @@ object FirUpperBoundViolatedChecker : FirQualifiedAccessChecker() {
|
||||
)
|
||||
|
||||
parameterPairs.forEach { (proto, actual) ->
|
||||
if (!satisfiesBounds(proto, actual.type, substitutor, typeCheckerContext)) {
|
||||
if (!satisfiesBounds(proto, actual.type, substitutor, typeSystemContext)) {
|
||||
// should report on the parameter instead!
|
||||
reporter.reportOn(reportTarget, proto, actual, context)
|
||||
return true
|
||||
}
|
||||
|
||||
val errorOccurred = analyzeTypeParameters(actual, context, reporter, typeCheckerContext, reportTarget)
|
||||
val errorOccurred = analyzeTypeParameters(actual, context, reporter, typeSystemContext, reportTarget)
|
||||
|
||||
if (errorOccurred) {
|
||||
return true
|
||||
@@ -242,14 +236,14 @@ object FirUpperBoundViolatedChecker : FirQualifiedAccessChecker() {
|
||||
prototypeSymbol: FirTypeParameterSymbol,
|
||||
target: ConeKotlinType,
|
||||
substitutor: ConeSubstitutor,
|
||||
typeCheckerContext: AbstractTypeCheckerContext
|
||||
typeSystemContext: ConeTypeContext
|
||||
): Boolean {
|
||||
var intersection = typeCheckerContext.intersectTypes(
|
||||
var intersection = typeSystemContext.intersectTypes(
|
||||
prototypeSymbol.fir.bounds.map { it.coneType }
|
||||
).safeAs<ConeKotlinType>() ?: return true
|
||||
|
||||
intersection = substitutor.substituteOrSelf(intersection)
|
||||
return AbstractTypeChecker.isSubtypeOf(typeCheckerContext, target, intersection)
|
||||
return AbstractTypeChecker.isSubtypeOf(typeSystemContext, target, intersection, stubTypesEqualToAnything = false)
|
||||
}
|
||||
|
||||
private fun DiagnosticReporter.reportOn(
|
||||
|
||||
+3
-2
@@ -30,6 +30,9 @@ import org.jetbrains.kotlin.fir.types.FirCorrespondingSupertypesCache
|
||||
|
||||
@OptIn(SessionConfiguration::class)
|
||||
fun FirSession.registerCommonComponents(languageVersionSettings: LanguageVersionSettings) {
|
||||
register(FirLanguageSettingsComponent::class, FirLanguageSettingsComponent(languageVersionSettings))
|
||||
register(InferenceComponents::class, InferenceComponents(this))
|
||||
|
||||
register(FirDeclaredMemberScopeProvider::class, FirDeclaredMemberScopeProvider())
|
||||
register(FirCorrespondingSupertypesCache::class, FirCorrespondingSupertypesCache(this))
|
||||
register(FirDefaultParametersResolver::class, FirDefaultParametersResolver())
|
||||
@@ -38,8 +41,6 @@ fun FirSession.registerCommonComponents(languageVersionSettings: LanguageVersion
|
||||
register(FirRegisteredPluginAnnotations::class, FirRegisteredPluginAnnotations.create(this))
|
||||
register(FirPredicateBasedProvider::class, FirPredicateBasedProvider.create(this))
|
||||
register(GeneratedClassIndex::class, GeneratedClassIndex.create())
|
||||
register(FirLanguageSettingsComponent::class, FirLanguageSettingsComponent(languageVersionSettings))
|
||||
register(InferenceComponents::class, InferenceComponents(this))
|
||||
}
|
||||
|
||||
@OptIn(SessionConfiguration::class)
|
||||
|
||||
+1
-1
@@ -636,7 +636,7 @@ class CallAndReferenceGenerator(
|
||||
// If the type of the argument is already an explicitly subtype of the type of the parameter, we don't need SAM conversion.
|
||||
if (argument.typeRef !is FirResolvedTypeRef ||
|
||||
AbstractTypeChecker.isSubtypeOf(
|
||||
session.inferenceComponents.ctx,
|
||||
session.inferenceComponents.ctx.newBaseTypeCheckerContext(errorTypesEqualToAnything = false, stubTypesEqualToAnything = true),
|
||||
argument.typeRef.coneType,
|
||||
parameter.returnTypeRef.coneType,
|
||||
isFromNullabilityConstraint = true
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.fir.resolve.toSymbol
|
||||
import org.jetbrains.kotlin.fir.resolve.transformers.resolveSupertypesInTheAir
|
||||
import org.jetbrains.kotlin.fir.symbols.StandardClassIds
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
|
||||
import org.jetbrains.kotlin.fir.typeContext
|
||||
import org.jetbrains.kotlin.fir.types.*
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
@@ -241,9 +242,7 @@ private fun ConeClassLikeType.mapToCanonicalNoExpansionString(session: FirSessio
|
||||
} + "[]"
|
||||
}
|
||||
|
||||
val context = ConeTypeCheckerContext(isErrorTypeEqualsToAnything = false, isStubTypeEqualsToAnything = true, session = session)
|
||||
|
||||
with(context) {
|
||||
with(session.typeContext) {
|
||||
val typeConstructor = typeConstructor()
|
||||
typeConstructor.getPrimitiveType()?.let { return JvmPrimitiveType.get(it).wrapperFqName.asString() }
|
||||
typeConstructor.getPrimitiveArrayType()?.let { return JvmPrimitiveType.get(it).javaKeywordName + "[]" }
|
||||
|
||||
@@ -6,21 +6,19 @@
|
||||
package org.jetbrains.kotlin.fir
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.Visibility
|
||||
import org.jetbrains.kotlin.fir.declarations.*
|
||||
import org.jetbrains.kotlin.fir.declarations.FirClass
|
||||
import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction
|
||||
import org.jetbrains.kotlin.fir.declarations.visibility
|
||||
import org.jetbrains.kotlin.fir.resolve.ScopeSession
|
||||
import org.jetbrains.kotlin.fir.resolve.inference.inferenceComponents
|
||||
import org.jetbrains.kotlin.fir.scopes.ProcessorAction
|
||||
import org.jetbrains.kotlin.fir.scopes.processOverriddenFunctions
|
||||
import org.jetbrains.kotlin.fir.scopes.unsubstitutedScope
|
||||
import org.jetbrains.kotlin.fir.types.ConeInferenceContext
|
||||
import org.jetbrains.kotlin.fir.types.ConeTypeCheckerContext
|
||||
|
||||
val FirSession.typeContext: ConeInferenceContext
|
||||
get() = inferenceComponents.ctx
|
||||
|
||||
val FirSession.typeCheckerContext: ConeTypeCheckerContext
|
||||
get() = inferenceComponents.ctx
|
||||
|
||||
/**
|
||||
* Returns the list of functions that overridden by given
|
||||
*/
|
||||
|
||||
@@ -17,7 +17,6 @@ import org.jetbrains.kotlin.fir.resolve.transformers.ensureResolvedTypeDeclarati
|
||||
import org.jetbrains.kotlin.fir.returnExpressions
|
||||
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag
|
||||
import org.jetbrains.kotlin.fir.symbols.StandardClassIds
|
||||
import org.jetbrains.kotlin.fir.typeCheckerContext
|
||||
import org.jetbrains.kotlin.fir.typeContext
|
||||
import org.jetbrains.kotlin.fir.types.*
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
@@ -26,6 +25,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.addSubtypeConstraintIfCompat
|
||||
import org.jetbrains.kotlin.resolve.calls.inference.model.SimpleConstraintSystemConstraintPosition
|
||||
import org.jetbrains.kotlin.types.AbstractTypeChecker
|
||||
import org.jetbrains.kotlin.types.model.CaptureStatus
|
||||
import org.jetbrains.kotlin.types.model.TypeSystemCommonSuperTypesContext
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.runIf
|
||||
|
||||
fun Candidate.resolveArgumentExpression(
|
||||
@@ -416,7 +416,10 @@ fun FirExpression.isFunctional(
|
||||
val returnTypeCompatible =
|
||||
expectedReturnType is ConeTypeParameterType ||
|
||||
AbstractTypeChecker.isSubtypeOf(
|
||||
session.inferenceComponents.ctx,
|
||||
session.inferenceComponents.ctx.newBaseTypeCheckerContext(
|
||||
errorTypesEqualToAnything = false,
|
||||
stubTypesEqualToAnything = true
|
||||
),
|
||||
invokeSymbol.fir.returnTypeRef.coneType,
|
||||
expectedReturnType,
|
||||
isFromNullabilityConstraint = false
|
||||
@@ -433,7 +436,10 @@ fun FirExpression.isFunctional(
|
||||
val expectedParameterType = expectedParameter!!.lowerBoundIfFlexible()
|
||||
expectedParameterType is ConeTypeParameterType ||
|
||||
AbstractTypeChecker.isSubtypeOf(
|
||||
session.inferenceComponents.ctx,
|
||||
session.inferenceComponents.ctx.newBaseTypeCheckerContext(
|
||||
errorTypesEqualToAnything = false,
|
||||
stubTypesEqualToAnything = true
|
||||
),
|
||||
invokeParameter.returnTypeRef.coneType,
|
||||
expectedParameterType,
|
||||
isFromNullabilityConstraint = false
|
||||
@@ -488,7 +494,7 @@ internal fun captureFromTypeParameterUpperBoundIfNeeded(
|
||||
val simplifiedArgumentType = argumentType.lowerBoundIfFlexible() as? ConeTypeParameterType ?: return argumentType
|
||||
val typeParameter = simplifiedArgumentType.lookupTag.typeParameterSymbol.fir
|
||||
|
||||
val context = session.typeCheckerContext
|
||||
val context = session.typeContext
|
||||
|
||||
val chosenSupertype = typeParameter.bounds.map { it.coneType }
|
||||
.singleOrNull { it.hasSupertypeWithGivenClassId(expectedTypeClassId, context) } ?: return argumentType
|
||||
@@ -501,7 +507,7 @@ internal fun captureFromTypeParameterUpperBoundIfNeeded(
|
||||
}
|
||||
}
|
||||
|
||||
private fun ConeKotlinType.hasSupertypeWithGivenClassId(classId: ClassId, context: ConeTypeCheckerContext): Boolean {
|
||||
private fun ConeKotlinType.hasSupertypeWithGivenClassId(classId: ClassId, context: TypeSystemCommonSuperTypesContext): Boolean {
|
||||
return with(context) {
|
||||
anySuperTypeConstructor {
|
||||
it is ConeClassLikeLookupTag && it.classId == classId
|
||||
|
||||
+4
-2
@@ -7,14 +7,16 @@ package org.jetbrains.kotlin.fir.resolve.inference
|
||||
|
||||
import org.jetbrains.kotlin.fir.*
|
||||
import org.jetbrains.kotlin.fir.types.ConeInferenceContext
|
||||
import org.jetbrains.kotlin.fir.types.ConeTypeCheckerContext
|
||||
import org.jetbrains.kotlin.resolve.calls.inference.components.*
|
||||
import org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImpl
|
||||
import org.jetbrains.kotlin.types.AbstractTypeApproximator
|
||||
|
||||
@NoMutableState
|
||||
class InferenceComponents(val session: FirSession) : FirSessionComponent {
|
||||
val ctx: ConeTypeCheckerContext = ConeTypeCheckerContext(isErrorTypeEqualsToAnything = false, isStubTypeEqualsToAnything = true, session)
|
||||
val ctx: ConeInferenceContext = object : ConeInferenceContext {
|
||||
override val session: FirSession
|
||||
get() = this@InferenceComponents.session
|
||||
}
|
||||
|
||||
val approximator: AbstractTypeApproximator = object : AbstractTypeApproximator(ctx) {}
|
||||
val trivialConstraintTypeInferenceOracle = TrivialConstraintTypeInferenceOracle.create(ctx)
|
||||
|
||||
+2
-1
@@ -496,7 +496,8 @@ open class FirExpressionsResolveTransformer(transformer: FirBodyResolveTransform
|
||||
if (baseType !is ConeClassLikeType) return this
|
||||
val baseFirClass = baseType.lookupTag.toSymbol(session)?.fir ?: return this
|
||||
|
||||
val newArguments = if (AbstractTypeChecker.isSubtypeOfClass(session.typeCheckerContext, baseType.lookupTag, type.lookupTag)) {
|
||||
val newArguments = if (AbstractTypeChecker.isSubtypeOfClass(
|
||||
session.typeContext.newBaseTypeCheckerContext(errorTypesEqualToAnything = false, stubTypesEqualToAnything = true), baseType.lookupTag, type.lookupTag)) {
|
||||
// If actual type of declaration is more specific than bare type then we should just find
|
||||
// corresponding supertype with proper arguments
|
||||
with(session.typeContext) {
|
||||
|
||||
+4
-3
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor
|
||||
import org.jetbrains.kotlin.fir.scopes.*
|
||||
import org.jetbrains.kotlin.fir.symbols.CallableId
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.*
|
||||
import org.jetbrains.kotlin.fir.typeContext
|
||||
import org.jetbrains.kotlin.fir.types.*
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.types.AbstractTypeChecker
|
||||
@@ -33,7 +34,7 @@ class FirTypeIntersectionScope private constructor(
|
||||
private val absentProperties: MutableSet<Name> = mutableSetOf()
|
||||
private val absentClassifiers: MutableSet<Name> = mutableSetOf()
|
||||
|
||||
private val typeContext = ConeTypeCheckerContext(isErrorTypeEqualsToAnything = false, isStubTypeEqualsToAnything = false, session)
|
||||
private val typeCheckerContext = session.typeContext.newBaseTypeCheckerContext(false, false)
|
||||
|
||||
private val overriddenSymbols: MutableMap<FirCallableSymbol<*>, Collection<MemberWithBaseScope<out FirCallableSymbol<*>>>> =
|
||||
mutableMapOf()
|
||||
@@ -357,7 +358,7 @@ class FirTypeIntersectionScope private constructor(
|
||||
require(bFir is FirProperty) { "b is " + b.javaClass }
|
||||
// TODO: if (!OverridingUtil.isAccessorMoreSpecific(pa.getSetter(), pb.getSetter())) return false
|
||||
return if (aFir.isVar && bFir.isVar) {
|
||||
AbstractTypeChecker.equalTypes(typeContext as AbstractTypeCheckerContext, aReturnType, bReturnType)
|
||||
AbstractTypeChecker.equalTypes(typeCheckerContext as AbstractTypeCheckerContext, aReturnType, bReturnType)
|
||||
} else { // both vals or var vs val: val can't be more specific then var
|
||||
!(!aFir.isVar && bFir.isVar) && isTypeMoreSpecific(aReturnType, bReturnType)
|
||||
}
|
||||
@@ -366,7 +367,7 @@ class FirTypeIntersectionScope private constructor(
|
||||
}
|
||||
|
||||
private fun isTypeMoreSpecific(a: ConeKotlinType, b: ConeKotlinType): Boolean =
|
||||
AbstractTypeChecker.isSubtypeOf(typeContext as AbstractTypeCheckerContext, a, b)
|
||||
AbstractTypeChecker.isSubtypeOf(typeCheckerContext as AbstractTypeCheckerContext, a, b)
|
||||
|
||||
private fun <D : FirCallableSymbol<*>> findMemberWithMaxVisibility(members: Collection<MemberWithBaseScope<D>>): MemberWithBaseScope<D> {
|
||||
assert(members.isNotEmpty())
|
||||
|
||||
@@ -97,8 +97,8 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo
|
||||
override fun newBaseTypeCheckerContext(
|
||||
errorTypesEqualToAnything: Boolean,
|
||||
stubTypesEqualToAnything: Boolean
|
||||
): AbstractTypeCheckerContext =
|
||||
ConeTypeCheckerContext(errorTypesEqualToAnything, stubTypesEqualToAnything, session)
|
||||
): ConeTypeCheckerContext =
|
||||
ConeTypeCheckerContext(errorTypesEqualToAnything, stubTypesEqualToAnything, this)
|
||||
|
||||
override fun KotlinTypeMarker.canHaveUndefinedNullability(): Boolean {
|
||||
require(this is ConeKotlinType)
|
||||
@@ -483,4 +483,12 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo
|
||||
return session.symbolProvider.getClassLikeSymbolByFqName(classId)?.toLookupTag()
|
||||
?: error("Can't find KFunction type")
|
||||
}
|
||||
|
||||
override fun createTypeWithAlternativeForIntersectionResult(
|
||||
firstCandidate: KotlinTypeMarker,
|
||||
secondCandidate: KotlinTypeMarker
|
||||
): KotlinTypeMarker {
|
||||
// TODO
|
||||
return firstCandidate
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.FqNameUnsafe
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.types.AbstractTypeCheckerContext
|
||||
import org.jetbrains.kotlin.types.AbstractTypeCheckerContext.SupertypesPolicy.*
|
||||
import org.jetbrains.kotlin.types.TypeSystemCommonBackendContext
|
||||
import org.jetbrains.kotlin.types.model.*
|
||||
|
||||
@@ -540,10 +541,13 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext, Ty
|
||||
class ConeTypeCheckerContext(
|
||||
override val isErrorTypeEqualsToAnything: Boolean,
|
||||
override val isStubTypeEqualsToAnything: Boolean,
|
||||
override val session: FirSession
|
||||
) : AbstractTypeCheckerContext(), ConeInferenceContext {
|
||||
override fun substitutionSupertypePolicy(type: SimpleTypeMarker): SupertypesPolicy {
|
||||
if (type.argumentsCount() == 0) return SupertypesPolicy.LowerIfFlexible
|
||||
override val typeSystemContext: ConeInferenceContext
|
||||
) : AbstractTypeCheckerContext() {
|
||||
|
||||
val session: FirSession = typeSystemContext.session
|
||||
|
||||
override fun substitutionSupertypePolicy(type: SimpleTypeMarker): SupertypesPolicy = with(typeSystemContext) {
|
||||
if (type.argumentsCount() == 0) return LowerIfFlexible
|
||||
require(type is ConeKotlinType)
|
||||
val declaration = when (type) {
|
||||
is ConeClassLikeType -> type.lookupTag.toSymbol(session)?.firUnsafe<FirClassLikeDeclaration<*>>()
|
||||
@@ -560,7 +564,7 @@ class ConeTypeCheckerContext(
|
||||
} else {
|
||||
ConeSubstitutor.Empty
|
||||
}
|
||||
return object : SupertypesPolicy.DoCustomTransform() {
|
||||
return object : DoCustomTransform() {
|
||||
override fun transformType(context: AbstractTypeCheckerContext, type: KotlinTypeMarker): SimpleTypeMarker {
|
||||
val lowerBound = type.lowerBoundIfFlexible()
|
||||
require(lowerBound is ConeKotlinType)
|
||||
@@ -570,35 +574,10 @@ class ConeTypeCheckerContext(
|
||||
}
|
||||
}
|
||||
|
||||
override fun areEqualTypeConstructors(c1: TypeConstructorMarker, c2: TypeConstructorMarker): Boolean {
|
||||
return c1 == c2
|
||||
}
|
||||
|
||||
override fun prepareType(type: KotlinTypeMarker): KotlinTypeMarker {
|
||||
return super<ConeInferenceContext>.prepareType(type)
|
||||
}
|
||||
|
||||
override fun refineType(type: KotlinTypeMarker): KotlinTypeMarker {
|
||||
return prepareType(type)
|
||||
return typeSystemContext.prepareType(type)
|
||||
}
|
||||
|
||||
override val KotlinTypeMarker.isAllowedTypeVariable: Boolean
|
||||
get() = this is ConeKotlinType && this is ConeTypeVariableType
|
||||
|
||||
override fun newBaseTypeCheckerContext(
|
||||
errorTypesEqualToAnything: Boolean,
|
||||
stubTypesEqualToAnything: Boolean
|
||||
): AbstractTypeCheckerContext =
|
||||
if (this.isErrorTypeEqualsToAnything == errorTypesEqualToAnything && this.isStubTypeEqualsToAnything == stubTypesEqualToAnything)
|
||||
this
|
||||
else
|
||||
ConeTypeCheckerContext(errorTypesEqualToAnything, stubTypesEqualToAnything, session)
|
||||
|
||||
override fun createTypeWithAlternativeForIntersectionResult(
|
||||
firstCandidate: KotlinTypeMarker,
|
||||
secondCandidate: KotlinTypeMarker
|
||||
): KotlinTypeMarker {
|
||||
// TODO
|
||||
return firstCandidate
|
||||
}
|
||||
}
|
||||
|
||||
+14
-7
@@ -13,10 +13,12 @@ import org.jetbrains.kotlin.fir.declarations.FirTypeParameterRefsOwner
|
||||
import org.jetbrains.kotlin.fir.resolve.toSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirClassLikeSymbol
|
||||
import org.jetbrains.kotlin.fir.typeContext
|
||||
import org.jetbrains.kotlin.types.AbstractTypeCheckerContext
|
||||
import org.jetbrains.kotlin.types.model.CaptureStatus
|
||||
import org.jetbrains.kotlin.types.model.SimpleTypeMarker
|
||||
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
|
||||
import org.jetbrains.kotlin.types.model.TypeSystemContext
|
||||
|
||||
@ThreadSafeMutableState
|
||||
class FirCorrespondingSupertypesCache(private val session: FirSession) : FirSessionComponent {
|
||||
@@ -28,9 +30,13 @@ class FirCorrespondingSupertypesCache(private val session: FirSession) : FirSess
|
||||
): List<ConeClassLikeType>? {
|
||||
if (type !is ConeClassLikeType || supertypeConstructor !is ConeClassLikeLookupTag) return null
|
||||
|
||||
val context = ConeTypeCheckerContext(isErrorTypeEqualsToAnything = false, isStubTypeEqualsToAnything = true, session = session)
|
||||
val context = session.typeContext.newBaseTypeCheckerContext(
|
||||
errorTypesEqualToAnything = false,
|
||||
stubTypesEqualToAnything = true
|
||||
)
|
||||
|
||||
val lookupTag = type.lookupTag
|
||||
if (lookupTag == supertypeConstructor) return listOf(captureType(type, context))
|
||||
if (lookupTag == supertypeConstructor) return listOf(captureType(type, context.typeSystemContext))
|
||||
if (lookupTag !in cache) {
|
||||
cache[lookupTag] = computeSupertypesMap(lookupTag, context)
|
||||
}
|
||||
@@ -38,15 +44,15 @@ class FirCorrespondingSupertypesCache(private val session: FirSession) : FirSess
|
||||
val resultTypes = cache[lookupTag]?.getOrDefault(supertypeConstructor, emptyList()) ?: return null
|
||||
if (type.typeArguments.isEmpty()) return resultTypes
|
||||
|
||||
val capturedType = captureType(type, context)
|
||||
val capturedType = captureType(type, context.typeSystemContext)
|
||||
val substitutionSupertypePolicy = context.substitutionSupertypePolicy(capturedType)
|
||||
return resultTypes.map {
|
||||
substitutionSupertypePolicy.transformType(context, it) as ConeClassLikeType
|
||||
}
|
||||
}
|
||||
|
||||
private fun captureType(type: ConeClassLikeType, context: ConeTypeCheckerContext): ConeClassLikeType =
|
||||
(context.captureFromArguments(type, CaptureStatus.FOR_SUBTYPING) ?: type) as ConeClassLikeType
|
||||
private fun captureType(type: ConeClassLikeType, typeSystemContext: ConeTypeContext): ConeClassLikeType =
|
||||
(typeSystemContext.captureFromArguments(type, CaptureStatus.FOR_SUBTYPING) ?: type) as ConeClassLikeType
|
||||
|
||||
private fun computeSupertypesMap(
|
||||
subtypeLookupTag: ConeClassLikeLookupTag,
|
||||
@@ -82,12 +88,13 @@ class FirCorrespondingSupertypesCache(private val session: FirSession) : FirSess
|
||||
context: ConeTypeCheckerContext
|
||||
): AbstractTypeCheckerContext.SupertypesPolicy {
|
||||
val supertypeLookupTag = (supertype as ConeClassLikeType).lookupTag
|
||||
val captured = context.captureFromArguments(supertype, CaptureStatus.FOR_SUBTYPING) as ConeClassLikeType? ?: supertype
|
||||
val captured =
|
||||
context.typeSystemContext.captureFromArguments(supertype, CaptureStatus.FOR_SUBTYPING) as ConeClassLikeType? ?: supertype
|
||||
|
||||
resultingMap[supertypeLookupTag] = listOf(captured)
|
||||
|
||||
return when {
|
||||
with(context) { captured.argumentsCount() } == 0 -> {
|
||||
with(context.typeSystemContext) { captured.argumentsCount() } == 0 -> {
|
||||
AbstractTypeCheckerContext.SupertypesPolicy.LowerIfFlexible
|
||||
}
|
||||
else -> {
|
||||
|
||||
+1
-1
@@ -1374,7 +1374,7 @@ class ExpressionCodegen(
|
||||
val reifiedTypeInliner = ReifiedTypeInliner(
|
||||
mappings,
|
||||
IrInlineIntrinsicsSupport(context, typeMapper),
|
||||
IrTypeCheckerContext(context.irBuiltIns),
|
||||
IrTypeSystemContextImpl(context.irBuiltIns),
|
||||
state.languageVersionSettings,
|
||||
state.unifiedNullChecks,
|
||||
)
|
||||
|
||||
+1
-1
@@ -39,7 +39,7 @@ import org.jetbrains.kotlin.ir.types.isKClass as isKClassImpl
|
||||
import org.jetbrains.kotlin.ir.util.isSuspendFunction as isSuspendFunctionImpl
|
||||
|
||||
class IrTypeMapper(private val context: JvmBackendContext) : KotlinTypeMapperBase(), TypeMappingContext<JvmSignatureWriter> {
|
||||
internal val typeSystem = IrTypeCheckerContext(context.irBuiltIns)
|
||||
internal val typeSystem = IrTypeSystemContextImpl(context.irBuiltIns)
|
||||
override val typeContext: TypeSystemCommonBackendContextForTypeMapping = IrTypeCheckerContextForTypeMapping(typeSystem, context)
|
||||
|
||||
override fun mapClass(classifier: ClassifierDescriptor): Type =
|
||||
|
||||
+7
-1
@@ -229,7 +229,13 @@ internal class CollectionStubMethodLowering(val context: JvmBackendContext) : Cl
|
||||
}
|
||||
|
||||
private fun createTypeChecker(overrideFun: IrSimpleFunction, parentFun: IrSimpleFunction): AbstractTypeCheckerContext =
|
||||
IrTypeCheckerContextWithAdditionalAxioms(context.irBuiltIns, overrideFun.typeParameters, parentFun.typeParameters)
|
||||
IrTypeCheckerContext(
|
||||
IrTypeSystemContextWithAdditionalAxioms(
|
||||
context.irBuiltIns,
|
||||
overrideFun.typeParameters,
|
||||
parentFun.typeParameters
|
||||
)
|
||||
)
|
||||
|
||||
private fun areTypeParametersEquivalent(
|
||||
overrideFun: IrSimpleFunction,
|
||||
|
||||
@@ -425,14 +425,14 @@ class IrOverridingUtil(
|
||||
return if (a == null || b == null) true else isVisibilityMoreSpecific(a, b)
|
||||
}
|
||||
|
||||
private fun IrTypeCheckerContextWithAdditionalAxioms.isSubtypeOf(a: IrType, b: IrType) =
|
||||
private fun IrTypeCheckerContext.isSubtypeOf(a: IrType, b: IrType) =
|
||||
AbstractTypeChecker.isSubtypeOf(this as AbstractTypeCheckerContext, a, b)
|
||||
|
||||
private fun IrTypeCheckerContextWithAdditionalAxioms.equalTypes(a: IrType, b: IrType) =
|
||||
private fun IrTypeCheckerContext.equalTypes(a: IrType, b: IrType) =
|
||||
AbstractTypeChecker.equalTypes(this as AbstractTypeCheckerContext, a, b)
|
||||
|
||||
private fun createTypeChecker(a: List<IrTypeParameter>, b: List<IrTypeParameter>) =
|
||||
IrTypeCheckerContextWithAdditionalAxioms(irBuiltIns, a, b)
|
||||
IrTypeCheckerContext(IrTypeSystemContextWithAdditionalAxioms(irBuiltIns, a, b))
|
||||
|
||||
private fun isReturnTypeMoreSpecific(
|
||||
a: IrOverridableMember,
|
||||
@@ -661,10 +661,12 @@ class IrOverridingUtil(
|
||||
}
|
||||
|
||||
val typeCheckerContext =
|
||||
IrTypeCheckerContextWithAdditionalAxioms(
|
||||
irBuiltIns,
|
||||
superTypeParameters,
|
||||
subTypeParameters
|
||||
IrTypeCheckerContext(
|
||||
IrTypeSystemContextWithAdditionalAxioms(
|
||||
irBuiltIns,
|
||||
superTypeParameters,
|
||||
subTypeParameters
|
||||
)
|
||||
)
|
||||
|
||||
/* TODO: check the bounds. See OverridingUtil.areTypeParametersEquivalent()
|
||||
|
||||
@@ -10,7 +10,9 @@ import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
|
||||
import org.jetbrains.kotlin.types.AbstractTypeCheckerContext
|
||||
import org.jetbrains.kotlin.types.model.*
|
||||
|
||||
open class IrTypeCheckerContext(override val irBuiltIns: IrBuiltIns) : IrTypeSystemContext, AbstractTypeCheckerContext() {
|
||||
open class IrTypeCheckerContext(override val typeSystemContext: IrTypeSystemContext): AbstractTypeCheckerContext() {
|
||||
|
||||
val irBuiltIns: IrBuiltIns get() = typeSystemContext.irBuiltIns
|
||||
|
||||
override fun substitutionSupertypePolicy(type: SimpleTypeMarker): SupertypesPolicy.DoCustomTransform {
|
||||
require(type is IrSimpleType)
|
||||
@@ -30,32 +32,4 @@ open class IrTypeCheckerContext(override val irBuiltIns: IrBuiltIns) : IrTypeSys
|
||||
|
||||
override val KotlinTypeMarker.isAllowedTypeVariable: Boolean
|
||||
get() = false
|
||||
|
||||
override fun newBaseTypeCheckerContext(
|
||||
errorTypesEqualToAnything: Boolean,
|
||||
stubTypesEqualToAnything: Boolean
|
||||
): AbstractTypeCheckerContext = IrTypeCheckerContext(irBuiltIns)
|
||||
|
||||
override fun KotlinTypeMarker.isUninferredParameter(): Boolean = false
|
||||
override fun KotlinTypeMarker.withNullability(nullable: Boolean): KotlinTypeMarker {
|
||||
if (this.isSimpleType()) {
|
||||
return this.asSimpleType()!!.withNullability(nullable)
|
||||
} else {
|
||||
error("withNullability for non-simple types is not supported in IR")
|
||||
}
|
||||
}
|
||||
|
||||
override fun captureFromExpression(type: KotlinTypeMarker): KotlinTypeMarker? =
|
||||
error("Captured type is unsupported in IR")
|
||||
|
||||
override fun DefinitelyNotNullTypeMarker.original(): SimpleTypeMarker =
|
||||
error("DefinitelyNotNull type is unsupported in IR")
|
||||
|
||||
override fun KotlinTypeMarker.makeDefinitelyNotNullOrNotNull(): KotlinTypeMarker {
|
||||
error("makeDefinitelyNotNullOrNotNull is not supported in IR")
|
||||
}
|
||||
|
||||
override fun SimpleTypeMarker.makeSimpleTypeDefinitelyNotNullOrNotNull(): SimpleTypeMarker {
|
||||
error("makeSimpleTypeDefinitelyNotNullOrNotNull is not yet supported in IR")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,11 +9,11 @@ import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
|
||||
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
|
||||
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
|
||||
|
||||
open class IrTypeCheckerContextWithAdditionalAxioms(
|
||||
class IrTypeSystemContextWithAdditionalAxioms(
|
||||
override val irBuiltIns: IrBuiltIns,
|
||||
firstParameters: List<IrTypeParameter>,
|
||||
secondParameters: List<IrTypeParameter>
|
||||
) : IrTypeCheckerContext(irBuiltIns) {
|
||||
) : IrTypeSystemContext {
|
||||
init {
|
||||
assert(firstParameters.size == secondParameters.size) {
|
||||
"different length of type parameter lists: $firstParameters vs $secondParameters"
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.ir.util.*
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.FqNameUnsafe
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.types.AbstractTypeCheckerContext
|
||||
import org.jetbrains.kotlin.types.TypeSystemCommonBackendContext
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
import org.jetbrains.kotlin.types.model.*
|
||||
@@ -454,6 +455,39 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon
|
||||
val irClass = (this as IrType).classOrNull?.owner
|
||||
return irClass != null && (irClass.isInterface || irClass.isAnnotationClass)
|
||||
}
|
||||
|
||||
|
||||
override fun newBaseTypeCheckerContext(
|
||||
errorTypesEqualToAnything: Boolean,
|
||||
stubTypesEqualToAnything: Boolean
|
||||
): AbstractTypeCheckerContext = IrTypeCheckerContext(this)
|
||||
|
||||
override fun KotlinTypeMarker.isUninferredParameter(): Boolean = false
|
||||
override fun KotlinTypeMarker.withNullability(nullable: Boolean): KotlinTypeMarker {
|
||||
if (this.isSimpleType()) {
|
||||
return this.asSimpleType()!!.withNullability(nullable)
|
||||
} else {
|
||||
error("withNullability for non-simple types is not supported in IR")
|
||||
}
|
||||
}
|
||||
|
||||
override fun captureFromExpression(type: KotlinTypeMarker): KotlinTypeMarker? =
|
||||
error("Captured type is unsupported in IR")
|
||||
|
||||
override fun DefinitelyNotNullTypeMarker.original(): SimpleTypeMarker =
|
||||
error("DefinitelyNotNull type is unsupported in IR")
|
||||
|
||||
override fun KotlinTypeMarker.makeDefinitelyNotNullOrNotNull(): KotlinTypeMarker {
|
||||
error("makeDefinitelyNotNullOrNotNull is not supported in IR")
|
||||
}
|
||||
|
||||
override fun SimpleTypeMarker.makeSimpleTypeDefinitelyNotNullOrNotNull(): SimpleTypeMarker {
|
||||
error("makeSimpleTypeDefinitelyNotNullOrNotNull is not yet supported in IR")
|
||||
}
|
||||
|
||||
override fun prepareType(type: KotlinTypeMarker): KotlinTypeMarker {
|
||||
return type
|
||||
}
|
||||
}
|
||||
|
||||
fun extractTypeParameters(parent: IrDeclarationParent): List<IrTypeParameter> {
|
||||
@@ -480,3 +514,6 @@ fun extractTypeParameters(parent: IrDeclarationParent): List<IrTypeParameter> {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
class IrTypeSystemContextImpl(override val irBuiltIns: IrBuiltIns) : IrTypeSystemContext
|
||||
@@ -32,7 +32,11 @@ fun IrType.isSubtypeOfClass(superClass: IrClassSymbol): Boolean {
|
||||
}
|
||||
|
||||
fun IrType.isSubtypeOf(superType: IrType, irBuiltIns: IrBuiltIns): Boolean {
|
||||
return AbstractTypeChecker.isSubtypeOf(IrTypeCheckerContext(irBuiltIns) as AbstractTypeCheckerContext, this, superType)
|
||||
return AbstractTypeChecker.isSubtypeOf(
|
||||
IrTypeCheckerContext(IrTypeSystemContextImpl(irBuiltIns)) as AbstractTypeCheckerContext,
|
||||
this,
|
||||
superType
|
||||
)
|
||||
}
|
||||
|
||||
fun IrType.isNullable(): Boolean =
|
||||
|
||||
+1
-3
@@ -267,9 +267,7 @@ object NewCommonSuperTypeCalculator {
|
||||
* but it is too complicated and we will return not so accurate type: CS(List<Int>, List<Double>, List<String>)
|
||||
*/
|
||||
val correspondingSuperTypes = types.flatMap {
|
||||
with(AbstractTypeChecker) {
|
||||
typeCheckerContext.findCorrespondingSupertypes(it, constructor)
|
||||
}
|
||||
AbstractTypeChecker.findCorrespondingSupertypes(typeCheckerContext, it, constructor)
|
||||
}
|
||||
|
||||
val arguments = ArrayList<TypeArgumentMarker>(constructor.parametersCount())
|
||||
|
||||
+80
-72
@@ -10,7 +10,8 @@ import org.jetbrains.kotlin.types.AbstractTypeChecker
|
||||
import org.jetbrains.kotlin.types.AbstractTypeCheckerContext
|
||||
import org.jetbrains.kotlin.types.model.*
|
||||
|
||||
abstract class AbstractTypeCheckerContextForConstraintSystem : AbstractTypeCheckerContext(), TypeSystemInferenceExtensionContext {
|
||||
abstract class AbstractTypeCheckerContextForConstraintSystem(override val typeSystemContext: TypeSystemInferenceExtensionContext) :
|
||||
AbstractTypeCheckerContext() {
|
||||
|
||||
override val KotlinTypeMarker.isAllowedTypeVariable: Boolean
|
||||
get() = false
|
||||
@@ -36,21 +37,22 @@ abstract class AbstractTypeCheckerContextForConstraintSystem : AbstractTypeCheck
|
||||
|
||||
abstract fun addEqualityConstraint(typeVariable: TypeConstructorMarker, type: KotlinTypeMarker)
|
||||
|
||||
override fun getLowerCapturedTypePolicy(subType: SimpleTypeMarker, superType: CapturedTypeMarker): LowerCapturedTypePolicy {
|
||||
return when {
|
||||
isMyTypeVariable(subType) -> {
|
||||
val projection = superType.typeConstructorProjection()
|
||||
val type = projection.getType().asSimpleType()
|
||||
if (projection.getVariance() == TypeVariance.IN && type != null && isMyTypeVariable(type)) {
|
||||
LowerCapturedTypePolicy.CHECK_ONLY_LOWER
|
||||
} else {
|
||||
LowerCapturedTypePolicy.SKIP_LOWER
|
||||
override fun getLowerCapturedTypePolicy(subType: SimpleTypeMarker, superType: CapturedTypeMarker): LowerCapturedTypePolicy =
|
||||
with(typeSystemContext) {
|
||||
return when {
|
||||
isMyTypeVariable(subType) -> {
|
||||
val projection = superType.typeConstructorProjection()
|
||||
val type = projection.getType().asSimpleType()
|
||||
if (projection.getVariance() == TypeVariance.IN && type != null && isMyTypeVariable(type)) {
|
||||
LowerCapturedTypePolicy.CHECK_ONLY_LOWER
|
||||
} else {
|
||||
LowerCapturedTypePolicy.SKIP_LOWER
|
||||
}
|
||||
}
|
||||
subType.contains { it.anyBound(::isMyTypeVariable) } -> LowerCapturedTypePolicy.CHECK_ONLY_LOWER
|
||||
else -> LowerCapturedTypePolicy.CHECK_SUBTYPE_AND_LOWER
|
||||
}
|
||||
subType.contains { it.anyBound(this::isMyTypeVariable) } -> LowerCapturedTypePolicy.CHECK_ONLY_LOWER
|
||||
else -> LowerCapturedTypePolicy.CHECK_SUBTYPE_AND_LOWER
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* todo: possible we should override this method, because otherwise OR in subtyping transformed to AND in constraint system
|
||||
@@ -70,9 +72,11 @@ abstract class AbstractTypeCheckerContextForConstraintSystem : AbstractTypeCheck
|
||||
|
||||
// we should strip annotation's because we have incorporation operation and they should be not affected
|
||||
val mySubType =
|
||||
if (hasExact) extractTypeForProjectedType(subType, out = true) ?: subType.removeExactAnnotation() else subType
|
||||
if (hasExact) extractTypeForProjectedType(subType, out = true)
|
||||
?: with(typeSystemContext) { subType.removeExactAnnotation() } else subType
|
||||
val mySuperType =
|
||||
if (hasExact) extractTypeForProjectedType(superType, out = false) ?: superType.removeExactAnnotation() else superType
|
||||
if (hasExact) extractTypeForProjectedType(superType, out = false)
|
||||
?: with(typeSystemContext) { superType.removeExactAnnotation() } else superType
|
||||
|
||||
val result = internalAddSubtypeConstraint(mySubType, mySuperType, isFromNullabilityConstraint)
|
||||
if (!hasExact) return result
|
||||
@@ -83,7 +87,7 @@ abstract class AbstractTypeCheckerContextForConstraintSystem : AbstractTypeCheck
|
||||
return (result ?: true) && (result2 ?: true)
|
||||
}
|
||||
|
||||
private fun extractTypeForProjectedType(type: KotlinTypeMarker, out: Boolean): KotlinTypeMarker? {
|
||||
private fun extractTypeForProjectedType(type: KotlinTypeMarker, out: Boolean): KotlinTypeMarker? = with(typeSystemContext) {
|
||||
val typeMarker = type.asSimpleType()?.asCapturedType() ?: return null
|
||||
|
||||
val projection = typeMarker.typeConstructorProjection()
|
||||
@@ -98,10 +102,10 @@ abstract class AbstractTypeCheckerContextForConstraintSystem : AbstractTypeCheck
|
||||
}
|
||||
|
||||
private fun KotlinTypeMarker.isTypeVariableWithExact() =
|
||||
hasExactAnnotation() && anyBound(this@AbstractTypeCheckerContextForConstraintSystem::isMyTypeVariable)
|
||||
with(typeSystemContext) { hasExactAnnotation() } && anyBound(this@AbstractTypeCheckerContextForConstraintSystem::isMyTypeVariable)
|
||||
|
||||
private fun KotlinTypeMarker.isTypeVariableWithNoInfer() =
|
||||
hasNoInferAnnotation() && anyBound(this@AbstractTypeCheckerContextForConstraintSystem::isMyTypeVariable)
|
||||
with(typeSystemContext) { hasNoInferAnnotation() } && anyBound(this@AbstractTypeCheckerContextForConstraintSystem::isMyTypeVariable)
|
||||
|
||||
private fun internalAddSubtypeConstraint(
|
||||
subType: KotlinTypeMarker,
|
||||
@@ -128,32 +132,33 @@ abstract class AbstractTypeCheckerContextForConstraintSystem : AbstractTypeCheck
|
||||
}
|
||||
|
||||
// extract type variable only from type like Captured(out T)
|
||||
private fun extractTypeVariableForSubtype(subType: KotlinTypeMarker, superType: KotlinTypeMarker): KotlinTypeMarker? {
|
||||
private fun extractTypeVariableForSubtype(subType: KotlinTypeMarker, superType: KotlinTypeMarker): KotlinTypeMarker? =
|
||||
with(typeSystemContext) {
|
||||
|
||||
val typeMarker = subType.asSimpleType()?.asCapturedType() ?: return null
|
||||
val typeMarker = subType.asSimpleType()?.asCapturedType() ?: return null
|
||||
|
||||
val projection = typeMarker.typeConstructorProjection()
|
||||
if (projection.isStarProjection()) return null
|
||||
if (projection.getVariance() == TypeVariance.IN) {
|
||||
val type = projection.getType().asSimpleType() ?: return null
|
||||
if (isMyTypeVariable(type)) {
|
||||
simplifyLowerConstraint(type, superType)
|
||||
if (isMyTypeVariable(superType.asSimpleType() ?: return null)) {
|
||||
addLowerConstraint(superType.typeConstructor(), nullableAnyType())
|
||||
val projection = typeMarker.typeConstructorProjection()
|
||||
if (projection.isStarProjection()) return null
|
||||
if (projection.getVariance() == TypeVariance.IN) {
|
||||
val type = projection.getType().asSimpleType() ?: return null
|
||||
if (isMyTypeVariable(type)) {
|
||||
simplifyLowerConstraint(type, superType)
|
||||
if (isMyTypeVariable(superType.asSimpleType() ?: return null)) {
|
||||
addLowerConstraint(superType.typeConstructor(), nullableAnyType())
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
return if (projection.getVariance() == TypeVariance.OUT) {
|
||||
val type = projection.getType()
|
||||
when {
|
||||
type is SimpleTypeMarker && isMyTypeVariable(type) -> type.asSimpleType()
|
||||
type is FlexibleTypeMarker && isMyTypeVariable(type.lowerBound()) -> type.asFlexibleType()?.lowerBound()
|
||||
else -> null
|
||||
}
|
||||
} else null
|
||||
}
|
||||
return if (projection.getVariance() == TypeVariance.OUT) {
|
||||
val type = projection.getType()
|
||||
when {
|
||||
type is SimpleTypeMarker && isMyTypeVariable(type) -> type.asSimpleType()
|
||||
type is FlexibleTypeMarker && isMyTypeVariable(type.lowerBound()) -> type.asFlexibleType()?.lowerBound()
|
||||
else -> null
|
||||
}
|
||||
} else null
|
||||
}
|
||||
|
||||
/**
|
||||
* Foo <: T -- leave as is
|
||||
@@ -196,7 +201,7 @@ abstract class AbstractTypeCheckerContextForConstraintSystem : AbstractTypeCheck
|
||||
typeVariable: KotlinTypeMarker,
|
||||
subType: KotlinTypeMarker,
|
||||
isFromNullabilityConstraint: Boolean = false
|
||||
): Boolean {
|
||||
): Boolean = with(typeSystemContext) {
|
||||
val lowerConstraint = when (typeVariable) {
|
||||
is SimpleTypeMarker ->
|
||||
/*
|
||||
@@ -256,7 +261,7 @@ abstract class AbstractTypeCheckerContextForConstraintSystem : AbstractTypeCheck
|
||||
return true
|
||||
}
|
||||
|
||||
private fun assertFlexibleTypeVariable(typeVariable: FlexibleTypeMarker) {
|
||||
private fun assertFlexibleTypeVariable(typeVariable: FlexibleTypeMarker) = with(typeSystemContext) {
|
||||
assert(typeVariable.lowerBound().typeConstructor() == typeVariable.upperBound().typeConstructor()) {
|
||||
"Flexible type variable ($typeVariable) should have bounds with the same type constructor, i.e. (T..T?)"
|
||||
}
|
||||
@@ -267,7 +272,7 @@ abstract class AbstractTypeCheckerContextForConstraintSystem : AbstractTypeCheck
|
||||
* T? <: Foo <=> T <: Foo && Nothing? <: Foo
|
||||
* T <: Foo -- leave as is
|
||||
*/
|
||||
private fun simplifyUpperConstraint(typeVariable: KotlinTypeMarker, superType: KotlinTypeMarker): Boolean {
|
||||
private fun simplifyUpperConstraint(typeVariable: KotlinTypeMarker, superType: KotlinTypeMarker): Boolean = with(typeSystemContext) {
|
||||
val typeVariableLowerBound = typeVariable.lowerBoundIfFlexible()
|
||||
val simplifiedSuperType = if (typeVariableLowerBound.isDefinitelyNotNullType()) {
|
||||
superType.withNullability(true)
|
||||
@@ -279,37 +284,38 @@ abstract class AbstractTypeCheckerContextForConstraintSystem : AbstractTypeCheck
|
||||
|
||||
if (typeVariableLowerBound.isMarkedNullable()) {
|
||||
// here is important that superType is singleClassifierType
|
||||
return simplifiedSuperType.anyBound(this::isMyTypeVariable) ||
|
||||
return simplifiedSuperType.anyBound(::isMyTypeVariable) ||
|
||||
isSubtypeOfByTypeChecker(nullableNothingType(), simplifiedSuperType)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
private fun simplifyConstraintForPossibleIntersectionSubType(subType: KotlinTypeMarker, superType: KotlinTypeMarker): Boolean? {
|
||||
@Suppress("NAME_SHADOWING")
|
||||
val subType = subType.lowerBoundIfFlexible()
|
||||
private fun simplifyConstraintForPossibleIntersectionSubType(subType: KotlinTypeMarker, superType: KotlinTypeMarker): Boolean? =
|
||||
with(typeSystemContext) {
|
||||
@Suppress("NAME_SHADOWING")
|
||||
val subType = subType.lowerBoundIfFlexible()
|
||||
|
||||
if (!subType.typeConstructor().isIntersection()) return null
|
||||
if (!subType.typeConstructor().isIntersection()) return null
|
||||
|
||||
assert(!subType.isMarkedNullable()) { "Intersection type should not be marked nullable!: $subType" }
|
||||
assert(!subType.isMarkedNullable()) { "Intersection type should not be marked nullable!: $subType" }
|
||||
|
||||
// TODO: may be we lose flexibility here
|
||||
val subIntersectionTypes = (subType.typeConstructor().supertypes()).map { it.lowerBoundIfFlexible() }
|
||||
// TODO: may be we lose flexibility here
|
||||
val subIntersectionTypes = (subType.typeConstructor().supertypes()).map { it.lowerBoundIfFlexible() }
|
||||
|
||||
val typeVariables = subIntersectionTypes.filter(this::isMyTypeVariable).takeIf { it.isNotEmpty() } ?: return null
|
||||
val notTypeVariables = subIntersectionTypes.filterNot(this::isMyTypeVariable)
|
||||
val typeVariables = subIntersectionTypes.filter(::isMyTypeVariable).takeIf { it.isNotEmpty() } ?: return null
|
||||
val notTypeVariables = subIntersectionTypes.filterNot(::isMyTypeVariable)
|
||||
|
||||
// todo: may be we can do better then that.
|
||||
if (notTypeVariables.isNotEmpty() &&
|
||||
AbstractTypeChecker.isSubtypeOf(
|
||||
this as TypeCheckerProviderContext,
|
||||
intersectTypes(notTypeVariables),
|
||||
superType
|
||||
)
|
||||
) {
|
||||
return true
|
||||
}
|
||||
// todo: may be we can do better then that.
|
||||
if (notTypeVariables.isNotEmpty() &&
|
||||
AbstractTypeChecker.isSubtypeOf(
|
||||
this as TypeCheckerProviderContext,
|
||||
intersectTypes(notTypeVariables),
|
||||
superType
|
||||
)
|
||||
) {
|
||||
return true
|
||||
}
|
||||
|
||||
// Consider the following example:
|
||||
// fun <T> id(x: T): T = x
|
||||
@@ -326,23 +332,25 @@ abstract class AbstractTypeCheckerContextForConstraintSystem : AbstractTypeCheck
|
||||
// here we try to add constraint {Any & T} <: S from `id(a)`
|
||||
// Previously we thought that if `Any` isn't a subtype of S => T <: S, which is wrong, now we use weaker upper constraint
|
||||
// TODO: rethink, maybe we should take nullability into account somewhere else
|
||||
if (notTypeVariables.any { AbstractNullabilityChecker.isSubtypeOfAny(this as TypeCheckerProviderContext, it) }) {
|
||||
return typeVariables.all { simplifyUpperConstraint(it, superType.withNullability(true)) }
|
||||
}
|
||||
if (notTypeVariables.any { AbstractNullabilityChecker.isSubtypeOfAny(this as TypeCheckerProviderContext, it) }) {
|
||||
return typeVariables.all { simplifyUpperConstraint(it, superType.withNullability(true)) }
|
||||
}
|
||||
|
||||
return typeVariables.all { simplifyUpperConstraint(it, superType) }
|
||||
}
|
||||
return typeVariables.all { simplifyUpperConstraint(it, superType) }
|
||||
}
|
||||
|
||||
private fun isSubtypeOfByTypeChecker(subType: KotlinTypeMarker, superType: KotlinTypeMarker) =
|
||||
AbstractTypeChecker.isSubtypeOf(this as AbstractTypeCheckerContext, subType, superType)
|
||||
|
||||
private fun assertInputTypes(subType: KotlinTypeMarker, superType: KotlinTypeMarker) {
|
||||
private fun assertInputTypes(subType: KotlinTypeMarker, superType: KotlinTypeMarker) = with(typeSystemContext) {
|
||||
if (!AbstractTypeChecker.RUN_SLOW_ASSERTIONS) return
|
||||
fun correctSubType(subType: SimpleTypeMarker) =
|
||||
subType.isSingleClassifierType() || subType.typeConstructor().isIntersection() || isMyTypeVariable(subType) || subType.isError() || subType.isIntegerLiteralType()
|
||||
subType.isSingleClassifierType() || subType.typeConstructor()
|
||||
.isIntersection() || isMyTypeVariable(subType) || subType.isError() || subType.isIntegerLiteralType()
|
||||
|
||||
fun correctSuperType(superType: SimpleTypeMarker) =
|
||||
superType.isSingleClassifierType() || superType.typeConstructor().isIntersection() || isMyTypeVariable(superType) || superType.isError() || superType.isIntegerLiteralType()
|
||||
superType.isSingleClassifierType() || superType.typeConstructor()
|
||||
.isIntersection() || isMyTypeVariable(superType) || superType.isError() || superType.isIntegerLiteralType()
|
||||
|
||||
assert(subType.bothBounds(::correctSubType)) {
|
||||
"Not singleClassifierType and not intersection subType: $subType"
|
||||
@@ -354,13 +362,13 @@ abstract class AbstractTypeCheckerContextForConstraintSystem : AbstractTypeCheck
|
||||
|
||||
private inline fun KotlinTypeMarker.bothBounds(f: (SimpleTypeMarker) -> Boolean) = when (this) {
|
||||
is SimpleTypeMarker -> f(this)
|
||||
is FlexibleTypeMarker -> f(lowerBound()) && f(upperBound())
|
||||
is FlexibleTypeMarker -> with(typeSystemContext) { f(lowerBound()) && f(upperBound()) }
|
||||
else -> error("sealed")
|
||||
}
|
||||
|
||||
private inline fun KotlinTypeMarker.anyBound(f: (SimpleTypeMarker) -> Boolean) = when (this) {
|
||||
is SimpleTypeMarker -> f(this)
|
||||
is FlexibleTypeMarker -> f(lowerBound()) || f(upperBound())
|
||||
is FlexibleTypeMarker -> with(typeSystemContext) { f(lowerBound()) || f(upperBound()) }
|
||||
else -> error("sealed")
|
||||
}
|
||||
}
|
||||
|
||||
+1
-9
@@ -165,7 +165,7 @@ class ConstraintInjector(
|
||||
type.typeDepth() <= maxTypeDepthFromInitialConstraints + ALLOWED_DEPTH_DELTA_FOR_INCORPORATION
|
||||
|
||||
private inner class TypeCheckerContext(val c: Context, val position: IncorporationConstraintPosition) :
|
||||
AbstractTypeCheckerContextForConstraintSystem(), ConstraintIncorporator.Context, TypeSystemInferenceExtensionContext by c {
|
||||
AbstractTypeCheckerContextForConstraintSystem(c), ConstraintIncorporator.Context, TypeSystemInferenceExtensionContext by c {
|
||||
// We use `var` intentionally to avoid extra allocations as this property is quite "hot"
|
||||
private var possibleNewConstraints: MutableList<Pair<TypeVariableMarker, Constraint>>? = null
|
||||
|
||||
@@ -198,14 +198,6 @@ class ConstraintInjector(
|
||||
return baseContext.substitutionSupertypePolicy(type)
|
||||
}
|
||||
|
||||
override fun areEqualTypeConstructors(c1: TypeConstructorMarker, c2: TypeConstructorMarker): Boolean {
|
||||
return baseContext.areEqualTypeConstructors(c1, c2)
|
||||
}
|
||||
|
||||
override fun prepareType(type: KotlinTypeMarker): KotlinTypeMarker {
|
||||
return baseContext.prepareType(type)
|
||||
}
|
||||
|
||||
override fun refineType(type: KotlinTypeMarker): KotlinTypeMarker {
|
||||
return with(constraintIncorporator.utilContext) {
|
||||
type.refineType()
|
||||
|
||||
+11
-6
@@ -23,7 +23,9 @@ import org.jetbrains.kotlin.types.TypeConstructor
|
||||
import org.jetbrains.kotlin.types.TypeConstructorSubstitution
|
||||
import org.jetbrains.kotlin.types.TypeSubstitutor
|
||||
import org.jetbrains.kotlin.types.checker.ClassicTypeCheckerContext
|
||||
import org.jetbrains.kotlin.types.checker.ClassicTypeSystemContext
|
||||
import org.jetbrains.kotlin.types.checker.NewKotlinTypeChecker
|
||||
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
|
||||
import org.jetbrains.kotlin.types.typeUtil.asTypeProjection
|
||||
import org.jetbrains.kotlin.utils.SmartList
|
||||
import org.jetbrains.kotlin.utils.keysToMap
|
||||
@@ -326,14 +328,17 @@ object ExpectedActualResolver {
|
||||
if (b == null) return false
|
||||
|
||||
with(NewKotlinTypeChecker.Default) {
|
||||
val context = object : ClassicTypeCheckerContext(false) {
|
||||
override fun areEqualTypeConstructors(a: TypeConstructor, b: TypeConstructor): Boolean {
|
||||
return isExpectedClassAndActualTypeAlias(a, b, platformModule) ||
|
||||
isExpectedClassAndActualTypeAlias(b, a, platformModule) ||
|
||||
super.areEqualTypeConstructors(a, b)
|
||||
val context = object : ClassicTypeSystemContext {
|
||||
override fun areEqualTypeConstructors(c1: TypeConstructorMarker, c2: TypeConstructorMarker): Boolean {
|
||||
require(c1 is TypeConstructor)
|
||||
require(c2 is TypeConstructor)
|
||||
return isExpectedClassAndActualTypeAlias(c1, c2, platformModule) ||
|
||||
isExpectedClassAndActualTypeAlias(c2, c1, platformModule) ||
|
||||
super.areEqualTypeConstructors(c1, c2)
|
||||
}
|
||||
}
|
||||
return context.equalTypes(a.unwrap(), b.unwrap())
|
||||
return ClassicTypeCheckerContext(errorTypeEqualsToAnything = false, typeSystemContext = context)
|
||||
.equalTypes(a.unwrap(), b.unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user