Abstract buildResultingSubstitutor & ResultTypeResolver from KotlinType

This commit is contained in:
Simon Ogorodnik
2019-04-05 17:53:59 +03:00
parent fe2e5b7301
commit 9306f3840f
7 changed files with 32 additions and 23 deletions
@@ -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
@@ -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<TypeConstructorMarker, StubTypeMarker>): 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() }
@@ -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
@@ -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<TypeConstructorMarker>,
postponedKtPrimitives: List<PostponedResolvedAtom>,
completionMode: ConstraintSystemCompletionMode,
topLevelType: UnwrappedType
topLevelType: KotlinTypeMarker
): VariableForFixation? = c.findTypeVariableForFixation(allTypeVariables, postponedKtPrimitives, completionMode, topLevelType)
private enum class TypeVariableFixationReadiness {
@@ -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)
}
}
@@ -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)
@@ -55,6 +55,8 @@ interface TypeSystemTypeFactoryContext {
fun createSimpleType(constructor: TypeConstructorMarker, arguments: List<TypeArgumentMarker>, nullable: Boolean): SimpleTypeMarker
fun createTypeArgument(type: KotlinTypeMarker, variance: TypeVariance): TypeArgumentMarker
fun createStarProjection(typeParameter: TypeParameterMarker): TypeArgumentMarker
fun createErrorTypeWithCustomConstructor(debugName: String, constructor: TypeConstructorMarker): KotlinTypeMarker
}