Skip to content

[ConstraintSystem] Simplify addOverloadSet #23194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 17 additions & 33 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1490,55 +1490,39 @@ void ConstraintSystem::addOverloadSet(Type boundType,
return;
}

auto recordChoice = [&](SmallVectorImpl<Constraint *> &choices,
const OverloadChoice &choice,
bool isFavored = false) {
auto *constraint = Constraint::createBindOverload(*this, boundType, choice,
useDC, locator);
if (isFavored)
constraint->setFavored();

choices.push_back(constraint);
};

SmallVector<Constraint *, 4> overloads;

// As we do for other favored constraints, if a favored overload has been
// specified, let it be the first term in the disjunction.
if (favoredChoice) {
auto bindOverloadConstraint =
Constraint::createBindOverload(*this,
boundType,
*favoredChoice,
useDC,
locator);

assert((!favoredChoice->isDecl() ||
!favoredChoice->getDecl()->getAttrs().isUnavailable(
getASTContext())) &&
"Cannot make unavailable decl favored!");
bindOverloadConstraint->setFavored();

overloads.push_back(bindOverloadConstraint);
recordChoice(overloads, *favoredChoice, /*isFavored=*/true);
}

for (auto &choice : choices) {
if (favoredChoice && (favoredChoice == &choice))
continue;

overloads.push_back(Constraint::createBindOverload(*this, boundType, choice,
useDC, locator));
}

auto innerDisjunction = Constraint::createDisjunction(*this, overloads,
locator, ForgetChoice);
if (outerAlternatives.empty()) {
if (favoredChoice)
innerDisjunction->setFavored();

addUnsolvedConstraint(innerDisjunction);
return;
recordChoice(overloads, choice);
}

SmallVector<Constraint *, 4> outerConstraints;
outerConstraints.push_back(innerDisjunction);
innerDisjunction->setFavored();
for (auto choice : outerAlternatives) {
outerConstraints.push_back(Constraint::createBindOverload(
*this, boundType, choice,
useDC, locator));
}
for (auto &choice : outerAlternatives)
recordChoice(overloads, choice);

addDisjunctionConstraint(outerConstraints, locator, ForgetChoice, favoredChoice);
addDisjunctionConstraint(overloads, locator, ForgetChoice);
}

/// If we're resolving an overload set with a decl that has special type
Expand Down
10 changes: 4 additions & 6 deletions lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2007,16 +2007,14 @@ class ConstraintSystem {
ConstraintLocatorBuilder locator);

/// Add a disjunction constraint.
void addDisjunctionConstraint(ArrayRef<Constraint *> constraints,
ConstraintLocatorBuilder locator,
RememberChoice_t rememberChoice = ForgetChoice,
bool isFavored = false) {
void
addDisjunctionConstraint(ArrayRef<Constraint *> constraints,
ConstraintLocatorBuilder locator,
RememberChoice_t rememberChoice = ForgetChoice) {
auto constraint =
Constraint::createDisjunction(*this, constraints,
getConstraintLocator(locator),
rememberChoice);
if (isFavored)
constraint->setFavored();

addUnsolvedConstraint(constraint);
}
Expand Down