coverage: Use a stable sort when grouping mapped regions by file

If two or more mappings cover exactly the same region, their relative order
will now be preserved from `get_expressions_and_counter_regions`, rather than
being disturbed by implementation details of an unstable sort.

The current order is: counter mappings, expression mappings, zero mappings.

(LLVM will also perform its own stable sort on these mappings, but that sort
only compares file ID, start location, and `RegionKind`.)
This commit is contained in:
Zalathar 2023-08-30 20:08:13 +10:00
parent 525ac15b66
commit 4f88aa0fbd

View File

@ -180,7 +180,7 @@ fn write_coverage_mapping<'a>(
// `file_id` (indexing files referenced by the current function), and construct the // `file_id` (indexing files referenced by the current function), and construct the
// function-specific `virtual_file_mapping` from `file_id` to its index in the module's // function-specific `virtual_file_mapping` from `file_id` to its index in the module's
// `filenames` array. // `filenames` array.
counter_regions.sort_unstable_by_key(|(_counter, region)| *region); counter_regions.sort_by_key(|(_counter, region)| *region);
for (counter, region) in counter_regions { for (counter, region) in counter_regions {
let CodeRegion { file_name, start_line, start_col, end_line, end_col } = *region; let CodeRegion { file_name, start_line, start_col, end_line, end_col } = *region;
let same_file = current_file_name.is_some_and(|p| p == file_name); let same_file = current_file_name.is_some_and(|p| p == file_name);