Skip to content

Commit 3ad6d12

Browse files
committed
Sort scraped call locations before serializing
1 parent ce943d2 commit 3ad6d12

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

src/librustdoc/html/highlight.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,18 @@ struct Decorations {
270270

271271
impl Decorations {
272272
fn new(info: DecorationInfo) -> Self {
273-
let (starts, ends) = info
273+
// Extract tuples (start, end, kind) into separate sequences of (start, kind) and (end).
274+
let (mut starts, mut ends): (Vec<_>, Vec<_>) = info
274275
.0
275276
.into_iter()
276277
.map(|(kind, ranges)| ranges.into_iter().map(move |(lo, hi)| ((lo, kind), hi)))
277278
.flatten()
278279
.unzip();
280+
281+
// Sort the sequences in document order.
282+
starts.sort_by_key(|(lo, _)| *lo);
283+
ends.sort();
284+
279285
Decorations { starts, ends }
280286
}
281287
}

src/librustdoc/scrape_examples.rs

+7
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,13 @@ crate fn run(
240240
let mut finder = FindCalls { calls: &mut calls, tcx, map: tcx.hir(), cx, target_crates };
241241
tcx.hir().visit_all_item_likes(&mut finder.as_deep_visitor());
242242

243+
// Sort call locations within a given file in document order
244+
for fn_calls in calls.values_mut() {
245+
for file_calls in fn_calls.values_mut() {
246+
file_calls.locations.sort_by_key(|loc| loc.call_expr.byte_span.0);
247+
}
248+
}
249+
243250
// Save output to provided path
244251
let mut encoder = FileEncoder::new(options.output_path).map_err(|e| e.to_string())?;
245252
calls.encode(&mut encoder).map_err(|e| e.to_string())?;

src/test/run-make/rustdoc-scrape-examples-ordering/examples/ex1.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
fn main() {
2-
foobar::ok();
2+
foobar::ok(0);
33

44
// this is a
55

6+
// ..
7+
68
// BIG
79

810
// item
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
fn main() {
2-
foobar::ok();
2+
foobar::ok(1);
33
// small item
44
}
5+
6+
fn f() {
7+
foobar::ok(2);
8+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// @has foobar/fn.ok.html '//*[@class="docblock scraped-example-list"]' 'ex2'
22
// @has foobar/fn.ok.html '//*[@class="more-scraped-examples"]' 'ex1'
3+
// @has foobar/fn.ok.html '//*[@class="highlight focus"]' '1'
4+
// @has foobar/fn.ok.html '//*[@class="highlight"]' '2'
5+
// @has foobar/fn.ok.html '//*[@class="highlight focus"]' '0'
36

4-
pub fn ok() {}
7+
pub fn ok(_x: i32) {}

0 commit comments

Comments
 (0)