Skip to content

Commit 43b1a38

Browse files
committed
coverage: Hoist the removal of unwanted macro expansion spans
1 parent 4f41a0d commit 43b1a38

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

compiler/rustc_mir_transform/src/coverage/spans.rs

+3-26
Original file line numberDiff line numberDiff line change
@@ -251,32 +251,9 @@ impl<'a> CoverageSpansGenerator<'a> {
251251
} else if curr.is_closure {
252252
self.carve_out_span_for_closure();
253253
} else if self.prev_original_span == curr.span {
254-
// Note that this compares the new (`curr`) span to `prev_original_span`.
255-
// In this branch, the actual span byte range of `prev_original_span` is not
256-
// important. What is important is knowing whether the new `curr` span was
257-
// **originally** the same as the original span of `prev()`. The original spans
258-
// reflect their original sort order, and for equal spans, conveys a partial
259-
// ordering based on CFG dominator priority.
260-
if prev.visible_macro.is_some() && curr.visible_macro.is_some() {
261-
// Macros that expand to include branching (such as
262-
// `assert_eq!()`, `assert_ne!()`, `info!()`, `debug!()`, or
263-
// `trace!()`) typically generate callee spans with identical
264-
// ranges (typically the full span of the macro) for all
265-
// `BasicBlocks`. This makes it impossible to distinguish
266-
// the condition (`if val1 != val2`) from the optional
267-
// branched statements (such as the call to `panic!()` on
268-
// assert failure). In this case it is better (or less
269-
// worse) to drop the optional branch bcbs and keep the
270-
// non-conditional statements, to count when reached.
271-
debug!(
272-
" curr and prev are part of a macro expansion, and curr has the same span \
273-
as prev, but is in a different bcb. Drop curr and keep prev for next iter. \
274-
prev={prev:?}",
275-
);
276-
self.take_curr(); // Discards curr.
277-
} else {
278-
self.update_pending_dups();
279-
}
254+
// `prev` and `curr` have the same span, or would have had the
255+
// same span before `prev` was modified by other spans.
256+
self.update_pending_dups();
280257
} else {
281258
self.cutoff_prev_at_overlapping_curr();
282259
self.maybe_push_macro_name_span();

compiler/rustc_mir_transform/src/coverage/spans/from_mir.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_data_structures::captures::Captures;
2+
use rustc_data_structures::fx::FxHashSet;
23
use rustc_middle::mir::{
34
self, AggregateKind, FakeReadCause, Rvalue, Statement, StatementKind, Terminator,
45
TerminatorKind,
@@ -35,6 +36,9 @@ pub(super) fn mir_to_initial_sorted_coverage_spans(
3536
}
3637
}
3738

39+
initial_spans.sort_by(|a, b| basic_coverage_blocks.cmp_in_dominator_order(a.bcb, b.bcb));
40+
remove_unwanted_macro_spans(&mut initial_spans);
41+
3842
initial_spans.sort_by(|a, b| {
3943
// First sort by span start.
4044
Ord::cmp(&a.span.lo(), &b.span.lo())
@@ -55,6 +59,26 @@ pub(super) fn mir_to_initial_sorted_coverage_spans(
5559
initial_spans
5660
}
5761

62+
/// Macros that expand into branches (e.g. `assert!`, `trace!`) tend to generate
63+
/// multiple condition/consequent blocks that have the span of the whole macro
64+
/// invocation, which is unhelpful. Keeping only the first such span seems to
65+
/// give better mappings, so remove the others.
66+
///
67+
/// (The input spans should be sorted in BCB dominator order, so that the
68+
/// retained "first" span is likely to dominate the others.)
69+
fn remove_unwanted_macro_spans(initial_spans: &mut Vec<CoverageSpan>) {
70+
let mut seen_macro_spans = FxHashSet::default();
71+
initial_spans.retain(|covspan| {
72+
// Ignore (retain) closure spans and non-macro-expansion spans.
73+
if covspan.is_closure || covspan.visible_macro.is_none() {
74+
return true;
75+
}
76+
77+
// Retain only the first macro-expanded covspan with this span.
78+
seen_macro_spans.insert(covspan.span)
79+
});
80+
}
81+
5882
// Generate a set of `CoverageSpan`s from the filtered set of `Statement`s and `Terminator`s of
5983
// the `BasicBlock`(s) in the given `BasicCoverageBlockData`. One `CoverageSpan` is generated
6084
// for each `Statement` and `Terminator`. (Note that subsequent stages of coverage analysis will

0 commit comments

Comments
 (0)