@@ -174,36 +174,40 @@ fn write_coverage_mapping<'a>(
174174
175175 let mut virtual_file_mapping = Vec :: new ( ) ;
176176 let mut mapping_regions = Vec :: new ( ) ;
177- let mut current_file_name = None ;
178- let mut current_file_id = 0 ;
179177
180178 // Convert the list of (Counter, CodeRegion) pairs to an array of `CounterMappingRegion`, sorted
181179 // by filename and position. Capture any new files to compute the `CounterMappingRegion`s
182180 // `file_id` (indexing files referenced by the current function), and construct the
183181 // function-specific `virtual_file_mapping` from `file_id` to its index in the module's
184182 // `filenames` array.
185- counter_regions. sort_by_key ( |( _counter, region) | * region) ;
186- for ( counter, region) in counter_regions {
187- let CodeRegion { file_name, start_line, start_col, end_line, end_col } = * region;
188- let same_file = current_file_name. is_some_and ( |p| p == file_name) ;
189- if !same_file {
190- if current_file_name. is_some ( ) {
191- current_file_id += 1 ;
192- }
193- current_file_name = Some ( file_name) ;
194- debug ! ( " file_id: {} = '{:?}'" , current_file_id, file_name) ;
195- let global_file_id = global_file_table. global_file_id_for_file_name ( file_name) ;
196- virtual_file_mapping. push ( global_file_id) ;
183+ counter_regions. sort_by_key ( |( _counter, region) | region. file_name ) ;
184+ for ( local_file_id, counter_regions_for_file) in
185+ ( 0u32 ..) . zip ( counter_regions. group_by ( |( _, a) , ( _, b) | a. file_name == b. file_name ) )
186+ {
187+ // Look up (or allocate) the global file ID for this file name.
188+ let file_name = counter_regions_for_file[ 0 ] . 1 . file_name ;
189+ let global_file_id = global_file_table. global_file_id_for_file_name ( file_name) ;
190+
191+ // Associate that global file ID with a local file ID for this function.
192+ assert_eq ! ( local_file_id as usize , virtual_file_mapping. len( ) ) ;
193+ virtual_file_mapping. push ( global_file_id) ;
194+ debug ! ( " file_id: local {local_file_id} => global {global_file_id} = '{file_name:?}'" ) ;
195+
196+ // For each counter/region pair in this function+file, convert it
197+ // to a form suitable for FFI.
198+ for & ( counter, region) in counter_regions_for_file {
199+ let CodeRegion { file_name : _, start_line, start_col, end_line, end_col } = * region;
200+
201+ debug ! ( "Adding counter {counter:?} to map for {region:?}" ) ;
202+ mapping_regions. push ( CounterMappingRegion :: code_region (
203+ counter,
204+ local_file_id,
205+ start_line,
206+ start_col,
207+ end_line,
208+ end_col,
209+ ) ) ;
197210 }
198- debug ! ( "Adding counter {:?} to map for {:?}" , counter, region) ;
199- mapping_regions. push ( CounterMappingRegion :: code_region (
200- counter,
201- current_file_id,
202- start_line,
203- start_col,
204- end_line,
205- end_col,
206- ) ) ;
207211 }
208212
209213 // Encode and append the current function's coverage mapping data
0 commit comments