Skip to content

Commit 0fff985

Browse files
committed
coverage: Never emit improperly-ordered coverage regions
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.
1 parent 5b49e17 commit 0fff985

File tree

1 file changed

+13
-0
lines changed
  • compiler/rustc_mir_transform/src/coverage

1 file changed

+13
-0
lines changed

compiler/rustc_mir_transform/src/coverage/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,19 @@ fn make_code_region(
286286
start_line = source_map.doctest_offset_line(&file.name, start_line);
287287
end_line = source_map.doctest_offset_line(&file.name, end_line);
288288
}
289+
290+
// If we ever emit a region that is improperly ordered (end < start),
291+
// `llvm-cov` will fail with `coveragemap_error::malformed`, which is
292+
// inconvenient for users and also very hard to debug.
293+
let is_ordered = (start_line, start_col) <= (end_line, end_col);
294+
debug_assert!(is_ordered, "{start_line}:{start_col} <= {end_line}:{end_col}");
295+
if !is_ordered {
296+
debug!(
297+
"Skipping improperly-ordered region: {start_line}:{start_col} to {end_line}:{end_col}"
298+
);
299+
return None;
300+
}
301+
289302
Some(CodeRegion {
290303
file_name,
291304
start_line: start_line as u32,

0 commit comments

Comments
 (0)