NI: remove type depth check during adding initial constraints

This commit is contained in:
Victor Petukhov
2020-05-28 18:55:05 +03:00
parent 35460fed19
commit 40a4cea530
2 changed files with 6 additions and 38 deletions
@@ -66,14 +66,7 @@ class ConstraintInjector(
updateAllowedTypeDepth(c, a) updateAllowedTypeDepth(c, a)
updateAllowedTypeDepth(c, b) updateAllowedTypeDepth(c, b)
val incorporationConstraintPosition = IncorporationConstraintPosition(position, initialConstraint) val typeCheckerContext = TypeCheckerContext(c, IncorporationConstraintPosition(position, initialConstraint))
val typeCheckerContext = TypeCheckerContext(c, incorporationConstraintPosition)
if (position is FixVariableConstraintPosition) {
c.incorporateEqualityConstraintForFixingTypeVariable(
incorporationConstraintPosition, position.variable, resultType = b, typeCheckerContext
)
}
addSubTypeConstraintAndIncorporateIt(c, a, b, typeCheckerContext) addSubTypeConstraintAndIncorporateIt(c, a, b, typeCheckerContext)
addSubTypeConstraintAndIncorporateIt(c, b, a, typeCheckerContext) addSubTypeConstraintAndIncorporateIt(c, b, a, typeCheckerContext)
@@ -88,11 +81,9 @@ class ConstraintInjector(
typeCheckerContext.setConstrainingTypesToPrintDebugInfo(lowerType, upperType) typeCheckerContext.setConstrainingTypesToPrintDebugInfo(lowerType, upperType)
typeCheckerContext.runIsSubtypeOf(lowerType, upperType) typeCheckerContext.runIsSubtypeOf(lowerType, upperType)
var constraintsFromIsSubtype = true
while (typeCheckerContext.hasConstraintsToProcess()) { while (typeCheckerContext.hasConstraintsToProcess()) {
for ((typeVariable, constraint) in typeCheckerContext.extractAllConstraints()!!) { for ((typeVariable, constraint) in typeCheckerContext.extractAllConstraints()!!) {
if (c.shouldWeSkipConstraint(typeVariable, constraint, constraintsFromIsSubtype)) continue if (c.shouldWeSkipConstraint(typeVariable, constraint)) continue
val constraints = val constraints =
c.notFixedTypeVariables[typeVariable.freshTypeConstructor(c)] ?: typeCheckerContext.fixedTypeVariable(typeVariable) c.notFixedTypeVariables[typeVariable.freshTypeConstructor(c)] ?: typeCheckerContext.fixedTypeVariable(typeVariable)
@@ -115,7 +106,6 @@ class ConstraintInjector(
) { ) {
break break
} }
constraintsFromIsSubtype = false
} }
} }
@@ -123,34 +113,10 @@ class ConstraintInjector(
c.maxTypeDepthFromInitialConstraints = max(c.maxTypeDepthFromInitialConstraints, initialType.typeDepth()) c.maxTypeDepthFromInitialConstraints = max(c.maxTypeDepthFromInitialConstraints, initialType.typeDepth())
} }
private fun Context.incorporateEqualityConstraintForFixingTypeVariable( private fun Context.shouldWeSkipConstraint(typeVariable: TypeVariableMarker, constraint: Constraint): Boolean {
incorporatePosition: IncorporationConstraintPosition, assert(constraint.kind != EQUALITY)
fixingTypeVariable: TypeVariableMarker,
resultType: KotlinTypeMarker,
typeCheckerContext: TypeCheckerContext
) {
if (resultType.isUninferredParameter() || resultType.isError()) return
assert(!resultType.contains { it.typeConstructor() is TypeVariableTypeConstructor })
constraintIncorporator.incorporateIntoOtherConstraints(
typeCheckerContext,
fixingTypeVariable,
Constraint(EQUALITY, resultType, incorporatePosition, derivedFrom = emptySet(), isNullabilityConstraint = false)
)
}
private fun Context.shouldWeSkipConstraint(
typeVariable: TypeVariableMarker,
constraint: Constraint,
constraintsFromIsSubtype: Boolean
): Boolean {
assert(constraint.kind != ConstraintKind.EQUALITY)
val constraintType = constraint.type val constraintType = constraint.type
// TODO: seems these coditions never met, at least in the tests, we need either to find a test for it or drop the check
if (!constraintsFromIsSubtype && !isAllowedType(constraintType))
return true
if (constraintType.typeConstructor() == typeVariable.freshTypeConstructor()) { if (constraintType.typeConstructor() == typeVariable.freshTypeConstructor()) {
if (constraintType.lowerBoundIfFlexible().isMarkedNullable() && constraint.kind == LOWER) return false // T? <: T if (constraintType.lowerBoundIfFlexible().isMarkedNullable() && constraint.kind == LOWER) return false // T? <: T
@@ -172,6 +138,7 @@ class ConstraintInjector(
private inner class TypeCheckerContext(val c: Context, val position: IncorporationConstraintPosition) : private inner class TypeCheckerContext(val c: Context, val position: IncorporationConstraintPosition) :
AbstractTypeCheckerContextForConstraintSystem(), ConstraintIncorporator.Context, TypeSystemInferenceExtensionContext by c { AbstractTypeCheckerContextForConstraintSystem(), 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 private var possibleNewConstraints: MutableList<Pair<TypeVariableMarker, Constraint>>? = null
private var baseLowerType = position.initialConstraint.a private var baseLowerType = position.initialConstraint.a
@@ -296,6 +296,7 @@ class NewConstraintSystemImpl(
} }
// KotlinConstraintSystemCompleter.Context // KotlinConstraintSystemCompleter.Context
// TODO: simplify this: do only substitution a fixing type variable rather than running of subtyping and full incorporation
override fun fixVariable(variable: TypeVariableMarker, resultType: KotlinTypeMarker, atom: ResolvedAtom?) { override fun fixVariable(variable: TypeVariableMarker, resultType: KotlinTypeMarker, atom: ResolvedAtom?) {
checkState(State.BUILDING, State.COMPLETION) checkState(State.BUILDING, State.COMPLETION)