Skip to content

Commit d800cd1

Browse files
committed
coverage: Skip spans that can't be un-expanded back to the function body
When we extract coverage spans from MIR, we try to "un-expand" them back to spans that are inside the function's body span. In cases where that doesn't succeed, the current code just swaps in the entire body span instead. But that tends to result in coverage spans that are completely unrelated to the control flow of the affected code, so it's better to just discard those spans.
1 parent 8c2b577 commit d800cd1

File tree

7 files changed

+28
-28
lines changed

7 files changed

+28
-28
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ fn bcb_to_initial_coverage_spans<'a, 'tcx>(
6363

6464
let statement_spans = data.statements.iter().filter_map(move |statement| {
6565
let expn_span = filtered_statement_span(statement)?;
66-
let span = function_source_span(expn_span, body_span);
66+
let span = unexpand_into_body_span(expn_span, body_span)?;
6767

6868
Some(CoverageSpan::new(span, expn_span, bcb, is_closure(statement)))
6969
});
7070

7171
let terminator_span = Some(data.terminator()).into_iter().filter_map(move |terminator| {
7272
let expn_span = filtered_terminator_span(terminator)?;
73-
let span = function_source_span(expn_span, body_span);
73+
let span = unexpand_into_body_span(expn_span, body_span)?;
7474

7575
Some(CoverageSpan::new(span, expn_span, bcb, false))
7676
});
@@ -180,14 +180,14 @@ fn filtered_terminator_span(terminator: &Terminator<'_>) -> Option<Span> {
180180
/// Returns an extrapolated span (pre-expansion[^1]) corresponding to a range
181181
/// within the function's body source. This span is guaranteed to be contained
182182
/// within, or equal to, the `body_span`. If the extrapolated span is not
183-
/// contained within the `body_span`, the `body_span` is returned.
183+
/// contained within the `body_span`, `None` is returned.
184184
///
185185
/// [^1]Expansions result from Rust syntax including macros, syntactic sugar,
186186
/// etc.).
187187
#[inline]
188-
fn function_source_span(span: Span, body_span: Span) -> Span {
188+
fn unexpand_into_body_span(span: Span, body_span: Span) -> Option<Span> {
189189
use rustc_span::source_map::original_sp;
190190

191191
let original_span = original_sp(span, body_span).with_ctxt(body_span.ctxt());
192-
if body_span.contains(original_span) { original_span } else { body_span }
192+
body_span.contains(original_span).then_some(original_span)
193193
}

tests/coverage/async.cov-map

+6-6
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,28 @@ Number of file 0 mappings: 6
7474
= ((c0 + c1) - c1)
7575

7676
Function name: async::executor::block_on::VTABLE::{closure#0}
77-
Raw bytes (9): 0x[01, 01, 00, 01, 01, 72, 11, 00, 33]
77+
Raw bytes (9): 0x[01, 01, 00, 01, 01, 72, 11, 00, 31]
7878
Number of files: 1
7979
- file 0 => global file 1
8080
Number of expressions: 0
8181
Number of file 0 mappings: 1
82-
- Code(Counter(0)) at (prev + 114, 17) to (start + 0, 51)
82+
- Code(Counter(0)) at (prev + 114, 17) to (start + 0, 49)
8383

8484
Function name: async::executor::block_on::VTABLE::{closure#1}
85-
Raw bytes (9): 0x[01, 01, 00, 01, 01, 73, 11, 00, 33]
85+
Raw bytes (9): 0x[01, 01, 00, 01, 01, 73, 11, 00, 31]
8686
Number of files: 1
8787
- file 0 => global file 1
8888
Number of expressions: 0
8989
Number of file 0 mappings: 1
90-
- Code(Counter(0)) at (prev + 115, 17) to (start + 0, 51)
90+
- Code(Counter(0)) at (prev + 115, 17) to (start + 0, 49)
9191

9292
Function name: async::executor::block_on::VTABLE::{closure#2}
93-
Raw bytes (9): 0x[01, 01, 00, 01, 01, 74, 11, 00, 33]
93+
Raw bytes (9): 0x[01, 01, 00, 01, 01, 74, 11, 00, 31]
9494
Number of files: 1
9595
- file 0 => global file 1
9696
Number of expressions: 0
9797
Number of file 0 mappings: 1
98-
- Code(Counter(0)) at (prev + 116, 17) to (start + 0, 51)
98+
- Code(Counter(0)) at (prev + 116, 17) to (start + 0, 49)
9999

100100
Function name: async::executor::block_on::VTABLE::{closure#3}
101101
Raw bytes (9): 0x[01, 01, 00, 01, 01, 75, 11, 00, 13]

tests/coverage/async2.cov-map

+6-6
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,28 @@ Number of file 0 mappings: 6
7878
= ((c0 + c1) - c1)
7979

8080
Function name: async2::executor::block_on::VTABLE::{closure#0}
81-
Raw bytes (9): 0x[01, 01, 00, 01, 01, 2b, 11, 00, 33]
81+
Raw bytes (9): 0x[01, 01, 00, 01, 01, 2b, 11, 00, 31]
8282
Number of files: 1
8383
- file 0 => global file 1
8484
Number of expressions: 0
8585
Number of file 0 mappings: 1
86-
- Code(Counter(0)) at (prev + 43, 17) to (start + 0, 51)
86+
- Code(Counter(0)) at (prev + 43, 17) to (start + 0, 49)
8787

8888
Function name: async2::executor::block_on::VTABLE::{closure#1}
89-
Raw bytes (9): 0x[01, 01, 00, 01, 01, 2c, 11, 00, 33]
89+
Raw bytes (9): 0x[01, 01, 00, 01, 01, 2c, 11, 00, 31]
9090
Number of files: 1
9191
- file 0 => global file 1
9292
Number of expressions: 0
9393
Number of file 0 mappings: 1
94-
- Code(Counter(0)) at (prev + 44, 17) to (start + 0, 51)
94+
- Code(Counter(0)) at (prev + 44, 17) to (start + 0, 49)
9595

9696
Function name: async2::executor::block_on::VTABLE::{closure#2}
97-
Raw bytes (9): 0x[01, 01, 00, 01, 01, 2d, 11, 00, 33]
97+
Raw bytes (9): 0x[01, 01, 00, 01, 01, 2d, 11, 00, 31]
9898
Number of files: 1
9999
- file 0 => global file 1
100100
Number of expressions: 0
101101
Number of file 0 mappings: 1
102-
- Code(Counter(0)) at (prev + 45, 17) to (start + 0, 51)
102+
- Code(Counter(0)) at (prev + 45, 17) to (start + 0, 49)
103103

104104
Function name: async2::executor::block_on::VTABLE::{closure#3}
105105
Raw bytes (9): 0x[01, 01, 00, 01, 01, 2e, 11, 00, 13]

tests/coverage/inline.cov-map

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ Number of file 0 mappings: 5
1515
= ((c0 + c1) - c1)
1616

1717
Function name: inline::error
18-
Raw bytes (9): 0x[01, 01, 00, 01, 01, 31, 01, 02, 02]
18+
Raw bytes (9): 0x[01, 01, 00, 01, 01, 31, 01, 01, 14]
1919
Number of files: 1
2020
- file 0 => global file 1
2121
Number of expressions: 0
2222
Number of file 0 mappings: 1
23-
- Code(Counter(0)) at (prev + 49, 1) to (start + 2, 2)
23+
- Code(Counter(0)) at (prev + 49, 1) to (start + 1, 20)
2424

2525
Function name: inline::length::<char>
2626
Raw bytes (9): 0x[01, 01, 00, 01, 01, 1e, 01, 02, 02]

tests/coverage/inline.coverage

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@
5050
LL| |#[inline(always)]
5151
LL| 0|fn error() {
5252
LL| 0| panic!("error");
53-
LL| 0|}
53+
LL| |}
5454

tests/coverage/unreachable.cov-map

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
Function name: unreachable::UNREACHABLE_CLOSURE::{closure#0}
2-
Raw bytes (9): 0x[01, 01, 00, 01, 01, 0f, 27, 00, 49]
2+
Raw bytes (9): 0x[01, 01, 00, 01, 01, 0f, 27, 00, 47]
33
Number of files: 1
44
- file 0 => global file 1
55
Number of expressions: 0
66
Number of file 0 mappings: 1
7-
- Code(Counter(0)) at (prev + 15, 39) to (start + 0, 73)
7+
- Code(Counter(0)) at (prev + 15, 39) to (start + 0, 71)
88

99
Function name: unreachable::unreachable_function
10-
Raw bytes (9): 0x[01, 01, 00, 01, 01, 11, 01, 02, 02]
10+
Raw bytes (9): 0x[01, 01, 00, 01, 01, 11, 01, 01, 25]
1111
Number of files: 1
1212
- file 0 => global file 1
1313
Number of expressions: 0
1414
Number of file 0 mappings: 1
15-
- Code(Counter(0)) at (prev + 17, 1) to (start + 2, 2)
15+
- Code(Counter(0)) at (prev + 17, 1) to (start + 1, 37)
1616

1717
Function name: unreachable::unreachable_intrinsic
18-
Raw bytes (9): 0x[01, 01, 00, 01, 01, 16, 01, 02, 02]
18+
Raw bytes (9): 0x[01, 01, 00, 01, 01, 16, 01, 01, 2c]
1919
Number of files: 1
2020
- file 0 => global file 1
2121
Number of expressions: 0
2222
Number of file 0 mappings: 1
23-
- Code(Counter(0)) at (prev + 22, 1) to (start + 2, 2)
23+
- Code(Counter(0)) at (prev + 22, 1) to (start + 1, 44)
2424

tests/coverage/unreachable.coverage

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
LL| |
1717
LL| 0|fn unreachable_function() {
1818
LL| 0| unsafe { unreachable_unchecked() }
19-
LL| 0|}
19+
LL| |}
2020
LL| |
2121
LL| |// Use an intrinsic to more reliably trigger unreachable-propagation.
2222
LL| 0|fn unreachable_intrinsic() {
2323
LL| 0| unsafe { std::intrinsics::unreachable() }
24-
LL| 0|}
24+
LL| |}
2525
LL| |
2626
LL| |#[coverage(off)]
2727
LL| |fn main() {

0 commit comments

Comments
 (0)