@@ -5313,12 +5313,18 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
53135313// / provided set.
53145314static unsigned countDistinctOverloads (ArrayRef<OverloadChoice> choices) {
53155315 llvm::SmallPtrSet<void *, 4 > uniqueChoices;
5316- unsigned result = 0 ;
53175316 for (auto choice : choices) {
5318- if (uniqueChoices.insert (choice.getOpaqueChoiceSimple ()).second )
5319- ++result;
5317+ uniqueChoices.insert (choice.getOpaqueChoiceSimple ());
53205318 }
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 );
53225328}
53235329
53245330// / Determine the name of the overload in a set of overload choices.
@@ -5400,6 +5406,25 @@ bool ConstraintSystem::diagnoseAmbiguity(ArrayRef<Solution> solutions) {
54005406 auto &overload = diff.overloads [i];
54015407 auto *locator = overload.locator ;
54025408
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+
54035428 ASTNode anchor;
54045429
54055430 // Simplification of member locator would produce a base expression,
0 commit comments