2023-12-15 05:38:27 +00:00
|
|
|
#![feature(coverage_attribute)]
|
2024-01-16 02:40:01 +00:00
|
|
|
#![feature(custom_inner_attributes)] // for #![rustfmt::skip]
|
2023-12-15 05:38:27 +00:00
|
|
|
#![feature(noop_waker)]
|
2020-12-02 23:39:40 +00:00
|
|
|
#![allow(unused_assignments, dead_code)]
|
2024-01-16 02:40:01 +00:00
|
|
|
#![rustfmt::skip]
|
2023-12-15 05:38:27 +00:00
|
|
|
//@ edition: 2018
|
|
|
|
//@ compile-flags: -Copt-level=1
|
Coverage tests for remaining TerminatorKinds and async, improve Assert
Tested and validate results for panic unwind, panic abort, assert!()
macro, TerminatorKind::Assert (for example, numeric overflow), and
async/await.
Implemented a previous documented idea to change Assert handling to be
the same as FalseUnwind and Goto, so it doesn't get its own
BasicCoverageBlock anymore. This changed a couple of coverage regions,
but I validated those changes are not any worse than the prior results,
and probably help assure some consistency (even if some people might
disagree with how the code region is consistently computed).
Fixed issue with async/await. AggregateKind::Generator needs to be
handled like AggregateKind::Closure; coverage span for the outer async
function should not "cover" the async body, which is actually executed
in a separate "closure" MIR.
2020-11-16 17:14:28 +00:00
|
|
|
|
2020-12-01 07:58:08 +00:00
|
|
|
async fn c(x: u8) -> u8 {
|
|
|
|
if x == 8 {
|
|
|
|
1
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Translate counters from Rust 1-based to LLVM 0-based counter ids
A colleague contacted me and asked why Rust's counters start at 1, when
Clangs appear to start at 0. There is a reason why Rust's internal
counters start at 1 (see the docs), and I tried to keep them consistent
when codegenned to LLVM's coverage mapping format. LLVM should be
tolerant of missing counters, but as my colleague pointed out,
`llvm-cov` will silently fail to generate a coverage report for a
function based on LLVM's assumption that the counters are 0-based.
See:
https://github.com/llvm/llvm-project/blob/main/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp#L170
Apparently, if, for example, a function has no branches, it would have
exactly 1 counter. `CounterValues.size()` would be 1, and (with the
1-based index), the counter ID would be 1. This would fail the check
and abort reporting coverage for the function.
It turns out that by correcting for this during coverage map generation,
by subtracting 1 from the Rust Counter ID (both when generating the
counter increment intrinsic call, and when adding counters to the map),
some uncovered functions (including in tests) now appear covered! This
corrects the coverage for a few tests!
2021-04-02 07:08:48 +00:00
|
|
|
async fn d() -> u8 { 1 }
|
2020-12-01 07:58:08 +00:00
|
|
|
|
|
|
|
async fn e() -> u8 { 1 } // unused function; executor does not block on `g()`
|
|
|
|
|
Coverage tests for remaining TerminatorKinds and async, improve Assert
Tested and validate results for panic unwind, panic abort, assert!()
macro, TerminatorKind::Assert (for example, numeric overflow), and
async/await.
Implemented a previous documented idea to change Assert handling to be
the same as FalseUnwind and Goto, so it doesn't get its own
BasicCoverageBlock anymore. This changed a couple of coverage regions,
but I validated those changes are not any worse than the prior results,
and probably help assure some consistency (even if some people might
disagree with how the code region is consistently computed).
Fixed issue with async/await. AggregateKind::Generator needs to be
handled like AggregateKind::Closure; coverage span for the outer async
function should not "cover" the async body, which is actually executed
in a separate "closure" MIR.
2020-11-16 17:14:28 +00:00
|
|
|
async fn f() -> u8 { 1 }
|
|
|
|
|
2020-12-01 07:58:08 +00:00
|
|
|
async fn foo() -> [bool; 10] { [false; 10] } // unused function; executor does not block on `h()`
|
Coverage tests for remaining TerminatorKinds and async, improve Assert
Tested and validate results for panic unwind, panic abort, assert!()
macro, TerminatorKind::Assert (for example, numeric overflow), and
async/await.
Implemented a previous documented idea to change Assert handling to be
the same as FalseUnwind and Goto, so it doesn't get its own
BasicCoverageBlock anymore. This changed a couple of coverage regions,
but I validated those changes are not any worse than the prior results,
and probably help assure some consistency (even if some people might
disagree with how the code region is consistently computed).
Fixed issue with async/await. AggregateKind::Generator needs to be
handled like AggregateKind::Closure; coverage span for the outer async
function should not "cover" the async body, which is actually executed
in a separate "closure" MIR.
2020-11-16 17:14:28 +00:00
|
|
|
|
|
|
|
pub async fn g(x: u8) {
|
|
|
|
match x {
|
2020-12-01 07:58:08 +00:00
|
|
|
y if e().await == y => (),
|
Coverage tests for remaining TerminatorKinds and async, improve Assert
Tested and validate results for panic unwind, panic abort, assert!()
macro, TerminatorKind::Assert (for example, numeric overflow), and
async/await.
Implemented a previous documented idea to change Assert handling to be
the same as FalseUnwind and Goto, so it doesn't get its own
BasicCoverageBlock anymore. This changed a couple of coverage regions,
but I validated those changes are not any worse than the prior results,
and probably help assure some consistency (even if some people might
disagree with how the code region is consistently computed).
Fixed issue with async/await. AggregateKind::Generator needs to be
handled like AggregateKind::Closure; coverage span for the outer async
function should not "cover" the async body, which is actually executed
in a separate "closure" MIR.
2020-11-16 17:14:28 +00:00
|
|
|
y if f().await == y => (),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 07:58:08 +00:00
|
|
|
async fn h(x: usize) { // The function signature is counted when called, but the body is not
|
|
|
|
// executed (not awaited) so the open brace has a `0` count (at least when
|
|
|
|
// displayed with `llvm-cov show` in color-mode).
|
Coverage tests for remaining TerminatorKinds and async, improve Assert
Tested and validate results for panic unwind, panic abort, assert!()
macro, TerminatorKind::Assert (for example, numeric overflow), and
async/await.
Implemented a previous documented idea to change Assert handling to be
the same as FalseUnwind and Goto, so it doesn't get its own
BasicCoverageBlock anymore. This changed a couple of coverage regions,
but I validated those changes are not any worse than the prior results,
and probably help assure some consistency (even if some people might
disagree with how the code region is consistently computed).
Fixed issue with async/await. AggregateKind::Generator needs to be
handled like AggregateKind::Closure; coverage span for the outer async
function should not "cover" the async body, which is actually executed
in a separate "closure" MIR.
2020-11-16 17:14:28 +00:00
|
|
|
match x {
|
|
|
|
y if foo().await[y] => (),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 07:58:08 +00:00
|
|
|
async fn i(x: u8) { // line coverage is 1, but there are 2 regions:
|
|
|
|
// (a) the function signature, counted when the function is called; and
|
|
|
|
// (b) the open brace for the function body, counted once when the body is
|
|
|
|
// executed asynchronously.
|
Coverage tests for remaining TerminatorKinds and async, improve Assert
Tested and validate results for panic unwind, panic abort, assert!()
macro, TerminatorKind::Assert (for example, numeric overflow), and
async/await.
Implemented a previous documented idea to change Assert handling to be
the same as FalseUnwind and Goto, so it doesn't get its own
BasicCoverageBlock anymore. This changed a couple of coverage regions,
but I validated those changes are not any worse than the prior results,
and probably help assure some consistency (even if some people might
disagree with how the code region is consistently computed).
Fixed issue with async/await. AggregateKind::Generator needs to be
handled like AggregateKind::Closure; coverage span for the outer async
function should not "cover" the async body, which is actually executed
in a separate "closure" MIR.
2020-11-16 17:14:28 +00:00
|
|
|
match x {
|
2020-12-01 07:58:08 +00:00
|
|
|
y if c(x).await == y + 1 => { d().await; }
|
Coverage tests for remaining TerminatorKinds and async, improve Assert
Tested and validate results for panic unwind, panic abort, assert!()
macro, TerminatorKind::Assert (for example, numeric overflow), and
async/await.
Implemented a previous documented idea to change Assert handling to be
the same as FalseUnwind and Goto, so it doesn't get its own
BasicCoverageBlock anymore. This changed a couple of coverage regions,
but I validated those changes are not any worse than the prior results,
and probably help assure some consistency (even if some people might
disagree with how the code region is consistently computed).
Fixed issue with async/await. AggregateKind::Generator needs to be
handled like AggregateKind::Closure; coverage span for the outer async
function should not "cover" the async body, which is actually executed
in a separate "closure" MIR.
2020-11-16 17:14:28 +00:00
|
|
|
y if f().await == y + 1 => (),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 07:58:08 +00:00
|
|
|
fn j(x: u8) {
|
|
|
|
// non-async versions of `c()`, `d()`, and `f()` to make it similar to async `i()`.
|
|
|
|
fn c(x: u8) -> u8 {
|
|
|
|
if x == 8 {
|
|
|
|
1 // This line appears covered, but the 1-character expression span covering the `1`
|
|
|
|
// is not executed. (`llvm-cov show` displays a `^0` below the `1` ). This is because
|
2022-08-18 02:13:37 +00:00
|
|
|
// `fn j()` executes the open brace for the function body, followed by the function's
|
2020-12-01 07:58:08 +00:00
|
|
|
// first executable statement, `match x`. Inner function declarations are not
|
|
|
|
// "visible" to the MIR for `j()`, so the code region counts all lines between the
|
|
|
|
// open brace and the first statement as executed, which is, in a sense, true.
|
|
|
|
// `llvm-cov show` overcomes this kind of situation by showing the actual counts
|
|
|
|
// of the enclosed coverages, (that is, the `1` expression was not executed, and
|
|
|
|
// accurately displays a `0`).
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
Translate counters from Rust 1-based to LLVM 0-based counter ids
A colleague contacted me and asked why Rust's counters start at 1, when
Clangs appear to start at 0. There is a reason why Rust's internal
counters start at 1 (see the docs), and I tried to keep them consistent
when codegenned to LLVM's coverage mapping format. LLVM should be
tolerant of missing counters, but as my colleague pointed out,
`llvm-cov` will silently fail to generate a coverage report for a
function based on LLVM's assumption that the counters are 0-based.
See:
https://github.com/llvm/llvm-project/blob/main/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp#L170
Apparently, if, for example, a function has no branches, it would have
exactly 1 counter. `CounterValues.size()` would be 1, and (with the
1-based index), the counter ID would be 1. This would fail the check
and abort reporting coverage for the function.
It turns out that by correcting for this during coverage map generation,
by subtracting 1 from the Rust Counter ID (both when generating the
counter increment intrinsic call, and when adding counters to the map),
some uncovered functions (including in tests) now appear covered! This
corrects the coverage for a few tests!
2021-04-02 07:08:48 +00:00
|
|
|
fn d() -> u8 { 1 } // inner function is defined in-line, but the function is not executed
|
2020-12-01 07:58:08 +00:00
|
|
|
fn f() -> u8 { 1 }
|
|
|
|
match x {
|
|
|
|
y if c(x) == y + 1 => { d(); }
|
|
|
|
y if f() == y + 1 => (),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn k(x: u8) { // unused function
|
|
|
|
match x {
|
|
|
|
1 => (),
|
|
|
|
2 => (),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn l(x: u8) {
|
|
|
|
match x {
|
|
|
|
1 => (),
|
|
|
|
2 => (),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:01:26 +00:00
|
|
|
async fn m(x: u8) -> u8 { x - 1 }
|
|
|
|
|
Coverage tests for remaining TerminatorKinds and async, improve Assert
Tested and validate results for panic unwind, panic abort, assert!()
macro, TerminatorKind::Assert (for example, numeric overflow), and
async/await.
Implemented a previous documented idea to change Assert handling to be
the same as FalseUnwind and Goto, so it doesn't get its own
BasicCoverageBlock anymore. This changed a couple of coverage regions,
but I validated those changes are not any worse than the prior results,
and probably help assure some consistency (even if some people might
disagree with how the code region is consistently computed).
Fixed issue with async/await. AggregateKind::Generator needs to be
handled like AggregateKind::Closure; coverage span for the outer async
function should not "cover" the async body, which is actually executed
in a separate "closure" MIR.
2020-11-16 17:14:28 +00:00
|
|
|
fn main() {
|
|
|
|
let _ = g(10);
|
|
|
|
let _ = h(9);
|
|
|
|
let mut future = Box::pin(i(8));
|
2020-12-01 07:58:08 +00:00
|
|
|
j(7);
|
|
|
|
l(6);
|
2020-12-02 07:01:26 +00:00
|
|
|
let _ = m(5);
|
Coverage tests for remaining TerminatorKinds and async, improve Assert
Tested and validate results for panic unwind, panic abort, assert!()
macro, TerminatorKind::Assert (for example, numeric overflow), and
async/await.
Implemented a previous documented idea to change Assert handling to be
the same as FalseUnwind and Goto, so it doesn't get its own
BasicCoverageBlock anymore. This changed a couple of coverage regions,
but I validated those changes are not any worse than the prior results,
and probably help assure some consistency (even if some people might
disagree with how the code region is consistently computed).
Fixed issue with async/await. AggregateKind::Generator needs to be
handled like AggregateKind::Closure; coverage span for the outer async
function should not "cover" the async body, which is actually executed
in a separate "closure" MIR.
2020-11-16 17:14:28 +00:00
|
|
|
executor::block_on(future.as_mut());
|
|
|
|
}
|
|
|
|
|
|
|
|
mod executor {
|
2023-12-15 05:38:27 +00:00
|
|
|
use core::future::Future;
|
|
|
|
use core::pin::pin;
|
|
|
|
use core::task::{Context, Poll, Waker};
|
Coverage tests for remaining TerminatorKinds and async, improve Assert
Tested and validate results for panic unwind, panic abort, assert!()
macro, TerminatorKind::Assert (for example, numeric overflow), and
async/await.
Implemented a previous documented idea to change Assert handling to be
the same as FalseUnwind and Goto, so it doesn't get its own
BasicCoverageBlock anymore. This changed a couple of coverage regions,
but I validated those changes are not any worse than the prior results,
and probably help assure some consistency (even if some people might
disagree with how the code region is consistently computed).
Fixed issue with async/await. AggregateKind::Generator needs to be
handled like AggregateKind::Closure; coverage span for the outer async
function should not "cover" the async body, which is actually executed
in a separate "closure" MIR.
2020-11-16 17:14:28 +00:00
|
|
|
|
2023-12-15 05:38:27 +00:00
|
|
|
#[coverage(off)]
|
Coverage tests for remaining TerminatorKinds and async, improve Assert
Tested and validate results for panic unwind, panic abort, assert!()
macro, TerminatorKind::Assert (for example, numeric overflow), and
async/await.
Implemented a previous documented idea to change Assert handling to be
the same as FalseUnwind and Goto, so it doesn't get its own
BasicCoverageBlock anymore. This changed a couple of coverage regions,
but I validated those changes are not any worse than the prior results,
and probably help assure some consistency (even if some people might
disagree with how the code region is consistently computed).
Fixed issue with async/await. AggregateKind::Generator needs to be
handled like AggregateKind::Closure; coverage span for the outer async
function should not "cover" the async body, which is actually executed
in a separate "closure" MIR.
2020-11-16 17:14:28 +00:00
|
|
|
pub fn block_on<F: Future>(mut future: F) -> F::Output {
|
2023-12-15 05:38:27 +00:00
|
|
|
let mut future = pin!(future);
|
2024-01-15 00:25:43 +00:00
|
|
|
let mut context = Context::from_waker(Waker::noop());
|
Coverage tests for remaining TerminatorKinds and async, improve Assert
Tested and validate results for panic unwind, panic abort, assert!()
macro, TerminatorKind::Assert (for example, numeric overflow), and
async/await.
Implemented a previous documented idea to change Assert handling to be
the same as FalseUnwind and Goto, so it doesn't get its own
BasicCoverageBlock anymore. This changed a couple of coverage regions,
but I validated those changes are not any worse than the prior results,
and probably help assure some consistency (even if some people might
disagree with how the code region is consistently computed).
Fixed issue with async/await. AggregateKind::Generator needs to be
handled like AggregateKind::Closure; coverage span for the outer async
function should not "cover" the async body, which is actually executed
in a separate "closure" MIR.
2020-11-16 17:14:28 +00:00
|
|
|
|
|
|
|
loop {
|
|
|
|
if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
|
|
|
|
break val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|