Skip to content

Commit ae4949d

Browse files
committed
[Constraint solver] Remove a hand-rolled clone of getUnboundBindOverloadDisjunction
Use the real one instead.
1 parent a13ba3b commit ae4949d

File tree

2 files changed

+57
-74
lines changed

2 files changed

+57
-74
lines changed

lib/Sema/CSGen.cpp

Lines changed: 54 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -605,98 +605,79 @@ namespace {
605605
mustConsider = nullptr) {
606606
// Find the type variable associated with the function, if any.
607607
auto tyvarType = CS.getType(expr->getFn())->getAs<TypeVariableType>();
608-
if (!tyvarType)
608+
if (!tyvarType || CS.getFixedType(tyvarType))
609609
return;
610610

611611
// This type variable is only currently associated with the function
612612
// being applied, and the only constraint attached to it should
613613
// be the disjunction constraint for the overload group.
614-
auto &CG = CS.getConstraintGraph();
615-
llvm::SetVector<Constraint *> disjunctions;
616-
CG.gatherConstraints(tyvarType, disjunctions,
617-
ConstraintGraph::GatheringKind::EquivalenceClass,
618-
[](Constraint *constraint) -> bool {
619-
return constraint->getKind() ==
620-
ConstraintKind::Disjunction;
621-
});
622-
if (disjunctions.empty())
614+
auto disjunction = CS.getUnboundBindOverloadDisjunction(tyvarType);
615+
if (!disjunction)
623616
return;
624617

625-
// Look for the disjunction that binds the overload set.
626-
for (auto *disjunction : disjunctions) {
627-
auto oldConstraints = disjunction->getNestedConstraints();
628-
auto csLoc = CS.getConstraintLocator(expr->getFn());
618+
auto oldConstraints = disjunction->getNestedConstraints();
619+
auto csLoc = CS.getConstraintLocator(expr->getFn());
629620

630-
// Only replace the disjunctive overload constraint.
631-
if (oldConstraints[0]->getKind() != ConstraintKind::BindOverload) {
632-
continue;
621+
if (mustConsider) {
622+
bool hasMustConsider = false;
623+
for (auto oldConstraint : oldConstraints) {
624+
auto overloadChoice = oldConstraint->getOverloadChoice();
625+
if (overloadChoice.isDecl() &&
626+
mustConsider(overloadChoice.getDecl()))
627+
hasMustConsider = true;
633628
}
634-
635-
if (mustConsider) {
636-
bool hasMustConsider = false;
637-
for (auto oldConstraint : oldConstraints) {
638-
auto overloadChoice = oldConstraint->getOverloadChoice();
639-
if (overloadChoice.isDecl() &&
640-
mustConsider(overloadChoice.getDecl()))
641-
hasMustConsider = true;
642-
}
643-
if (hasMustConsider) {
644-
continue;
645-
}
629+
if (hasMustConsider) {
630+
return;
646631
}
632+
}
647633

648-
// Copy over the existing bindings, dividing the constraints up
649-
// into "favored" and non-favored lists.
650-
SmallVector<Constraint *, 4> favoredConstraints;
651-
SmallVector<Constraint *, 4> fallbackConstraints;
652-
for (auto oldConstraint : oldConstraints) {
653-
if (!oldConstraint->getOverloadChoice().isDecl())
654-
continue;
655-
auto decl = oldConstraint->getOverloadChoice().getDecl();
656-
if (!decl->getAttrs().isUnavailable(CS.getASTContext()) &&
657-
isFavored(decl))
658-
favoredConstraints.push_back(oldConstraint);
659-
else
660-
fallbackConstraints.push_back(oldConstraint);
661-
}
634+
// Copy over the existing bindings, dividing the constraints up
635+
// into "favored" and non-favored lists.
636+
SmallVector<Constraint *, 4> favoredConstraints;
637+
SmallVector<Constraint *, 4> fallbackConstraints;
638+
for (auto oldConstraint : oldConstraints) {
639+
if (!oldConstraint->getOverloadChoice().isDecl())
640+
return;
641+
auto decl = oldConstraint->getOverloadChoice().getDecl();
642+
if (!decl->getAttrs().isUnavailable(CS.getASTContext()) &&
643+
isFavored(decl))
644+
favoredConstraints.push_back(oldConstraint);
645+
else
646+
fallbackConstraints.push_back(oldConstraint);
647+
}
662648

663-
// If we did not find any favored constraints, we're done.
664-
if (favoredConstraints.empty()) break;
649+
// If we did not find any favored constraints, we're done.
650+
if (favoredConstraints.empty()) return;
665651

666-
if (favoredConstraints.size() == 1) {
667-
auto overloadChoice = favoredConstraints[0]->getOverloadChoice();
668-
auto overloadType = overloadChoice.getDecl()->getInterfaceType();
669-
auto resultType = overloadType->getAs<AnyFunctionType>()->getResult();
670-
CS.setFavoredType(expr, resultType.getPointer());
671-
}
652+
if (favoredConstraints.size() == 1) {
653+
auto overloadChoice = favoredConstraints[0]->getOverloadChoice();
654+
auto overloadType = overloadChoice.getDecl()->getInterfaceType();
655+
auto resultType = overloadType->getAs<AnyFunctionType>()->getResult();
656+
CS.setFavoredType(expr, resultType.getPointer());
657+
}
672658

673-
// Remove the original constraint from the inactive constraint
674-
// list and add the new one.
675-
CS.removeInactiveConstraint(disjunction);
659+
// Remove the original constraint from the inactive constraint
660+
// list and add the new one.
661+
CS.removeInactiveConstraint(disjunction);
676662

677-
// Create the disjunction of favored constraints.
678-
auto favoredConstraintsDisjunction =
679-
Constraint::createDisjunction(CS,
680-
favoredConstraints,
681-
csLoc);
682-
683-
favoredConstraintsDisjunction->setFavored();
684-
685-
llvm::SmallVector<Constraint *, 2> aggregateConstraints;
686-
aggregateConstraints.push_back(favoredConstraintsDisjunction);
663+
// Create the disjunction of favored constraints.
664+
auto favoredConstraintsDisjunction =
665+
Constraint::createDisjunction(CS, favoredConstraints, csLoc);
666+
favoredConstraintsDisjunction->setFavored();
687667

688-
if (!fallbackConstraints.empty()) {
689-
// Find the disjunction of fallback constraints. If any
690-
// constraints were added here, create a new disjunction.
691-
Constraint *fallbackConstraintsDisjunction =
692-
Constraint::createDisjunction(CS, fallbackConstraints, csLoc);
668+
llvm::SmallVector<Constraint *, 2> aggregateConstraints;
669+
aggregateConstraints.push_back(favoredConstraintsDisjunction);
693670

694-
aggregateConstraints.push_back(fallbackConstraintsDisjunction);
695-
}
671+
if (!fallbackConstraints.empty()) {
672+
// Find the disjunction of fallback constraints. If any
673+
// constraints were added here, create a new disjunction.
674+
Constraint *fallbackConstraintsDisjunction =
675+
Constraint::createDisjunction(CS, fallbackConstraints, csLoc);
696676

697-
CS.addDisjunctionConstraint(aggregateConstraints, csLoc);
698-
break;
677+
aggregateConstraints.push_back(fallbackConstraintsDisjunction);
699678
}
679+
680+
CS.addDisjunctionConstraint(aggregateConstraints, csLoc);
700681
}
701682

702683
size_t getOperandCount(Type t) {

lib/Sema/ConstraintSystem.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3122,12 +3122,14 @@ class ConstraintSystem {
31223122
bool restoreOnFail,
31233123
llvm::function_ref<bool(Constraint *)> pred);
31243124

3125+
public:
31253126
// Given a type variable, attempt to find the disjunction of
31263127
// bind overloads associated with it. This may return null in cases where
31273128
// the disjunction has either not been created or binds the type variable
31283129
// in some manner other than by binding overloads.
31293130
Constraint *getUnboundBindOverloadDisjunction(TypeVariableType *tyvar);
31303131

3132+
private:
31313133
/// Given a type variable that might represent an overload set, retrieve
31323134
///
31333135
/// \returns the set of overload choices to which this type variable
@@ -3748,7 +3750,7 @@ class TypeVarBindingProducer : public BindingProducer<TypeVariableBinding> {
37483750
/// easy to work with disjunction and encapsulates
37493751
/// some other important information such as locator.
37503752
class DisjunctionChoiceProducer : public BindingProducer<DisjunctionChoice> {
3751-
// The disjunciton choices that this producer will iterate through.
3753+
// The disjunction choices that this producer will iterate through.
37523754
ArrayRef<Constraint *> Choices;
37533755

37543756
// The ordering of disjunction choices. We index into Choices

0 commit comments

Comments
 (0)