Skip to content

Commit fd85fd3

Browse files
committed
addressed review feedback
1 parent bbf6bce commit fd85fd3

File tree

3 files changed

+78
-18
lines changed

3 files changed

+78
-18
lines changed

compiler/rustc_mir/src/transform/coverage/spans.rs

+40-12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_middle::ty::TyCtxt;
1313
use rustc_span::source_map::original_sp;
1414
use rustc_span::{BytePos, ExpnKind, MacroKind, Span, Symbol};
1515

16+
use std::cell::RefCell;
1617
use std::cmp::Ordering;
1718

1819
#[derive(Debug, Copy, Clone)]
@@ -68,6 +69,7 @@ impl CoverageStatement {
6869
pub(super) struct CoverageSpan {
6970
pub span: Span,
7071
pub expn_span: Span,
72+
pub current_macro_or_none: RefCell<Option<Option<Symbol>>>,
7173
pub bcb: BasicCoverageBlock,
7274
pub coverage_statements: Vec<CoverageStatement>,
7375
pub is_closure: bool,
@@ -78,6 +80,7 @@ impl CoverageSpan {
7880
Self {
7981
span: fn_sig_span,
8082
expn_span: fn_sig_span,
83+
current_macro_or_none: Default::default(),
8184
bcb: START_BCB,
8285
coverage_statements: vec![],
8386
is_closure: false,
@@ -103,6 +106,7 @@ impl CoverageSpan {
103106
Self {
104107
span,
105108
expn_span,
109+
current_macro_or_none: Default::default(),
106110
bcb,
107111
coverage_statements: vec![CoverageStatement::Statement(bb, span, stmt_index)],
108112
is_closure,
@@ -118,6 +122,7 @@ impl CoverageSpan {
118122
Self {
119123
span,
120124
expn_span,
125+
current_macro_or_none: Default::default(),
121126
bcb,
122127
coverage_statements: vec![CoverageStatement::Terminator(bb, span)],
123128
is_closure: false,
@@ -174,15 +179,19 @@ impl CoverageSpan {
174179
.join("\n")
175180
}
176181

177-
/// If the span is part of a macro, and the macro is visible (expands directly to the given
178-
/// body_span), returns the macro name symbol.
182+
/// If the span is part of a macro, returns the macro name symbol.
179183
pub fn current_macro(&self) -> Option<Symbol> {
180-
if let ExpnKind::Macro(MacroKind::Bang, current_macro) =
181-
self.expn_span.ctxt().outer_expn_data().kind
182-
{
183-
return Some(current_macro);
184-
}
185-
None
184+
self.current_macro_or_none
185+
.borrow_mut()
186+
.get_or_insert_with(|| {
187+
if let ExpnKind::Macro(MacroKind::Bang, current_macro) =
188+
self.expn_span.ctxt().outer_expn_data().kind
189+
{
190+
return Some(current_macro);
191+
}
192+
None
193+
})
194+
.map(|symbol| symbol)
186195
}
187196

188197
/// If the span is part of a macro, and the macro is visible (expands directly to the given
@@ -474,8 +483,8 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
474483
self.curr().expn_span.ctxt() != prev_expn_span.ctxt()
475484
}) {
476485
let merged_prefix_len = self.curr_original_span.lo() - self.curr().span.lo();
477-
let after_macro_bang = merged_prefix_len
478-
+ BytePos(visible_macro.to_string().bytes().count() as u32 + 1);
486+
let after_macro_bang =
487+
merged_prefix_len + BytePos(visible_macro.as_str().bytes().count() as u32 + 1);
479488
let mut macro_name_cov = self.curr().clone();
480489
self.curr_mut().span =
481490
self.curr().span.with_lo(self.curr().span.lo() + after_macro_bang);
@@ -766,6 +775,9 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
766775
}
767776
}
768777

778+
/// See `function_source_span()` for a description of the two returned spans.
779+
/// If the MIR `Statement` is not contributive to computing coverage spans,
780+
/// returns `None`.
769781
pub(super) fn filtered_statement_span(
770782
statement: &'a Statement<'tcx>,
771783
body_span: Span,
@@ -811,6 +823,9 @@ pub(super) fn filtered_statement_span(
811823
}
812824
}
813825

826+
/// See `function_source_span()` for a description of the two returned spans.
827+
/// If the MIR `Terminator` is not contributive to computing coverage spans,
828+
/// returns `None`.
814829
pub(super) fn filtered_terminator_span(
815830
terminator: &'a Terminator<'tcx>,
816831
body_span: Span,
@@ -844,8 +859,21 @@ pub(super) fn filtered_terminator_span(
844859
}
845860
}
846861

847-
/// Returns the span within the function source body, and the given span, which will be different
848-
/// if the given span is an expansion (macro, syntactic sugar, etc.).
862+
/// Returns two spans from the given span (the span associated with a
863+
/// `Statement` or `Terminator`):
864+
///
865+
/// 1. An extrapolated span (pre-expansion[^1]) corresponding to a range within
866+
/// the function's body source. This span is guaranteed to be contained
867+
/// within, or equal to, the `body_span`. If the extrapolated span is not
868+
/// contained within the `body_span`, the `body_span` is returned.
869+
/// 2. The actual `span` value from the `Statement`, before expansion.
870+
///
871+
/// Only the first span is used when computing coverage code regions. The second
872+
/// span is useful if additional expansion data is needed (such as to look up
873+
/// the macro name for a composed span within that macro).)
874+
///
875+
/// [^1]Expansions result from Rust syntax including macros, syntactic
876+
/// sugar, etc.).
849877
#[inline]
850878
fn function_source_span(span: Span, body_span: Span) -> (Span, Span) {
851879
let original_span = original_sp(span, body_span).with_ctxt(body_span.ctxt());

src/test/run-make-fulldeps/coverage-reports/expected_show_coverage.issue-84561.txt

+21-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
| Unexecuted instantiation: <issue_84561::Foo as core::cmp::PartialEq>::ne
1111
------------------
1212
5| |struct Foo(u32);
13-
6| 1|fn test2() {
13+
6| 1|fn test3() {
1414
7| 1| let is_true = std::env::args().len() == 1;
1515
8| 1| let bar = Foo(1);
1616
9| 1| assert_eq!(bar, Foo(1));
@@ -173,8 +173,24 @@
173173
160| 1| debug!("debug is enabled");
174174
161| 1|}
175175
162| |
176-
163| 1|fn main() {
177-
164| 1| test1();
178-
165| 1| test2();
179-
166| 1|}
176+
163| |macro_rules! call_debug {
177+
164| | ($($arg:tt)+) => (
178+
165| 1| fn call_print(s: &str) {
179+
166| 1| print!("{}", s);
180+
167| 1| }
181+
168| |
182+
169| | call_print("called from call_debug: ");
183+
170| | debug!($($arg)+);
184+
171| | );
185+
172| |}
186+
173| |
187+
174| 1|fn test2() {
188+
175| 1| call_debug!("debug is enabled");
189+
176| 1|}
190+
177| |
191+
178| 1|fn main() {
192+
179| 1| test1();
193+
180| 1| test2();
194+
181| 1| test3();
195+
182| 1|}
180196

src/test/run-make-fulldeps/coverage/issue-84561.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// expect-exit-status-101
44
#[derive(PartialEq, Eq)]
55
struct Foo(u32);
6-
fn test2() {
6+
fn test3() {
77
let is_true = std::env::args().len() == 1;
88
let bar = Foo(1);
99
assert_eq!(bar, Foo(1));
@@ -160,7 +160,23 @@ fn test1() {
160160
debug!("debug is enabled");
161161
}
162162

163+
macro_rules! call_debug {
164+
($($arg:tt)+) => (
165+
fn call_print(s: &str) {
166+
print!("{}", s);
167+
}
168+
169+
call_print("called from call_debug: ");
170+
debug!($($arg)+);
171+
);
172+
}
173+
174+
fn test2() {
175+
call_debug!("debug is enabled");
176+
}
177+
163178
fn main() {
164179
test1();
165180
test2();
181+
test3();
166182
}

0 commit comments

Comments
 (0)