@@ -12,7 +12,7 @@ use rustc_codegen_ssa::traits::{
12
12
} ;
13
13
use rustc_middle:: bug;
14
14
use rustc_middle:: mir:: coverage:: {
15
- CoverageIdsInfo , Expression , FunctionCoverageInfo , MappingKind , Op ,
15
+ CovTerm , CoverageIdsInfo , Expression , FunctionCoverageInfo , Mapping , MappingKind , Op ,
16
16
} ;
17
17
use rustc_middle:: ty:: { Instance , TyCtxt } ;
18
18
use rustc_target:: spec:: HasTargetSpec ;
@@ -65,7 +65,7 @@ pub(crate) fn prepare_covfun_record<'tcx>(
65
65
regions : ffi:: Regions :: default ( ) ,
66
66
} ;
67
67
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) ;
69
69
70
70
if covfun. regions . has_no_regions ( ) {
71
71
if covfun. is_used {
@@ -117,16 +117,12 @@ fn prepare_expressions(
117
117
fn fill_region_tables < ' tcx > (
118
118
tcx : TyCtxt < ' tcx > ,
119
119
global_file_table : & mut GlobalFileTable ,
120
- function_coverage : & FunctionCoverage < ' tcx > ,
120
+ fn_cov_info : & ' tcx FunctionCoverageInfo ,
121
+ ids_info : & ' tcx CoverageIdsInfo ,
121
122
covfun : & mut CovfunRecord < ' tcx > ,
122
123
) {
123
- let counter_regions = function_coverage. counter_regions ( ) ;
124
- if counter_regions. is_empty ( ) {
125
- return ;
126
- }
127
-
128
124
// 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 ) ;
130
126
131
127
// Look up the global file ID for that filename.
132
128
let global_file_id = global_file_table. global_file_id_for_file_name ( file_name) ;
@@ -140,10 +136,14 @@ fn fill_region_tables<'tcx>(
140
136
141
137
// For each counter/region pair in this function+file, convert it to a
142
138
// 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 {
147
147
MappingKind :: Code ( term) => {
148
148
code_regions. push ( ffi:: CodeRegion { span, counter : ffi:: Counter :: from_term ( term) } ) ;
149
149
}
0 commit comments