Skip to content

Commit e4e5b5e

Browse files
committed
Allow getCalleeLocator to not look through applies
Some clients may only want a callee locator for an immediate decl reference such as `x.foo`, but not for `x.foo()`.
1 parent e3df69a commit e4e5b5e

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,8 @@ ConstraintLocator *ConstraintSystem::getConstraintLocator(
415415
}
416416

417417
ConstraintLocator *
418-
ConstraintSystem::getCalleeLocator(ConstraintLocator *locator) {
418+
ConstraintSystem::getCalleeLocator(ConstraintLocator *locator,
419+
bool lookThroughApply) {
419420
auto *anchor = locator->getAnchor();
420421
assert(anchor && "Expected an anchor!");
421422

@@ -456,26 +457,28 @@ ConstraintSystem::getCalleeLocator(ConstraintLocator *locator) {
456457
if (isa<SubscriptExpr>(anchor))
457458
return getConstraintLocator(anchor, ConstraintLocator::SubscriptMember);
458459

459-
if (auto *applyExpr = dyn_cast<ApplyExpr>(anchor)) {
460-
auto *fnExpr = applyExpr->getFn();
461-
// For an apply of a metatype, we have a short-form constructor. Unlike
462-
// other locators to callees, these are anchored on the apply expression
463-
// rather than the function expr.
464-
auto fnTy = getFixedTypeRecursive(getType(fnExpr), /*wantRValue*/ true);
465-
if (fnTy->is<AnyMetatypeType>()) {
466-
auto *fnLocator =
467-
getConstraintLocator(applyExpr, ConstraintLocator::ApplyFunction);
468-
return getConstraintLocator(fnLocator,
469-
ConstraintLocator::ConstructorMember);
470-
}
460+
if (lookThroughApply) {
461+
if (auto *applyExpr = dyn_cast<ApplyExpr>(anchor)) {
462+
auto *fnExpr = applyExpr->getFn();
463+
// For an apply of a metatype, we have a short-form constructor. Unlike
464+
// other locators to callees, these are anchored on the apply expression
465+
// rather than the function expr.
466+
auto fnTy = getFixedTypeRecursive(getType(fnExpr), /*wantRValue*/ true);
467+
if (fnTy->is<AnyMetatypeType>()) {
468+
auto *fnLocator =
469+
getConstraintLocator(applyExpr, ConstraintLocator::ApplyFunction);
470+
return getConstraintLocator(fnLocator,
471+
ConstraintLocator::ConstructorMember);
472+
}
471473

472-
// Otherwise fall through and look for locators anchored on the function
473-
// expr. For CallExprs, this can look through things like parens and
474-
// optional chaining.
475-
if (auto *callExpr = dyn_cast<CallExpr>(anchor)) {
476-
anchor = callExpr->getDirectCallee();
477-
} else {
478-
anchor = fnExpr;
474+
// Otherwise fall through and look for locators anchored on the function
475+
// expr. For CallExprs, this can look through things like parens and
476+
// optional chaining.
477+
if (auto *callExpr = dyn_cast<CallExpr>(anchor)) {
478+
anchor = callExpr->getDirectCallee();
479+
} else {
480+
anchor = fnExpr;
481+
}
479482
}
480483
}
481484

lib/Sema/ConstraintSystem.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2050,7 +2050,13 @@ class ConstraintSystem {
20502050
/// anchored on \c functionA(functionB()) with path elements pointing to the
20512051
/// argument \c functionB(), the returned callee locator will describe
20522052
/// \c functionA rather than \c functionB.
2053-
ConstraintLocator *getCalleeLocator(ConstraintLocator *locator);
2053+
///
2054+
/// \param locator The input locator.
2055+
/// \param lookThroughApply Whether to look through applies. If false, a
2056+
/// callee locator will only be returned for a direct reference such as
2057+
/// \c x.foo rather than \c x.foo().
2058+
ConstraintLocator *getCalleeLocator(ConstraintLocator *locator,
2059+
bool lookThroughApply = true);
20542060

20552061
public:
20562062

0 commit comments

Comments
 (0)