Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
Zalathar committed Oct 16, 2023
commit b1c44f4a25bb7cec5941362143f2fc0abbc67925
42 changes: 20 additions & 22 deletions compiler/rustc_mir_transform/src/coverage/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,43 +284,48 @@ impl<'a> CoverageSpansGenerator<'a> {
/// de-duplicated `CoverageSpan`s.
fn to_refined_spans(mut self) -> Vec<CoverageSpan> {
while self.next_coverage_span() {
// For the first span we don't have `prev` set, so most of the
// span-processing steps don't make sense yet.
if self.some_prev.is_none() {
debug!(" initial span");
self.maybe_push_macro_name_span();
} else if self.curr().is_mergeable(self.prev()) {
debug!(" same bcb (and neither is a closure), merge with prev={:?}", self.prev());
continue;
}

// The remaining cases assume that `prev` and `curr` are set.
let prev = self.prev();
let curr = self.curr();

if curr.is_mergeable(prev) {
debug!(" same bcb (and neither is a closure), merge with prev={prev:?}");
let prev = self.take_prev();
self.curr_mut().merge_from(prev);
self.maybe_push_macro_name_span();
// Note that curr.span may now differ from curr_original_span
} else if self.prev_ends_before_curr() {
} else if prev.span.hi() <= curr.span.lo() {
debug!(
" different bcbs and disjoint spans, so keep curr for next iter, and add \
prev={:?}",
self.prev()
" different bcbs and disjoint spans, so keep curr for next iter, and add prev={prev:?}",
);
let prev = self.take_prev();
self.push_refined_span(prev);
self.maybe_push_macro_name_span();
} else if self.prev().is_closure {
} else if prev.is_closure {
// drop any equal or overlapping span (`curr`) and keep `prev` to test again in the
// next iter
debug!(
" curr overlaps a closure (prev). Drop curr and keep prev for next iter. \
prev={:?}",
self.prev()
" curr overlaps a closure (prev). Drop curr and keep prev for next iter. prev={prev:?}",
);
self.take_curr();
} else if self.curr().is_closure {
} else if curr.is_closure {
self.carve_out_span_for_closure();
} else if self.prev_original_span == self.curr().span {
} else if self.prev_original_span == curr.span {
// Note that this compares the new (`curr`) span to `prev_original_span`.
// In this branch, the actual span byte range of `prev_original_span` is not
// important. What is important is knowing whether the new `curr` span was
// **originally** the same as the original span of `prev()`. The original spans
// reflect their original sort order, and for equal spans, conveys a partial
// ordering based on CFG dominator priority.
if self.prev().is_macro_expansion() && self.curr().is_macro_expansion() {
if prev.is_macro_expansion() && curr.is_macro_expansion() {
// Macros that expand to include branching (such as
// `assert_eq!()`, `assert_ne!()`, `info!()`, `debug!()`, or
// `trace!()`) typically generate callee spans with identical
Expand All @@ -334,8 +339,7 @@ impl<'a> CoverageSpansGenerator<'a> {
debug!(
" curr and prev are part of a macro expansion, and curr has the same span \
as prev, but is in a different bcb. Drop curr and keep prev for next iter. \
prev={:?}",
self.prev()
prev={prev:?}",
);
self.take_curr();
} else {
Expand All @@ -347,8 +351,8 @@ impl<'a> CoverageSpansGenerator<'a> {
}
}

debug!(" AT END, adding last prev={:?}", self.prev());
let prev = self.take_prev();
debug!(" AT END, adding last prev={prev:?}");
let pending_dups = self.pending_dups.split_off(0);
for dup in pending_dups {
debug!(" ...adding at least one pending dup={:?}", dup);
Expand Down Expand Up @@ -511,12 +515,6 @@ impl<'a> CoverageSpansGenerator<'a> {
self.prev().span.lo() > next_curr.span.lo()
}

/// Returns true if the curr span starts past the end of the prev span, which means they don't
/// overlap, so we now know the prev can be added to the refined coverage spans.
fn prev_ends_before_curr(&self) -> bool {
self.prev().span.hi() <= self.curr().span.lo()
}

/// If `prev`s span extends left of the closure (`curr`), carve out the closure's span from
/// `prev`'s span. (The closure's coverage counters will be injected when processing the
/// closure's own MIR.) Add the portion of the span to the left of the closure; and if the span
Expand Down