Skip to content

Commit 6d7e80c

Browse files
committed
Add #[coverage(off)] to closures introduced by #[test]/#[bench]
1 parent bdfc64a commit 6d7e80c

File tree

8 files changed

+28
-27
lines changed

8 files changed

+28
-27
lines changed

compiler/rustc_builtin_macros/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![doc(rust_logo)]
77
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
88
#![feature(array_windows)]
9+
#![feature(assert_matches)]
910
#![feature(box_patterns)]
1011
#![feature(decl_macro)]
1112
#![feature(if_let_guard)]

compiler/rustc_builtin_macros/src/test.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_errors::{Applicability, DiagnosticBuilder, Level};
99
use rustc_expand::base::*;
1010
use rustc_span::symbol::{sym, Ident, Symbol};
1111
use rustc_span::{ErrorGuaranteed, FileNameDisplayPreference, Span};
12+
use std::assert_matches::assert_matches;
1213
use std::iter;
1314
use thin_vec::{thin_vec, ThinVec};
1415

@@ -182,6 +183,16 @@ pub fn expand_test_or_bench(
182183
// creates $name: $expr
183184
let field = |name, expr| cx.field_imm(sp, Ident::from_str_and_span(name, sp), expr);
184185

186+
// Adds `#[coverage(off)]` to a closure, so it won't be instrumented in
187+
// `-Cinstrument-coverage` builds.
188+
// This requires `#[allow_internal_unstable(coverage_attribute)]` on the
189+
// corresponding macro declaration in `core::macros`.
190+
let coverage_off = |mut expr: P<ast::Expr>| {
191+
assert_matches!(expr.kind, ast::ExprKind::Closure(_));
192+
expr.attrs.push(cx.attr_nested_word(sym::coverage, sym::off, sp));
193+
expr
194+
};
195+
185196
let test_fn = if is_bench {
186197
// A simple ident for a lambda
187198
let b = Ident::from_str_and_span("b", attr_sp);
@@ -190,8 +201,9 @@ pub fn expand_test_or_bench(
190201
sp,
191202
cx.expr_path(test_path("StaticBenchFn")),
192203
thin_vec![
204+
// #[coverage(off)]
193205
// |b| self::test::assert_test_result(
194-
cx.lambda1(
206+
coverage_off(cx.lambda1(
195207
sp,
196208
cx.expr_call(
197209
sp,
@@ -206,16 +218,17 @@ pub fn expand_test_or_bench(
206218
],
207219
),
208220
b,
209-
), // )
221+
)), // )
210222
],
211223
)
212224
} else {
213225
cx.expr_call(
214226
sp,
215227
cx.expr_path(test_path("StaticTestFn")),
216228
thin_vec![
229+
// #[coverage(off)]
217230
// || {
218-
cx.lambda0(
231+
coverage_off(cx.lambda0(
219232
sp,
220233
// test::assert_test_result(
221234
cx.expr_call(
@@ -230,7 +243,7 @@ pub fn expand_test_or_bench(
230243
), // )
231244
],
232245
), // }
233-
), // )
246+
)), // )
234247
],
235248
)
236249
};

library/core/src/macros/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1596,7 +1596,7 @@ pub(crate) mod builtin {
15961596
///
15971597
/// [the reference]: ../../../reference/attributes/testing.html#the-test-attribute
15981598
#[stable(feature = "rust1", since = "1.0.0")]
1599-
#[allow_internal_unstable(test, rustc_attrs)]
1599+
#[allow_internal_unstable(test, rustc_attrs, coverage_attribute)]
16001600
#[rustc_builtin_macro]
16011601
pub macro test($item:item) {
16021602
/* compiler built-in */
@@ -1609,7 +1609,7 @@ pub(crate) mod builtin {
16091609
soft,
16101610
reason = "`bench` is a part of custom test frameworks which are unstable"
16111611
)]
1612-
#[allow_internal_unstable(test, rustc_attrs)]
1612+
#[allow_internal_unstable(test, rustc_attrs, coverage_attribute)]
16131613
#[rustc_builtin_macro]
16141614
pub macro bench($item:item) {
16151615
/* compiler built-in */

tests/coverage/bench.cov-map

-8
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,3 @@ Number of expressions: 0
66
Number of file 0 mappings: 1
77
- Code(Counter(0)) at (prev + 8, 1) to (start + 0, 39)
88

9-
Function name: bench::my_bench::{closure#0}
10-
Raw bytes (9): 0x[01, 01, 00, 01, 01, 07, 01, 00, 09]
11-
Number of files: 1
12-
- file 0 => global file 1
13-
Number of expressions: 0
14-
Number of file 0 mappings: 1
15-
- Code(Counter(0)) at (prev + 7, 1) to (start + 0, 9)
16-

tests/coverage/bench.coverage

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
LL| |
55
LL| |extern crate test;
66
LL| |
7-
LL| 1|#[bench]
7+
LL| |#[bench]
88
LL| 1|fn my_bench(_b: &mut test::Bencher) {}
99

tests/coverage/test_harness.cov-map

-8
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@ Number of expressions: 0
66
Number of file 0 mappings: 1
77
- Code(Counter(0)) at (prev + 10, 1) to (start + 0, 16)
88

9-
Function name: test_harness::my_test::{closure#0}
10-
Raw bytes (9): 0x[01, 01, 00, 01, 01, 09, 01, 00, 08]
11-
Number of files: 1
12-
- file 0 => global file 1
13-
Number of expressions: 0
14-
Number of file 0 mappings: 1
15-
- Code(Counter(0)) at (prev + 9, 1) to (start + 0, 8)
16-
179
Function name: test_harness::unused (unused)
1810
Raw bytes (9): 0x[01, 01, 00, 01, 00, 07, 01, 00, 0f]
1911
Number of files: 1

tests/coverage/test_harness.coverage

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
LL| |#[allow(dead_code)]
77
LL| 0|fn unused() {}
88
LL| |
9-
LL| 1|#[test]
9+
LL| |#[test]
1010
LL| 1|fn my_test() {}
1111

tests/pretty/tests-are-sorted.pp

+6-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
should_panic: test::ShouldPanic::No,
2929
test_type: test::TestType::Unknown,
3030
},
31-
testfn: test::StaticTestFn(|| test::assert_test_result(m_test())),
31+
testfn: test::StaticTestFn(#[coverage(off)] ||
32+
test::assert_test_result(m_test())),
3233
};
3334
fn m_test() {}
3435

@@ -51,7 +52,8 @@
5152
should_panic: test::ShouldPanic::No,
5253
test_type: test::TestType::Unknown,
5354
},
54-
testfn: test::StaticTestFn(|| test::assert_test_result(z_test())),
55+
testfn: test::StaticTestFn(#[coverage(off)] ||
56+
test::assert_test_result(z_test())),
5557
};
5658
#[ignore = "not yet implemented"]
5759
fn z_test() {}
@@ -75,7 +77,8 @@
7577
should_panic: test::ShouldPanic::No,
7678
test_type: test::TestType::Unknown,
7779
},
78-
testfn: test::StaticTestFn(|| test::assert_test_result(a_test())),
80+
testfn: test::StaticTestFn(#[coverage(off)] ||
81+
test::assert_test_result(a_test())),
7982
};
8083
fn a_test() {}
8184
#[rustc_main]

0 commit comments

Comments
 (0)