Skip to content

Commit

Permalink
coverage: Merge refined spans in a separate final pass
Browse files Browse the repository at this point in the history
This makes `push_refined_span` trivial, which will let us inline it and benefit
from partial borrows of `refined_spans`.
  • Loading branch information
Zalathar committed Dec 7, 2023
1 parent c9d85d6 commit ec0110b
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions compiler/rustc_mir_transform/src/coverage/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ impl CoverageSpan {
}
}

pub fn merge_from(&mut self, mut other: CoverageSpan) {
debug_assert!(self.is_mergeable(&other));
pub fn merge_from(&mut self, other: &Self) {
debug_assert!(self.is_mergeable(other));
self.span = self.span.to(other.span);
self.merged_spans.append(&mut other.merged_spans);
self.merged_spans.extend_from_slice(&other.merged_spans);
}

pub fn cutoff_statements_at(&mut self, cutoff_pos: BytePos) {
Expand Down Expand Up @@ -267,7 +267,7 @@ impl<'a> CoverageSpansGenerator<'a> {
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.curr_mut().merge_from(&prev);
self.maybe_push_macro_name_span();
// Note that curr.span may now differ from curr_original_span
} else if prev.span.hi() <= curr.span.lo() {
Expand Down Expand Up @@ -346,6 +346,17 @@ impl<'a> CoverageSpansGenerator<'a> {
self.push_refined_span(prev);
}

// Do one last merge pass, to simplify the output.
self.refined_spans.dedup_by(|b, a| {
if a.is_mergeable(b) {
debug!(?a, ?b, "merging list-adjacent refined spans");
a.merge_from(b);
true
} else {
false
}
});

// Remove `CoverageSpan`s derived from closures, originally added to ensure the coverage
// regions for the current function leave room for the closure's own coverage regions
// (injected separately, from the closure's own MIR).
Expand All @@ -354,15 +365,7 @@ impl<'a> CoverageSpansGenerator<'a> {
}

fn push_refined_span(&mut self, covspan: CoverageSpan) {
if let Some(last) = self.refined_spans.last_mut()
&& last.is_mergeable(&covspan)
{
// Instead of pushing the new span, merge it with the last refined span.
debug!(?last, ?covspan, "merging new refined span with last refined span");
last.merge_from(covspan);
} else {
self.refined_spans.push(covspan);
}
self.refined_spans.push(covspan);
}

/// If `curr` is part of a new macro expansion, carve out and push a separate
Expand Down

0 comments on commit ec0110b

Please sign in to comment.