@@ -1480,56 +1480,31 @@ void ConstraintSystem::addOverloadSet(Type boundType,
14801480 ArrayRef<OverloadChoice> choices,
14811481 DeclContext *useDC,
14821482 ConstraintLocator *locator,
1483- OverloadChoice *favoredChoice,
1484- ArrayRef<OverloadChoice> outerAlternatives) {
1485- assert (!choices.empty () && " Empty overload set" );
1486-
1483+ OverloadChoice *favoredChoice) {
14871484 // If there is a single choice, add the bind overload directly.
1488- if (choices.size () == 1 && outerAlternatives. empty () ) {
1485+ if (choices.size () == 1 ) {
14891486 addBindOverloadConstraint (boundType, choices.front (), locator, useDC);
14901487 return ;
14911488 }
14921489
1493- auto recordChoice = [&](SmallVectorImpl<Constraint *> &choices,
1494- const OverloadChoice &choice,
1495- bool isFavored = false ) {
1496- auto *constraint = Constraint::createBindOverload (*this , boundType, choice,
1497- useDC, locator);
1498- if (isFavored)
1499- constraint->setFavored ();
1500-
1501- choices.push_back (constraint);
1502- };
1503-
1504- SmallVector<Constraint *, 4 > overloads;
1505- // As we do for other favored constraints, if a favored overload has been
1506- // specified, let it be the first term in the disjunction.
1507- if (favoredChoice) {
1508- assert ((!favoredChoice->isDecl () ||
1509- !favoredChoice->getDecl ()->getAttrs ().isUnavailable (
1510- getASTContext ())) &&
1511- " Cannot make unavailable decl favored!" );
1512- recordChoice (overloads, *favoredChoice, /* isFavored=*/ true );
1513- }
1514-
1515- for (auto &choice : choices) {
1516- if (favoredChoice && (favoredChoice == &choice))
1517- continue ;
1518-
1519- recordChoice (overloads, choice);
1520- }
1490+ SmallVector<Constraint *, 4 > candidates;
1491+ generateConstraints (candidates, boundType, choices, useDC, locator,
1492+ favoredChoice);
1493+ // For an overload set (disjunction) from newly generated candidates.
1494+ addOverloadSet (candidates, locator);
1495+ }
15211496
1522- if (!outerAlternatives.empty ()) {
1523- // If local scope has a single choice,
1524- // it should always be preferred.
1525- if (overloads.size () == 1 )
1526- overloads.front ()->setFavored ();
1497+ void ConstraintSystem::addOverloadSet (ArrayRef<Constraint *> choices,
1498+ ConstraintLocator *locator) {
1499+ assert (!choices.empty () && " Empty overload set" );
15271500
1528- for (auto &choice : outerAlternatives)
1529- recordChoice (overloads, choice);
1501+ // If there is a single choice, attempt it right away.
1502+ if (choices.size () == 1 ) {
1503+ simplifyConstraint (*choices.front ());
1504+ return ;
15301505 }
15311506
1532- addDisjunctionConstraint (overloads , locator, ForgetChoice);
1507+ addDisjunctionConstraint (choices , locator, ForgetChoice);
15331508}
15341509
15351510// / If we're resolving an overload set with a decl that has special type
@@ -2677,3 +2652,34 @@ Expr *constraints::getArgumentExpr(Expr *expr, unsigned index) {
26772652 assert (isa<TupleExpr>(argExpr));
26782653 return cast<TupleExpr>(argExpr)->getElement (index);
26792654}
2655+
2656+ void ConstraintSystem::generateConstraints (
2657+ SmallVectorImpl<Constraint *> &constraints, Type type,
2658+ ArrayRef<OverloadChoice> choices, DeclContext *useDC,
2659+ ConstraintLocator *locator, OverloadChoice *favoredChoice) {
2660+ auto recordChoice = [&](SmallVectorImpl<Constraint *> &choices,
2661+ const OverloadChoice &choice,
2662+ bool isFavored = false ) {
2663+ auto *constraint =
2664+ Constraint::createBindOverload (*this , type, choice, useDC, locator);
2665+ if (isFavored)
2666+ constraint->setFavored ();
2667+
2668+ choices.push_back (constraint);
2669+ };
2670+
2671+ if (favoredChoice) {
2672+ assert ((!favoredChoice->isDecl () ||
2673+ !favoredChoice->getDecl ()->getAttrs ().isUnavailable (
2674+ getASTContext ())) &&
2675+ " Cannot make unavailable decl favored!" );
2676+ recordChoice (constraints, *favoredChoice, /* isFavored=*/ true );
2677+ }
2678+
2679+ for (const auto &choice : choices) {
2680+ if (favoredChoice && (favoredChoice == &choice))
2681+ continue ;
2682+
2683+ recordChoice (constraints, choice);
2684+ }
2685+ }
0 commit comments