@@ -12,7 +12,7 @@ use rustc_codegen_ssa::traits::{
1212} ;
1313use rustc_middle:: bug;
1414use rustc_middle:: mir:: coverage:: {
15- CoverageIdsInfo , Expression , FunctionCoverageInfo , MappingKind , Op ,
15+ CovTerm , CoverageIdsInfo , Expression , FunctionCoverageInfo , Mapping , MappingKind , Op ,
1616} ;
1717use rustc_middle:: ty:: { Instance , TyCtxt } ;
1818use rustc_target:: spec:: HasTargetSpec ;
@@ -65,7 +65,7 @@ pub(crate) fn prepare_covfun_record<'tcx>(
6565 regions : ffi:: Regions :: default ( ) ,
6666 } ;
6767
68- fill_region_tables ( tcx, global_file_table, function_coverage , & mut covfun) ;
68+ fill_region_tables ( tcx, global_file_table, fn_cov_info , ids_info , & mut covfun) ;
6969
7070 if covfun. regions . has_no_regions ( ) {
7171 if covfun. is_used {
@@ -117,16 +117,12 @@ fn prepare_expressions(
117117fn fill_region_tables < ' tcx > (
118118 tcx : TyCtxt < ' tcx > ,
119119 global_file_table : & mut GlobalFileTable ,
120- function_coverage : & FunctionCoverage < ' tcx > ,
120+ fn_cov_info : & ' tcx FunctionCoverageInfo ,
121+ ids_info : & ' tcx CoverageIdsInfo ,
121122 covfun : & mut CovfunRecord < ' tcx > ,
122123) {
123- let counter_regions = function_coverage. counter_regions ( ) ;
124- if counter_regions. is_empty ( ) {
125- return ;
126- }
127-
128124 // Currently a function's mappings must all be in the same file as its body span.
129- let file_name = span_file_name ( tcx, function_coverage . function_coverage_info . body_span ) ;
125+ let file_name = span_file_name ( tcx, fn_cov_info . body_span ) ;
130126
131127 // Look up the global file ID for that filename.
132128 let global_file_id = global_file_table. global_file_id_for_file_name ( file_name) ;
@@ -140,10 +136,14 @@ fn fill_region_tables<'tcx>(
140136
141137 // For each counter/region pair in this function+file, convert it to a
142138 // form suitable for FFI.
143- for ( mapping_kind, region) in counter_regions {
144- debug ! ( "Adding counter {mapping_kind:?} to map for {region:?}" ) ;
145- let span = ffi:: CoverageSpan :: from_source_region ( local_file_id, region) ;
146- match mapping_kind {
139+ let is_zero_term = |term| !covfun. is_used || ids_info. is_zero_term ( term) ;
140+ for Mapping { kind, ref source_region } in & fn_cov_info. mappings {
141+ // If the mapping refers to counters/expressions that were removed by
142+ // MIR opts, replace those occurrences with zero.
143+ let kind = kind. map_terms ( |term| if is_zero_term ( term) { CovTerm :: Zero } else { term } ) ;
144+
145+ let span = ffi:: CoverageSpan :: from_source_region ( local_file_id, source_region) ;
146+ match kind {
147147 MappingKind :: Code ( term) => {
148148 code_regions. push ( ffi:: CodeRegion { span, counter : ffi:: Counter :: from_term ( term) } ) ;
149149 }
0 commit comments