rust/tests/coverage/attr/nested.rs
Zalathar 5093658632 Add more thorough coverage tests for #[coverage(..)] in nested functions
These tests reflect the current implementation behaviour, which is not
necessarily the desired behaviour.
2024-06-18 21:27:34 +10:00

111 lines
2.4 KiB
Rust

#![feature(coverage_attribute, stmt_expr_attributes)]
//@ edition: 2021
// Demonstrates the interaction between #[coverage(off)] and various kinds of
// nested function.
// FIXME(#126625): Coverage attributes should apply recursively to nested functions.
// FIXME(#126626): When an inner (non-closure) function has `#[coverage(off)]`,
// its lines can still be marked with misleading execution counts from its enclosing
// function.
#[coverage(off)]
fn do_stuff() {}
#[coverage(off)]
fn outer_fn() {
fn middle_fn() {
fn inner_fn() {
do_stuff();
}
do_stuff();
}
do_stuff();
}
struct MyOuter;
impl MyOuter {
#[coverage(off)]
fn outer_method(&self) {
struct MyMiddle;
impl MyMiddle {
fn middle_method(&self) {
struct MyInner;
impl MyInner {
fn inner_method(&self) {
do_stuff();
}
}
do_stuff();
}
}
do_stuff();
}
}
trait MyTrait {
fn trait_method(&self);
}
impl MyTrait for MyOuter {
#[coverage(off)]
fn trait_method(&self) {
struct MyMiddle;
impl MyTrait for MyMiddle {
fn trait_method(&self) {
struct MyInner;
impl MyTrait for MyInner {
fn trait_method(&self) {
do_stuff();
}
}
do_stuff();
}
}
do_stuff();
}
}
fn closure_expr() {
let _outer = #[coverage(off)]
|| {
let _middle = || {
let _inner = || {
do_stuff();
};
do_stuff();
};
do_stuff();
};
do_stuff();
}
// This syntax is allowed, even without #![feature(stmt_expr_attributes)].
fn closure_tail() {
let _outer = {
#[coverage(off)]
|| {
let _middle = {
|| {
let _inner = {
|| {
do_stuff();
}
};
do_stuff();
}
};
do_stuff();
}
};
do_stuff();
}
#[coverage(off)]
fn main() {
outer_fn();
MyOuter.outer_method();
MyOuter.trait_method();
closure_expr();
closure_tail();
}