Skip to content

Commit 26cea8a

Browse files
committed
coverage: Don't split bang-macro spans, just truncate them
1 parent 62a533c commit 26cea8a

25 files changed

+143
-160
lines changed

compiler/rustc_mir_transform/src/coverage/mappings.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub(super) fn extract_all_mapping_info_from_mir<'tcx>(
9696
}
9797
} else {
9898
// Extract coverage spans from MIR statements/terminators as normal.
99-
extract_refined_covspans(mir_body, hir_info, graph, &mut code_mappings);
99+
extract_refined_covspans(tcx, mir_body, hir_info, graph, &mut code_mappings);
100100
}
101101

102102
branch_pairs.extend(extract_branch_pairs(mir_body, hir_info, graph));

compiler/rustc_mir_transform/src/coverage/spans.rs

+14-31
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::iter;
33

44
use rustc_data_structures::fx::FxHashSet;
55
use rustc_middle::mir;
6+
use rustc_middle::ty::TyCtxt;
67
use rustc_span::{DesugaringKind, ExpnKind, MacroKind, Span};
78
use tracing::{debug, debug_span, instrument};
89

@@ -12,8 +13,9 @@ use crate::coverage::{ExtractedHirInfo, mappings, unexpand};
1213

1314
mod from_mir;
1415

