@@ -352,6 +352,36 @@ void ConstraintGraphNode::retractFromInference(Constraint *constraint) {
352352 }
353353}
354354
355+ void ConstraintGraphNode::retractFromInference (Type fixedType) {
356+ // Notify all of the type variables that reference this one.
357+ //
358+ // Since this type variable has been replaced with a fixed type
359+ // all of the concrete types that reference it are going to change,
360+ // which means that all of the not-yet-attempted bindings should
361+ // change as well.
362+ notifyReferencingVars (
363+ [&](ConstraintGraphNode &node, Constraint *constraint) {
364+ node.retractFromInference (constraint);
365+ });
366+
367+ if (!fixedType->hasTypeVariable ())
368+ return ;
369+
370+ SmallPtrSet<TypeVariableType *, 4 > referencedVars;
371+ fixedType->getTypeVariables (referencedVars);
372+
373+ for (auto *referencedVar : referencedVars) {
374+ auto &node = CG[referencedVar];
375+
376+ // Newly referred vars need to re-introduce all constraints associated
377+ // with this type variable since they are now going to be used in
378+ // all of the constraints that reference bound type variable.
379+ for (auto *constraint : getConstraints ()) {
380+ if (isUsefulForReferencedVars (constraint))
381+ node.retractFromInference (constraint);
382+ }
383+ }
384+ }
355385
356386void ConstraintGraphNode::introduceToInference (Type fixedType) {
357387 // Notify all of the type variables that reference this one.
@@ -362,7 +392,6 @@ void ConstraintGraphNode::introduceToInference(Type fixedType) {
362392 // change as well.
363393 notifyReferencingVars (
364394 [&](ConstraintGraphNode &node, Constraint *constraint) {
365- node.retractFromInference (constraint);
366395 node.introduceToInference (constraint);
367396 });
368397
@@ -380,7 +409,6 @@ void ConstraintGraphNode::introduceToInference(Type fixedType) {
380409 // all of the constraints that reference bound type variable.
381410 for (auto *constraint : getConstraints ()) {
382411 if (isUsefulForReferencedVars (constraint)) {
383- node.retractFromInference (constraint);
384412 node.introduceToInference (constraint);
385413 }
386414 }
@@ -548,6 +576,10 @@ void ConstraintGraph::bindTypeVariable(TypeVariableType *typeVar, Type fixed) {
548576 }
549577}
550578
579+ void ConstraintGraph::retractFromInference (TypeVariableType *typeVar, Type fixed) {
580+ (*this )[typeVar].retractFromInference (fixed);
581+ }
582+
551583void ConstraintGraph::introduceToInference (TypeVariableType *typeVar, Type fixed) {
552584 (*this )[typeVar].introduceToInference (fixed);
553585}
0 commit comments