@@ -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 , LocalDefId } ;
25
+ use rustc_span:: def_id:: LocalDefId ;
26
26
use rustc_span:: source_map:: SourceMap ;
27
27
use rustc_span:: { ExpnKind , SourceFile , Span , Symbol } ;
28
28
@@ -77,29 +77,15 @@ struct Instrumentor<'a, 'tcx> {
77
77
impl < ' a , ' tcx > Instrumentor < ' a , ' tcx > {
78
78
fn new ( tcx : TyCtxt < ' tcx > , mir_body : & ' a mut mir:: Body < ' tcx > ) -> Self {
79
79
let source_map = tcx. sess . source_map ( ) ;
80
- let def_id = mir_body. source . def_id ( ) ;
81
- let ( some_fn_sig, hir_body) = fn_sig_and_body ( tcx, def_id) ;
82
80
83
- let body_span = get_body_span ( tcx, hir_body, mir_body) ;
81
+ let def_id = mir_body. source . def_id ( ) . expect_local ( ) ;
82
+ let mir:: coverage:: HirInfo { function_source_hash, fn_sig_span, body_span, .. } =
83
+ make_coverage_hir_info ( tcx, def_id) ;
84
84
85
85
let source_file = source_map. lookup_source_file ( body_span. lo ( ) ) ;
86
- let fn_sig_span = match some_fn_sig. filter ( |fn_sig| {
87
- fn_sig. span . eq_ctxt ( body_span)
88
- && Lrc :: ptr_eq ( & source_file, & source_map. lookup_source_file ( fn_sig. span . lo ( ) ) )
89
- } ) {
90
- Some ( fn_sig) => fn_sig. span . with_hi ( body_span. lo ( ) ) ,
91
- None => body_span. shrink_to_lo ( ) ,
92
- } ;
93
-
94
- debug ! (
95
- "instrumenting {}: {:?}, fn sig span: {:?}, body span: {:?}" ,
96
- if tcx. is_closure( def_id) { "closure" } else { "function" } ,
97
- def_id,
98
- fn_sig_span,
99
- body_span
100
- ) ;
101
86
102
- let function_source_hash = hash_mir_source ( tcx, hir_body) ;
87
+ debug ! ( ?fn_sig_span, ?body_span, "instrumenting {def_id:?}" , ) ;
88
+
103
89
let basic_coverage_blocks = CoverageGraph :: from_mir ( mir_body) ;
104
90
let coverage_counters = CoverageCounters :: new ( & basic_coverage_blocks) ;
105
91
@@ -327,13 +313,33 @@ fn is_eligible_for_coverage(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
327
313
true
328
314
}
329
315
316
+ fn make_coverage_hir_info ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> mir:: coverage:: HirInfo {
317
+ let source_map = tcx. sess . source_map ( ) ;
318
+ let ( some_fn_sig, hir_body) = fn_sig_and_body ( tcx, def_id) ;
319
+
320
+ let body_span = get_body_span ( tcx, hir_body, def_id) ;
321
+
322
+ let source_file = source_map. lookup_source_file ( body_span. lo ( ) ) ;
323
+ let fn_sig_span = match some_fn_sig. filter ( |fn_sig| {
324
+ fn_sig. span . eq_ctxt ( body_span)
325
+ && Lrc :: ptr_eq ( & source_file, & source_map. lookup_source_file ( fn_sig. span . lo ( ) ) )
326
+ } ) {
327
+ Some ( fn_sig) => fn_sig. span . with_hi ( body_span. lo ( ) ) ,
328
+ None => body_span. shrink_to_lo ( ) ,
329
+ } ;
330
+
331
+ let function_source_hash = hash_mir_source ( tcx, hir_body) ;
332
+
333
+ mir:: coverage:: HirInfo { function_source_hash, fn_sig_span, body_span }
334
+ }
335
+
330
336
fn fn_sig_and_body (
331
337
tcx : TyCtxt < ' _ > ,
332
- def_id : DefId ,
338
+ def_id : LocalDefId ,
333
339
) -> ( Option < & rustc_hir:: FnSig < ' _ > > , & rustc_hir:: Body < ' _ > ) {
334
340
// FIXME(#79625): Consider improving MIR to provide the information needed, to avoid going back
335
341
// to HIR for it.
336
- let hir_node = tcx. hir ( ) . get_if_local ( def_id) . expect ( "expected DefId is local" ) ;
342
+ let hir_node = tcx. hir ( ) . get_by_def_id ( def_id) ;
337
343
let ( _, fn_body_id) =
338
344
hir:: map:: associated_body ( hir_node) . expect ( "HIR node is a function with body" ) ;
339
345
( hir_node. fn_sig ( ) , tcx. hir ( ) . body ( fn_body_id) )
@@ -342,12 +348,11 @@ fn fn_sig_and_body(
342
348
fn get_body_span < ' tcx > (
343
349
tcx : TyCtxt < ' tcx > ,
344
350
hir_body : & rustc_hir:: Body < ' tcx > ,
345
- mir_body : & mut mir :: Body < ' tcx > ,
351
+ def_id : LocalDefId ,
346
352
) -> Span {
347
353
let mut body_span = hir_body. value . span ;
348
- let def_id = mir_body. source . def_id ( ) ;
349
354
350
- if tcx. is_closure ( def_id) {
355
+ if tcx. is_closure ( def_id. to_def_id ( ) ) {
351
356
// If the MIR function is a closure, and if the closure body span
352
357
// starts from a macro, but it's content is not in that macro, try
353
358
// to find a non-macro callsite, and instrument the spans there
0 commit comments