15-
pub(super) fn extract_refined_covspans(
16-
mir_body: &mir::Body<'_>,
16+
pub(super) fn extract_refined_covspans<'tcx>(
17+
tcx: TyCtxt<'tcx>,
18+
mir_body: &mir::Body<'tcx>,
1719
hir_info: &ExtractedHirInfo,
1820
graph: &CoverageGraph,
1921
code_mappings: &mut impl Extend<mappings::CodeMapping>,
@@ -51,7 +53,7 @@ pub(super) fn extract_refined_covspans(
5153
// First, perform the passes that need macro information.
5254
covspans.sort_by(|a, b| graph.cmp_in_dominator_order(a.bcb, b.bcb));
5355
remove_unwanted_expansion_spans(&mut covspans);
54-
split_visible_macro_spans(&mut covspans);
56+
shrink_visible_macro_spans(tcx, &mut covspans);
5557

5658
// We no longer need the extra information in `SpanFromMir`, so convert to `Covspan`.
5759
let mut covspans = covspans.into_iter().map(SpanFromMir::into_covspan).collect::<Vec<_>>();
@@ -128,35 +130,16 @@ fn remove_unwanted_expansion_spans(covspans: &mut Vec<SpanFromMir>) {
128130
}
129131

130132
/// When a span corresponds to a macro invocation that is visible from the
131-
/// function body, split it into two parts. The first part covers just the
132-
/// macro name plus `!`, and the second part covers the rest of the macro
133-
/// invocation. This seems to give better results for code that uses macros.
134-
fn split_visible_macro_spans(covspans: &mut Vec<SpanFromMir>) {
135-
let mut extra_spans = vec![];
136-
137-
covspans.retain(|covspan| {
138-
let Some(ExpnKind::Macro(MacroKind::Bang, visible_macro)) = covspan.expn_kind else {
139-
return true;
140-
};
141-
142-
let split_len = visible_macro.as_str().len() as u32 + 1;
143-
let (before, after) = covspan.span.split_at(split_len);
144-
if !covspan.span.contains(before) || !covspan.span.contains(after) {
145-
// Something is unexpectedly wrong with the split point.
146-
// The debug assertion in `split_at` will have already caught this,
147-
// but in release builds it's safer to do nothing and maybe get a
148-
// bug report for unexpected coverage, rather than risk an ICE.
149-
return true;
133+
/// function body, truncate it to just the macro name plus `!`.
134+
/// This seems to give better results for code that uses macros.
135+
fn shrink_visible_macro_spans(tcx: TyCtxt<'_>, covspans: &mut Vec<SpanFromMir>) {
136+
let source_map = tcx.sess.source_map();
137+
138+
for covspan in covspans {
139+
if matches!(covspan.expn_kind, Some(ExpnKind::Macro(MacroKind::Bang, _))) {
140+
covspan.span = source_map.span_through_char(covspan.span, '!');
150141
}
151-
152-
extra_spans.push(SpanFromMir::new(before, covspan.expn_kind.clone(), covspan.bcb));
153-
extra_spans.push(SpanFromMir::new(after, covspan.expn_kind.clone(), covspan.bcb));
154-
false // Discard the original covspan that we just split.
155-
});
156-
157-
// The newly-split spans are added at the end, so any previous sorting
158-
// is not preserved.
159-
covspans.extend(extra_spans);
142+
}
160143
}
161144

162145
/// Uses the holes to divide the given covspans into buckets, such that:

tests/coverage/abort.cov-map

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ Number of file 0 mappings: 13
3434
Highest counter ID seen: c4
3535

3636
Function name: abort::might_abort
37-
Raw bytes (21): 0x[01, 01, 01, 01, 05, 03, 01, 03, 01, 01, 14, 05, 02, 09, 01, 24, 02, 02, 0c, 03, 02]
37+
Raw bytes (21): 0x[01, 01, 01, 01, 05, 03, 01, 03, 01, 01, 14, 05, 02, 09, 01, 0f, 02, 02, 0c, 03, 02]
3838
Number of files: 1
3939
- file 0 => global file 1
4040
Number of expressions: 1
4141
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
4242
Number of file 0 mappings: 3
4343
- Code(Counter(0)) at (prev + 3, 1) to (start + 1, 20)
44-
- Code(Counter(1)) at (prev + 2, 9) to (start + 1, 36)
44+
- Code(Counter(1)) at (prev + 2, 9) to (start + 1, 15)
4545
- Code(Expression(0, Sub)) at (prev + 2, 12) to (start + 3, 2)
4646
= (c0 - c1)
4747
Highest counter ID seen: c1

tests/coverage/assert_not.cov-map

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
Function name: assert_not::main
2-
Raw bytes (29): 0x[01, 01, 00, 05, 01, 06, 01, 01, 12, 01, 02, 05, 00, 14, 01, 01, 05, 00, 14, 01, 01, 05, 00, 16, 01, 01, 01, 00, 02]
2+
Raw bytes (29): 0x[01, 01, 00, 05, 01, 06, 01, 01, 11, 01, 02, 05, 00, 13, 01, 01, 05, 00, 13, 01, 01, 05, 00, 15, 01, 01, 01, 00, 02]
33
Number of files: 1
44
- file 0 => global file 1
55
Number of expressions: 0
66
Number of file 0 mappings: 5
7-
- Code(Counter(0)) at (prev + 6, 1) to (start + 1, 18)
8-
- Code(Counter(0)) at (prev + 2, 5) to (start + 0, 20)
9-
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 20)
10-
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 22)
7+
- Code(Counter(0)) at (prev + 6, 1) to (start + 1, 17)
8+
- Code(Counter(0)) at (prev + 2, 5) to (start + 0, 19)
9+
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 19)
10+
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 21)
1111
- Code(Counter(0)) at (prev + 1, 1) to (start + 0, 2)
1212
Highest counter ID seen: c0
1313

tests/coverage/bad_counter_ids.cov-map

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
Function name: bad_counter_ids::eq_bad
2-
Raw bytes (14): 0x[01, 01, 00, 02, 01, 24, 01, 02, 1f, 00, 03, 01, 00, 02]
2+
Raw bytes (14): 0x[01, 01, 00, 02, 01, 24, 01, 02, 0f, 00, 03, 01, 00, 02]
33
Number of files: 1
44
- file 0 => global file 1
55
Number of expressions: 0
66
Number of file 0 mappings: 2
7-
- Code(Counter(0)) at (prev + 36, 1) to (start + 2, 31)
7+
- Code(Counter(0)) at (prev + 36, 1) to (start + 2, 15)
88
- Code(Zero) at (prev + 3, 1) to (start + 0, 2)
99
Highest counter ID seen: c0
1010

