@@ -155,16 +155,6 @@ void ConstraintGraphNode::addConstraint(Constraint *constraint) {
155
155
assert (ConstraintIndex.count (constraint) == 0 && " Constraint re-insertion" );
156
156
ConstraintIndex[constraint] = Constraints.size ();
157
157
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
- }
168
158
}
169
159
170
160
void ConstraintGraphNode::removeConstraint (Constraint *constraint) {
@@ -176,16 +166,6 @@ void ConstraintGraphNode::removeConstraint(Constraint *constraint) {
176
166
ConstraintIndex.erase (pos);
177
167
assert (Constraints[index] == constraint && " Mismatched constraint" );
178
168
179
- {
180
- retractFromInference (constraint);
181
-
182
- if (isUsefulForReferencedVars (constraint)) {
183
- notifyReferencedVars ([&](ConstraintGraphNode &referencedVar) {
184
- referencedVar.retractFromInference (constraint);
185
- });
186
- }
187
- }
188
-
189
169
// If this is the last constraint, just pop it off the list and we're done.
190
170
unsigned lastIndex = Constraints.size ()-1 ;
191
171
if (index == lastIndex) {
@@ -471,48 +451,97 @@ void ConstraintGraph::addConstraint(Constraint *constraint) {
471
451
// For the nodes corresponding to each type variable...
472
452
auto referencedTypeVars = constraint->getTypeVariables ();
473
453
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) {
474
475
// Find the node for this type variable.
475
476
auto &node = (*this )[typeVar];
476
477
477
478
// Note the constraint within the node for that type variable.
478
479
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 ;
479
490
}
480
491
481
492
// If the constraint doesn't reference any type variables, it's orphaned;
482
493
// 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);
490
495
}
491
496
492
497
void ConstraintGraph::removeConstraint (Constraint *constraint) {
493
498
// For the nodes corresponding to each type variable...
494
499
auto referencedTypeVars = constraint->getTypeVariables ();
495
500
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) {
496
521
// Find the node for this type variable.
497
522
auto &node = (*this )[typeVar];
498
523
524
+ node.retractFromInference (constraint);
525
+
526
+ if (isUsefulForReferencedVars (constraint)) {
527
+ node.notifyReferencedVars ([&](ConstraintGraphNode &referencedVar) {
528
+ referencedVar.retractFromInference (constraint);
529
+ });
530
+ }
531
+
499
532
// Remove the constraint.
500
533
node.removeConstraint (constraint);
501
- }
502
534
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 ;
511
536
}
512
537
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 ();
516
545
}
517
546
518
547
void ConstraintGraph::mergeNodes (TypeVariableType *typeVar1,
0 commit comments