Skip to content

Commit

Permalink
Sema: Extract duplication from ConstraintGraph::introduceTo/retractFr…
Browse files Browse the repository at this point in the history
…omInference(Type) overloads
  • Loading branch information
slavapestov committed Nov 20, 2024
1 parent eeab483 commit a19c92a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 34 deletions.
4 changes: 4 additions & 0 deletions include/swift/Sema/ConstraintGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ class ConstraintGraphNode {
void notifyReferencedVars(
llvm::function_ref<void(ConstraintGraphNode &)> notification) const;

void updateFixedType(
Type fixedType,
llvm::function_ref<void (ConstraintGraphNode &,
Constraint *)> notification) const;
/// }

/// The constraint graph this node belongs to.
Expand Down
52 changes: 18 additions & 34 deletions lib/Sema/ConstraintGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,17 +328,17 @@ void ConstraintGraphNode::retractFromInference(Constraint *constraint) {
}
}

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

if (!fixedType->hasTypeVariable())
return;
Expand All @@ -354,41 +354,25 @@ void ConstraintGraphNode::retractFromInference(Type fixedType) {
// all of the constraints that reference bound type variable.
for (auto *constraint : getConstraints()) {
if (isUsefulForReferencedVars(constraint))
node.retractFromInference(constraint);
notification(node, constraint);
}
}
}

void ConstraintGraphNode::introduceToInference(Type fixedType) {
// Notify all of the type variables that reference this one.
//
// Since this type variable has been replaced with a fixed type
// all of the concrete types that reference it are going to change,
// which means that all of the not-yet-attempted bindings should
// change as well.
notifyReferencingVars(
[&](ConstraintGraphNode &node, Constraint *constraint) {
node.introduceToInference(constraint);
});

if (!fixedType->hasTypeVariable())
return;

SmallPtrSet<TypeVariableType *, 4> referencedVars;
fixedType->getTypeVariables(referencedVars);

for (auto *referencedVar : referencedVars) {
auto &node = CG[referencedVar];
void ConstraintGraphNode::retractFromInference(Type fixedType) {
return updateFixedType(
fixedType,
[](ConstraintGraphNode &node, Constraint *constraint) {
node.retractFromInference(constraint);
});
}

// Newly referred vars need to re-introduce all constraints associated
// with this type variable since they are now going to be used in
// all of the constraints that reference bound type variable.
for (auto *constraint : getConstraints()) {
if (isUsefulForReferencedVars(constraint)) {
void ConstraintGraphNode::introduceToInference(Type fixedType) {
return updateFixedType(
fixedType,
[](ConstraintGraphNode &node, Constraint *constraint) {
node.introduceToInference(constraint);
}
}
}
});
}

#pragma mark Graph mutation
Expand Down

0 comments on commit a19c92a

Please sign in to comment.