@@ -596,41 +596,34 @@ bool ConstraintLocator::isResultOfKeyPathDynamicMemberLookup() const {
596
596
});
597
597
}
598
598
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 ());
602
603
if (!KPE)
603
604
return false ;
604
605
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) {
607
607
auto keyPathElt = elt.getAs <LocatorPathElt::KeyPathComponent>();
608
608
if (!keyPathElt)
609
609
return false ;
610
610
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 ());
615
613
});
616
614
}
617
615
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
+ }
629
622
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;
634
627
});
635
628
}
636
629
0 commit comments