Skip to content

Commit 0b74224

Browse files
committed
[GSB/Diagnostics] Use llvm::array_pod_sort to sort "as written" edges
Instead of using `std::sort`, transform source-ordered edge vector to reference `IntercomponentEdge *` instead of their indices in `intercomponentEdges` vector and use `llvm::array_pod_sort` to sort them. (cherry picked from commit 439784d)
1 parent b43c0c4 commit 0b74224

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6907,35 +6907,32 @@ void GenericSignatureBuilder::checkSameTypeConstraints(
69076907
// connect all of the components, or else we wouldn't have an equivalence
69086908
// class.
69096909
if (intercomponentEdges.size() > numComponents - 1) {
6910-
auto &sourceMgr = getASTContext().SourceMgr;
6911-
69126910
// First let's order all of the intercomponent edges
69136911
// as written in source, this helps us to diagnose
69146912
// all of the duplicate constraints in correct order.
6915-
std::vector<unsigned> sourceOrderedEdges;
6916-
for (unsigned i : indices(intercomponentEdges))
6917-
sourceOrderedEdges.push_back(i);
6913+
std::vector<IntercomponentEdge *> sourceOrderedEdges;
6914+
for (auto &edge : intercomponentEdges)
6915+
sourceOrderedEdges.push_back(&edge);
69186916

6919-
std::sort(sourceOrderedEdges.begin(), sourceOrderedEdges.end(),
6920-
[&sourceMgr, &intercomponentEdges](
6921-
const unsigned &indexA, const unsigned &indexB) -> bool {
6922-
auto &a = intercomponentEdges[indexA];
6923-
auto &b = intercomponentEdges[indexB];
6917+
llvm::array_pod_sort(
6918+
sourceOrderedEdges.begin(), sourceOrderedEdges.end(),
6919+
[](IntercomponentEdge *const *a, IntercomponentEdge *const *b) -> int {
6920+
auto &sourceMgr = (*a)->constraint.value->getASTContext().SourceMgr;
69246921

6925-
auto locA = a.constraint.source->getLoc();
6926-
auto locB = b.constraint.source->getLoc();
6922+
auto locA = (*a)->constraint.source->getLoc();
6923+
auto locB = (*b)->constraint.source->getLoc();
69276924

6928-
auto bufferA = sourceMgr.findBufferContainingLoc(locA);
6929-
auto bufferB = sourceMgr.findBufferContainingLoc(locB);
6925+
auto bufferA = sourceMgr.findBufferContainingLoc(locA);
6926+
auto bufferB = sourceMgr.findBufferContainingLoc(locB);
69306927

6931-
if (bufferA != bufferB)
6932-
return bufferA < bufferB;
6928+
if (bufferA != bufferB)
6929+
return bufferA < bufferB ? -1 : 1;
69336930

6934-
auto offsetA = sourceMgr.getLocOffsetInBuffer(locA, bufferA);
6935-
auto offsetB = sourceMgr.getLocOffsetInBuffer(locB, bufferB);
6931+
auto offsetA = sourceMgr.getLocOffsetInBuffer(locA, bufferA);
6932+
auto offsetB = sourceMgr.getLocOffsetInBuffer(locB, bufferB);
69366933

6937-
return offsetA < offsetB;
6938-
});
6934+
return offsetA < offsetB ? -1 : (offsetA == offsetB ? 0 : 1);
6935+
});
69396936

69406937
auto isDiagnosable = [](const IntercomponentEdge &edge, bool isPrimary) {
69416938
return edge.constraint.source->shouldDiagnoseRedundancy(isPrimary);
@@ -6949,8 +6946,8 @@ void GenericSignatureBuilder::checkSameTypeConstraints(
69496946
// entry is found, that entry is going to point to previous
69506947
// declaration and is going to mark current edge as a duplicate of
69516948
// such entry.
6952-
for (const unsigned edgeIdx : sourceOrderedEdges) {
6953-
const auto &edge = intercomponentEdges[edgeIdx];
6949+
for (auto edgeIdx : indices(sourceOrderedEdges)) {
6950+
const auto &edge = *sourceOrderedEdges[edgeIdx];
69546951

69556952
Type lhs = edge.constraint.getSubjectDependentType(genericParams);
69566953
Type rhs = edge.constraint.value;
@@ -7016,7 +7013,7 @@ void GenericSignatureBuilder::checkSameTypeConstraints(
70167013
edge.constraint.value);
70177014

70187015
if (previousIndex) {
7019-
auto &prevEquiv = intercomponentEdges[*previousIndex].constraint;
7016+
auto &prevEquiv = sourceOrderedEdges[*previousIndex]->constraint;
70207017
Diags.diagnose(
70217018
prevEquiv.source->getLoc(), diag::previous_same_type_constraint,
70227019
prevEquiv.source->classifyDiagKind(),

0 commit comments

Comments
 (0)