Skip to content

Commit 61d0486

Browse files
committed
[CS] NFC: Factor out hasKeyPathComponent
1 parent 4a54598 commit 61d0486

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

lib/Sema/ConstraintLocator.cpp

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -596,41 +596,34 @@ bool ConstraintLocator::isResultOfKeyPathDynamicMemberLookup() const {
596596
});
597597
}
598598

599-
bool ConstraintLocator::isKeyPathSubscriptComponent() const {
600-
auto *anchor = getAsExpr(getAnchor());
601-
auto *KPE = dyn_cast_or_null<KeyPathExpr>(anchor);
599+
static bool hasKeyPathComponent(
600+
const ConstraintLocator *loc,
601+
llvm::function_ref<bool(KeyPathExpr::Component::Kind)> predicate) {
602+
auto *KPE = getAsExpr<KeyPathExpr>(loc->getAnchor());
602603
if (!KPE)
603604
return false;
604605

605-
using ComponentKind = KeyPathExpr::Component::Kind;
606-
return llvm::any_of(getPath(), [&](const LocatorPathElt &elt) {
606+
return llvm::any_of(loc->getPath(), [&](const LocatorPathElt &elt) {
607607
auto keyPathElt = elt.getAs<LocatorPathElt::KeyPathComponent>();
608608
if (!keyPathElt)
609609
return false;
610610

611-
auto index = keyPathElt->getIndex();
612-
auto &component = KPE->getComponents()[index];
613-
return component.getKind() == ComponentKind::Subscript ||
614-
component.getKind() == ComponentKind::UnresolvedSubscript;
611+
auto &component = KPE->getComponents()[keyPathElt->getIndex()];
612+
return predicate(component.getKind());
615613
});
616614
}
617615

618-
bool ConstraintLocator::isKeyPathMemberComponent() const {
619-
auto *anchor = getAsExpr(getAnchor());
620-
auto *KPE = dyn_cast_or_null<KeyPathExpr>(anchor);
621-
if (!KPE)
622-
return false;
623-
624-
using ComponentKind = KeyPathExpr::Component::Kind;
625-
return llvm::any_of(getPath(), [&](const LocatorPathElt &elt) {
626-
auto keyPathElt = elt.getAs<LocatorPathElt::KeyPathComponent>();
627-
if (!keyPathElt)
628-
return false;
616+
bool ConstraintLocator::isKeyPathSubscriptComponent() const {
617+
return hasKeyPathComponent(this, [](auto kind) {
618+
return kind == KeyPathExpr::Component::Kind::Subscript ||
619+
kind == KeyPathExpr::Component::Kind::UnresolvedSubscript;
620+
});
621+
}
629622

630-
auto index = keyPathElt->getIndex();
631-
auto &component = KPE->getComponents()[index];
632-
return component.getKind() == ComponentKind::Member ||
633-
component.getKind() == ComponentKind::UnresolvedMember;
623+
bool ConstraintLocator::isKeyPathMemberComponent() const {
624+
return hasKeyPathComponent(this, [](auto kind) {
625+
return kind == KeyPathExpr::Component::Kind::Member ||
626+
kind == KeyPathExpr::Component::Kind::UnresolvedMember;
634627
});
635628
}
636629

0 commit comments

Comments
 (0)