Skip to content

Commit bfae015

Browse files
committed
Sort DeclAttributeAndRanges Deterministically
Comparing the start and end of ranges doesn't work in all cases since both a < b and b < a can be true if a = b. This behavior was causing an assertion fail in the Windows standard library which while sorting, asserts that if `a < b`, then `!(b < a)` in debug mode. Since ranges don't overlap otherwise, comparing just the starts of the ranges will properly sort them.
1 parent 75a5496 commit bfae015

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

lib/IDE/SyntaxModel.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,10 +1080,13 @@ bool ModelASTWalker::handleAttrRanges(ArrayRef<DeclAttributeAndRange> DeclRanges
10801080

10811081
SmallVector<DeclAttributeAndRange, 4> SortedRanges(DeclRanges.begin(),
10821082
DeclRanges.end());
1083-
std::sort(SortedRanges.begin(), SortedRanges.end(),
1084-
[&](DeclAttributeAndRange LHS, DeclAttributeAndRange RHS) {
1085-
return SM.isBeforeInBuffer(LHS.second.Start, RHS.second.End);
1086-
});
1083+
std::sort(
1084+
SortedRanges.begin(), SortedRanges.end(),
1085+
[&](DeclAttributeAndRange LHS, DeclAttributeAndRange RHS) {
1086+
// Since attributes don't overlap it's safe to compare just by the
1087+
// range's Start
1088+
return SM.isBeforeInBuffer(LHS.second.Start, RHS.second.Start);
1089+
});
10871090
// Handle duplicate synthesized attributes due to * in @available
10881091
auto NewEnd = std::unique(SortedRanges.begin(), SortedRanges.end(),
10891092
[&](DeclAttributeAndRange LHS, DeclAttributeAndRange RHS) {

0 commit comments

Comments
 (0)