Skip to content

Commit 77781b8

Browse files
committed
[NFC] Use std::tuple instead of hand-written lexicographical comparison.
1 parent 4e3a010 commit 77781b8

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,14 @@ static bool diagnoseAmbiguity(ConstraintSystem &cs,
387387

388388
// Find the locators which have the largest numbers of distinct overloads.
389389
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());
393398

394399
// Get a map of expressions to their depths and post-order traversal indices.
395400
// Heuristically, all other things being equal, we should complain about the
@@ -428,27 +433,10 @@ static bool diagnoseAmbiguity(ConstraintSystem &cs,
428433

429434
// If we have more distinct overload choices for this locator than for
430435
// 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;
448439
bestOverload = i;
449-
maxDistinctOverloads = distinctOverloads;
450-
maxDepth = depth;
451-
minIndex = index;
452440
continue;
453441
}
454442

0 commit comments

Comments
 (0)