Repository: apple/swift
File: lib/Sema/ConstraintSystem.cpp (C++)

Code before:
      return getConstraintLocator(anchor, newPath);
    }
  }

  {
    // Pattern match is always a callee regardless of what comes after it.
    auto iter = path.rbegin();
    if (locator->findLast<LocatorPathElt::PatternMatch>(iter)) {
      auto newPath = path.drop_back(iter - path.rbegin());
      return getConstraintLocator(anchor, newPath);
    }
  }

  if (locator->findLast<LocatorPathElt::DynamicCallable>()) {
    return getConstraintLocator(anchor, LocatorPathElt::ApplyFunction());
  }

  if (locator->isLastElement<LocatorPathElt::ArgumentAttribute>()) {
    auto argLoc = getConstraintLocator(anchor, path.drop_back());
    return getCalleeLocator(argLoc, lookThroughApply, getType, simplifyType,
                            getOverloadFor);
  }

  {
    auto iter = path.rbegin();
    if (locator->findLast<LocatorPathElt::KeyPathComponent>(iter)) {
      auto componentElt = iter->castTo<LocatorPathElt::KeyPathComponent>();
      auto *kpExpr = castToExpr<KeyPathExpr>(anchor);
      auto component = kpExpr->getComponents()[componentElt.getIndex()];

      using ComponentKind = KeyPathExpr::Component::Kind;
      if (component.getKind() == ComponentKind::Apply ||
          component.getKind() == ComponentKind::UnresolvedApply) {
        // The callee is the previous component in the list.
        // We must specifically target index - 1 to distinguish this call from others
        // and avoid "recordArgumentList" collisions.
        if (componentElt.getIndex() > 0) {
          unsigned prevIndex = componentElt.getIndex() - 1;

          // Drop the current component (the Apply)
          auto prefix = path.drop_back(iter - path.rbegin() + 1);

          // Build the new path: Prefix + KeyPathComponent(prevIndex)
          SmallVector<LocatorPathElt, 4> newPathVec(prefix.begin(), prefix.end());
          newPathVec.push_back(LocatorPathElt::KeyPathComponent(prevIndex));

          return getConstraintLocator(anchor, newPathVec);
        }
          }
    }
  }

Review comment (suggestion):
Please explain to me why we need to this instead of simply changing `getCalleeLocator` like the following and no other changes?

```
diff --git a/lib/Sema/ConstraintSystem.cpp b/lib/Sema/ConstraintSystem.cpp
index cbddf562b79..c3d2bc63e32 100644
--- a/lib/Sema/ConstraintSystem.cpp
+++ b/lib/Sema/ConstraintSystem.cpp
@@ -784,7 +784,8 @@ ConstraintLocator *ConstraintSystem::getCalleeLocator(
       break;
     case ComponentKind::UnresolvedApply:
     case ComponentKind::Apply:
-      return getConstraintLocator(anchor, *componentElt);
+      return getConstraintLocator(anchor, LocatorPathElt::KeyPathComponent(
+                                              componentElt->getIndex() - 1));
     case ComponentKind::Invalid:
     case ComponentKind::OptionalForce:
     case ComponentKind::OptionalChain:
```

Code after:
      return getConstraintLocator(anchor, newPath);
    }
  }

  {
    // Pattern match is always a callee regardless of what comes after it.
    auto iter = path.rbegin();
    if (locator->findLast<LocatorPathElt::PatternMatch>(iter)) {
      auto newPath = path.drop_back(iter - path.rbegin());
      return getConstraintLocator(anchor, newPath);
    }
  }

  if (locator->findLast<LocatorPathElt::DynamicCallable>()) {
    return getConstraintLocator(anchor, LocatorPathElt::ApplyFunction());
  }

  if (locator->isLastElement<LocatorPathElt::ArgumentAttribute>()) {
    auto argLoc = getConstraintLocator(anchor, path.drop_back());
    return getCalleeLocator(argLoc, lookThroughApply, getType, simplifyType,
                            getOverloadFor);
  }

  // If we have a locator that starts with a key path component element, we
  // may have a callee given by a property, subscript, initializer, or method
  // component.
  if (auto componentElt =
      locator->getFirstElementAs<LocatorPathElt::KeyPathComponent>()) {
    auto *kpExpr = castToExpr<KeyPathExpr>(anchor);
    auto component = kpExpr->getComponents()[componentElt->getIndex()];

    using ComponentKind = KeyPathExpr::Component::Kind;
    switch (component.getKind()) {
    case ComponentKind::UnresolvedSubscript:
    case ComponentKind::Subscript:
      // For a subscript the callee is given by 'component -> subscript member'.
      return getConstraintLocator(
                                  anchor, {*componentElt, ConstraintLocator::SubscriptMember});
    case ComponentKind::UnresolvedMember:
    case ComponentKind::Member:
      // For a property, the choice is just given by the component.
      return getConstraintLocator(anchor, *componentElt);
    case ComponentKind::TupleElement:
      llvm_unreachable("Not implemented by CSGen");
      break;
    case ComponentKind::UnresolvedApply:
    case ComponentKind::Apply:
      return getConstraintLocator(anchor, LocatorPathElt::KeyPathComponent(componentElt->getIndex() - 1));
    case ComponentKind::Invalid:
    case ComponentKind::OptionalForce:
    case ComponentKind::OptionalChain: