Skip to content

Commit b063cd1

Browse files
committed
[Diagnostics] Skip overloaded locations where all solutions have the same type
If there are multiple overloads, let's skip locations that produce the same type across all of the solutions, such location is most likely a consequence of ambiguity and not its source. Resolves: rdar://109245375
1 parent 84b212c commit b063cd1

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

lib/Sema/ConstraintSystem.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -5308,6 +5308,14 @@ static unsigned countDistinctOverloads(ArrayRef<OverloadChoice> choices) {
53085308
return uniqueChoices.size();
53095309
}
53105310

5311+
static Type getOverloadChoiceType(ConstraintLocator *overloadLoc,
5312+
const Solution &solution) {
5313+
auto selectedOverload = solution.overloadChoices.find(overloadLoc);
5314+
if (selectedOverload == solution.overloadChoices.end())
5315+
return Type();
5316+
return solution.simplifyType(selectedOverload->second.adjustedOpenedType);
5317+
}
5318+
53115319
/// Determine the name of the overload in a set of overload choices.
53125320
static DeclName getOverloadChoiceName(ArrayRef<OverloadChoice> choices) {
53135321
DeclName name;
@@ -5387,6 +5395,25 @@ bool ConstraintSystem::diagnoseAmbiguity(ArrayRef<Solution> solutions) {
53875395
auto &overload = diff.overloads[i];
53885396
auto *locator = overload.locator;
53895397

5398+
// If there is only one overload difference, it's the best.
5399+
if (n == 1) {
5400+
bestOverload = i;
5401+
break;
5402+
}
5403+
5404+
// If there are multiple overload sets involved, let's pick the
5405+
// one that has choices with different types, because that is
5406+
// most likely the source of ambiguity.
5407+
{
5408+
auto overloadTy = getOverloadChoiceType(locator, solutions.front());
5409+
if (std::all_of(solutions.begin() + 1, solutions.end(),
5410+
[&](const Solution &solution) {
5411+
return overloadTy->isEqual(
5412+
getOverloadChoiceType(locator, solution));
5413+
}))
5414+
continue;
5415+
}
5416+
53905417
ASTNode anchor;
53915418

53925419
// Simplification of member locator would produce a base expression,

0 commit comments

Comments
 (0)