@@ -387,9 +387,14 @@ static bool diagnoseAmbiguity(ConstraintSystem &cs,
387
387
388
388
// Find the locators which have the largest numbers of distinct overloads.
389
389
Optional<unsigned > bestOverload;
390
- unsigned maxDistinctOverloads = 0 ;
391
- unsigned maxDepth = 0 ;
392
- unsigned minIndex = std::numeric_limits<unsigned >::max ();
390
+ // Overloads are scored by lexicographical comparison of (# of distinct
391
+ // overloads, depth, *reverse* of the index). N.B. - cannot be used for the
392
+ // reversing: the score version of index == 0 should be > than that of 1, but
393
+ // -0 == 0 < UINT_MAX == -1, whereas ~0 == UINT_MAX > UINT_MAX - 1 == ~1.
394
+ auto score = [](unsigned distinctOverloads, unsigned depth, unsigned index) {
395
+ return std::make_tuple (distinctOverloads, depth, ~index);
396
+ };
397
+ auto bestScore = score (0 , 0 , std::numeric_limits<unsigned >::max ());
393
398
394
399
// Get a map of expressions to their depths and post-order traversal indices.
395
400
// Heuristically, all other things being equal, we should complain about the
@@ -428,27 +433,10 @@ static bool diagnoseAmbiguity(ConstraintSystem &cs,
428
433
429
434
// If we have more distinct overload choices for this locator than for
430
435
// prior locators, just keep this locator.
431
-
432
- bool better = false ;
433
- if (bestOverload) {
434
- if (distinctOverloads > maxDistinctOverloads) {
435
- better = true ;
436
- } else if (distinctOverloads == maxDistinctOverloads) {
437
- if (depth > maxDepth) {
438
- better = true ;
439
- } else if (depth == maxDepth) {
440
- if (index < minIndex) {
441
- better = true ;
442
- }
443
- }
444
- }
445
- }
446
-
447
- if (!bestOverload || better) {
436
+ auto thisScore = score (distinctOverloads, depth, index);
437
+ if (thisScore > bestScore) {
438
+ bestScore = thisScore;
448
439
bestOverload = i;
449
- maxDistinctOverloads = distinctOverloads;
450
- maxDepth = depth;
451
- minIndex = index;
452
440
continue ;
453
441
}
454
442
0 commit comments