Skip to content

Commit 63df26a

Browse files
committed
[CS] Use callee locators for fix ambiguities
Currently we check that the fixes share the same anchor, however this doesn't account for the case where, given an ApplyExpr, one fix is anchored on its function expr, and another is anchored on the apply itself (because the former fix might be looking at the callee's requirements, and the latter fix might be looking at an argument of the call). This commit changes the logic such that we check that fixes share the same callee locator, which covers the above case. In addition, now that we have the callee locator, we can use this to find the overload directly.
1 parent 894a1e5 commit 63df26a

File tree

2 files changed

+10
-23
lines changed

2 files changed

+10
-23
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,9 +2443,9 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
24432443

24442444
// Problems related to fixes forming ambiguous solution set
24452445
// could only be diagnosed (at the moment), if all of the fixes
2446-
// are attached to the same anchor, which means they fix
2447-
// different overloads of the same declaration.
2448-
Expr *commonAnchor = nullptr;
2446+
// have the same callee locator, which means they fix different
2447+
// overloads of the same declaration.
2448+
ConstraintLocator *commonCalleeLocator = nullptr;
24492449
SmallPtrSet<ValueDecl *, 4> distinctChoices;
24502450
SmallVector<std::pair<const Solution *, const ConstraintFix *>, 4>
24512451
viableSolutions;
@@ -2459,21 +2459,17 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
24592459
return false;
24602460

24612461
const auto *fix = fixes.front();
2462-
if (commonAnchor && commonAnchor != fix->getAnchor())
2462+
auto *calleeLocator = getCalleeLocator(fix->getAnchor());
2463+
if (commonCalleeLocator && commonCalleeLocator != calleeLocator)
24632464
return false;
24642465

2465-
commonAnchor = fix->getAnchor();
2466+
commonCalleeLocator = calleeLocator;
24662467

2467-
SmallVector<SelectedOverload, 2> overloads;
2468-
solution.getOverloadChoices(commonAnchor, overloads);
2469-
// There is unfortunately no way, at the moment, to figure out
2470-
// what declaration the fix is attached to, so we have to make
2471-
// sure that there is only one declaration associated with common
2472-
// anchor to be sure that the right problem is being diagnosed.
2473-
if (overloads.size() != 1)
2468+
auto overload = solution.getOverloadChoiceIfAvailable(calleeLocator);
2469+
if (!overload)
24742470
return false;
24752471

2476-
auto *decl = getOverloadDecl(overloads.front());
2472+
auto *decl = getOverloadDecl(*overload);
24772473
if (!decl)
24782474
return false;
24792475

@@ -2496,6 +2492,7 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
24962492
DiagnosticTransaction transaction(TC.Diags);
24972493

24982494
const auto *fix = viableSolutions.front().second;
2495+
auto *commonAnchor = commonCalleeLocator->getAnchor();
24992496
if (fix->getKind() == FixKind::UseSubscriptOperator) {
25002497
auto *UDE = cast<UnresolvedDotExpr>(commonAnchor);
25012498
TC.diagnose(commonAnchor->getLoc(),

lib/Sema/ConstraintSystem.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -715,16 +715,6 @@ class Solution {
715715
return None;
716716
}
717717

718-
/// Retrieve overload choices associated with given expression.
719-
void getOverloadChoices(Expr *anchor,
720-
SmallVectorImpl<SelectedOverload> &overloads) const {
721-
for (auto &e : overloadChoices) {
722-
auto *locator = e.first;
723-
if (locator->getAnchor() == anchor)
724-
overloads.push_back(e.second);
725-
}
726-
}
727-
728718
LLVM_ATTRIBUTE_DEPRECATED(
729719
void dump() const LLVM_ATTRIBUTE_USED,
730720
"only for use within the debugger");

0 commit comments

Comments
 (0)