@@ -20,12 +20,12 @@ Number of file 0 mappings: 3
2020
Highest counter ID seen: c0
2121

2222
Function name: bad_counter_ids::eq_good
23-
Raw bytes (14): 0x[01, 01, 00, 02, 01, 10, 01, 02, 1f, 01, 03, 01, 00, 02]
23+
Raw bytes (14): 0x[01, 01, 00, 02, 01, 10, 01, 02, 0f, 01, 03, 01, 00, 02]
2424
Number of files: 1
2525
- file 0 => global file 1
2626
Number of expressions: 0
2727
Number of file 0 mappings: 2
28-
- Code(Counter(0)) at (prev + 16, 1) to (start + 2, 31)
28+
- Code(Counter(0)) at (prev + 16, 1) to (start + 2, 15)
2929
- Code(Counter(0)) at (prev + 3, 1) to (start + 0, 2)
3030
Highest counter ID seen: c0
3131

@@ -41,12 +41,12 @@ Number of file 0 mappings: 3
4141
Highest counter ID seen: c0
4242

4343
Function name: bad_counter_ids::ne_bad
44-
Raw bytes (14): 0x[01, 01, 00, 02, 01, 2e, 01, 02, 1f, 00, 03, 01, 00, 02]
44+
Raw bytes (14): 0x[01, 01, 00, 02, 01, 2e, 01, 02, 0f, 00, 03, 01, 00, 02]
4545
Number of files: 1
4646
- file 0 => global file 1
4747
Number of expressions: 0
4848
Number of file 0 mappings: 2
49-
- Code(Counter(0)) at (prev + 46, 1) to (start + 2, 31)
49+
- Code(Counter(0)) at (prev + 46, 1) to (start + 2, 15)
5050
- Code(Zero) at (prev + 3, 1) to (start + 0, 2)
5151
Highest counter ID seen: c0
5252

@@ -62,12 +62,12 @@ Number of file 0 mappings: 3
6262
Highest counter ID seen: c0
6363

6464
Function name: bad_counter_ids::ne_good
65-
Raw bytes (14): 0x[01, 01, 00, 02, 01, 1a, 01, 02, 1f, 01, 03, 01, 00, 02]
65+
Raw bytes (14): 0x[01, 01, 00, 02, 01, 1a, 01, 02, 0f, 01, 03, 01, 00, 02]
6666
Number of files: 1
6767
- file 0 => global file 1
6868
Number of expressions: 0
6969
Number of file 0 mappings: 2
70-
- Code(Counter(0)) at (prev + 26, 1) to (start + 2, 31)
70+
- Code(Counter(0)) at (prev + 26, 1) to (start + 2, 15)
7171
- Code(Counter(0)) at (prev + 3, 1) to (start + 0, 2)
7272
Highest counter ID seen: c0
7373

