@@ -4,7 +4,7 @@ use rustc_middle::mir::coverage::*;
44use rustc_middle:: mir:: visit:: Visitor ;
55use rustc_middle:: mir:: { self , Coverage , CoverageInfo , Location } ;
66use rustc_middle:: ty:: query:: Providers ;
7- use rustc_middle:: ty:: TyCtxt ;
7+ use rustc_middle:: ty:: { self , TyCtxt } ;
88use rustc_span:: def_id:: DefId ;
99
1010/// The `query` provider for `CoverageInfo`, requested by `codegen_coverage()` (to inject each
@@ -112,7 +112,7 @@ impl Visitor<'_> for CoverageVisitor {
112112}
113113
114114fn coverageinfo_from_mir < ' tcx > ( tcx : TyCtxt < ' tcx > , def_id : DefId ) -> CoverageInfo {
115- let mir_body = tcx . optimized_mir ( def_id) ;
115+ let mir_body = mir_body ( tcx , def_id) ;
116116
117117 let mut coverage_visitor = CoverageVisitor {
118118 // num_counters always has at least the `ZERO` counter.
@@ -129,8 +129,7 @@ fn coverageinfo_from_mir<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> CoverageInfo
129129}
130130
131131fn covered_file_name < ' tcx > ( tcx : TyCtxt < ' tcx > , def_id : DefId ) -> Option < Symbol > {
132- let mir_body = tcx. optimized_mir ( def_id) ;
133- for bb_data in mir_body. basic_blocks ( ) . iter ( ) {
132+ for bb_data in mir_body ( tcx, def_id) . basic_blocks ( ) . iter ( ) {
134133 for statement in bb_data. statements . iter ( ) {
135134 if let StatementKind :: Coverage ( box ref coverage) = statement. kind {
136135 if let Some ( code_region) = coverage. code_region . as_ref ( ) {
@@ -142,9 +141,17 @@ fn covered_file_name<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<Symbol> {
142141 None
143142}
144143
144+ /// This function ensures we obtain the correct MIR for the given item irrespective of
145+ /// whether that means const mir or runtime mir. For `const fn` this opts for runtime
146+ /// mir.
147+ fn mir_body < ' tcx > ( tcx : TyCtxt < ' tcx > , def_id : DefId ) -> & ' tcx mir:: Body < ' tcx > {
148+ let id = ty:: WithOptConstParam :: unknown ( def_id) ;
149+ let def = ty:: InstanceDef :: Item ( id) ;
150+ tcx. instance_mir ( def)
151+ }
152+
145153fn covered_code_regions < ' tcx > ( tcx : TyCtxt < ' tcx > , def_id : DefId ) -> Vec < & ' tcx CodeRegion > {
146- let mir_body: & ' tcx mir:: Body < ' tcx > = tcx. optimized_mir ( def_id) ;
147- mir_body
154+ mir_body ( tcx, def_id)
148155 . basic_blocks ( )
149156 . iter ( )
150157 . map ( |data| {
0 commit comments