Skip to content

Commit d761f94

Browse files
hamishknightxedin
authored andcommitted
[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. (cherry picked from commit 63df26a)
1 parent f2cae86 commit d761f94

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
@@ -2464,9 +2464,9 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
24642464

24652465
// Problems related to fixes forming ambiguous solution set
24662466
// could only be diagnosed (at the moment), if all of the fixes
2467-
// are attached to the same anchor, which means they fix
2468-
// different overloads of the same declaration.
2469-
Expr *commonAnchor = nullptr;
2467+
// have the same callee locator, which means they fix different
2468+
// overloads of the same declaration.
2469+
ConstraintLocator *commonCalleeLocator = nullptr;
24702470
SmallPtrSet<ValueDecl *, 4> distinctChoices;
24712471
SmallVector<std::pair<const Solution *, const ConstraintFix *>, 4>
24722472
viableSolutions;
@@ -2480,21 +2480,17 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
24802480
return false;
24812481

24822482
const auto *fix = fixes.front();
2483-
if (commonAnchor && commonAnchor != fix->getAnchor())
2483+
auto *calleeLocator = getCalleeLocator(fix->getAnchor());
2484+
if (commonCalleeLocator && commonCalleeLocator != calleeLocator)
24842485
return false;
24852486

2486-
commonAnchor = fix->getAnchor();
2487+
commonCalleeLocator = calleeLocator;
24872488

2488-
SmallVector<SelectedOverload, 2> overloads;
2489-
solution.getOverloadChoices(commonAnchor, overloads);
2490-
// There is unfortunately no way, at the moment, to figure out
2491-
// what declaration the fix is attached to, so we have to make
2492-
// sure that there is only one declaration associated with common
2493-
// anchor to be sure that the right problem is being diagnosed.
2494-
if (overloads.size() != 1)
2489+
auto overload = solution.getOverloadChoiceIfAvailable(calleeLocator);
2490+
if (!overload)
24952491
return false;
24962492

2497-
auto *decl = getOverloadDecl(overloads.front());
2493+
auto *decl = getOverloadDecl(*overload);
24982494
if (!decl)
24992495
return false;
25002496

@@ -2517,6 +2513,7 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
25172513
DiagnosticTransaction transaction(TC.Diags);
25182514

25192515
const auto *fix = viableSolutions.front().second;
2516+
auto *commonAnchor = commonCalleeLocator->getAnchor();
25202517
if (fix->getKind() == FixKind::UseSubscriptOperator) {
25212518
auto *UDE = cast<UnresolvedDotExpr>(commonAnchor);
25222519
TC.diagnose(commonAnchor->getLoc(),

lib/Sema/ConstraintSystem.h

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

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

0 commit comments

Comments
 (0)