tests/coverage/branch/guard.cov-map

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Function name: guard::branch_match_guard
2-
Raw bytes (89): 0x[01, 01, 08, 05, 0d, 09, 05, 05, 0f, 0d, 11, 17, 1b, 01, 05, 1f, 11, 09, 0d, 0d, 01, 0c, 01, 01, 10, 02, 03, 0b, 00, 0c, 06, 01, 14, 02, 0a, 0d, 03, 0e, 00, 0f, 05, 00, 14, 00, 19, 20, 0d, 02, 00, 14, 00, 1e, 0d, 00, 1d, 02, 0a, 11, 03, 0e, 00, 0f, 02, 00, 14, 00, 19, 20, 11, 0a, 00, 14, 00, 1e, 11, 00, 1d, 02, 0a, 12, 03, 0e, 02, 0a, 01, 04, 01, 00, 02]
2+
Raw bytes (89): 0x[01, 01, 08, 05, 0d, 09, 05, 05, 0f, 0d, 11, 17, 1b, 01, 05, 1f, 11, 09, 0d, 0d, 01, 0c, 01, 01, 0e, 02, 03, 0b, 00, 0c, 06, 01, 14, 02, 0a, 0d, 03, 0e, 00, 0f, 05, 00, 14, 00, 19, 20, 0d, 02, 00, 14, 00, 1e, 0d, 00, 1d, 02, 0a, 11, 03, 0e, 00, 0f, 02, 00, 14, 00, 19, 20, 11, 0a, 00, 14, 00, 1e, 11, 00, 1d, 02, 0a, 12, 03, 0e, 02, 0a, 01, 04, 01, 00, 02]
33
Number of files: 1
44
- file 0 => global file 1
55
Number of expressions: 8
@@ -12,7 +12,7 @@ Number of expressions: 8
1212
- expression 6 operands: lhs = Expression(7, Add), rhs = Counter(4)
1313
- expression 7 operands: lhs = Counter(2), rhs = Counter(3)
1414
Number of file 0 mappings: 13
15-
- Code(Counter(0)) at (prev + 12, 1) to (start + 1, 16)
15+
- Code(Counter(0)) at (prev + 12, 1) to (start + 1, 14)
1616
- Code(Expression(0, Sub)) at (prev + 3, 11) to (start + 0, 12)
1717
= (c1 - c3)
1818
- Code(Expression(1, Sub)) at (prev + 1, 20) to (start + 2, 10)

tests/coverage/branch/if-let.cov-map

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Function name: if_let::if_let
2-
Raw bytes (43): 0x[01, 01, 01, 01, 05, 07, 01, 0c, 01, 01, 10, 20, 02, 05, 03, 0c, 00, 13, 02, 00, 11, 00, 12, 01, 00, 16, 00, 1b, 02, 00, 1c, 02, 06, 05, 02, 0c, 02, 06, 01, 03, 05, 01, 02]
2+
Raw bytes (43): 0x[01, 01, 01, 01, 05, 07, 01, 0c, 01, 01, 0e, 20, 02, 05, 03, 0c, 00, 13, 02, 00, 11, 00, 12, 01, 00, 16, 00, 1b, 02, 00, 1c, 02, 06, 05, 02, 0c, 02, 06, 01, 03, 05, 01, 02]
33
Number of files: 1
44
- file 0 => global file 1
55
Number of expressions: 1
66
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
77
Number of file 0 mappings: 7
8-
- Code(Counter(0)) at (prev + 12, 1) to (start + 1, 16)
8+
- Code(Counter(0)) at (prev + 12, 1) to (start + 1, 14)
99
- Branch { true: Expression(0, Sub), false: Counter(1) } at (prev + 3, 12) to (start + 0, 19)
1010
true = (c0 - c1)
1111
false = c1

tests/coverage/branch/if.cov-map

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
Function name: if::branch_and
2-
Raw bytes (54): 0x[01, 01, 03, 01, 05, 05, 09, 01, 09, 08, 01, 2b, 01, 01, 10, 01, 03, 08, 00, 09, 20, 05, 02, 00, 08, 00, 09, 05, 00, 0d, 00, 0e, 20, 09, 06, 00, 0d, 00, 0e, 09, 00, 0f, 02, 06, 0a, 02, 0c, 02, 06, 01, 03, 01, 00, 02]
2+
Raw bytes (54): 0x[01, 01, 03, 01, 05, 05, 09, 01, 09, 08, 01, 2b, 01, 01, 0e, 01, 03, 08, 00, 09, 20, 05, 02, 00, 08, 00, 09, 05, 00, 0d, 00, 0e, 20, 09, 06, 00, 0d, 00, 0e, 09, 00, 0f, 02, 06, 0a, 02, 0c, 02, 06, 01, 03, 01, 00, 02]
33
Number of files: 1
44
- file 0 => global file 1
55
Number of expressions: 3
66
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
77
- expression 1 operands: lhs = Counter(1), rhs = Counter(2)
88
- expression 2 operands: lhs = Counter(0), rhs = Counter(2)
99
Number of file 0 mappings: 8
10-
- Code(Counter(0)) at (prev + 43, 1) to (start + 1, 16)
10+
- Code(Counter(0)) at (prev + 43, 1) to (start + 1, 14)
1111
- Code(Counter(0)) at (prev + 3, 8) to (start + 0, 9)
1212
- Branch { true: Counter(1), false: Expression(0, Sub) } at (prev + 0, 8) to (start + 0, 9)
1313
true = c1
@@ -23,7 +23,7 @@ Number of file 0 mappings: 8
2323
Highest counter ID seen: c2
2424

