@@ -155,16 +155,6 @@ void ConstraintGraphNode::addConstraint(Constraint *constraint) {
155155 assert (ConstraintIndex.count (constraint) == 0 && " Constraint re-insertion" );
156156 ConstraintIndex[constraint] = Constraints.size ();
157157 Constraints.push_back (constraint);
158-
159- {
160- introduceToInference (constraint);
161-
162- if (isUsefulForReferencedVars (constraint)) {
163- notifyReferencedVars ([&](ConstraintGraphNode &referencedVar) {
164- referencedVar.introduceToInference (constraint);
165- });
166- }
167- }
168158}
169159
170160void ConstraintGraphNode::removeConstraint (Constraint *constraint) {
@@ -176,16 +166,6 @@ void ConstraintGraphNode::removeConstraint(Constraint *constraint) {
176166 ConstraintIndex.erase (pos);
177167 assert (Constraints[index] == constraint && " Mismatched constraint" );
178168
179- {
180- retractFromInference (constraint);
181-
182- if (isUsefulForReferencedVars (constraint)) {
183- notifyReferencedVars ([&](ConstraintGraphNode &referencedVar) {
184- referencedVar.retractFromInference (constraint);
185- });
186- }
187- }
188-
189169 // If this is the last constraint, just pop it off the list and we're done.
190170 unsigned lastIndex = Constraints.size ()-1 ;
191171 if (index == lastIndex) {
@@ -471,48 +451,97 @@ void ConstraintGraph::addConstraint(Constraint *constraint) {
471451 // For the nodes corresponding to each type variable...
472452 auto referencedTypeVars = constraint->getTypeVariables ();
473453 for (auto typeVar : referencedTypeVars) {
454+ // Record the change, if there are active scopes.
455+ if (CS.isRecordingChanges ())
456+ CS.recordChange (SolverTrail::Change::addedConstraint (typeVar, constraint));
457+
458+ addConstraint (typeVar, constraint);
459+ }
460+
461+ // If the constraint doesn't reference any type variables, it's orphaned;
462+ // track it as such.
463+ if (referencedTypeVars.empty ()) {
464+ // Record the change, if there are active scopes.
465+ if (CS.isRecordingChanges ())
466+ CS.recordChange (SolverTrail::Change::addedConstraint (nullptr , constraint));
467+
468+ addConstraint (nullptr , constraint);
469+ }
470+ }
471+
472+ void ConstraintGraph::addConstraint (TypeVariableType *typeVar,
473+ Constraint *constraint) {
474+ if (typeVar) {
474475 // Find the node for this type variable.
475476 auto &node = (*this )[typeVar];
476477
477478 // Note the constraint within the node for that type variable.
478479 node.addConstraint (constraint);
480+
481+ node.introduceToInference (constraint);
482+
483+ if (isUsefulForReferencedVars (constraint)) {
484+ node.notifyReferencedVars ([&](ConstraintGraphNode &referencedVar) {
485+ referencedVar.introduceToInference (constraint);
486+ });
487+ }
488+
489+ return ;
479490 }
480491
481492 // If the constraint doesn't reference any type variables, it's orphaned;
482493 // track it as such.
483- if (referencedTypeVars.empty ()) {
484- OrphanedConstraints.push_back (constraint);
485- }
486-
487- // Record the change, if there are active scopes.
488- if (CS.isRecordingChanges ())
489- CS.recordChange (SolverTrail::Change::addedConstraint (constraint));
494+ OrphanedConstraints.push_back (constraint);
490495}
491496
492497void ConstraintGraph::removeConstraint (Constraint *constraint) {
493498 // For the nodes corresponding to each type variable...
494499 auto referencedTypeVars = constraint->getTypeVariables ();
495500 for (auto typeVar : referencedTypeVars) {
501+ // Record the change, if there are active scopes.
502+ if (CS.isRecordingChanges ())
503+ CS.recordChange (SolverTrail::Change::removedConstraint (typeVar, constraint));
504+
505+ removeConstraint (typeVar, constraint);
506+ }
507+
508+ // If this is an orphaned constraint, remove it from the list.
509+ if (referencedTypeVars.empty ()) {
510+ // Record the change, if there are active scopes.
511+ if (CS.isRecordingChanges ())
512+ CS.recordChange (SolverTrail::Change::removedConstraint (nullptr , constraint));
513+
514+ removeConstraint (nullptr , constraint);
515+ }
516+ }
517+
518+ void ConstraintGraph::removeConstraint (TypeVariableType *typeVar,
519+ Constraint *constraint) {
520+ if (typeVar) {
496521 // Find the node for this type variable.
497522 auto &node = (*this )[typeVar];
498523
524+ node.retractFromInference (constraint);
525+
526+ if (isUsefulForReferencedVars (constraint)) {
527+ node.notifyReferencedVars ([&](ConstraintGraphNode &referencedVar) {
528+ referencedVar.retractFromInference (constraint);
529+ });
530+ }
531+
499532 // Remove the constraint.
500533 node.removeConstraint (constraint);
501- }
502534
503- // If this is an orphaned constraint, remove it from the list.
504- if (referencedTypeVars.empty ()) {
505- auto known = std::find (OrphanedConstraints.begin (),
506- OrphanedConstraints.end (),
507- constraint);
508- assert (known != OrphanedConstraints.end () && " missing orphaned constraint" );
509- *known = OrphanedConstraints.back ();
510- OrphanedConstraints.pop_back ();
535+ return ;
511536 }
512537
513- // Record the change, if there are active scopes.
514- if (CS.isRecordingChanges ())
515- CS.recordChange (SolverTrail::Change::removedConstraint (constraint));
538+ // If this is an orphaned constraint, remove it from the list.
539+ auto known = std::find (OrphanedConstraints.begin (),
540+ OrphanedConstraints.end (),
541+ constraint);
542+ assert (known != OrphanedConstraints.end () && " missing orphaned constraint" );
543+ *known = OrphanedConstraints.back ();
544+ OrphanedConstraints.pop_back ();
516545}
517546
518547void ConstraintGraph::mergeNodes (TypeVariableType *typeVar1,
0 commit comments