Skip to content

Commit

Permalink
coverage: Allow #[coverage(..)] on impl and mod
Browse files Browse the repository at this point in the history
These attributes apply to all enclosed functions/methods/closures, unless
explicitly overridden by another coverage attribute.
  • Loading branch information
Zalathar committed Jun 25, 2024
1 parent 472e256 commit f0f220b
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 170 deletions.
9 changes: 6 additions & 3 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,16 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}
}

/// Checks that `#[coverage(..)]` is applied to a function or closure.
/// Checks that `#[coverage(..)]` is applied to a function/closure/method,
/// or to an impl block or module.
fn check_coverage(&self, attr: &Attribute, span: Span, target: Target) -> bool {
match target {
// #[coverage(..)] on function is fine
Target::Fn
| Target::Closure
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent)
| Target::Impl
| Target::Mod => true,

_ => {
self.dcx().emit_err(errors::CoverageNotFnOrClosure {
attr_span: attr.span,
Expand Down
24 changes: 24 additions & 0 deletions tests/coverage/attr/impl.cov-map
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Function name: <impl::MyStruct>::off_on (unused)
Raw bytes (9): 0x[01, 01, 00, 01, 00, 0e, 05, 00, 13]
Number of files: 1
- file 0 => global file 1
Number of expressions: 0
Number of file 0 mappings: 1
- Code(Zero) at (prev + 14, 5) to (start + 0, 19)

Function name: <impl::MyStruct>::on_inherit (unused)
Raw bytes (9): 0x[01, 01, 00, 01, 00, 16, 05, 00, 17]
Number of files: 1
- file 0 => global file 1
Number of expressions: 0
Number of file 0 mappings: 1
- Code(Zero) at (prev + 22, 5) to (start + 0, 23)

Function name: <impl::MyStruct>::on_on (unused)
Raw bytes (9): 0x[01, 01, 00, 01, 00, 19, 05, 00, 12]
Number of files: 1
- file 0 => global file 1
Number of expressions: 0
Number of file 0 mappings: 1
- Code(Zero) at (prev + 25, 5) to (start + 0, 18)

42 changes: 42 additions & 0 deletions tests/coverage/attr/impl.coverage
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
LL| |#![feature(coverage_attribute)]
LL| |//@ edition: 2021
LL| |
LL| |// Checks that `#[coverage(..)]` can be applied to impl and impl-trait blocks,
LL| |// and is inherited by any enclosed functions.
LL| |
LL| |struct MyStruct;
LL| |
LL| |#[coverage(off)]
LL| |impl MyStruct {
LL| | fn off_inherit() {}
LL| |
LL| | #[coverage(on)]
LL| 0| fn off_on() {}
LL| |
LL| | #[coverage(off)]
LL| | fn off_off() {}
LL| |}
LL| |
LL| |#[coverage(on)]
LL| |impl MyStruct {
LL| 0| fn on_inherit() {}
LL| |
LL| | #[coverage(on)]
LL| 0| fn on_on() {}
LL| |
LL| | #[coverage(off)]
LL| | fn on_off() {}
LL| |}
LL| |
LL| |trait MyTrait {
LL| | fn method();
LL| |}
LL| |
LL| |#[coverage(off)]
LL| |impl MyTrait for MyStruct {
LL| | fn method() {}
LL| |}
LL| |
LL| |#[coverage(off)]
LL| |fn main() {}

41 changes: 41 additions & 0 deletions tests/coverage/attr/impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#![feature(coverage_attribute)]
//@ edition: 2021

// Checks that `#[coverage(..)]` can be applied to impl and impl-trait blocks,
// and is inherited by any enclosed functions.

struct MyStruct;

#[coverage(off)]
impl MyStruct {
fn off_inherit() {}

#[coverage(on)]
fn off_on() {}

#[coverage(off)]
fn off_off() {}
}

#[coverage(on)]
impl MyStruct {
fn on_inherit() {}

#[coverage(on)]
fn on_on() {}

#[coverage(off)]
fn on_off() {}
}

trait MyTrait {
fn method();
}

#[coverage(off)]
impl MyTrait for MyStruct {
fn method() {}
}

#[coverage(off)]
fn main() {}
24 changes: 24 additions & 0 deletions tests/coverage/attr/module.cov-map
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Function name: module::off::on (unused)
Raw bytes (9): 0x[01, 01, 00, 01, 00, 0c, 05, 00, 0f]
Number of files: 1
- file 0 => global file 1
Number of expressions: 0
Number of file 0 mappings: 1
- Code(Zero) at (prev + 12, 5) to (start + 0, 15)

Function name: module::on::inherit (unused)
Raw bytes (9): 0x[01, 01, 00, 01, 00, 14, 05, 00, 14]
Number of files: 1
- file 0 => global file 1
Number of expressions: 0
Number of file 0 mappings: 1
- Code(Zero) at (prev + 20, 5) to (start + 0, 20)

Function name: module::on::on (unused)
Raw bytes (9): 0x[01, 01, 00, 01, 00, 17, 05, 00, 0f]
Number of files: 1
- file 0 => global file 1
Number of expressions: 0
Number of file 0 mappings: 1
- Code(Zero) at (prev + 23, 5) to (start + 0, 15)

38 changes: 38 additions & 0 deletions tests/coverage/attr/module.coverage
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
LL| |#![feature(coverage_attribute)]
LL| |//@ edition: 2021
LL| |
LL| |// Checks that `#[coverage(..)]` can be applied to modules, and is inherited
LL| |// by any enclosed functions.
LL| |
LL| |#[coverage(off)]
LL| |mod off {
LL| | fn inherit() {}
LL| |
LL| | #[coverage(on)]
LL| 0| fn on() {}
LL| |
LL| | #[coverage(off)]
LL| | fn off() {}
LL| |}
LL| |
LL| |#[coverage(on)]
LL| |mod on {
LL| 0| fn inherit() {}
LL| |
LL| | #[coverage(on)]
LL| 0| fn on() {}
LL| |
LL| | #[coverage(off)]
LL| | fn off() {}
LL| |}
LL| |
LL| |#[coverage(off)]
LL| |mod nested_a {
LL| | mod nested_b {
LL| | fn inner() {}
LL| | }
LL| |}
LL| |
LL| |#[coverage(off)]
LL| |fn main() {}

37 changes: 37 additions & 0 deletions tests/coverage/attr/module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#![feature(coverage_attribute)]
//@ edition: 2021

// Checks that `#[coverage(..)]` can be applied to modules, and is inherited
// by any enclosed functions.

#[coverage(off)]
mod off {
fn inherit() {}

#[coverage(on)]
fn on() {}

#[coverage(off)]
fn off() {}
}

#[coverage(on)]
mod on {
fn inherit() {}

#[coverage(on)]
fn on() {}

#[coverage(off)]
fn off() {}
}

#[coverage(off)]
mod nested_a {
mod nested_b {
fn inner() {}
}
}

#[coverage(off)]
fn main() {}
4 changes: 0 additions & 4 deletions tests/ui/coverage-attr/name-value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@

#[coverage = "off"]
//~^ ERROR malformed `coverage` attribute input
//~| ERROR attribute should be applied to a function definition or closure
mod my_mod {}

mod my_mod_inner {
#![coverage = "off"]
//~^ ERROR malformed `coverage` attribute input
//~| ERROR attribute should be applied to a function definition or closure
}

#[coverage = "off"]
Expand All @@ -26,7 +24,6 @@ struct MyStruct;

#[coverage = "off"]
//~^ ERROR malformed `coverage` attribute input
//~| ERROR attribute should be applied to a function definition or closure
impl MyStruct {
#[coverage = "off"]
//~^ ERROR malformed `coverage` attribute input
Expand All @@ -51,7 +48,6 @@ trait MyTrait {

#[coverage = "off"]
//~^ ERROR malformed `coverage` attribute input
//~| ERROR attribute should be applied to a function definition or closure
impl MyTrait for MyStruct {
#[coverage = "off"]
//~^ ERROR malformed `coverage` attribute input
Expand Down
Loading

0 comments on commit f0f220b

Please sign in to comment.