2525
Function name: if::branch_not
26-
Raw bytes (116): 0x[01, 01, 07, 01, 05, 01, 09, 01, 09, 01, 0d, 01, 0d, 01, 11, 01, 11, 12, 01, 0c, 01, 01, 10, 01, 03, 08, 00, 09, 20, 05, 02, 00, 08, 00, 09, 05, 01, 09, 00, 10, 02, 01, 05, 00, 06, 01, 01, 08, 00, 0a, 20, 0a, 09, 00, 08, 00, 0a, 0a, 00, 0b, 02, 06, 09, 02, 05, 00, 06, 01, 01, 08, 00, 0b, 20, 0d, 12, 00, 08, 00, 0b, 0d, 00, 0c, 02, 06, 12, 02, 05, 00, 06, 01, 01, 08, 00, 0c, 20, 1a, 11, 00, 08, 00, 0c, 1a, 00, 0d, 02, 06, 11, 02, 05, 00, 06, 01, 01, 01, 00, 02]
26+
Raw bytes (116): 0x[01, 01, 07, 01, 05, 01, 09, 01, 09, 01, 0d, 01, 0d, 01, 11, 01, 11, 12, 01, 0c, 01, 01, 0e, 01, 03, 08, 00, 09, 20, 05, 02, 00, 08, 00, 09, 05, 01, 09, 00, 10, 02, 01, 05, 00, 06, 01, 01, 08, 00, 0a, 20, 0a, 09, 00, 08, 00, 0a, 0a, 00, 0b, 02, 06, 09, 02, 05, 00, 06, 01, 01, 08, 00, 0b, 20, 0d, 12, 00, 08, 00, 0b, 0d, 00, 0c, 02, 06, 12, 02, 05, 00, 06, 01, 01, 08, 00, 0c, 20, 1a, 11, 00, 08, 00, 0c, 1a, 00, 0d, 02, 06, 11, 02, 05, 00, 06, 01, 01, 01, 00, 02]
2727
Number of files: 1
2828
- file 0 => global file 1
2929
Number of expressions: 7
@@ -35,7 +35,7 @@ Number of expressions: 7
3535
- expression 5 operands: lhs = Counter(0), rhs = Counter(4)
3636
- expression 6 operands: lhs = Counter(0), rhs = Counter(4)
3737
Number of file 0 mappings: 18
38-
- Code(Counter(0)) at (prev + 12, 1) to (start + 1, 16)
38+
- Code(Counter(0)) at (prev + 12, 1) to (start + 1, 14)
3939
- Code(Counter(0)) at (prev + 3, 8) to (start + 0, 9)
4040
- Branch { true: Counter(1), false: Expression(0, Sub) } at (prev + 0, 8) to (start + 0, 9)
4141
true = c1
@@ -68,7 +68,7 @@ Number of file 0 mappings: 18
6868
Highest counter ID seen: c4
6969

