Skip to content

Commit

Permalink
coverage: Apply #[coverage(..)] recursively to nested functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Zalathar committed Jun 25, 2024
1 parent 9aa6a6c commit 472e256
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 156 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,8 @@ rustc_queries! {
separate_provide_extern
}

/// Checks for `#[coverage(off)]` or `#[coverage(on)]`.
/// Checks for the nearest `#[coverage(off)]` or `#[coverage(on)]` on
/// this def and any enclosing defs, up to the crate root.
///
/// Returns `false` if `#[coverage(off)]` was found, or `true` if
/// either `#[coverage(on)]` or no coverage attribute was found.
Expand Down
12 changes: 9 additions & 3 deletions compiler/rustc_mir_transform/src/coverage/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ fn is_eligible_for_coverage(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {

/// Query implementation for `coverage_attr_on`.
fn coverage_attr_on(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
// Check for annotations directly on this def.
if let Some(attr) = tcx.get_attr(def_id, sym::coverage) {
match attr.meta_item_list().as_deref() {
Some([item]) if item.has_name(sym::off) => return false,
Expand All @@ -66,9 +67,14 @@ fn coverage_attr_on(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
}
}

// We didn't see an explicit coverage attribute, so
// allow coverage instrumentation by default.
true
match tcx.opt_local_parent(def_id) {
// Check the parent def (and so on recursively) until we find an
// enclosing attribute or reach the crate root.
Some(parent) => tcx.coverage_attr_on(parent),
// We reached the crate root without seeing a coverage attribute, so
// allow coverage instrumentation by default.
None => true,
}
}

/// Query implementation for `coverage_ids_info`.
Expand Down
82 changes: 0 additions & 82 deletions tests/coverage/attr/nested.cov-map
Original file line number Diff line number Diff line change
@@ -1,35 +1,3 @@
Function name: <<<nested::MyOuter as nested::MyTrait>::trait_method::MyMiddle as nested::MyTrait>::trait_method::MyInner as nested::MyTrait>::trait_method (unused)
Raw bytes (9): 0x[01, 01, 00, 01, 00, 39, 15, 02, 16]
Number of files: 1
- file 0 => global file 1
Number of expressions: 0
Number of file 0 mappings: 1
- Code(Zero) at (prev + 57, 21) to (start + 2, 22)

Function name: <<<nested::MyOuter>::outer_method::MyMiddle>::middle_method::MyInner>::inner_method (unused)
Raw bytes (9): 0x[01, 01, 00, 01, 00, 23, 15, 02, 16]
Number of files: 1
- file 0 => global file 1
Number of expressions: 0
Number of file 0 mappings: 1
- Code(Zero) at (prev + 35, 21) to (start + 2, 22)

Function name: <<nested::MyOuter as nested::MyTrait>::trait_method::MyMiddle as nested::MyTrait>::trait_method (unused)
Raw bytes (9): 0x[01, 01, 00, 01, 00, 36, 0d, 08, 0e]
Number of files: 1
- file 0 => global file 1
Number of expressions: 0
Number of file 0 mappings: 1
- Code(Zero) at (prev + 54, 13) to (start + 8, 14)

Function name: <<nested::MyOuter>::outer_method::MyMiddle>::middle_method (unused)
Raw bytes (9): 0x[01, 01, 00, 01, 00, 20, 0d, 08, 0e]
Number of files: 1
- file 0 => global file 1
Number of expressions: 0
Number of file 0 mappings: 1
- Code(Zero) at (prev + 32, 13) to (start + 8, 14)

Function name: nested::closure_expr
Raw bytes (14): 0x[01, 01, 00, 02, 01, 44, 01, 01, 0f, 01, 0b, 05, 01, 02]
Number of files: 1
Expand All @@ -39,23 +7,6 @@ Number of file 0 mappings: 2
- Code(Counter(0)) at (prev + 68, 1) to (start + 1, 15)
- Code(Counter(0)) at (prev + 11, 5) to (start + 1, 2)

Function name: nested::closure_expr::{closure#0}::{closure#0} (unused)
Raw bytes (14): 0x[01, 01, 00, 02, 00, 47, 1a, 01, 17, 00, 04, 0d, 01, 0a]
Number of files: 1
- file 0 => global file 1
Number of expressions: 0
Number of file 0 mappings: 2
- Code(Zero) at (prev + 71, 26) to (start + 1, 23)
- Code(Zero) at (prev + 4, 13) to (start + 1, 10)

Function name: nested::closure_expr::{closure#0}::{closure#0}::{closure#0} (unused)
Raw bytes (9): 0x[01, 01, 00, 01, 00, 48, 1d, 02, 0e]
Number of files: 1
- file 0 => global file 1
Number of expressions: 0
Number of file 0 mappings: 1
- Code(Zero) at (prev + 72, 29) to (start + 2, 14)

Function name: nested::closure_tail
Raw bytes (14): 0x[01, 01, 00, 02, 01, 53, 01, 01, 0f, 01, 11, 05, 01, 02]
Number of files: 1
Expand All @@ -65,36 +16,3 @@ Number of file 0 mappings: 2
- Code(Counter(0)) at (prev + 83, 1) to (start + 1, 15)
- Code(Counter(0)) at (prev + 17, 5) to (start + 1, 2)

Function name: nested::closure_tail::{closure#0}::{closure#0} (unused)
Raw bytes (14): 0x[01, 01, 00, 02, 00, 58, 14, 01, 1f, 00, 06, 15, 01, 12]
Number of files: 1
- file 0 => global file 1
Number of expressions: 0
Number of file 0 mappings: 2
- Code(Zero) at (prev + 88, 20) to (start + 1, 31)
- Code(Zero) at (prev + 6, 21) to (start + 1, 18)

Function name: nested::closure_tail::{closure#0}::{closure#0}::{closure#0} (unused)
Raw bytes (9): 0x[01, 01, 00, 01, 00, 5a, 1c, 02, 1a]
Number of files: 1
- file 0 => global file 1
Number of expressions: 0
Number of file 0 mappings: 1
- Code(Zero) at (prev + 90, 28) to (start + 2, 26)

Function name: nested::outer_fn::middle_fn (unused)
Raw bytes (9): 0x[01, 01, 00, 01, 00, 11, 05, 05, 06]
Number of files: 1
- file 0 => global file 1
Number of expressions: 0
Number of file 0 mappings: 1
- Code(Zero) at (prev + 17, 5) to (start + 5, 6)

Function name: nested::outer_fn::middle_fn::inner_fn (unused)
Raw bytes (9): 0x[01, 01, 00, 01, 00, 12, 09, 02, 0a]
Number of files: 1
- file 0 => global file 1
Number of expressions: 0
Number of file 0 mappings: 1
- Code(Zero) at (prev + 18, 9) to (start + 2, 10)

74 changes: 37 additions & 37 deletions tests/coverage/attr/nested.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
LL| |
LL| |#[coverage(off)]
LL| |fn outer_fn() {
LL| 0| fn middle_fn() {
LL| 0| fn inner_fn() {
LL| 0| do_stuff();
LL| 0| }
LL| 0| do_stuff();
LL| 0| }
LL| | fn middle_fn() {
LL| | fn inner_fn() {
LL| | do_stuff();
LL| | }
LL| | do_stuff();
LL| | }
LL| | do_stuff();
LL| |}
LL| |
Expand All @@ -29,15 +29,15 @@
LL| | fn outer_method(&self) {
LL| | struct MyMiddle;
LL| | impl MyMiddle {
LL| 0| fn middle_method(&self) {
LL| 0| struct MyInner;
LL| 0| impl MyInner {
LL| 0| fn inner_method(&self) {
LL| 0| do_stuff();
LL| 0| }
LL| 0| }
LL| 0| do_stuff();
LL| 0| }
LL| | fn middle_method(&self) {
LL| | struct MyInner;
LL| | impl MyInner {
LL| | fn inner_method(&self) {
LL| | do_stuff();
LL| | }
LL| | }
LL| | do_stuff();
LL| | }
LL| | }
LL| | do_stuff();
LL| | }
Expand All @@ -51,15 +51,15 @@
LL| | fn trait_method(&self) {
LL| | struct MyMiddle;
LL| | impl MyTrait for MyMiddle {
LL| 0| fn trait_method(&self) {
LL| 0| struct MyInner;
LL| 0| impl MyTrait for MyInner {
LL| 0| fn trait_method(&self) {
LL| 0| do_stuff();
LL| 0| }
LL| 0| }
LL| 0| do_stuff();
LL| 0| }
LL| | fn trait_method(&self) {
LL| | struct MyInner;
LL| | impl MyTrait for MyInner {
LL| | fn trait_method(&self) {
LL| | do_stuff();
LL| | }
LL| | }
LL| | do_stuff();
LL| | }
LL| | }
LL| | do_stuff();
LL| | }
Expand All @@ -68,12 +68,12 @@
LL| 1|fn closure_expr() {
LL| 1| let _outer = #[coverage(off)]
LL| | || {
LL| 0| let _middle = || {
LL| 0| let _inner = || {
LL| 0| do_stuff();
LL| 0| };
LL| 0| do_stuff();
LL| 0| };
LL| | let _middle = || {
LL| | let _inner = || {
LL| | do_stuff();
LL| | };
LL| | do_stuff();
LL| | };
LL| | do_stuff();
LL| | };
LL| 1| do_stuff();
Expand All @@ -85,14 +85,14 @@
LL| | #[coverage(off)]
LL| | || {
LL| | let _middle = {
LL| 0| || {
LL| 0| let _inner = {
LL| 0| || {
LL| 0| do_stuff();
LL| 0| }
LL| | || {
LL| | let _inner = {
LL| | || {
LL| | do_stuff();
LL| | }
LL| | };
LL| 0| do_stuff();
LL| 0| }
LL| | do_stuff();
LL| | }
LL| | };
LL| | do_stuff();
LL| | }
Expand Down
8 changes: 0 additions & 8 deletions tests/coverage/attr/off-on-sandwich.cov-map
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ Number of expressions: 0
Number of file 0 mappings: 1
- Code(Counter(0)) at (prev + 20, 5) to (start + 7, 6)

Function name: off_on_sandwich::sparse_a::sparse_b
Raw bytes (9): 0x[01, 01, 00, 01, 01, 22, 05, 10, 06]
Number of files: 1
- file 0 => global file 1
Number of expressions: 0
Number of file 0 mappings: 1
- Code(Counter(0)) at (prev + 34, 5) to (start + 16, 6)

Function name: off_on_sandwich::sparse_a::sparse_b::sparse_c
Raw bytes (9): 0x[01, 01, 00, 01, 01, 26, 09, 0b, 0a]
Number of files: 1
Expand Down
10 changes: 5 additions & 5 deletions tests/coverage/attr/off-on-sandwich.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
LL| |fn sparse_a() {
LL| | sparse_b();
LL| | sparse_b();
LL| 2| fn sparse_b() {
LL| 2| sparse_c();
LL| 2| sparse_c();
LL| 2| #[coverage(on)]
LL| | fn sparse_b() {
LL| | sparse_c();
LL| | sparse_c();
LL| | #[coverage(on)]
LL| 4| fn sparse_c() {
LL| 4| sparse_d();
LL| 4| sparse_d();
Expand All @@ -47,7 +47,7 @@
LL| 8| }
LL| 8| }
LL| 4| }
LL| 2| }
LL| | }
LL| |}
LL| |
LL| |#[coverage(off)]
Expand Down
13 changes: 0 additions & 13 deletions tests/coverage/no_cov_crate.cov-map
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,3 @@ Number of file 0 mappings: 4
= (c0 - c1)
- Code(Counter(0)) at (prev + 3, 9) to (start + 0, 10)

Function name: no_cov_crate::nested_fns::outer_not_covered::inner
Raw bytes (26): 0x[01, 01, 01, 01, 05, 04, 01, 26, 09, 01, 17, 05, 01, 18, 02, 0e, 02, 02, 14, 02, 0e, 01, 03, 09, 00, 0a]
Number of files: 1
- file 0 => global file 1
Number of expressions: 1
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
Number of file 0 mappings: 4
- Code(Counter(0)) at (prev + 38, 9) to (start + 1, 23)
- Code(Counter(1)) at (prev + 1, 24) to (start + 2, 14)
- Code(Expression(0, Sub)) at (prev + 2, 20) to (start + 2, 14)
= (c0 - c1)
- Code(Counter(0)) at (prev + 3, 9) to (start + 0, 10)

14 changes: 7 additions & 7 deletions tests/coverage/no_cov_crate.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
LL| |mod nested_fns {
LL| | #[coverage(off)]
LL| | pub fn outer_not_covered(is_true: bool) {
LL| 1| fn inner(is_true: bool) {
LL| 1| if is_true {
LL| 1| println!("called and covered");
LL| 1| } else {
LL| 0| println!("absolutely not covered");
LL| 0| }
LL| 1| }
LL| | fn inner(is_true: bool) {
LL| | if is_true {
LL| | println!("called and covered");
LL| | } else {
LL| | println!("absolutely not covered");
LL| | }
LL| | }
LL| | println!("called but not covered");
LL| | inner(is_true);
LL| | }
Expand Down

0 comments on commit 472e256

Please sign in to comment.