@@ -596,41 +596,41 @@ 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 ;
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+ }
623622
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 ;
623+ bool ConstraintLocator::isKeyPathMemberComponent () const {
624+ return hasKeyPathComponent (this , [](auto kind) {
625+ return kind == KeyPathExpr::Component::Kind::Member ||
626+ kind == KeyPathExpr::Component::Kind::UnresolvedMember;
627+ });
628+ }
629629
630- auto index = keyPathElt-> getIndex ();
631- auto &component = KPE-> getComponents ()[index];
632- return component. getKind () == ComponentKind::Member ||
633- component. getKind () == ComponentKind::UnresolvedMember ;
630+ bool ConstraintLocator::isKeyPathApplyComponent () const {
631+ return hasKeyPathComponent ( this , []( auto kind) {
632+ return kind == KeyPathExpr::Component::Kind::Apply ||
633+ kind == KeyPathExpr::Component::Kind::UnresolvedApply ;
634634 });
635635}
636636
0 commit comments