-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #130013 - jonathan-conder:await_coverage, r=Zalathar
coverage: Count await when the Future is immediately ready Currently `await` is only counted towards coverage if the containing function is suspended and resumed at least once. This is because it expands to code which contains a branch on the discriminant of `Poll`. By treating it like a branching macro (e.g. `assert!`), these implementation details will be hidden from the coverage results. I added a test to ensure the fix works in simple cases, but the heuristic of picking only the first await-related covspan might be unreliable. I plan on testing more thoroughly with a real codebase over the next couple of weeks. closes #98712
- Loading branch information
Showing
8 changed files
with
160 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Function name: await_ready::await_ready | ||
Raw bytes (9): 0x[01, 01, 00, 01, 01, 0a, 01, 00, 1e] | ||
Number of files: 1 | ||
- file 0 => global file 1 | ||
Number of expressions: 0 | ||
Number of file 0 mappings: 1 | ||
- Code(Counter(0)) at (prev + 10, 1) to (start + 0, 30) | ||
|
||
Function name: await_ready::await_ready::{closure#0} | ||
Raw bytes (14): 0x[01, 01, 00, 02, 01, 0a, 1e, 03, 0f, 05, 04, 01, 00, 02] | ||
Number of files: 1 | ||
- file 0 => global file 1 | ||
Number of expressions: 0 | ||
Number of file 0 mappings: 2 | ||
- Code(Counter(0)) at (prev + 10, 30) to (start + 3, 15) | ||
- Code(Counter(1)) at (prev + 4, 1) to (start + 0, 2) | ||
|
||
Function name: await_ready::main | ||
Raw bytes (9): 0x[01, 01, 00, 01, 01, 10, 01, 03, 02] | ||
Number of files: 1 | ||
- file 0 => global file 1 | ||
Number of expressions: 0 | ||
Number of file 0 mappings: 1 | ||
- Code(Counter(0)) at (prev + 16, 1) to (start + 3, 2) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
LL| |#![feature(coverage_attribute)] | ||
LL| |#![feature(custom_inner_attributes)] // for #![rustfmt::skip] | ||
LL| |#![feature(noop_waker)] | ||
LL| |#![rustfmt::skip] | ||
LL| |//@ edition: 2021 | ||
LL| | | ||
LL| |#[coverage(off)] | ||
LL| |async fn ready() -> u8 { 1 } | ||
LL| | | ||
LL| 1|async fn await_ready() -> u8 { | ||
LL| 1| // await should be covered even if the function never yields | ||
LL| 1| ready() | ||
LL| 1| .await | ||
LL| 1|} | ||
LL| | | ||
LL| 1|fn main() { | ||
LL| 1| let mut future = Box::pin(await_ready()); | ||
LL| 1| executor::block_on(future.as_mut()); | ||
LL| 1|} | ||
LL| | | ||
LL| |mod executor { | ||
LL| | use core::future::Future; | ||
LL| | use core::pin::pin; | ||
LL| | use core::task::{Context, Poll, Waker}; | ||
LL| | | ||
LL| | #[coverage(off)] | ||
LL| | pub fn block_on<F: Future>(mut future: F) -> F::Output { | ||
LL| | let mut future = pin!(future); | ||
LL| | let mut context = Context::from_waker(Waker::noop()); | ||
LL| | | ||
LL| | loop { | ||
LL| | if let Poll::Ready(val) = future.as_mut().poll(&mut context) { | ||
LL| | break val; | ||
LL| | } | ||
LL| | } | ||
LL| | } | ||
LL| |} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#![feature(coverage_attribute)] | ||
#![feature(custom_inner_attributes)] // for #![rustfmt::skip] | ||
#![feature(noop_waker)] | ||
#![rustfmt::skip] | ||
//@ edition: 2021 | ||
|
||
#[coverage(off)] | ||
async fn ready() -> u8 { 1 } | ||
|
||
async fn await_ready() -> u8 { | ||
// await should be covered even if the function never yields | ||
ready() | ||
.await | ||
} | ||
|
||
fn main() { | ||
let mut future = Box::pin(await_ready()); | ||
executor::block_on(future.as_mut()); | ||
} | ||
|
||
mod executor { | ||
use core::future::Future; | ||
use core::pin::pin; | ||
use core::task::{Context, Poll, Waker}; | ||
|
||
#[coverage(off)] | ||
pub fn block_on<F: Future>(mut future: F) -> F::Output { | ||
let mut future = pin!(future); | ||
let mut context = Context::from_waker(Waker::noop()); | ||
|
||
loop { | ||
if let Poll::Ready(val) = future.as_mut().poll(&mut context) { | ||
break val; | ||
} | ||
} | ||
} | ||
} |