From 9306f3840fafa2e640a96c1cbdfe99470d67c019 Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Fri, 5 Apr 2019 17:53:59 +0300 Subject: [PATCH] Abstract buildResultingSubstitutor & ResultTypeResolver from KotlinType --- .../inference/CoroutineInferenceSession.kt | 2 +- .../resolve/calls/inference/InferenceUtils.kt | 14 +++++++---- .../components/ResultTypeResolver.kt | 4 +-- .../components/VariableFixationFinder.kt | 3 +-- .../inference/model/ConstraintStorage.kt | 25 +++++++++---------- .../types/checker/ClassicTypeSystemContext.kt | 5 ++++ .../kotlin/types/model/TypeSystemContext.kt | 2 ++ 7 files changed, 32 insertions(+), 23 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceSession.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceSession.kt index 532c122be6e..a37ca4a0397 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceSession.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceSession.kt @@ -116,7 +116,7 @@ class CoroutineInferenceSession( * * while substitutor from parameter map non-fixed types to the original type variable * */ - val callSubstitutor = storage.buildResultingSubstitutor() // substitutor only for fixed variables + val callSubstitutor = storage.buildResultingSubstitutor(commonSystem) as NewTypeSubstitutor // substitutor only for fixed variables for (initialConstraint in storage.initialConstraints) { val lower = nonFixedToVariablesSubstitutor.safeSubstitute(callSubstitutor.safeSubstitute(initialConstraint.a as UnwrappedType)) // TODO: SUB diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/InferenceUtils.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/InferenceUtils.kt index 649e5431158..e1f91eeb242 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/InferenceUtils.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/InferenceUtils.kt @@ -20,30 +20,34 @@ import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstitutor import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstitutorByConstructorMap import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage -import org.jetbrains.kotlin.resolve.calls.inference.model.NewTypeVariable import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.model.StubTypeMarker import org.jetbrains.kotlin.types.model.TypeConstructorMarker +import org.jetbrains.kotlin.types.model.TypeSubstitutorMarker +import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContext import org.jetbrains.kotlin.utils.addToStdlib.cast fun ConstraintStorage.buildCurrentSubstitutor(additionalBindings: Map): NewTypeSubstitutorByConstructorMap = NewTypeSubstitutorByConstructorMap( (fixedTypeVariables.entries.associate { it.key to it.value } + additionalBindings).cast() ) // TODO: SUB -fun ConstraintStorage.buildResultingSubstitutor(): NewTypeSubstitutor { +fun ConstraintStorage.buildResultingSubstitutor(context: TypeSystemInferenceExtensionContext): TypeSubstitutorMarker = with(context) { val currentSubstitutorMap = fixedTypeVariables.entries.associate { it.key to it.value } val uninferredSubstitutorMap = notFixedTypeVariables.entries.associate { (freshTypeConstructor, typeVariable) -> - freshTypeConstructor to ErrorUtils.createErrorTypeWithCustomConstructor( + freshTypeConstructor to context.createErrorTypeWithCustomConstructor( "Uninferred type", - (typeVariable.typeVariable as NewTypeVariable).freshTypeConstructor// TODO: SUB + (typeVariable.typeVariable).freshTypeConstructor() ) } - return NewTypeSubstitutorByConstructorMap((currentSubstitutorMap + uninferredSubstitutorMap).cast()) // TODO: SUB + return context.typeSubstitutorByTypeConstructor(currentSubstitutorMap + uninferredSubstitutorMap) } +fun ConstraintStorage.buildResultingSubstitutor(): NewTypeSubstitutor = + buildResultingSubstitutor(this as TypeSystemInferenceExtensionContext) as NewTypeSubstitutor + val CallableDescriptor.returnTypeOrNothing: UnwrappedType get() { returnType?.let { return it.unwrap() } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/ResultTypeResolver.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/ResultTypeResolver.kt index 0738950bee9..88bfbe44d3b 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/ResultTypeResolver.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/ResultTypeResolver.kt @@ -25,7 +25,7 @@ import org.jetbrains.kotlin.types.model.KotlinTypeMarker import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContext class ResultTypeResolver( - val typeApproximator: TypeApproximator, + val typeApproximator: AbstractTypeApproximator, val trivialConstraintTypeInferenceOracle: TrivialConstraintTypeInferenceOracle ) { interface Context : TypeSystemInferenceExtensionContext { @@ -74,7 +74,7 @@ class ResultTypeResolver( private fun Context.isSuitableType(resultType: KotlinTypeMarker, variableWithConstraints: VariableWithConstraints): Boolean { for (constraint in variableWithConstraints.constraints) { if (!isProperType(constraint.type)) continue - if (!checkConstraint(constraint.type, constraint.kind, resultType)) return false + if (!checkConstraint(this, constraint.type, constraint.kind, resultType)) return false } if (!trivialConstraintTypeInferenceOracle.isSuitableResultedType(resultType)) return false diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/VariableFixationFinder.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/VariableFixationFinder.kt index 49c08abd48f..88ae1d6081d 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/VariableFixationFinder.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/VariableFixationFinder.kt @@ -22,7 +22,6 @@ import org.jetbrains.kotlin.resolve.calls.inference.model.Constraint import org.jetbrains.kotlin.resolve.calls.inference.model.DeclaredUpperBoundConstraintPosition import org.jetbrains.kotlin.resolve.calls.inference.model.VariableWithConstraints import org.jetbrains.kotlin.resolve.calls.model.PostponedResolvedAtom -import org.jetbrains.kotlin.types.UnwrappedType import org.jetbrains.kotlin.types.model.KotlinTypeMarker import org.jetbrains.kotlin.types.model.TypeConstructorMarker import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContext @@ -47,7 +46,7 @@ class VariableFixationFinder( allTypeVariables: List, postponedKtPrimitives: List, completionMode: ConstraintSystemCompletionMode, - topLevelType: UnwrappedType + topLevelType: KotlinTypeMarker ): VariableForFixation? = c.findTypeVariableForFixation(allTypeVariables, postponedKtPrimitives, completionMode, topLevelType) private enum class TypeVariableFixationReadiness { diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintStorage.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintStorage.kt index ecc8f1ec387..3705a777a05 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintStorage.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintStorage.kt @@ -6,12 +6,8 @@ package org.jetbrains.kotlin.resolve.calls.inference.model import org.jetbrains.kotlin.resolve.calls.model.KotlinCallDiagnostic -import org.jetbrains.kotlin.types.TypeSubstitutor -import org.jetbrains.kotlin.types.UnwrappedType -import org.jetbrains.kotlin.types.checker.KotlinTypeChecker -import org.jetbrains.kotlin.types.model.KotlinTypeMarker -import org.jetbrains.kotlin.types.model.TypeConstructorMarker -import org.jetbrains.kotlin.types.model.TypeVariableMarker +import org.jetbrains.kotlin.types.AbstractTypeChecker +import org.jetbrains.kotlin.types.model.* /** * Every type variable can be in the following states: @@ -120,15 +116,18 @@ class InitialConstraint( // return checkConstraint(newB as KotlinTypeMarker, constraintKind, newA as KotlinTypeMarker) //} -fun checkConstraint(constraintType: KotlinTypeMarker, constraintKind: ConstraintKind, resultType: KotlinTypeMarker): Boolean { - require(constraintType is UnwrappedType) - require(resultType is UnwrappedType) +fun checkConstraint( + context: TypeCheckerProviderContext, + constraintType: KotlinTypeMarker, + constraintKind: ConstraintKind, + resultType: KotlinTypeMarker +): Boolean { - val typeChecker = KotlinTypeChecker.DEFAULT + val typeChecker = AbstractTypeChecker return when (constraintKind) { - ConstraintKind.EQUALITY -> typeChecker.equalTypes(constraintType, resultType) - ConstraintKind.LOWER -> typeChecker.isSubtypeOf(constraintType, resultType) - ConstraintKind.UPPER -> typeChecker.isSubtypeOf(resultType, constraintType) + ConstraintKind.EQUALITY -> typeChecker.equalTypes(context, constraintType, resultType) + ConstraintKind.LOWER -> typeChecker.isSubtypeOf(context, constraintType, resultType) + ConstraintKind.UPPER -> typeChecker.isSubtypeOf(context, resultType, constraintType) } } \ No newline at end of file diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt b/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt index 4e3723df460..b21dd43c32d 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt @@ -476,6 +476,11 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext { override fun captureFromExpression(type: KotlinTypeMarker): KotlinTypeMarker? { return captureFromExpressionInternal(type as UnwrappedType) } + + override fun createErrorTypeWithCustomConstructor(debugName: String, constructor: TypeConstructorMarker): KotlinTypeMarker { + require(constructor is TypeConstructor, constructor::errorMessage) + return ErrorUtils.createErrorTypeWithCustomConstructor(debugName, constructor) + } } private fun captureFromExpressionInternal(type: UnwrappedType) = captureFromExpression(type) diff --git a/core/type-system/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt b/core/type-system/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt index ef4a57de9a4..aa23c622bbb 100644 --- a/core/type-system/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt +++ b/core/type-system/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt @@ -55,6 +55,8 @@ interface TypeSystemTypeFactoryContext { fun createSimpleType(constructor: TypeConstructorMarker, arguments: List, nullable: Boolean): SimpleTypeMarker fun createTypeArgument(type: KotlinTypeMarker, variance: TypeVariance): TypeArgumentMarker fun createStarProjection(typeParameter: TypeParameterMarker): TypeArgumentMarker + + fun createErrorTypeWithCustomConstructor(debugName: String, constructor: TypeConstructorMarker): KotlinTypeMarker }