Skip to content

Commit 475ead5

Browse files
committed
Sema: Simplify PotentialBindings updates a little
1 parent 0f15ef0 commit 475ead5

File tree

2 files changed

+31
-56
lines changed

2 files changed

+31
-56
lines changed

include/swift/Sema/ConstraintGraph.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,6 @@ class ConstraintGraphNode {
146146

147147
/// Binding Inference {
148148

149-
/// Infer bindings from the given constraint and notify referenced variables
150-
/// about its arrival (if requested). This happens every time a new constraint
151-
/// gets added to a constraint graph node.
152-
void introduceToInference(Constraint *constraint);
153-
154-
/// Forget about the given constraint. This happens every time a constraint
155-
/// gets removed for a constraint graph.
156-
void retractFromInference(Constraint *constraint);
157-
158149
/// Perform graph updates that must be undone after we bind a fixed type
159150
/// to a type variable.
160151
void retractFromInference(Type fixedType);

lib/Sema/ConstraintGraph.cpp

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,10 @@ void ConstraintGraphNode::notifyReferencingVars(
242242

243243
void ConstraintGraphNode::notifyReferencedVars(
244244
llvm::function_ref<void(ConstraintGraphNode &)> notification) const {
245-
for (auto *fixedBinding : getReferencedVars()) {
246-
notification(CG[fixedBinding]);
245+
for (auto *referencedVar : getReferencedVars()) {
246+
auto *repr = referencedVar->getImpl().getRepresentative(/*record=*/nullptr);
247+
if (!repr->getImpl().getFixedType(/*record=*/nullptr))
248+
notification(CG[repr]);
247249
}
248250
}
249251

@@ -297,30 +299,6 @@ void ConstraintGraphNode::removeReferencedBy(TypeVariableType *typeVar) {
297299
}
298300
}
299301

300-
void ConstraintGraphNode::introduceToInference(Constraint *constraint) {
301-
if (forRepresentativeVar()) {
302-
auto fixedType = TypeVar->getImpl().getFixedType(/*record=*/nullptr);
303-
if (!fixedType)
304-
getPotentialBindings().infer(CG.getConstraintSystem(), TypeVar, constraint);
305-
} else {
306-
auto *repr =
307-
getTypeVariable()->getImpl().getRepresentative(/*record=*/nullptr);
308-
CG[repr].introduceToInference(constraint);
309-
}
310-
}
311-
312-
void ConstraintGraphNode::retractFromInference(Constraint *constraint) {
313-
if (forRepresentativeVar()) {
314-
auto fixedType = TypeVar->getImpl().getFixedType(/*record=*/nullptr);
315-
if (!fixedType)
316-
getPotentialBindings().retract(CG.getConstraintSystem(), TypeVar,constraint);
317-
} else {
318-
auto *repr =
319-
getTypeVariable()->getImpl().getRepresentative(/*record=*/nullptr);
320-
CG[repr].retractFromInference(constraint);
321-
}
322-
}
323-
324302
void ConstraintGraphNode::updateFixedType(
325303
Type fixedType,
326304
llvm::function_ref<void (ConstraintGraphNode &,
@@ -340,7 +318,11 @@ void ConstraintGraphNode::updateFixedType(
340318
fixedType->getTypeVariables(referencedVars);
341319

342320
for (auto *referencedVar : referencedVars) {
343-
auto &node = CG[referencedVar];
321+
auto *repr = referencedVar->getImpl().getRepresentative(/*record=*/nullptr);
322+
if (repr->getImpl().getFixedType(/*record=*/nullptr))
323+
continue;
324+
325+
auto &node = CG[repr];
344326

345327
// Newly referred vars need to re-introduce all constraints associated
346328
// with this type variable since they are now going to be used in
@@ -353,18 +335,20 @@ void ConstraintGraphNode::updateFixedType(
353335
}
354336

355337
void ConstraintGraphNode::retractFromInference(Type fixedType) {
338+
auto &cs = CG.getConstraintSystem();
356339
return updateFixedType(
357340
fixedType,
358-
[](ConstraintGraphNode &node, Constraint *constraint) {
359-
node.retractFromInference(constraint);
341+
[&cs](ConstraintGraphNode &node, Constraint *constraint) {
342+
node.getPotentialBindings().retract(cs, node.getTypeVariable(), constraint);
360343
});
361344
}
362345

363346
void ConstraintGraphNode::introduceToInference(Type fixedType) {
347+
auto &cs = CG.getConstraintSystem();
364348
return updateFixedType(
365349
fixedType,
366-
[](ConstraintGraphNode &node, Constraint *constraint) {
367-
node.introduceToInference(constraint);
350+
[&cs](ConstraintGraphNode &node, Constraint *constraint) {
351+
node.getPotentialBindings().infer(cs, node.getTypeVariable(), constraint);
368352
});
369353
}
370354

@@ -396,13 +380,13 @@ void ConstraintGraph::addConstraint(Constraint *constraint) {
396380

397381
addConstraint(typeVar, constraint);
398382

399-
auto &node = (*this)[typeVar];
400-
401-
node.introduceToInference(constraint);
383+
auto *repr = typeVar->getImpl().getRepresentative(/*record=*/nullptr);
384+
if (!repr->getImpl().getFixedType(/*record=*/nullptr))
385+
(*this)[repr].getPotentialBindings().infer(CS, repr, constraint);
402386

403387
if (isUsefulForReferencedVars(constraint)) {
404-
node.notifyReferencedVars([&](ConstraintGraphNode &referencedVar) {
405-
referencedVar.introduceToInference(constraint);
388+
(*this)[typeVar].notifyReferencedVars([&](ConstraintGraphNode &node) {
389+
node.getPotentialBindings().infer(CS, node.getTypeVariable(), constraint);
406390
});
407391
}
408392
}
@@ -434,14 +418,13 @@ void ConstraintGraph::removeConstraint(Constraint *constraint) {
434418
// For the nodes corresponding to each type variable...
435419
auto referencedTypeVars = constraint->getTypeVariables();
436420
for (auto typeVar : referencedTypeVars) {
437-
// Find the node for this type variable.
438-
auto &node = (*this)[typeVar];
439-
440-
node.retractFromInference(constraint);
421+
auto *repr = typeVar->getImpl().getRepresentative(/*record=*/nullptr);
422+
if (!repr->getImpl().getFixedType(/*record=*/nullptr))
423+
(*this)[repr].getPotentialBindings().retract(CS, repr, constraint);
441424

442425
if (isUsefulForReferencedVars(constraint)) {
443-
node.notifyReferencedVars([&](ConstraintGraphNode &referencedVar) {
444-
referencedVar.retractFromInference(constraint);
426+
(*this)[typeVar].notifyReferencedVars([&](ConstraintGraphNode &node) {
427+
node.getPotentialBindings().retract(CS, node.getTypeVariable(), constraint);
445428
});
446429
}
447430

@@ -487,7 +470,7 @@ void ConstraintGraph::mergeNodesPre(TypeVariableType *typeVar2) {
487470

488471
node.notifyReferencingVars(
489472
[&](ConstraintGraphNode &node, Constraint *constraint) {
490-
node.retractFromInference(constraint);
473+
node.getPotentialBindings().retract(CS, node.getTypeVariable(), constraint);
491474
});
492475
}
493476
}
@@ -517,19 +500,20 @@ void ConstraintGraph::mergeNodes(TypeVariableType *typeVar1,
517500
auto &node = (*this)[newMember];
518501

519502
for (auto *constraint : node.getConstraints()) {
520-
repNode.introduceToInference(constraint);
503+
if (!typeVar1->getImpl().getFixedType(/*record=*/nullptr))
504+
repNode.getPotentialBindings().infer(CS, typeVar1, constraint);
521505

522506
if (!isUsefulForReferencedVars(constraint))
523507
continue;
524508

525-
repNode.notifyReferencedVars([&](ConstraintGraphNode &referencedVar) {
526-
referencedVar.introduceToInference(constraint);
509+
repNode.notifyReferencedVars([&](ConstraintGraphNode &node) {
510+
node.getPotentialBindings().infer(CS, node.getTypeVariable(), constraint);
527511
});
528512
}
529513

530514
node.notifyReferencingVars(
531515
[&](ConstraintGraphNode &node, Constraint *constraint) {
532-
node.introduceToInference(constraint);
516+
node.getPotentialBindings().infer(CS, node.getTypeVariable(), constraint);
533517
});
534518
}
535519
}

0 commit comments

Comments
 (0)