@@ -9,6 +9,7 @@ use rustc_errors::{Applicability, DiagnosticBuilder, Level};
9
9
use rustc_expand:: base:: * ;
10
10
use rustc_span:: symbol:: { sym, Ident , Symbol } ;
11
11
use rustc_span:: { ErrorGuaranteed , FileNameDisplayPreference , Span } ;
12
+ use std:: assert_matches:: assert_matches;
12
13
use std:: iter;
13
14
use thin_vec:: { thin_vec, ThinVec } ;
14
15
@@ -182,6 +183,16 @@ pub fn expand_test_or_bench(
182
183
// creates $name: $expr
183
184
let field = |name, expr| cx. field_imm ( sp, Ident :: from_str_and_span ( name, sp) , expr) ;
184
185
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
+
185
196
let test_fn = if is_bench {
186
197
// A simple ident for a lambda
187
198
let b = Ident :: from_str_and_span ( "b" , attr_sp) ;
@@ -190,8 +201,9 @@ pub fn expand_test_or_bench(
190
201
sp,
191
202
cx. expr_path ( test_path ( "StaticBenchFn" ) ) ,
192
203
thin_vec ! [
204
+ // #[coverage(off)]
193
205
// |b| self::test::assert_test_result(
194
- cx. lambda1(
206
+ coverage_off ( cx. lambda1(
195
207
sp,
196
208
cx. expr_call(
197
209
sp,
@@ -206,16 +218,17 @@ pub fn expand_test_or_bench(
206
218
] ,
207
219
) ,
208
220
b,
209
- ) , // )
221
+ ) ) , // )
210
222
] ,
211
223
)
212
224
} else {
213
225
cx. expr_call (
214
226
sp,
215
227
cx. expr_path ( test_path ( "StaticTestFn" ) ) ,
216
228
thin_vec ! [
229
+ // #[coverage(off)]
217
230
// || {
218
- cx. lambda0(
231
+ coverage_off ( cx. lambda0(
219
232
sp,
220
233
// test::assert_test_result(
221
234
cx. expr_call(
@@ -230,7 +243,7 @@ pub fn expand_test_or_bench(
230
243
) , // )
231
244
] ,
232
245
) , // }
233
- ) , // )
246
+ ) ) , // )
234
247
] ,
235
248
)
236
249
} ;
0 commit comments