7070
Function name: if::branch_not_as
71-
Raw bytes (90): 0x[01, 01, 05, 01, 05, 01, 09, 01, 09, 01, 0d, 01, 0d, 0e, 01, 1d, 01, 01, 10, 01, 03, 08, 00, 14, 20, 02, 05, 00, 08, 00, 14, 02, 00, 15, 02, 06, 05, 02, 05, 00, 06, 01, 01, 08, 00, 15, 20, 09, 0a, 00, 08, 00, 15, 09, 00, 16, 02, 06, 0a, 02, 05, 00, 06, 01, 01, 08, 00, 16, 20, 12, 0d, 00, 08, 00, 16, 12, 00, 17, 02, 06, 0d, 02, 05, 00, 06, 01, 01, 01, 00, 02]
71+
Raw bytes (90): 0x[01, 01, 05, 01, 05, 01, 09, 01, 09, 01, 0d, 01, 0d, 0e, 01, 1d, 01, 01, 0e, 01, 03, 08, 00, 14, 20, 02, 05, 00, 08, 00, 14, 02, 00, 15, 02, 06, 05, 02, 05, 00, 06, 01, 01, 08, 00, 15, 20, 09, 0a, 00, 08, 00, 15, 09, 00, 16, 02, 06, 0a, 02, 05, 00, 06, 01, 01, 08, 00, 16, 20, 12, 0d, 00, 08, 00, 16, 12, 00, 17, 02, 06, 0d, 02, 05, 00, 06, 01, 01, 01, 00, 02]
7272
Number of files: 1
7373
- file 0 => global file 1
7474
Number of expressions: 5
@@ -78,7 +78,7 @@ Number of expressions: 5
7878
- expression 3 operands: lhs = Counter(0), rhs = Counter(3)
7979
- expression 4 operands: lhs = Counter(0), rhs = Counter(3)
8080
Number of file 0 mappings: 14
81-
- Code(Counter(0)) at (prev + 29, 1) to (start + 1, 16)
81+
- Code(Counter(0)) at (prev + 29, 1) to (start + 1, 14)
8282
- Code(Counter(0)) at (prev + 3, 8) to (start + 0, 20)
8383
- Branch { true: Expression(0, Sub), false: Counter(1) } at (prev + 0, 8) to (start + 0, 20)
8484
true = (c0 - c1)
@@ -104,7 +104,7 @@ Number of file 0 mappings: 14
104104
Highest counter ID seen: c3
105105

106106
Function name: if::branch_or
107-
Raw bytes (60): 0x[01, 01, 06, 01, 05, 01, 17, 05, 09, 05, 09, 01, 17, 05, 09, 08, 01, 35, 01, 01, 10, 01, 03, 08, 00, 09, 20, 05, 02, 00, 08, 00, 09, 02, 00, 0d, 00, 0e, 20, 09, 12, 00, 0d, 00, 0e, 17, 00, 0f, 02, 06, 12, 02, 0c, 02, 06, 01, 03, 01, 00, 02]
107+
Raw bytes (60): 0x[01, 01, 06, 01, 05, 01, 17, 05, 09, 05, 09, 01, 17, 05, 09, 08, 01, 35, 01, 01, 0e, 01, 03, 08, 00, 09, 20, 05, 02, 00, 08, 00, 09, 02, 00, 0d, 00, 0e, 20, 09, 12, 00, 0d, 00, 0e, 17, 00, 0f, 02, 06, 12, 02, 0c, 02, 06, 01, 03, 01, 00, 02]
108108
Number of files: 1
109109
- file 0 => global file 1
110110
Number of expressions: 6
@@ -115,7 +115,7 @@ Number of expressions: 6
115115
- expression 4 operands: lhs = Counter(0), rhs = Expression(5, Add)
116116
- expression 5 operands: lhs = Counter(1), rhs = Counter(2)
117117
Number of file 0 mappings: 8
118-
- Code(Counter(0)) at (prev + 53, 1) to (start + 1, 16)
118+
- Code(Counter(0)) at (prev + 53, 1) to (start + 1, 14)
119119
- Code(Counter(0)) at (prev + 3, 8) to (start + 0, 9)
120120
- Branch { true: Counter(1), false: Expression(0, Sub) } at (prev + 0, 8) to (start + 0, 9)
121121
true = c1

0 commit comments

Comments
 (0)