Skip to content

Commit 99595e6

Browse files
committed
Revert "Sema: Split up gatherConstraints() into gatherAllConstraints() and gatherNearbyConstraints()"
This reverts commit 2230c3a.
1 parent 8127c83 commit 99595e6

7 files changed

+62
-37
lines changed

include/swift/Sema/ConstraintGraph.h

+17-10
Original file line numberDiff line numberDiff line change
@@ -300,18 +300,25 @@ class ConstraintGraph {
300300
/// to a type variable.
301301
void introduceToInference(TypeVariableType *typeVar, Type fixedType);
302302

303-
/// Gather constraints associated with all of the variables within the
304-
/// same equivalence class as the given type variable, as well as its
305-
/// immediate fixed bindings.
306-
llvm::TinyPtrVector<Constraint *>
307-
gatherAllConstraints(TypeVariableType *typeVar);
303+
/// Describes which constraints \c gatherConstraints should gather.
304+
enum class GatheringKind {
305+
/// Gather constraints associated with all of the variables within the
306+
/// same equivalence class as the given type variable, as well as its
307+
/// immediate fixed bindings.
308+
EquivalenceClass,
309+
/// Gather all constraints that mention this type variable or type variables
310+
/// that it is a fixed binding of. Unlike EquivalenceClass, this looks
311+
/// through transitive fixed bindings. This can be used to find all the
312+
/// constraints that may be affected when binding a type variable.
313+
AllMentions,
314+
};
308315

309-
/// Gather all constraints that mention this type variable or type variables
310-
/// that it is a fixed binding of. Unlike EquivalenceClass, this looks
311-
/// through transitive fixed bindings. This can be used to find all the
312-
/// constraints that may be affected when binding a type variable.
316+
/// Gather the set of constraints that involve the given type variable,
317+
/// i.e., those constraints that will be affected when the type variable
318+
/// gets merged or bound to a fixed type.
313319
llvm::TinyPtrVector<Constraint *>
314-
gatherNearbyConstraints(TypeVariableType *typeVar,
320+
gatherConstraints(TypeVariableType *typeVar,
321+
GatheringKind kind,
315322
llvm::function_ref<bool(Constraint *)> acceptConstraint =
316323
[](Constraint *constraint) { return true; });
317324

lib/Sema/CSOptimizer.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ NullablePtr<Constraint> getApplicableFnConstraint(ConstraintGraph &CG,
152152
if (!boundVar)
153153
return nullptr;
154154

155-
auto constraints = CG.gatherNearbyConstraints(
156-
boundVar,
155+
auto constraints = CG.gatherConstraints(
156+
boundVar, ConstraintGraph::GatheringKind::EquivalenceClass,
157157
[](Constraint *constraint) {
158158
return constraint->getKind() == ConstraintKind::ApplicableFunction;
159159
});
@@ -1172,8 +1172,8 @@ selectBestBindingDisjunction(ConstraintSystem &cs,
11721172
if (!firstBindDisjunction)
11731173
firstBindDisjunction = disjunction;
11741174

1175-
auto constraints = cs.getConstraintGraph().gatherNearbyConstraints(
1176-
typeVar,
1175+
auto constraints = cs.getConstraintGraph().gatherConstraints(
1176+
typeVar, ConstraintGraph::GatheringKind::EquivalenceClass,
11771177
[](Constraint *constraint) {
11781178
return constraint->getKind() == ConstraintKind::Conversion;
11791179
});

lib/Sema/CSSimplify.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -15793,8 +15793,8 @@ ConstraintSystem::addKeyPathApplicationRootConstraint(Type root, ConstraintLocat
1579315793
if (!typeVar)
1579415794
return;
1579515795

15796-
auto constraints = CG.gatherNearbyConstraints(
15797-
typeVar,
15796+
auto constraints = CG.gatherConstraints(
15797+
typeVar, ConstraintGraph::GatheringKind::EquivalenceClass,
1579815798
[&keyPathExpr](Constraint *constraint) -> bool {
1579915799
if (constraint->getKind() != ConstraintKind::KeyPath)
1580015800
return false;

lib/Sema/CSSolver.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -1297,8 +1297,8 @@ ConstraintSystem::findConstraintThroughOptionals(
12971297
while (visitedVars.insert(rep).second) {
12981298
// Look for a disjunction that binds this type variable to an overload set.
12991299
TypeVariableType *optionalObjectTypeVar = nullptr;
1300-
auto constraints = getConstraintGraph().gatherNearbyConstraints(
1301-
rep,
1300+
auto constraints = getConstraintGraph().gatherConstraints(
1301+
rep, ConstraintGraph::GatheringKind::EquivalenceClass,
13021302
[&](Constraint *match) {
13031303
// If we have an "optional object of" constraint, we may need to
13041304
// look through it to find the constraint we're looking for.
@@ -1906,8 +1906,9 @@ void DisjunctionChoice::propagateConversionInfo(ConstraintSystem &cs) const {
19061906
}
19071907
}
19081908

1909-
auto constraints = cs.CG.gatherNearbyConstraints(
1909+
auto constraints = cs.CG.gatherConstraints(
19101910
typeVar,
1911+
ConstraintGraph::GatheringKind::EquivalenceClass,
19111912
[](Constraint *constraint) -> bool {
19121913
switch (constraint->getKind()) {
19131914
case ConstraintKind::Conversion:

lib/Sema/ConstraintGraph.cpp

+31-15
Original file line numberDiff line numberDiff line change
@@ -552,11 +552,18 @@ void ConstraintGraph::retractBindings(TypeVariableType *typeVar,
552552

553553
#pragma mark Algorithms
554554

555+
/// Perform a depth-first search.
556+
///
557+
/// \param cg The constraint graph.
558+
/// \param typeVar The type variable we're searching from.
559+
/// \param visitConstraint Called before considering a constraint.
560+
/// \param visitedConstraints Set of already-visited constraints, used
561+
/// internally to avoid duplicated work.
555562
static void depthFirstSearch(
556563
ConstraintGraph &cg,
557564
TypeVariableType *typeVar,
565+
llvm::function_ref<void(Constraint *)> visitConstraint,
558566
llvm::SmallPtrSet<TypeVariableType *, 4> &typeVars,
559-
llvm::TinyPtrVector<Constraint *> &constraints,
560567
llvm::SmallPtrSet<Constraint *, 8> &visitedConstraints) {
561568
// If we're not looking at this type variable right now because we're
562569
// solving a conjunction element, don't consider its adjacencies.
@@ -570,8 +577,11 @@ static void depthFirstSearch(
570577
// Local function to visit adjacent type variables.
571578
auto visitAdjacencies = [&](ArrayRef<TypeVariableType *> adjTypeVars) {
572579
for (auto adj : adjTypeVars) {
573-
if (adj != typeVar)
574-
depthFirstSearch(cg, adj, typeVars, constraints, visitedConstraints);
580+
if (adj == typeVar)
581+
continue;
582+
583+
// Recurse into this node.
584+
depthFirstSearch(cg, adj, visitConstraint, typeVars, visitedConstraints);
575585
}
576586
};
577587

@@ -582,7 +592,7 @@ static void depthFirstSearch(
582592
if (!visitedConstraints.insert(constraint).second)
583593
continue;
584594

585-
constraints.push_back(constraint);
595+
visitConstraint(constraint);
586596
}
587597

588598
// Visit all of the other nodes in the equivalence class.
@@ -601,22 +611,28 @@ static void depthFirstSearch(
601611
visitAdjacencies(node.getReferencedVars());
602612
}
603613

604-
llvm::TinyPtrVector<Constraint *> ConstraintGraph::gatherAllConstraints(
605-
TypeVariableType *typeVar) {
614+
llvm::TinyPtrVector<Constraint *> ConstraintGraph::gatherConstraints(
615+
TypeVariableType *typeVar, GatheringKind kind,
616+
llvm::function_ref<bool(Constraint *)> acceptConstraintFn) {
606617
llvm::TinyPtrVector<Constraint *> constraints;
607618
llvm::SmallPtrSet<TypeVariableType *, 4> typeVars;
608619
llvm::SmallPtrSet<Constraint *, 8> visitedConstraints;
609620

610-
depthFirstSearch(*this, typeVar, typeVars, constraints, visitedConstraints);
611-
return constraints;
612-
}
621+
if (kind == GatheringKind::AllMentions) {
622+
// If we've been asked for "all mentions" of a type variable, search for
623+
// constraints involving both it and its fixed bindings.
624+
depthFirstSearch(
625+
*this, typeVar,
626+
[&](Constraint *constraint) {
627+
if (acceptConstraintFn(constraint))
628+
constraints.push_back(constraint);
629+
},
630+
typeVars, visitedConstraints);
631+
return constraints;
632+
}
613633

614-
llvm::TinyPtrVector<Constraint *> ConstraintGraph::gatherNearbyConstraints(
615-
TypeVariableType *typeVar,
616-
llvm::function_ref<bool(Constraint *)> acceptConstraintFn) {
617-
llvm::TinyPtrVector<Constraint *> constraints;
618-
llvm::SmallPtrSet<TypeVariableType *, 4> typeVars;
619-
llvm::SmallPtrSet<Constraint *, 8> visitedConstraints;
634+
// Otherwise only search in the type var's equivalence class and immediate
635+
// fixed bindings.
620636

621637
// Local function to add constraints.
622638
auto addTypeVarConstraints = [&](TypeVariableType *adjTypeVar) {

lib/Sema/ConstraintSystem.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ void ConstraintSystem::assignFixedType(TypeVariableType *typeVar, Type type,
267267
void ConstraintSystem::addTypeVariableConstraintsToWorkList(
268268
TypeVariableType *typeVar) {
269269
// Activate the constraints affected by a change to this type variable.
270-
for (auto *constraint : CG.gatherAllConstraints(typeVar))
270+
auto gatheringKind = ConstraintGraph::GatheringKind::AllMentions;
271+
for (auto *constraint : CG.gatherConstraints(typeVar, gatheringKind))
271272
if (!constraint->isActive())
272273
activateConstraint(constraint);
273274
}

lib/Sema/TypeOfReference.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2044,8 +2044,8 @@ void ConstraintSystem::bindOverloadType(
20442044
increaseScore(SK_KeyPathSubscript, locator);
20452045

20462046
auto boundTypeVar = boundType->castTo<TypeVariableType>();
2047-
auto constraints = getConstraintGraph().gatherNearbyConstraints(
2048-
boundTypeVar,
2047+
auto constraints = getConstraintGraph().gatherConstraints(
2048+
boundTypeVar, ConstraintGraph::GatheringKind::EquivalenceClass,
20492049
[](Constraint *constraint) {
20502050
return constraint->getKind() == ConstraintKind::ApplicableFunction;
20512051
});

0 commit comments

Comments
 (0)