Skip to content

Commit 4f41a0d

Browse files
committed
coverage: Overhaul how "visible macros" are determined
1 parent 9bd802d commit 4f41a0d

File tree

4 files changed

+61
-69
lines changed

4 files changed

+61
-69
lines changed

compiler/rustc_mir_transform/src/coverage/spans.rs

Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
use std::cell::OnceCell;
2-
31
use rustc_data_structures::graph::WithNumNodes;
42
use rustc_index::IndexVec;
53
use rustc_middle::mir;
6-
use rustc_span::{BytePos, ExpnKind, MacroKind, Span, Symbol, DUMMY_SP};
4+
use rustc_span::{BytePos, Span, Symbol, DUMMY_SP};
75

86
use super::graph::{BasicCoverageBlock, CoverageGraph, START_BCB};
97
use crate::coverage::ExtractedHirInfo;
@@ -71,8 +69,7 @@ impl CoverageSpans {
7169
#[derive(Debug, Clone)]
7270
struct CoverageSpan {
7371
pub span: Span,
74-
pub expn_span: Span,
75-
pub current_macro_or_none: OnceCell<Option<Symbol>>,
72+
pub visible_macro: Option<Symbol>,
7673
pub bcb: BasicCoverageBlock,
7774
/// List of all the original spans from MIR that have been merged into this
7875
/// span. Mainly used to precisely skip over gaps when truncating a span.
@@ -82,23 +79,16 @@ struct CoverageSpan {
8279

8380
impl CoverageSpan {
8481
pub fn for_fn_sig(fn_sig_span: Span) -> Self {
85-
Self::new(fn_sig_span, fn_sig_span, START_BCB, false)
82+
Self::new(fn_sig_span, None, START_BCB, false)
8683
}
8784

8885
pub(super) fn new(
8986
span: Span,
90-
expn_span: Span,
87+
visible_macro: Option<Symbol>,
9188
bcb: BasicCoverageBlock,
9289
is_closure: bool,
9390
) -> Self {
94-
Self {
95-
span,
96-
expn_span,
97-
current_macro_or_none: Default::default(),
98-
bcb,
99-
merged_spans: vec![span],
100-
is_closure,
101-
}
91+
Self { span, visible_macro, bcb, merged_spans: vec![span], is_closure }
10292
}
10393

10494
pub fn merge_from(&mut self, other: &Self) {
@@ -123,37 +113,6 @@ impl CoverageSpan {
123113
pub fn is_in_same_bcb(&self, other: &Self) -> bool {
124114
self.bcb == other.bcb
125115
}
126-
127-
/// If the span is part of a macro, returns the macro name symbol.
128-
pub fn current_macro(&self) -> Option<Symbol> {
129-
self.current_macro_or_none
130-
.get_or_init(|| {
131-
if let ExpnKind::Macro(MacroKind::Bang, current_macro) =
132-
self.expn_span.ctxt().outer_expn_data().kind
133-
{
134-
return Some(current_macro);
135-
}
136-
None
137-
})
138-
.map(|symbol| symbol)
139-
}
140-
141-
/// If the span is part of a macro, and the macro is visible (expands directly to the given
142-
/// body_span), returns the macro name symbol.
143-
pub fn visible_macro(&self, body_span: Span) -> Option<Symbol> {
144-
let current_macro = self.current_macro()?;
145-
let parent_callsite = self.expn_span.parent_callsite()?;
146-
147-
// In addition to matching the context of the body span, the parent callsite
148-
// must also be the source callsite, i.e. the parent must have no parent.
149-
let is_visible_macro =
150-
parent_callsite.parent_callsite().is_none() && parent_callsite.eq_ctxt(body_span);
151-
is_visible_macro.then_some(current_macro)
152-
}
153-
154-
pub fn is_macro_expansion(&self) -> bool {
155-
self.current_macro().is_some()
156-
}
157116
}
158117

159118
/// Converts the initial set of `CoverageSpan`s (one per MIR `Statement` or `Terminator`) into a
@@ -164,10 +123,6 @@ impl CoverageSpan {
164123
/// execution
165124
/// * Carve out (leave uncovered) any span that will be counted by another MIR (notably, closures)
166125
struct CoverageSpansGenerator<'a> {
167-
/// A `Span` covering the function body of the MIR (typically from left curly brace to right
168-
/// curly brace).
169-
body_span: Span,
170-
171126
/// The BasicCoverageBlock Control Flow Graph (BCB CFG).
172127
basic_coverage_blocks: &'a CoverageGraph,
173128

@@ -244,7 +199,6 @@ impl<'a> CoverageSpansGenerator<'a> {
244199
);
245200

246201
let coverage_spans = Self {
247-
body_span: hir_info.body_span,
248202
basic_coverage_blocks,
249203
sorted_spans_iter: sorted_spans.into_iter(),
250204
some_curr: None,
@@ -303,7 +257,7 @@ impl<'a> CoverageSpansGenerator<'a> {
303257
// **originally** the same as the original span of `prev()`. The original spans
304258
// reflect their original sort order, and for equal spans, conveys a partial
305259
// ordering based on CFG dominator priority.
306-
if prev.is_macro_expansion() && curr.is_macro_expansion() {
260+
if prev.visible_macro.is_some() && curr.visible_macro.is_some() {
307261
// Macros that expand to include branching (such as
308262
// `assert_eq!()`, `assert_ne!()`, `info!()`, `debug!()`, or
309263
// `trace!()`) typically generate callee spans with identical
@@ -365,12 +319,7 @@ impl<'a> CoverageSpansGenerator<'a> {
365319
fn maybe_push_macro_name_span(&mut self) {
366320
let curr = self.curr();
367321

368-
let Some(visible_macro) = curr.visible_macro(self.body_span) else { return };
369-
if let Some(prev) = &self.some_prev
370-
&& prev.expn_span.eq_ctxt(curr.expn_span)
371-
{
372-
return;
373-
}
322+
let Some(visible_macro) = curr.visible_macro else { return };
374323

375324
// The split point is relative to `curr_original_span`,
376325
// because `curr.span` may have been merged with preceding spans.

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

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_middle::mir::{
33
self, AggregateKind, FakeReadCause, Rvalue, Statement, StatementKind, Terminator,
44
TerminatorKind,
55
};
6-
use rustc_span::Span;
6+
use rustc_span::{ExpnKind, MacroKind, Span, Symbol};
77

88
use crate::coverage::graph::{BasicCoverageBlock, BasicCoverageBlockData, CoverageGraph};
99
use crate::coverage::spans::CoverageSpan;
@@ -71,16 +71,18 @@ fn bcb_to_initial_coverage_spans<'a, 'tcx>(
7171

7272
let statement_spans = data.statements.iter().filter_map(move |statement| {
7373
let expn_span = filtered_statement_span(statement)?;
74-
let span = unexpand_into_body_span(expn_span, body_span)?;
74+
let (span, visible_macro) =
75+
unexpand_into_body_span_with_visible_macro(expn_span, body_span)?;
7576

76-
Some(CoverageSpan::new(span, expn_span, bcb, is_closure(statement)))
77+
Some(CoverageSpan::new(span, visible_macro, bcb, is_closure(statement)))
7778
});
7879

7980
let terminator_span = Some(data.terminator()).into_iter().filter_map(move |terminator| {
8081
let expn_span = filtered_terminator_span(terminator)?;
81-
let span = unexpand_into_body_span(expn_span, body_span)?;
82+
let (span, visible_macro) =
83+
unexpand_into_body_span_with_visible_macro(expn_span, body_span)?;
8284

83-
Some(CoverageSpan::new(span, expn_span, bcb, false))
85+
Some(CoverageSpan::new(span, visible_macro, bcb, false))
8486
});
8587

8688
statement_spans.chain(terminator_span)
@@ -201,7 +203,48 @@ fn filtered_terminator_span(terminator: &Terminator<'_>) -> Option<Span> {
201203
///
202204
/// [^1]Expansions result from Rust syntax including macros, syntactic sugar,
203205
/// etc.).
204-
#[inline]
205-
fn unexpand_into_body_span(span: Span, body_span: Span) -> Option<Span> {
206-
span.find_ancestor_inside_same_ctxt(body_span)
206+
fn unexpand_into_body_span_with_visible_macro(
207+
original_span: Span,
208+
body_span: Span,
209+
) -> Option<(Span, Option<Symbol>)> {
210+
let (span, prev) = unexpand_into_body_span_with_prev(original_span, body_span)?;
211+
212+
let visible_macro = prev
213+
.map(|prev| match prev.ctxt().outer_expn_data().kind {
214+
ExpnKind::Macro(MacroKind::Bang, name) => Some(name),
215+
_ => None,
216+
})
217+
.flatten();
218+
219+
Some((span, visible_macro))
220+
}
221+
222+
/// Walks through the expansion ancestors of `original_span` to find a span that
223+
/// is contained in `body_span` and has the same [`SyntaxContext`] as `body_span`.
224+
/// The ancestor that was traversed just before the matching span (if any) is
225+
/// also returned.
226+
///
227+
/// For example, a return value of `Some((ancestor, Some(prev))` means that:
228+
/// - `ancestor == original_span.find_ancestor_inside_same_ctxt(body_span)`
229+
/// - `ancestor == prev.parent_callsite()`
230+
///
231+
/// [`SyntaxContext`]: rustc_span::SyntaxContext
232+
fn unexpand_into_body_span_with_prev(
233+
original_span: Span,
234+
body_span: Span,
235+
) -> Option<(Span, Option<Span>)> {
236+
let mut prev = None;
237+
let mut curr = original_span;
238+
239+
while !body_span.contains(curr) || !curr.eq_ctxt(body_span) {
240+
prev = Some(curr);
241+
curr = curr.parent_callsite()?;
242+
}
243+
244+
debug_assert_eq!(Some(curr), original_span.find_ancestor_in_same_ctxt(body_span));
245+
if let Some(prev) = prev {
246+
debug_assert_eq!(Some(curr), prev.parent_callsite());
247+
}
248+
249+
Some((curr, prev))
207250
}

tests/coverage/inline-dead.cov-map

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ Number of file 0 mappings: 2
3131
- Code(Counter(0)) at (prev + 7, 6) to (start + 2, 2)
3232

3333
Function name: inline_dead::main::{closure#0}
34-
Raw bytes (23): 0x[01, 01, 02, 00, 06, 01, 00, 03, 01, 07, 17, 00, 18, 00, 02, 0d, 00, 0e, 03, 02, 05, 00, 06]
34+
Raw bytes (23): 0x[01, 01, 02, 00, 06, 01, 00, 03, 01, 07, 17, 01, 16, 00, 02, 0d, 00, 0e, 03, 02, 05, 00, 06]
3535
Number of files: 1
3636
- file 0 => global file 1
3737
Number of expressions: 2
3838
- expression 0 operands: lhs = Zero, rhs = Expression(1, Sub)
3939
- expression 1 operands: lhs = Counter(0), rhs = Zero
4040
Number of file 0 mappings: 3
41-
- Code(Counter(0)) at (prev + 7, 23) to (start + 0, 24)
41+
- Code(Counter(0)) at (prev + 7, 23) to (start + 1, 22)
4242
- Code(Zero) at (prev + 2, 13) to (start + 0, 14)
4343
- Code(Expression(0, Add)) at (prev + 2, 5) to (start + 0, 6)
4444
= (Zero + (c0 - Zero))

tests/coverage/inline-dead.coverage

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
LL| 1| println!("{}", live::<false>());
66
LL| 1|
77
LL| 1| let f = |x: bool| {
8-
LL| | debug_assert!(
8+
LL| 1| debug_assert!(
99
LL| 0| x
1010
LL| | );
1111
LL| 1| };

0 commit comments

Comments
 (0)