Skip to content

Commit 8da7501

Browse files
hamishknightxedin
authored andcommitted
[Sema] Add getDeclOrNull to OverloadChoice
This helps clean up a bunch of call sites. (cherry picked from commit 8aa7401)
1 parent d761f94 commit 8da7501

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
@@ -4591,8 +4591,7 @@ namespace {
45914591
buildKeyPathPropertyComponent(const SelectedOverload &overload,
45924592
SourceLoc componentLoc,
45934593
ConstraintLocator *locator) {
4594-
if (overload.choice.isDecl()) {
4595-
auto property = overload.choice.getDecl();
4594+
if (auto *property = overload.choice.getDeclOrNull()) {
45964595
// Key paths can only refer to properties currently.
45974596
auto varDecl = cast<VarDecl>(property);
45984597
// Key paths don't work with mutating-get properties.
@@ -4909,10 +4908,8 @@ static ConcreteDeclRef resolveLocatorToDecl(
49094908
// Overloaded and unresolved cases: find the resolved overload.
49104909
auto anchorLocator = cs.getConstraintLocator(anchor);
49114910
if (auto selected = findOvlChoice(anchorLocator)) {
4912-
if (selected->choice.isDecl())
4913-
return getConcreteDeclRef(selected->choice.getDecl(),
4914-
selected->openedType,
4915-
anchorLocator);
4911+
if (auto *decl = selected->choice.getDeclOrNull())
4912+
return getConcreteDeclRef(decl, selected->openedType, anchorLocator);
49164913
}
49174914
}
49184915

@@ -4921,10 +4918,8 @@ static ConcreteDeclRef resolveLocatorToDecl(
49214918
auto anchorLocator = cs.getConstraintLocator(anchor,
49224919
ConstraintLocator::UnresolvedMember);
49234920
if (auto selected = findOvlChoice(anchorLocator)) {
4924-
if (selected->choice.isDecl())
4925-
return getConcreteDeclRef(selected->choice.getDecl(),
4926-
selected->openedType,
4927-
anchorLocator);
4921+
if (auto *decl = selected->choice.getDeclOrNull())
4922+
return getConcreteDeclRef(decl, selected->openedType, anchorLocator);
49284923
}
49294924
}
49304925

@@ -4933,10 +4928,8 @@ static ConcreteDeclRef resolveLocatorToDecl(
49334928
auto anchorLocator = cs.getConstraintLocator(anchor,
49344929
ConstraintLocator::Member);
49354930
if (auto selected = findOvlChoice(anchorLocator)) {
4936-
if (selected->choice.isDecl())
4937-
return getConcreteDeclRef(selected->choice.getDecl(),
4938-
selected->openedType,
4939-
anchorLocator);
4931+
if (auto *decl = selected->choice.getDeclOrNull())
4932+
return getConcreteDeclRef(decl, selected->openedType, anchorLocator);
49404933
}
49414934
}
49424935

@@ -4949,10 +4942,8 @@ static ConcreteDeclRef resolveLocatorToDecl(
49494942
auto anchorLocator =
49504943
cs.getConstraintLocator(anchor, ConstraintLocator::SubscriptMember);
49514944
if (auto selected = findOvlChoice(anchorLocator)) {
4952-
if (selected->choice.isDecl())
4953-
return getConcreteDeclRef(selected->choice.getDecl(),
4954-
selected->openedType,
4955-
anchorLocator);
4945+
if (auto *decl = selected->choice.getDeclOrNull())
4946+
return getConcreteDeclRef(decl, selected->openedType, anchorLocator);
49564947
}
49574948
}
49584949

lib/Sema/CSDiag.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -940,9 +940,8 @@ bool FailureDiagnosis::diagnoseGeneralOverloadFailure(Constraint *constraint) {
940940
if (constraint->getKind() == ConstraintKind::Disjunction) {
941941
for (auto elt : constraint->getNestedConstraints()) {
942942
if (elt->getKind() != ConstraintKind::BindOverload) continue;
943-
if (!elt->getOverloadChoice().isDecl()) continue;
944-
auto candidate = elt->getOverloadChoice().getDecl();
945-
diagnose(candidate, diag::found_candidate);
943+
if (auto *candidate = elt->getOverloadChoice().getDeclOrNull())
944+
diagnose(candidate, diag::found_candidate);
946945
}
947946
}
948947

lib/Sema/CSGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,9 @@ namespace {
602602
unsigned numFavoredConstraints = 0;
603603
Constraint *firstFavored = nullptr;
604604
for (auto constraint : disjunction->getNestedConstraints()) {
605-
if (!constraint->getOverloadChoice().isDecl())
605+
auto *decl = constraint->getOverloadChoice().getDeclOrNull();
606+
if (!decl)
606607
continue;
607-
auto decl = constraint->getOverloadChoice().getDecl();
608608

609609
if (mustConsider && mustConsider(decl)) {
610610
// Roll back any constraints we favored.

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -828,8 +828,7 @@ getCalleeDeclAndArgs(ConstraintSystem &cs,
828828
hasTrailingClosure, nullptr);
829829

830830
// If there's a declaration, return it.
831-
if (choice->isDecl()) {
832-
auto decl = choice->getDecl();
831+
if (auto *decl = choice->getDeclOrNull()) {
833832
bool hasCurriedSelf = false;
834833
if (decl->getDeclContext()->isTypeContext()) {
835834
if (auto function = dyn_cast<AbstractFunctionDecl>(decl)) {
@@ -4570,9 +4569,7 @@ fixMemberRef(ConstraintSystem &cs, Type baseTy,
45704569
Optional<MemberLookupResult::UnviableReason> reason = None) {
45714570
// Not all of the choices handled here are going
45724571
// to refer to a declaration.
4573-
if (choice.isDecl()) {
4574-
auto *decl = choice.getDecl();
4575-
4572+
if (auto *decl = choice.getDeclOrNull()) {
45764573
if (auto *CD = dyn_cast<ConstructorDecl>(decl)) {
45774574
if (auto *fix = validateInitializerRef(cs, CD, locator))
45784575
return fix;

lib/Sema/CSSolver.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,11 +2104,9 @@ void ConstraintSystem::partitionDisjunction(
21042104
forEachChoice(Choices, [&](unsigned index, Constraint *constraint) -> bool {
21052105
if (constraint->getKind() != ConstraintKind::BindOverload)
21062106
return false;
2107-
if (!constraint->getOverloadChoice().isDecl())
2108-
return false;
21092107

2110-
auto *decl = constraint->getOverloadChoice().getDecl();
2111-
auto *funcDecl = dyn_cast<FuncDecl>(decl);
2108+
auto *decl = constraint->getOverloadChoice().getDeclOrNull();
2109+
auto *funcDecl = dyn_cast_or_null<FuncDecl>(decl);
21122110
if (!funcDecl)
21132111
return false;
21142112

lib/Sema/ConstraintSystem.cpp

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

2137-
if (choice.isDecl()) {
2138-
auto decl = choice.getDecl();
2137+
if (auto *decl = choice.getDeclOrNull()) {
21392138
// If we're binding to an init member, the 'throws' need to line up between
21402139
// the bound and reference types.
21412140
if (auto CD = dyn_cast<ConstructorDecl>(decl)) {
@@ -2457,11 +2456,6 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
24572456
if (solutions.empty())
24582457
return false;
24592458

2460-
auto getOverloadDecl = [&](SelectedOverload &overload) -> ValueDecl * {
2461-
auto &choice = overload.choice;
2462-
return choice.isDecl() ? choice.getDecl() : nullptr;
2463-
};
2464-
24652459
// Problems related to fixes forming ambiguous solution set
24662460
// could only be diagnosed (at the moment), if all of the fixes
24672461
// have the same callee locator, which means they fix different
@@ -2490,7 +2484,7 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
24902484
if (!overload)
24912485
return false;
24922486

2493-
auto *decl = getOverloadDecl(*overload);
2487+
auto *decl = overload->choice.getDeclOrNull();
24942488
if (!decl)
24952489
return false;
24962490

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)