Skip to content

Commit fbd7949

Browse files
authored
Merge pull request #23194 from xedin/simplify-add-overload-set
[ConstraintSystem] Simplify `addOverloadSet`
2 parents 20a1781 + c2590c0 commit fbd7949

File tree

2 files changed

+21
-39
lines changed

2 files changed

+21
-39
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,55 +1490,39 @@ void ConstraintSystem::addOverloadSet(Type boundType,
14901490
return;
14911491
}
14921492

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+
14931504
SmallVector<Constraint *, 4> overloads;
1494-
14951505
// As we do for other favored constraints, if a favored overload has been
14961506
// specified, let it be the first term in the disjunction.
14971507
if (favoredChoice) {
1498-
auto bindOverloadConstraint =
1499-
Constraint::createBindOverload(*this,
1500-
boundType,
1501-
*favoredChoice,
1502-
useDC,
1503-
locator);
1504-
15051508
assert((!favoredChoice->isDecl() ||
15061509
!favoredChoice->getDecl()->getAttrs().isUnavailable(
15071510
getASTContext())) &&
15081511
"Cannot make unavailable decl favored!");
1509-
bindOverloadConstraint->setFavored();
1510-
1511-
overloads.push_back(bindOverloadConstraint);
1512+
recordChoice(overloads, *favoredChoice, /*isFavored=*/true);
15121513
}
1513-
1514+
15141515
for (auto &choice : choices) {
15151516
if (favoredChoice && (favoredChoice == &choice))
15161517
continue;
1517-
1518-
overloads.push_back(Constraint::createBindOverload(*this, boundType, choice,
1519-
useDC, locator));
1520-
}
15211518

1522-
auto innerDisjunction = Constraint::createDisjunction(*this, overloads,
1523-
locator, ForgetChoice);
1524-
if (outerAlternatives.empty()) {
1525-
if (favoredChoice)
1526-
innerDisjunction->setFavored();
1527-
1528-
addUnsolvedConstraint(innerDisjunction);
1529-
return;
1519+
recordChoice(overloads, choice);
15301520
}
15311521

1532-
SmallVector<Constraint *, 4> outerConstraints;
1533-
outerConstraints.push_back(innerDisjunction);
1534-
innerDisjunction->setFavored();
1535-
for (auto choice : outerAlternatives) {
1536-
outerConstraints.push_back(Constraint::createBindOverload(
1537-
*this, boundType, choice,
1538-
useDC, locator));
1539-
}
1522+
for (auto &choice : outerAlternatives)
1523+
recordChoice(overloads, choice);
15401524

1541-
addDisjunctionConstraint(outerConstraints, locator, ForgetChoice, favoredChoice);
1525+
addDisjunctionConstraint(overloads, locator, ForgetChoice);
15421526
}
15431527

15441528
/// If we're resolving an overload set with a decl that has special type

lib/Sema/ConstraintSystem.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,16 +2007,14 @@ class ConstraintSystem {
20072007
ConstraintLocatorBuilder locator);
20082008

20092009
/// Add a disjunction constraint.
2010-
void addDisjunctionConstraint(ArrayRef<Constraint *> constraints,
2011-
ConstraintLocatorBuilder locator,
2012-
RememberChoice_t rememberChoice = ForgetChoice,
2013-
bool isFavored = false) {
2010+
void
2011+
addDisjunctionConstraint(ArrayRef<Constraint *> constraints,
2012+
ConstraintLocatorBuilder locator,
2013+
RememberChoice_t rememberChoice = ForgetChoice) {
20142014
auto constraint =
20152015
Constraint::createDisjunction(*this, constraints,
20162016
getConstraintLocator(locator),
20172017
rememberChoice);
2018-
if (isFavored)
2019-
constraint->setFavored();
20202018

20212019
addUnsolvedConstraint(constraint);
20222020
}

0 commit comments

Comments
 (0)