Skip to content

Commit

Permalink
coverage: Never emit improperly-ordered coverage regions
Browse files Browse the repository at this point in the history
If we emit a coverage region that is improperly ordered (end < start),
`llvm-cov` will fail with `coveragemap_error::malformed`, which is inconvenient
for users and also very hard to debug.

Ideally we would fix the root causes of these situations, but they tend to
occur in very obscure edge-case scenarios (often involving nested macros), and
we don't always have a good MCVE to work from. So it makes sense to also have a
catch-all check that will prevent improperly-ordered regions from ever being
emitted.
  • Loading branch information
Zalathar committed Dec 31, 2023
1 parent 5b49e17 commit 0fff985
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions compiler/rustc_mir_transform/src/coverage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,19 @@ fn make_code_region(
start_line = source_map.doctest_offset_line(&file.name, start_line);
end_line = source_map.doctest_offset_line(&file.name, end_line);
}

// If we ever emit a region that is improperly ordered (end < start),
// `llvm-cov` will fail with `coveragemap_error::malformed`, which is
// inconvenient for users and also very hard to debug.
let is_ordered = (start_line, start_col) <= (end_line, end_col);
debug_assert!(is_ordered, "{start_line}:{start_col} <= {end_line}:{end_col}");
if !is_ordered {
debug!(
"Skipping improperly-ordered region: {start_line}:{start_col} to {end_line}:{end_col}"
);
return None;
}

Some(CodeRegion {
file_name,
start_line: start_line as u32,
Expand Down

0 comments on commit 0fff985

Please sign in to comment.