Skip to content

Commit 1893721

Browse files
committed
Fixes the issue with uncovered source in async function bodies
The body_span was assumed to be in the Span root context, but this was not the case for async function bodies.
1 parent 3ece606 commit 1893721

File tree

4 files changed

+71
-89
lines changed

4 files changed

+71
-89
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
246246
) -> Vec<CoverageSpan> {
247247
let mut coverage_spans = CoverageSpans {
248248
mir_body,
249-
fn_sig_span,
250-
body_span,
249+
fn_sig_span: fn_sig_span.with_ctxt(SyntaxContext::root()),
250+
body_span: body_span.with_ctxt(SyntaxContext::root()),
251251
basic_coverage_blocks,
252252
sorted_spans_iter: None,
253253
refined_spans: Vec::with_capacity(basic_coverage_blocks.num_nodes() * 2),

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

Lines changed: 68 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
18| |// the println!() and `let` assignment lines in the coverage code region(s), as it does in the
1919
19| |// non-async function above, unless the `println!()` is inside a covered block.
2020
20| 1|async fn async_func() {
21-
21| | println!("async_func was covered");
22-
22| | let b = true;
21+
21| 1| println!("async_func was covered");
22+
22| 1| let b = true;
2323
23| 1| if b {
2424
24| 1| println!("async_func println in block");
2525
25| 1| }
@@ -30,8 +30,8 @@
3030
29| |// showing coverage, so the entire async closure _appears_ uncovered; but this is not exactly true.
3131
30| |// It's only certain kinds of lines and/or their context that results in missing coverage.
3232
31| 1|async fn async_func_just_println() {
33-
32| | println!("async_func_just_println was covered");
34-
33| |}
33+
32| 1| println!("async_func_just_println was covered");
34+
33| 1|}
3535
34| |
3636
35| 1|fn main() {
3737
36| 1| println!("codecovsample::main");
@@ -40,85 +40,76 @@
4040
39| 1|
4141
40| 1| executor::block_on(async_func());
4242
41| 1| executor::block_on(async_func_just_println());
43-
42| 1|
44-
43| 1| // let mut future = Box::pin(async_func());
45-
44| 1| // executor::block_on(future.as_mut());
46-
45| 1|
47-
46| 1| // let mut future = Box::pin(async_func());
48-
47| 1| // executor::block_on(future.as_mut());
49-
48| 1|
50-
49| 1| // let mut future = Box::pin(async_func_just_println());
51-
50| 1| // executor::block_on(future.as_mut());
52-
51| 1|}
53-
52| |
54-
53| |mod executor {
55-
54| | use core::{
56-
55| | future::Future,
57-
56| | pin::Pin,
58-
57| | task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
59-
58| | };
60-
59| |
61-
60| 2| pub fn block_on<F: Future>(mut future: F) -> F::Output {
62-
61| 2| let mut future = unsafe { Pin::new_unchecked(&mut future) };
63-
62| 2| use std::hint::unreachable_unchecked;
64-
63| 2| static VTABLE: RawWakerVTable = RawWakerVTable::new(
65-
64| 2| |_| unsafe { unreachable_unchecked() }, // clone
43+
42| 1|}
44+
43| |
45+
44| |mod executor {
46+
45| | use core::{
47+
46| | future::Future,
48+
47| | pin::Pin,
49+
48| | task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
50+
49| | };
51+
50| |
52+
51| 2| pub fn block_on<F: Future>(mut future: F) -> F::Output {
53+
52| 2| let mut future = unsafe { Pin::new_unchecked(&mut future) };
54+
53| 2| use std::hint::unreachable_unchecked;
55+
54| 2| static VTABLE: RawWakerVTable = RawWakerVTable::new(
56+
55| 2| |_| unsafe { unreachable_unchecked() }, // clone
6657
^0
67-
65| 2| |_| unsafe { unreachable_unchecked() }, // wake
58+
56| 2| |_| unsafe { unreachable_unchecked() }, // wake
6859
^0
69-
66| 2| |_| unsafe { unreachable_unchecked() }, // wake_by_ref
60+
57| 2| |_| unsafe { unreachable_unchecked() }, // wake_by_ref
7061
^0
71-
67| 2| |_| (),
72-
68| 2| );
73-
69| 2| let waker = unsafe { Waker::from_raw(RawWaker::new(core::ptr::null(), &VTABLE)) };
74-
70| 2| let mut context = Context::from_waker(&waker);
75-
71| |
76-
72| | loop {
77-
73| 2| if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
78-
74| 2| break val;
79-
75| 0| }
80-
76| | }
81-
77| 2| }
62+
58| 2| |_| (),
63+
59| 2| );
64+
60| 2| let waker = unsafe { Waker::from_raw(RawWaker::new(core::ptr::null(), &VTABLE)) };
65+
61| 2| let mut context = Context::from_waker(&waker);
66+
62| |
67+
63| | loop {
68+
64| 2| if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
69+
65| 2| break val;
70+
66| 0| }
71+
67| | }
72+
68| 2| }
8273
------------------
8374
| async2::executor::block_on::<core::future::from_generator::GenFuture<async2::async_func::{closure#0}>>:
84-
| 60| 1| pub fn block_on<F: Future>(mut future: F) -> F::Output {
85-
| 61| 1| let mut future = unsafe { Pin::new_unchecked(&mut future) };
86-
| 62| 1| use std::hint::unreachable_unchecked;
87-
| 63| 1| static VTABLE: RawWakerVTable = RawWakerVTable::new(
88-
| 64| 1| |_| unsafe { unreachable_unchecked() }, // clone
89-
| 65| 1| |_| unsafe { unreachable_unchecked() }, // wake
90-
| 66| 1| |_| unsafe { unreachable_unchecked() }, // wake_by_ref
91-
| 67| 1| |_| (),
92-
| 68| 1| );
93-
| 69| 1| let waker = unsafe { Waker::from_raw(RawWaker::new(core::ptr::null(), &VTABLE)) };
94-
| 70| 1| let mut context = Context::from_waker(&waker);
95-
| 71| |
96-
| 72| | loop {
97-
| 73| 1| if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
98-
| 74| 1| break val;
99-
| 75| 0| }
100-
| 76| | }
101-
| 77| 1| }
75+
| 51| 1| pub fn block_on<F: Future>(mut future: F) -> F::Output {
76+
| 52| 1| let mut future = unsafe { Pin::new_unchecked(&mut future) };
77+
| 53| 1| use std::hint::unreachable_unchecked;
78+
| 54| 1| static VTABLE: RawWakerVTable = RawWakerVTable::new(
79+
| 55| 1| |_| unsafe { unreachable_unchecked() }, // clone
80+
| 56| 1| |_| unsafe { unreachable_unchecked() }, // wake
81+
| 57| 1| |_| unsafe { unreachable_unchecked() }, // wake_by_ref
82+
| 58| 1| |_| (),
83+
| 59| 1| );
84+
| 60| 1| let waker = unsafe { Waker::from_raw(RawWaker::new(core::ptr::null(), &VTABLE)) };
85+
| 61| 1| let mut context = Context::from_waker(&waker);
86+
| 62| |
87+
| 63| | loop {
88+
| 64| 1| if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
89+
| 65| 1| break val;
90+
| 66| 0| }
91+
| 67| | }
92+
| 68| 1| }
10293
------------------
10394
| async2::executor::block_on::<core::future::from_generator::GenFuture<async2::async_func_just_println::{closure#0}>>:
104-
| 60| 1| pub fn block_on<F: Future>(mut future: F) -> F::Output {
105-
| 61| 1| let mut future = unsafe { Pin::new_unchecked(&mut future) };
106-
| 62| 1| use std::hint::unreachable_unchecked;
107-
| 63| 1| static VTABLE: RawWakerVTable = RawWakerVTable::new(
108-
| 64| 1| |_| unsafe { unreachable_unchecked() }, // clone
109-
| 65| 1| |_| unsafe { unreachable_unchecked() }, // wake
110-
| 66| 1| |_| unsafe { unreachable_unchecked() }, // wake_by_ref
111-
| 67| 1| |_| (),
112-
| 68| 1| );
113-
| 69| 1| let waker = unsafe { Waker::from_raw(RawWaker::new(core::ptr::null(), &VTABLE)) };
114-
| 70| 1| let mut context = Context::from_waker(&waker);
115-
| 71| |
116-
| 72| | loop {
117-
| 73| 1| if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
118-
| 74| 1| break val;
119-
| 75| 0| }
120-
| 76| | }
121-
| 77| 1| }
95+
| 51| 1| pub fn block_on<F: Future>(mut future: F) -> F::Output {
96+
| 52| 1| let mut future = unsafe { Pin::new_unchecked(&mut future) };
97+
| 53| 1| use std::hint::unreachable_unchecked;
98+
| 54| 1| static VTABLE: RawWakerVTable = RawWakerVTable::new(
99+
| 55| 1| |_| unsafe { unreachable_unchecked() }, // clone
100+
| 56| 1| |_| unsafe { unreachable_unchecked() }, // wake
101+
| 57| 1| |_| unsafe { unreachable_unchecked() }, // wake_by_ref
102+
| 58| 1| |_| (),
103+
| 59| 1| );
104+
| 60| 1| let waker = unsafe { Waker::from_raw(RawWaker::new(core::ptr::null(), &VTABLE)) };
105+
| 61| 1| let mut context = Context::from_waker(&waker);
106+
| 62| |
107+
| 63| | loop {
108+
| 64| 1| if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
109+
| 65| 1| break val;
110+
| 66| 0| }
111+
| 67| | }
112+
| 68| 1| }
122113
------------------
123-
78| |}
114+
69| |}
124115

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
2| |// structure of this test.
33
3| |
44
4| 2|#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
5-
^0 ^0 ^0 ^0 ^1 ^1 ^0^0
5+
^0 ^0 ^0 ^1 ^0
66
------------------
77
| Unexecuted instantiation: <partial_eq::Version as core::cmp::PartialEq>::ne
88
------------------

src/test/run-make-fulldeps/coverage/async2.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@ fn main() {
3939

4040
executor::block_on(async_func());
4141
executor::block_on(async_func_just_println());
42-
43-
// let mut future = Box::pin(async_func());
44-
// executor::block_on(future.as_mut());
45-
46-
// let mut future = Box::pin(async_func());
47-
// executor::block_on(future.as_mut());
48-
49-
// let mut future = Box::pin(async_func_just_println());
50-
// executor::block_on(future.as_mut());
5142
}
5243

5344
mod executor {

0 commit comments

Comments
 (0)