@@ -5313,12 +5313,18 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
5313
5313
// / provided set.
5314
5314
static unsigned countDistinctOverloads (ArrayRef<OverloadChoice> choices) {
5315
5315
llvm::SmallPtrSet<void *, 4 > uniqueChoices;
5316
- unsigned result = 0 ;
5317
5316
for (auto choice : choices) {
5318
- if (uniqueChoices.insert (choice.getOpaqueChoiceSimple ()).second )
5319
- ++result;
5317
+ uniqueChoices.insert (choice.getOpaqueChoiceSimple ());
5320
5318
}
5321
- return result;
5319
+ return uniqueChoices.size ();
5320
+ }
5321
+
5322
+ static Type getOverloadChoiceType (ConstraintLocator *overloadLoc,
5323
+ const Solution &solution) {
5324
+ auto selectedOverload = solution.overloadChoices .find (overloadLoc);
5325
+ if (selectedOverload == solution.overloadChoices .end ())
5326
+ return Type ();
5327
+ return solution.simplifyType (selectedOverload->second .adjustedOpenedType );
5322
5328
}
5323
5329
5324
5330
// / Determine the name of the overload in a set of overload choices.
@@ -5400,6 +5406,25 @@ bool ConstraintSystem::diagnoseAmbiguity(ArrayRef<Solution> solutions) {
5400
5406
auto &overload = diff.overloads [i];
5401
5407
auto *locator = overload.locator ;
5402
5408
5409
+ // If there is only one overload difference, it's the best.
5410
+ if (n == 1 ) {
5411
+ bestOverload = i;
5412
+ break ;
5413
+ }
5414
+
5415
+ // If there are multiple overload sets involved, let's pick the
5416
+ // one that has choices with different types, because that is
5417
+ // most likely the source of ambiguity.
5418
+ {
5419
+ auto overloadTy = getOverloadChoiceType (locator, solutions.front ());
5420
+ if (std::all_of (solutions.begin () + 1 , solutions.end (),
5421
+ [&](const Solution &solution) {
5422
+ return overloadTy->isEqual (
5423
+ getOverloadChoiceType (locator, solution));
5424
+ }))
5425
+ continue ;
5426
+ }
5427
+
5403
5428
ASTNode anchor;
5404
5429
5405
5430
// Simplification of member locator would produce a base expression,
0 commit comments