Skip to content

Commit a19c92a

Browse files
committed
Sema: Extract duplication from ConstraintGraph::introduceTo/retractFromInference(Type) overloads
1 parent eeab483 commit a19c92a

File tree

2 files changed

+22
-34
lines changed

2 files changed

+22
-34
lines changed

include/swift/Sema/ConstraintGraph.h

+4
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ class ConstraintGraphNode {
167167
void notifyReferencedVars(
168168
llvm::function_ref<void(ConstraintGraphNode &)> notification) const;
169169

170+
void updateFixedType(
171+
Type fixedType,
172+
llvm::function_ref<void (ConstraintGraphNode &,
173+
Constraint *)> notification) const;
170174
/// }
171175

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

lib/Sema/ConstraintGraph.cpp

+18-34
Original file line numberDiff line numberDiff line change
@@ -328,17 +328,17 @@ void ConstraintGraphNode::retractFromInference(Constraint *constraint) {
328328
}
329329
}
330330

331-
void ConstraintGraphNode::retractFromInference(Type fixedType) {
331+
void ConstraintGraphNode::updateFixedType(
332+
Type fixedType,
333+
llvm::function_ref<void (ConstraintGraphNode &,
334+
Constraint *)> notification) const {
332335
// Notify all of the type variables that reference this one.
333336
//
334337
// Since this type variable has been replaced with a fixed type
335338
// all of the concrete types that reference it are going to change,
336339
// which means that all of the not-yet-attempted bindings should
337340
// change as well.
338-
notifyReferencingVars(
339-
[&](ConstraintGraphNode &node, Constraint *constraint) {
340-
node.retractFromInference(constraint);
341-
});
341+
notifyReferencingVars(notification);
342342

343343
if (!fixedType->hasTypeVariable())
344344
return;
@@ -354,41 +354,25 @@ void ConstraintGraphNode::retractFromInference(Type fixedType) {
354354
// all of the constraints that reference bound type variable.
355355
for (auto *constraint : getConstraints()) {
356356
if (isUsefulForReferencedVars(constraint))
357-
node.retractFromInference(constraint);
357+
notification(node, constraint);
358358
}
359359
}
360360
}
361361

362-
void ConstraintGraphNode::introduceToInference(Type fixedType) {
363-
// Notify all of the type variables that reference this one.
364-
//
365-
// Since this type variable has been replaced with a fixed type
366-
// all of the concrete types that reference it are going to change,
367-
// which means that all of the not-yet-attempted bindings should
368-
// change as well.
369-
notifyReferencingVars(
370-
[&](ConstraintGraphNode &node, Constraint *constraint) {
371-
node.introduceToInference(constraint);
372-
});
373-
374-
if (!fixedType->hasTypeVariable())
375-
return;
376-
377-
SmallPtrSet<TypeVariableType *, 4> referencedVars;
378-
fixedType->getTypeVariables(referencedVars);
379-
380-
for (auto *referencedVar : referencedVars) {
381-
auto &node = CG[referencedVar];
362+
void ConstraintGraphNode::retractFromInference(Type fixedType) {
363+
return updateFixedType(
364+
fixedType,
365+
[](ConstraintGraphNode &node, Constraint *constraint) {
366+
node.retractFromInference(constraint);
367+
});
368+
}
382369

383-
// Newly referred vars need to re-introduce all constraints associated
384-
// with this type variable since they are now going to be used in
385-
// all of the constraints that reference bound type variable.
386-
for (auto *constraint : getConstraints()) {
387-
if (isUsefulForReferencedVars(constraint)) {
370+
void ConstraintGraphNode::introduceToInference(Type fixedType) {
371+
return updateFixedType(
372+
fixedType,
373+
[](ConstraintGraphNode &node, Constraint *constraint) {
388374
node.introduceToInference(constraint);
389-
}
390-
}
391-
}
375+
});
392376
}
393377

394378
#pragma mark Graph mutation

0 commit comments

Comments
 (0)