Skip to content

Commit 6fe8067

Browse files
committed
coverage: Overhaul how "visible macros" are determined
1 parent 057f422 commit 6fe8067

File tree

4 files changed

+59
-69
lines changed

4 files changed

+59
-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;
@@ -66,8 +64,7 @@ impl CoverageSpans {
6664
#[derive(Debug, Clone)]
6765
struct CoverageSpan {
6866
pub span: Span,
69-
pub expn_span: Span,
70-
pub current_macro_or_none: OnceCell<Option<Symbol>>,
67+
pub visible_macro: Option<Symbol>,
7168
pub bcb: BasicCoverageBlock,
7269
/// List of all the original spans from MIR that have been merged into this
7370
/// span. Mainly used to precisely skip over gaps when truncating a span.
@@ -77,23 +74,16 @@ struct CoverageSpan {
7774

7875
impl CoverageSpan {
7976
pub fn for_fn_sig(fn_sig_span: Span) -> Self {
80-
Self::new(fn_sig_span, fn_sig_span, START_BCB, false)
77+
Self::new(fn_sig_span, None, START_BCB, false)
8178
}
8279

8380
pub(super) fn new(
8481
span: Span,
85-
expn_span: Span,
82+
visible_macro: Option<Symbol>,
8683
bcb: BasicCoverageBlock,
8784
is_closure: bool,
8885
) -> Self {
89-
Self {
90-
span,
91-
expn_span,
92-
current_macro_or_none: Default::default(),
93-
bcb,
94-
merged_spans: vec![span],
95-
is_closure,
96-
}
86+
Self { span, visible_macro, bcb, merged_spans: vec![span], is_closure }
9787
}
9888

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

154113
/// Converts the initial set of `CoverageSpan`s (one per MIR `Statement` or `Terminator`) into a
@@ -159,10 +118,6 @@ impl CoverageSpan {
159118
/// execution
160119
/// * Carve out (leave uncovered) any span that will be counted by another MIR (notably, closures)
161120
struct CoverageSpansGenerator<'a> {
162-
/// A `Span` covering the function body of the MIR (typically from left curly brace to right
163-
/// curly brace).
164-
body_span: Span,
165-
166121
/// The BasicCoverageBlock Control Flow Graph (BCB CFG).
167122
basic_coverage_blocks: &'a CoverageGraph,
168123

@@ -239,7 +194,6 @@ impl<'a> CoverageSpansGenerator<'a> {
239194
);
240195

241196
let coverage_spans = Self {
242-
body_span: hir_info.body_span,
243197
basic_coverage_blocks,
244198
sorted_spans_iter: sorted_spans.into_iter(),
245199
some_curr: None,
@@ -298,7 +252,7 @@ impl<'a> CoverageSpansGenerator<'a> {
298252
// **originally** the same as the original span of `prev()`. The original spans
299253
// reflect their original sort order, and for equal spans, conveys a partial
300254
// ordering based on CFG dominator priority.
301-
if prev.is_macro_expansion() && curr.is_macro_expansion() {
255+
if prev.visible_macro.is_some() && curr.visible_macro.is_some() {
302256
// Macros that expand to include branching (such as
303257
// `assert_eq!()`, `assert_ne!()`, `info!()`, `debug!()`, or
304258
// `trace!()`) typically generate callee spans with identical
@@ -360,12 +314,7 @@ impl<'a> CoverageSpansGenerator<'a> {
360314
fn maybe_push_macro_name_span(&mut self) {
361315
let curr = self.curr();
362316

363-
let Some(visible_macro) = curr.visible_macro(self.body_span) else { return };
364-
if let Some(prev) = &self.some_prev
365-
&& prev.expn_span.eq_ctxt(curr.expn_span)
366-
{
367-
return;
368-
}
317+
let Some(visible_macro) = curr.visible_macro else { return };
369318

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

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

Lines changed: 49 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,46 @@ 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+
fn unexpand_into_body_span_with_prev(
231+
original_span: Span,
232+
body_span: Span,
233+
) -> Option<(Span, Option<Span>)> {
234+
let mut prev = None;
235+
let mut curr = original_span;
236+
237+
while !body_span.contains(curr) || !curr.eq_ctxt(body_span) {
238+
prev = Some(curr);
239+
curr = curr.parent_callsite()?;
240+
}
241+
242+
debug_assert_eq!(Some(curr), original_span.find_ancestor_in_same_ctxt(body_span));
243+
if let Some(prev) = prev {
244+
debug_assert_eq!(Some(curr), prev.parent_callsite());
245+
}
246+
247+
Some((curr, prev))
207248
}

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)