Skip to content

Commit 8aa7401

Browse files
committed
[Sema] Add getDeclOrNull to OverloadChoice
This helps clean up a bunch of call sites.
1 parent 63df26a commit 8aa7401

File tree

7 files changed

+25
-40
lines changed

7 files changed

+25
-40
lines changed

lib/Sema/CSApply.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4479,8 +4479,7 @@ namespace {
44794479
buildKeyPathPropertyComponent(const SelectedOverload &overload,
44804480
SourceLoc componentLoc,
44814481
ConstraintLocator *locator) {
4482-
if (overload.choice.isDecl()) {
4483-
auto property = overload.choice.getDecl();
4482+
if (auto *property = overload.choice.getDeclOrNull()) {
44844483
// Key paths can only refer to properties currently.
44854484
auto varDecl = cast<VarDecl>(property);
44864485
// Key paths don't work with mutating-get properties.
@@ -4784,10 +4783,8 @@ static ConcreteDeclRef resolveLocatorToDecl(
47844783
// Overloaded and unresolved cases: find the resolved overload.
47854784
auto anchorLocator = cs.getConstraintLocator(anchor);
47864785
if (auto selected = findOvlChoice(anchorLocator)) {
4787-
if (selected->choice.isDecl())
4788-
return getConcreteDeclRef(selected->choice.getDecl(),
4789-
selected->openedType,
4790-
anchorLocator);
4786+
if (auto *decl = selected->choice.getDeclOrNull())
4787+
return getConcreteDeclRef(decl, selected->openedType, anchorLocator);
47914788
}
47924789
}
47934790

@@ -4796,10 +4793,8 @@ static ConcreteDeclRef resolveLocatorToDecl(
47964793
auto anchorLocator = cs.getConstraintLocator(anchor,
47974794
ConstraintLocator::UnresolvedMember);
47984795
if (auto selected = findOvlChoice(anchorLocator)) {
4799-
if (selected->choice.isDecl())
4800-
return getConcreteDeclRef(selected->choice.getDecl(),
4801-
selected->openedType,
4802-
anchorLocator);
4796+
if (auto *decl = selected->choice.getDeclOrNull())
4797+
return getConcreteDeclRef(decl, selected->openedType, anchorLocator);
48034798
}
48044799
}
48054800

@@ -4808,10 +4803,8 @@ static ConcreteDeclRef resolveLocatorToDecl(
48084803
auto anchorLocator = cs.getConstraintLocator(anchor,
48094804
ConstraintLocator::Member);
48104805
if (auto selected = findOvlChoice(anchorLocator)) {
4811-
if (selected->choice.isDecl())
4812-
return getConcreteDeclRef(selected->choice.getDecl(),
4813-
selected->openedType,
4814-
anchorLocator);
4806+
if (auto *decl = selected->choice.getDeclOrNull())
4807+
return getConcreteDeclRef(decl, selected->openedType, anchorLocator);
48154808
}
48164809
}
48174810

@@ -4824,10 +4817,8 @@ static ConcreteDeclRef resolveLocatorToDecl(
48244817
auto anchorLocator =
48254818
cs.getConstraintLocator(anchor, ConstraintLocator::SubscriptMember);
48264819
if (auto selected = findOvlChoice(anchorLocator)) {
4827-
if (selected->choice.isDecl())
4828-
return getConcreteDeclRef(selected->choice.getDecl(),
4829-
selected->openedType,
4830-
anchorLocator);
4820+
if (auto *decl = selected->choice.getDeclOrNull())
4821+
return getConcreteDeclRef(decl, selected->openedType, anchorLocator);
48314822
}
48324823
}
48334824

lib/Sema/CSDiag.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -867,9 +867,8 @@ bool FailureDiagnosis::diagnoseGeneralOverloadFailure(Constraint *constraint) {
867867
if (constraint->getKind() == ConstraintKind::Disjunction) {
868868
for (auto elt : constraint->getNestedConstraints()) {
869869
if (elt->getKind() != ConstraintKind::BindOverload) continue;
870-
if (!elt->getOverloadChoice().isDecl()) continue;
871-
auto candidate = elt->getOverloadChoice().getDecl();
872-
diagnose(candidate, diag::found_candidate);
870+
if (auto *candidate = elt->getOverloadChoice().getDeclOrNull())
871+
diagnose(candidate, diag::found_candidate);
873872
}
874873
}
875874

lib/Sema/CSGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,9 +622,9 @@ namespace {
622622
unsigned numFavoredConstraints = 0;
623623
Constraint *firstFavored = nullptr;
624624
for (auto constraint : disjunction->getNestedConstraints()) {
625-
if (!constraint->getOverloadChoice().isDecl())
625+
auto *decl = constraint->getOverloadChoice().getDeclOrNull();
626+
if (!decl)
626627
continue;
627-
auto decl = constraint->getOverloadChoice().getDecl();
628628

629629
if (mustConsider && mustConsider(decl)) {
630630
// Roll back any constraints we favored.

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -859,8 +859,7 @@ getCalleeDeclAndArgs(ConstraintSystem &cs,
859859
hasTrailingClosure);
860860

861861
// If there's a declaration, return it.
862-
if (choice->isDecl()) {
863-
auto decl = choice->getDecl();
862+
if (auto *decl = choice->getDeclOrNull()) {
864863
bool hasCurriedSelf = false;
865864
if (decl->getDeclContext()->isTypeContext()) {
866865
if (auto function = dyn_cast<AbstractFunctionDecl>(decl)) {
@@ -4475,9 +4474,7 @@ fixMemberRef(ConstraintSystem &cs, Type baseTy,
44754474
Optional<MemberLookupResult::UnviableReason> reason = None) {
44764475
// Not all of the choices handled here are going
44774476
// to refer to a declaration.
4478-
if (choice.isDecl()) {
4479-
auto *decl = choice.getDecl();
4480-
4477+
if (auto *decl = choice.getDeclOrNull()) {
44814478
if (auto *CD = dyn_cast<ConstructorDecl>(decl)) {
44824479
if (auto *fix = validateInitializerRef(cs, CD, locator))
44834480
return fix;

lib/Sema/CSSolver.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,11 +2092,9 @@ void ConstraintSystem::partitionDisjunction(
20922092
forEachChoice(Choices, [&](unsigned index, Constraint *constraint) -> bool {
20932093
if (constraint->getKind() != ConstraintKind::BindOverload)
20942094
return false;
2095-
if (!constraint->getOverloadChoice().isDecl())
2096-
return false;
20972095

2098-
auto *decl = constraint->getOverloadChoice().getDecl();
2099-
auto *funcDecl = dyn_cast<FuncDecl>(decl);
2096+
auto *decl = constraint->getOverloadChoice().getDeclOrNull();
2097+
auto *funcDecl = dyn_cast_or_null<FuncDecl>(decl);
21002098
if (!funcDecl)
21012099
return false;
21022100

lib/Sema/ConstraintSystem.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,8 +2113,7 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
21132113
}
21142114
assert(!refType->hasTypeParameter() && "Cannot have a dependent type here");
21152115

2116-
if (choice.isDecl()) {
2117-
auto decl = choice.getDecl();
2116+
if (auto *decl = choice.getDeclOrNull()) {
21182117
// If we're binding to an init member, the 'throws' need to line up between
21192118
// the bound and reference types.
21202119
if (auto CD = dyn_cast<ConstructorDecl>(decl)) {
@@ -2436,11 +2435,6 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
24362435
if (solutions.empty())
24372436
return false;
24382437

2439-
auto getOverloadDecl = [&](SelectedOverload &overload) -> ValueDecl * {
2440-
auto &choice = overload.choice;
2441-
return choice.isDecl() ? choice.getDecl() : nullptr;
2442-
};
2443-
24442438
// Problems related to fixes forming ambiguous solution set
24452439
// could only be diagnosed (at the moment), if all of the fixes
24462440
// have the same callee locator, which means they fix different
@@ -2469,7 +2463,7 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
24692463
if (!overload)
24702464
return false;
24712465

2472-
auto *decl = getOverloadDecl(*overload);
2466+
auto *decl = overload->choice.getDeclOrNull();
24732467
if (!decl)
24742468
return false;
24752469

lib/Sema/OverloadChoice.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ class OverloadChoice {
244244
return DeclOrKind.get<ValueDecl*>();
245245
}
246246

247+
/// Retrieves the declaration that corresponds to this overload choice, or
248+
/// \c nullptr if this choice is not for a declaration.
249+
ValueDecl *getDeclOrNull() const {
250+
return isDecl() ? getDecl() : nullptr;
251+
}
252+
247253
/// Returns true if this is either a decl for an optional that was
248254
/// declared as one that can be implicitly unwrapped, or is a
249255
/// function-typed decl that has a return value that is implicitly

0 commit comments

Comments
 (0)