@@ -22,7 +22,7 @@ use rustc_middle::mir::{
22
22
TerminatorKind ,
23
23
} ;
24
24
use rustc_middle:: ty:: TyCtxt ;
25
- use rustc_span:: def_id:: DefId ;
25
+ use rustc_span:: def_id:: { DefId , LocalDefId } ;
26
26
use rustc_span:: source_map:: SourceMap ;
27
27
use rustc_span:: { ExpnKind , SourceFile , Span , Symbol } ;
28
28
@@ -43,18 +43,9 @@ impl<'tcx> MirPass<'tcx> for InstrumentCoverage {
43
43
// be transformed, so it should never see promoted MIR.
44
44
assert ! ( mir_source. promoted. is_none( ) ) ;
45
45
46
- let is_fn_like =
47
- tcx. hir ( ) . get_by_def_id ( mir_source. def_id ( ) . expect_local ( ) ) . fn_kind ( ) . is_some ( ) ;
48
-
49
- // Only instrument functions, methods, and closures (not constants since they are evaluated
50
- // at compile time by Miri).
51
- // FIXME(#73156): Handle source code coverage in const eval, but note, if and when const
52
- // expressions get coverage spans, we will probably have to "carve out" space for const
53
- // expressions from coverage spans in enclosing MIR's, like we do for closures. (That might
54
- // be tricky if const expressions have no corresponding statements in the enclosing MIR.
55
- // Closures are carved out by their initial `Assign` statement.)
56
- if !is_fn_like {
57
- trace ! ( "InstrumentCoverage skipped for {:?} (not an fn-like)" , mir_source. def_id( ) ) ;
46
+ let def_id = mir_source. def_id ( ) . expect_local ( ) ;
47
+ if !is_eligible_for_coverage ( tcx, def_id) {
48
+ trace ! ( "InstrumentCoverage skipped for {def_id:?} (not eligible)" ) ;
58
49
return ;
59
50
}
60
51
@@ -66,14 +57,9 @@ impl<'tcx> MirPass<'tcx> for InstrumentCoverage {
66
57
_ => { }
67
58
}
68
59
69
- let codegen_fn_attrs = tcx. codegen_fn_attrs ( mir_source. def_id ( ) ) ;
70
- if codegen_fn_attrs. flags . contains ( CodegenFnAttrFlags :: NO_COVERAGE ) {
71
- return ;
72
- }
73
-
74
- trace ! ( "InstrumentCoverage starting for {:?}" , mir_source. def_id( ) ) ;
60
+ trace ! ( "InstrumentCoverage starting for {def_id:?}" ) ;
75
61
Instrumentor :: new ( tcx, mir_body) . inject_counters ( ) ;
76
- trace ! ( "InstrumentCoverage done for {:?}" , mir_source . def_id ( ) ) ;
62
+ trace ! ( "InstrumentCoverage done for {def_id :?}" ) ;
77
63
}
78
64
}
79
65
@@ -319,6 +305,28 @@ fn make_code_region(
319
305
}
320
306
}
321
307
308
+ fn is_eligible_for_coverage ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> bool {
309
+ let is_fn_like = tcx. hir ( ) . get_by_def_id ( def_id) . fn_kind ( ) . is_some ( ) ;
310
+
311
+ // Only instrument functions, methods, and closures (not constants since they are evaluated
312
+ // at compile time by Miri).
313
+ // FIXME(#73156): Handle source code coverage in const eval, but note, if and when const
314
+ // expressions get coverage spans, we will probably have to "carve out" space for const
315
+ // expressions from coverage spans in enclosing MIR's, like we do for closures. (That might
316
+ // be tricky if const expressions have no corresponding statements in the enclosing MIR.
317
+ // Closures are carved out by their initial `Assign` statement.)
318
+ if !is_fn_like {
319
+ return false ;
320
+ }
321
+
322
+ let codegen_fn_attrs = tcx. codegen_fn_attrs ( def_id) ;
323
+ if codegen_fn_attrs. flags . contains ( CodegenFnAttrFlags :: NO_COVERAGE ) {
324
+ return false ;
325
+ }
326
+
327
+ true
328
+ }
329
+
322
330
fn fn_sig_and_body (
323
331
tcx : TyCtxt < ' _ > ,
324
332
def_id : DefId ,
0 commit comments