Skip to content

Commit b1c44f4

Browse files
committed
coverage: Call prev/curr less in to_refined_spans
This makes it easier to see that the non-initial cases assume that `prev` and `curr` are set, and all operate on the same prev/curr references.
1 parent 9bb27f3 commit b1c44f4

File tree

1 file changed

+20
-22
lines changed
  • compiler/rustc_mir_transform/src/coverage

1 file changed

+20
-22
lines changed

compiler/rustc_mir_transform/src/coverage/spans.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -284,43 +284,48 @@ impl<'a> CoverageSpansGenerator<'a> {
284284
/// de-duplicated `CoverageSpan`s.
285285
fn to_refined_spans(mut self) -> Vec<CoverageSpan> {
286286
while self.next_coverage_span() {
287+
// For the first span we don't have `prev` set, so most of the
288+
// span-processing steps don't make sense yet.
287289
if self.some_prev.is_none() {
288290
debug!(" initial span");
289291
self.maybe_push_macro_name_span();
290-
} else if self.curr().is_mergeable(self.prev()) {
291-
debug!(" same bcb (and neither is a closure), merge with prev={:?}", self.prev());
292+
continue;
293+
}
294+
295+
// The remaining cases assume that `prev` and `curr` are set.
296+
let prev = self.prev();
297+
let curr = self.curr();
298+
299+
if curr.is_mergeable(prev) {
300+
debug!(" same bcb (and neither is a closure), merge with prev={prev:?}");
292301
let prev = self.take_prev();
293302
self.curr_mut().merge_from(prev);
294303
self.maybe_push_macro_name_span();
295304
// Note that curr.span may now differ from curr_original_span
296-
} else if self.prev_ends_before_curr() {
305+
} else if prev.span.hi() <= curr.span.lo() {
297306
debug!(
298-
" different bcbs and disjoint spans, so keep curr for next iter, and add \
299-
prev={:?}",
300-
self.prev()
307+
" different bcbs and disjoint spans, so keep curr for next iter, and add prev={prev:?}",
301308
);
302309
let prev = self.take_prev();
303310
self.push_refined_span(prev);
304311
self.maybe_push_macro_name_span();
305-
} else if self.prev().is_closure {
312+
} else if prev.is_closure {
306313
// drop any equal or overlapping span (`curr`) and keep `prev` to test again in the
307314
// next iter
308315
debug!(
309-
" curr overlaps a closure (prev). Drop curr and keep prev for next iter. \
310-
prev={:?}",
311-
self.prev()
316+
" curr overlaps a closure (prev). Drop curr and keep prev for next iter. prev={prev:?}",
312317
);
313318
self.take_curr();
314-
} else if self.curr().is_closure {
319+
} else if curr.is_closure {
315320
self.carve_out_span_for_closure();
316-
} else if self.prev_original_span == self.curr().span {
321+
} else if self.prev_original_span == curr.span {
317322
// Note that this compares the new (`curr`) span to `prev_original_span`.
318323
// In this branch, the actual span byte range of `prev_original_span` is not
319324
// important. What is important is knowing whether the new `curr` span was
320325
// **originally** the same as the original span of `prev()`. The original spans
321326
// reflect their original sort order, and for equal spans, conveys a partial
322327
// ordering based on CFG dominator priority.
323-
if self.prev().is_macro_expansion() && self.curr().is_macro_expansion() {
328+
if prev.is_macro_expansion() && curr.is_macro_expansion() {
324329
// Macros that expand to include branching (such as
325330
// `assert_eq!()`, `assert_ne!()`, `info!()`, `debug!()`, or
326331
// `trace!()`) typically generate callee spans with identical
@@ -334,8 +339,7 @@ impl<'a> CoverageSpansGenerator<'a> {
334339
debug!(
335340
" curr and prev are part of a macro expansion, and curr has the same span \
336341
as prev, but is in a different bcb. Drop curr and keep prev for next iter. \
337-
prev={:?}",
338-
self.prev()
342+
prev={prev:?}",
339343
);
340344
self.take_curr();
341345
} else {
@@ -347,8 +351,8 @@ impl<'a> CoverageSpansGenerator<'a> {
347351
}
348352
}
349353

350-
debug!(" AT END, adding last prev={:?}", self.prev());
351354
let prev = self.take_prev();
355+
debug!(" AT END, adding last prev={prev:?}");
352356
let pending_dups = self.pending_dups.split_off(0);
353357
for dup in pending_dups {
354358
debug!(" ...adding at least one pending dup={:?}", dup);
@@ -511,12 +515,6 @@ impl<'a> CoverageSpansGenerator<'a> {
511515
self.prev().span.lo() > next_curr.span.lo()
512516
}
513517

514-
/// Returns true if the curr span starts past the end of the prev span, which means they don't
515-
/// overlap, so we now know the prev can be added to the refined coverage spans.
516-
fn prev_ends_before_curr(&self) -> bool {
517-
self.prev().span.hi() <= self.curr().span.lo()
518-
}
519-
520518
/// If `prev`s span extends left of the closure (`curr`), carve out the closure's span from
521519
/// `prev`'s span. (The closure's coverage counters will be injected when processing the
522520
/// closure's own MIR.) Add the portion of the span to the left of the closure; and if the span

0 commit comments

Comments
 (0)