Skip to content

Commit 682f744

Browse files
committed
coverage: Treat #[automatically_derived] as #[coverage(off)]
1 parent b4d0c91 commit 682f744

File tree

5 files changed

+34
-45
lines changed

5 files changed

+34
-45
lines changed

compiler/rustc_mir_transform/src/coverage/query.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,6 @@ fn is_eligible_for_coverage(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
3232
return false;
3333
}
3434

35-
// Don't instrument functions with `#[automatically_derived]` on their
36-
// enclosing impl block, on the assumption that most users won't care about
37-
// coverage for derived impls.
38-
if let Some(impl_of) = tcx.impl_of_assoc(def_id.to_def_id())
39-
&& tcx.is_automatically_derived(impl_of)
40-
{
41-
trace!("InstrumentCoverage skipped for {def_id:?} (automatically derived)");
42-
return false;
43-
}
44-
4535
if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::NAKED) {
4636
trace!("InstrumentCoverage skipped for {def_id:?} (`#[naked]`)");
4737
return false;
@@ -67,6 +57,15 @@ fn coverage_attr_on(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
6757
}
6858
};
6959

60+
// Treat `#[automatically_derived]` as an implied `#[coverage(off)]`, on
61+
// the assumption that most users won't want coverage for derived impls.
62+
//
63+
// This affects not just the associated items of an impl block, but also
64+
// any closures and other nested functions within those associated items.
65+
if tcx.is_automatically_derived(def_id.to_def_id()) {
66+
return false;
67+
}
68+
7069
// Check the parent def (and so on recursively) until we find an
7170
// enclosing attribute or reach the crate root.
7271
match tcx.opt_local_parent(def_id) {

tests/coverage/auto-derived.auto.cov-map

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
Function name: <auto_derived::MyStruct as auto_derived::MyTrait>::my_assoc_fn::inner_fn
2-
Raw bytes (24): 0x[01, 01, 00, 04, 01, 1a, 09, 00, 16, 01, 01, 0d, 00, 10, 01, 00, 11, 00, 1e, 01, 01, 09, 00, 0a]
3-
Number of files: 1
4-
- file 0 => $DIR/auto-derived.rs
5-
Number of expressions: 0
6-
Number of file 0 mappings: 4
7-
- Code(Counter(0)) at (prev + 26, 9) to (start + 0, 22)
8-
- Code(Counter(0)) at (prev + 1, 13) to (start + 0, 16)
9-
- Code(Counter(0)) at (prev + 0, 17) to (start + 0, 30)
10-
- Code(Counter(0)) at (prev + 1, 9) to (start + 0, 10)
11-
Highest counter ID seen: c0
12-
131
Function name: <auto_derived::MyStruct as auto_derived::MyTrait>::my_assoc_fn::inner_fn_on
142
Raw bytes (24): 0x[01, 01, 00, 04, 01, 1f, 09, 00, 19, 01, 01, 0d, 00, 10, 01, 00, 11, 00, 23, 01, 01, 09, 00, 0a]
153
Number of files: 1
@@ -22,18 +10,6 @@ Number of file 0 mappings: 4
2210
- Code(Counter(0)) at (prev + 1, 9) to (start + 0, 10)
2311
Highest counter ID seen: c0
2412

25-
Function name: <auto_derived::MyStruct as auto_derived::MyTrait>::my_assoc_fn::{closure#0}
26-
Raw bytes (24): 0x[01, 01, 00, 04, 01, 23, 1a, 00, 1b, 01, 01, 0d, 00, 10, 01, 00, 11, 00, 1d, 01, 01, 09, 00, 0a]
27-
Number of files: 1
28-
- file 0 => $DIR/auto-derived.rs
29-
Number of expressions: 0
30-
Number of file 0 mappings: 4
31-
- Code(Counter(0)) at (prev + 35, 26) to (start + 0, 27)
32-
- Code(Counter(0)) at (prev + 1, 13) to (start + 0, 16)
33-
- Code(Counter(0)) at (prev + 0, 17) to (start + 0, 29)
34-
- Code(Counter(0)) at (prev + 1, 9) to (start + 0, 10)
35-
Highest counter ID seen: c0
36-
3713
Function name: auto_derived::main
3814
Raw bytes (19): 0x[01, 01, 00, 03, 01, 33, 01, 00, 0a, 01, 01, 05, 00, 1a, 01, 01, 01, 00, 02]
3915
Number of files: 1

tests/coverage/auto-derived.auto.coverage

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@
2323
LL| |#[cfg_attr(on, coverage(on))]
2424
LL| |impl MyTrait for MyStruct {
2525
LL| | fn my_assoc_fn() {
26-
LL| 1| fn inner_fn() {
27-
LL| 1| say("in inner fn");
28-
LL| 1| }
26+
LL| | fn inner_fn() {
27+
LL| | say("in inner fn");
28+
LL| | }
2929
LL| |
3030
LL| | #[coverage(on)]
3131
LL| 1| fn inner_fn_on() {
3232
LL| 1| say("in inner fn (on)");
3333
LL| 1| }
3434
LL| |
35-
LL| 1| let closure = || {
36-
LL| 1| say("in closure");
37-
LL| 1| };
35+
LL| | let closure = || {
36+
LL| | say("in closure");
37+
LL| | };
3838
LL| |
3939
LL| | closure();
4040
LL| | inner_fn();

tests/coverage/auto-derived.on.cov-map

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
Function name: <auto_derived::MyStruct as auto_derived::MyTrait>::my_assoc_fn
2+
Raw bytes (34): 0x[01, 01, 00, 06, 01, 19, 05, 00, 15, 01, 0a, 0d, 00, 14, 01, 04, 09, 00, 12, 01, 01, 09, 00, 11, 01, 01, 09, 00, 14, 01, 01, 05, 00, 06]
3+
Number of files: 1
4+
- file 0 => $DIR/auto-derived.rs
5+
Number of expressions: 0
6+
Number of file 0 mappings: 6
7+
- Code(Counter(0)) at (prev + 25, 5) to (start + 0, 21)
8+
- Code(Counter(0)) at (prev + 10, 13) to (start + 0, 20)
9+
- Code(Counter(0)) at (prev + 4, 9) to (start + 0, 18)
10+
- Code(Counter(0)) at (prev + 1, 9) to (start + 0, 17)
11+
- Code(Counter(0)) at (prev + 1, 9) to (start + 0, 20)
12+
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 6)
13+
Highest counter ID seen: c0
14+
115
Function name: <auto_derived::MyStruct as auto_derived::MyTrait>::my_assoc_fn::inner_fn
216
Raw bytes (24): 0x[01, 01, 00, 04, 01, 1a, 09, 00, 16, 01, 01, 0d, 00, 10, 01, 00, 11, 00, 1e, 01, 01, 09, 00, 0a]
317
Number of files: 1

tests/coverage/auto-derived.on.coverage

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
LL| |#[cfg_attr(on, automatically_derived)]
2323
LL| |#[cfg_attr(on, coverage(on))]
2424
LL| |impl MyTrait for MyStruct {
25-
LL| | fn my_assoc_fn() {
25+
LL| 1| fn my_assoc_fn() {
2626
LL| 1| fn inner_fn() {
2727
LL| 1| say("in inner fn");
2828
LL| 1| }
@@ -36,10 +36,10 @@
3636
LL| 1| say("in closure");
3737
LL| 1| };
3838
LL| |
39-
LL| | closure();
40-
LL| | inner_fn();
41-
LL| | inner_fn_on();
42-
LL| | }
39+
LL| 1| closure();
40+
LL| 1| inner_fn();
41+
LL| 1| inner_fn_on();
42+
LL| 1| }
4343
LL| |}
4444
LL| |
4545
LL| |#[coverage(off)]

0 commit comments

Comments
 (0)