Skip to content

Commit 4220cc0

Browse files
authored
Merge pull request #37822 from xedin/incremental-bindings-cleanups
[ConstraintGraph] NFC: Remove flags from inference methods
2 parents 6c51fc4 + 12e9686 commit 4220cc0

File tree

2 files changed

+53
-29
lines changed

2 files changed

+53
-29
lines changed

include/swift/Sema/ConstraintGraph.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,16 @@ class ConstraintGraphNode {
125125
/// Infer bindings from the given constraint and notify referenced variables
126126
/// about its arrival (if requested). This happens every time a new constraint
127127
/// gets added to a constraint graph node.
128-
void introduceToInference(Constraint *constraint, bool notifyReferencedVars);
128+
void introduceToInference(Constraint *constraint);
129129

130130
/// Forget about the given constraint. This happens every time a constraint
131131
/// gets removed for a constraint graph.
132-
void retractFromInference(Constraint *constraint, bool notifyReferencedVars);
132+
void retractFromInference(Constraint *constraint);
133133

134134
/// Re-evaluate the given constraint. This happens when there are changes
135135
/// in associated type variables e.g. bound/unbound to/from a fixed type,
136136
/// equivalence class changes.
137-
void reintroduceToInference(Constraint *constraint, bool notifyReferencedVars);
137+
void reintroduceToInference(Constraint *constraint);
138138

139139
/// Similar to \c introduceToInference(Constraint *, ...) this method is going
140140
/// to notify inference that this type variable has been bound to a concrete
@@ -166,6 +166,10 @@ class ConstraintGraphNode {
166166
/// or equivalence class changes.
167167
void notifyReferencingVars() const;
168168

169+
/// Notify all of the type variables referenced by this one about a change.
170+
void notifyReferencedVars(
171+
llvm::function_ref<void(ConstraintGraphNode &)> notification);
172+
169173
/// }
170174

171175
/// The constraint graph this node belongs to.

lib/Sema/ConstraintGraph.cpp

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,14 @@ void ConstraintGraphNode::addConstraint(Constraint *constraint) {
107107
assert(ConstraintIndex.count(constraint) == 0 && "Constraint re-insertion");
108108
ConstraintIndex[constraint] = Constraints.size();
109109
Constraints.push_back(constraint);
110-
introduceToInference(constraint, /*notifyFixedBindings=*/true);
110+
111+
{
112+
introduceToInference(constraint);
113+
114+
notifyReferencedVars([&](ConstraintGraphNode &referencedVar) {
115+
referencedVar.introduceToInference(constraint);
116+
});
117+
}
111118
}
112119

113120
void ConstraintGraphNode::removeConstraint(Constraint *constraint) {
@@ -119,8 +126,13 @@ void ConstraintGraphNode::removeConstraint(Constraint *constraint) {
119126
ConstraintIndex.erase(pos);
120127
assert(Constraints[index] == constraint && "Mismatched constraint");
121128

122-
retractFromInference(constraint,
123-
/*notifyFixedBindings=*/true);
129+
{
130+
retractFromInference(constraint);
131+
132+
notifyReferencedVars([&](ConstraintGraphNode &referencedVar) {
133+
referencedVar.retractFromInference(constraint);
134+
});
135+
}
124136

125137
// If this is the last constraint, just pop it off the list and we're done.
126138
unsigned lastIndex = Constraints.size()-1;
@@ -160,8 +172,7 @@ void ConstraintGraphNode::notifyReferencingVars() const {
160172
affectedVar->getImpl().getRepresentative(/*record=*/nullptr);
161173

162174
if (!repr->getImpl().getFixedType(/*record=*/nullptr))
163-
CG[repr].reintroduceToInference(constraint,
164-
/*notifyReferencedVars=*/false);
175+
CG[repr].reintroduceToInference(constraint);
165176
}
166177
}
167178
};
@@ -197,6 +208,13 @@ void ConstraintGraphNode::notifyReferencingVars() const {
197208
}
198209
}
199210

211+
void ConstraintGraphNode::notifyReferencedVars(
212+
llvm::function_ref<void(ConstraintGraphNode &)> notification) {
213+
for (auto *fixedBinding : getReferencedVars()) {
214+
notification(CG[fixedBinding]);
215+
}
216+
}
217+
200218
void ConstraintGraphNode::addToEquivalenceClass(
201219
ArrayRef<TypeVariableType *> typeVars) {
202220
assert(forRepresentativeVar() &&
@@ -210,7 +228,11 @@ void ConstraintGraphNode::addToEquivalenceClass(
210228
auto &node = CG[newMember];
211229

212230
for (auto *constraint : node.getConstraints()) {
213-
introduceToInference(constraint, /*notifyReferencedVars=*/true);
231+
introduceToInference(constraint);
232+
233+
notifyReferencedVars([&](ConstraintGraphNode &referencedVar) {
234+
referencedVar.introduceToInference(constraint);
235+
});
214236
}
215237

216238
node.notifyReferencingVars();
@@ -289,52 +311,51 @@ static bool isUsefulForReferencedVars(Constraint *constraint) {
289311
}
290312
}
291313

292-
void ConstraintGraphNode::introduceToInference(Constraint *constraint,
293-
bool notifyReferencedVars) {
314+
void ConstraintGraphNode::introduceToInference(Constraint *constraint) {
294315
if (forRepresentativeVar()) {
295316
auto fixedType = TypeVar->getImpl().getFixedType(/*record=*/nullptr);
296317
if (!fixedType)
297318
getCurrentBindings().infer(constraint);
298319
} else {
299320
auto *repr =
300321
getTypeVariable()->getImpl().getRepresentative(/*record=*/nullptr);
301-
CG[repr].introduceToInference(constraint, /*notifyReferencedVars=*/false);
322+
CG[repr].introduceToInference(constraint);
302323
}
303324

325+
/*
304326
if (!notifyReferencedVars || !isUsefulForReferencedVars(constraint))
305327
return;
306328
307-
for (auto *fixedBinding : getReferencedVars()) {
308-
CG[fixedBinding].introduceToInference(constraint,
309-
/*notifyReferencedVars=*/false);
310-
}
329+
this->notifyReferencedVars([&](ConstraintGraphNode &referencedVar) {
330+
referencedVar.introduceToInference(constraint);
331+
});
332+
*/
311333
}
312334

313-
void ConstraintGraphNode::retractFromInference(Constraint *constraint,
314-
bool notifyReferencedVars) {
335+
void ConstraintGraphNode::retractFromInference(Constraint *constraint) {
315336
if (forRepresentativeVar()) {
316337
auto fixedType = TypeVar->getImpl().getFixedType(/*record=*/nullptr);
317338
if (!fixedType)
318339
getCurrentBindings().retract(constraint);
319340
} else {
320341
auto *repr =
321342
getTypeVariable()->getImpl().getRepresentative(/*record=*/nullptr);
322-
CG[repr].retractFromInference(constraint, /*notifyReferencedVars=*/false);
343+
CG[repr].retractFromInference(constraint);
323344
}
324345

346+
/*
325347
if (!notifyReferencedVars || !isUsefulForReferencedVars(constraint))
326348
return;
327349
328-
for (auto *fixedBinding : getReferencedVars()) {
329-
CG[fixedBinding].retractFromInference(constraint,
330-
/*notifyReferencedVars=*/false);
331-
}
350+
this->notifyReferencedVars([&](ConstraintGraphNode &referencedVar) {
351+
referencedVar.retractFromInference(constraint);
352+
});
353+
*/
332354
}
333355

334-
void ConstraintGraphNode::reintroduceToInference(Constraint *constraint,
335-
bool notifyReferencedVars) {
336-
retractFromInference(constraint, notifyReferencedVars);
337-
introduceToInference(constraint, notifyReferencedVars);
356+
void ConstraintGraphNode::reintroduceToInference(Constraint *constraint) {
357+
retractFromInference(constraint);
358+
introduceToInference(constraint);
338359
}
339360

340361
void ConstraintGraphNode::introduceToInference(Type fixedType) {
@@ -360,8 +381,7 @@ void ConstraintGraphNode::introduceToInference(Type fixedType) {
360381
// all of the constraints that reference bound type variable.
361382
for (auto *constraint : getConstraints()) {
362383
if (isUsefulForReferencedVars(constraint))
363-
node.reintroduceToInference(constraint,
364-
/*notifyReferencedVars=*/false);
384+
node.reintroduceToInference(constraint);
365385
}
366386
}
367387
}

0 commit comments

Comments
 (0)