Abstract buildResultingSubstitutor & ResultTypeResolver from KotlinType
This commit is contained in:
+1
-1
@@ -116,7 +116,7 @@ class CoroutineInferenceSession(
|
|||||||
*
|
*
|
||||||
* while substitutor from parameter map non-fixed types to the original type variable
|
* 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) {
|
for (initialConstraint in storage.initialConstraints) {
|
||||||
val lower = nonFixedToVariablesSubstitutor.safeSubstitute(callSubstitutor.safeSubstitute(initialConstraint.a as UnwrappedType)) // TODO: SUB
|
val lower = nonFixedToVariablesSubstitutor.safeSubstitute(callSubstitutor.safeSubstitute(initialConstraint.a as UnwrappedType)) // TODO: SUB
|
||||||
|
|||||||
+9
-5
@@ -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.NewTypeSubstitutor
|
||||||
import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstitutorByConstructorMap
|
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.ConstraintStorage
|
||||||
import org.jetbrains.kotlin.resolve.calls.inference.model.NewTypeVariable
|
|
||||||
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
|
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
|
||||||
import org.jetbrains.kotlin.types.*
|
import org.jetbrains.kotlin.types.*
|
||||||
import org.jetbrains.kotlin.types.model.StubTypeMarker
|
import org.jetbrains.kotlin.types.model.StubTypeMarker
|
||||||
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
|
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
|
import org.jetbrains.kotlin.utils.addToStdlib.cast
|
||||||
|
|
||||||
fun ConstraintStorage.buildCurrentSubstitutor(additionalBindings: Map<TypeConstructorMarker, StubTypeMarker>): NewTypeSubstitutorByConstructorMap =
|
fun ConstraintStorage.buildCurrentSubstitutor(additionalBindings: Map<TypeConstructorMarker, StubTypeMarker>): NewTypeSubstitutorByConstructorMap =
|
||||||
NewTypeSubstitutorByConstructorMap( (fixedTypeVariables.entries.associate { it.key to it.value } + additionalBindings).cast() ) // TODO: SUB
|
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 {
|
val currentSubstitutorMap = fixedTypeVariables.entries.associate {
|
||||||
it.key to it.value
|
it.key to it.value
|
||||||
}
|
}
|
||||||
val uninferredSubstitutorMap = notFixedTypeVariables.entries.associate { (freshTypeConstructor, typeVariable) ->
|
val uninferredSubstitutorMap = notFixedTypeVariables.entries.associate { (freshTypeConstructor, typeVariable) ->
|
||||||
freshTypeConstructor to ErrorUtils.createErrorTypeWithCustomConstructor(
|
freshTypeConstructor to context.createErrorTypeWithCustomConstructor(
|
||||||
"Uninferred type",
|
"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
|
val CallableDescriptor.returnTypeOrNothing: UnwrappedType
|
||||||
get() {
|
get() {
|
||||||
returnType?.let { return it.unwrap() }
|
returnType?.let { return it.unwrap() }
|
||||||
|
|||||||
+2
-2
@@ -25,7 +25,7 @@ import org.jetbrains.kotlin.types.model.KotlinTypeMarker
|
|||||||
import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContext
|
import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContext
|
||||||
|
|
||||||
class ResultTypeResolver(
|
class ResultTypeResolver(
|
||||||
val typeApproximator: TypeApproximator,
|
val typeApproximator: AbstractTypeApproximator,
|
||||||
val trivialConstraintTypeInferenceOracle: TrivialConstraintTypeInferenceOracle
|
val trivialConstraintTypeInferenceOracle: TrivialConstraintTypeInferenceOracle
|
||||||
) {
|
) {
|
||||||
interface Context : TypeSystemInferenceExtensionContext {
|
interface Context : TypeSystemInferenceExtensionContext {
|
||||||
@@ -74,7 +74,7 @@ class ResultTypeResolver(
|
|||||||
private fun Context.isSuitableType(resultType: KotlinTypeMarker, variableWithConstraints: VariableWithConstraints): Boolean {
|
private fun Context.isSuitableType(resultType: KotlinTypeMarker, variableWithConstraints: VariableWithConstraints): Boolean {
|
||||||
for (constraint in variableWithConstraints.constraints) {
|
for (constraint in variableWithConstraints.constraints) {
|
||||||
if (!isProperType(constraint.type)) continue
|
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
|
if (!trivialConstraintTypeInferenceOracle.isSuitableResultedType(resultType)) return false
|
||||||
|
|||||||
+1
-2
@@ -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.DeclaredUpperBoundConstraintPosition
|
||||||
import org.jetbrains.kotlin.resolve.calls.inference.model.VariableWithConstraints
|
import org.jetbrains.kotlin.resolve.calls.inference.model.VariableWithConstraints
|
||||||
import org.jetbrains.kotlin.resolve.calls.model.PostponedResolvedAtom
|
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.KotlinTypeMarker
|
||||||
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
|
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
|
||||||
import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContext
|
import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContext
|
||||||
@@ -47,7 +46,7 @@ class VariableFixationFinder(
|
|||||||
allTypeVariables: List<TypeConstructorMarker>,
|
allTypeVariables: List<TypeConstructorMarker>,
|
||||||
postponedKtPrimitives: List<PostponedResolvedAtom>,
|
postponedKtPrimitives: List<PostponedResolvedAtom>,
|
||||||
completionMode: ConstraintSystemCompletionMode,
|
completionMode: ConstraintSystemCompletionMode,
|
||||||
topLevelType: UnwrappedType
|
topLevelType: KotlinTypeMarker
|
||||||
): VariableForFixation? = c.findTypeVariableForFixation(allTypeVariables, postponedKtPrimitives, completionMode, topLevelType)
|
): VariableForFixation? = c.findTypeVariableForFixation(allTypeVariables, postponedKtPrimitives, completionMode, topLevelType)
|
||||||
|
|
||||||
private enum class TypeVariableFixationReadiness {
|
private enum class TypeVariableFixationReadiness {
|
||||||
|
|||||||
+12
-13
@@ -6,12 +6,8 @@
|
|||||||
package org.jetbrains.kotlin.resolve.calls.inference.model
|
package org.jetbrains.kotlin.resolve.calls.inference.model
|
||||||
|
|
||||||
import org.jetbrains.kotlin.resolve.calls.model.KotlinCallDiagnostic
|
import org.jetbrains.kotlin.resolve.calls.model.KotlinCallDiagnostic
|
||||||
import org.jetbrains.kotlin.types.TypeSubstitutor
|
import org.jetbrains.kotlin.types.AbstractTypeChecker
|
||||||
import org.jetbrains.kotlin.types.UnwrappedType
|
import org.jetbrains.kotlin.types.model.*
|
||||||
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
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Every type variable can be in the following states:
|
* Every type variable can be in the following states:
|
||||||
@@ -120,15 +116,18 @@ class InitialConstraint(
|
|||||||
// return checkConstraint(newB as KotlinTypeMarker, constraintKind, newA as KotlinTypeMarker)
|
// return checkConstraint(newB as KotlinTypeMarker, constraintKind, newA as KotlinTypeMarker)
|
||||||
//}
|
//}
|
||||||
|
|
||||||
fun checkConstraint(constraintType: KotlinTypeMarker, constraintKind: ConstraintKind, resultType: KotlinTypeMarker): Boolean {
|
fun checkConstraint(
|
||||||
require(constraintType is UnwrappedType)
|
context: TypeCheckerProviderContext,
|
||||||
require(resultType is UnwrappedType)
|
constraintType: KotlinTypeMarker,
|
||||||
|
constraintKind: ConstraintKind,
|
||||||
|
resultType: KotlinTypeMarker
|
||||||
|
): Boolean {
|
||||||
|
|
||||||
|
|
||||||
val typeChecker = KotlinTypeChecker.DEFAULT
|
val typeChecker = AbstractTypeChecker
|
||||||
return when (constraintKind) {
|
return when (constraintKind) {
|
||||||
ConstraintKind.EQUALITY -> typeChecker.equalTypes(constraintType, resultType)
|
ConstraintKind.EQUALITY -> typeChecker.equalTypes(context, constraintType, resultType)
|
||||||
ConstraintKind.LOWER -> typeChecker.isSubtypeOf(constraintType, resultType)
|
ConstraintKind.LOWER -> typeChecker.isSubtypeOf(context, constraintType, resultType)
|
||||||
ConstraintKind.UPPER -> typeChecker.isSubtypeOf(resultType, constraintType)
|
ConstraintKind.UPPER -> typeChecker.isSubtypeOf(context, resultType, constraintType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -476,6 +476,11 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext {
|
|||||||
override fun captureFromExpression(type: KotlinTypeMarker): KotlinTypeMarker? {
|
override fun captureFromExpression(type: KotlinTypeMarker): KotlinTypeMarker? {
|
||||||
return captureFromExpressionInternal(type as UnwrappedType)
|
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)
|
private fun captureFromExpressionInternal(type: UnwrappedType) = captureFromExpression(type)
|
||||||
|
|||||||
@@ -55,6 +55,8 @@ interface TypeSystemTypeFactoryContext {
|
|||||||
fun createSimpleType(constructor: TypeConstructorMarker, arguments: List<TypeArgumentMarker>, nullable: Boolean): SimpleTypeMarker
|
fun createSimpleType(constructor: TypeConstructorMarker, arguments: List<TypeArgumentMarker>, nullable: Boolean): SimpleTypeMarker
|
||||||
fun createTypeArgument(type: KotlinTypeMarker, variance: TypeVariance): TypeArgumentMarker
|
fun createTypeArgument(type: KotlinTypeMarker, variance: TypeVariance): TypeArgumentMarker
|
||||||
fun createStarProjection(typeParameter: TypeParameterMarker): TypeArgumentMarker
|
fun createStarProjection(typeParameter: TypeParameterMarker): TypeArgumentMarker
|
||||||
|
|
||||||
|
fun createErrorTypeWithCustomConstructor(debugName: String, constructor: TypeConstructorMarker): KotlinTypeMarker
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user