Skip to content

Commit 18b0fb0

Browse files
committed
coverage: Check for some other edge cases that might confuse llvm-cov
1 parent 7732fd9 commit 18b0fb0

File tree

1 file changed

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

1 file changed

+20
-0
lines changed

compiler/rustc_mir_transform/src/coverage/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,26 @@ fn make_code_region(
294294
let end_line = end_line as u32;
295295
let end_col = end_col as u32;
296296

297+
// Line/column coordinates are supposed to be 1-based. If we ever emit
298+
// coordinates of 0, `llvm-cov` might misinterpret them.
299+
let all_nonzero = [start_line, start_col, end_line, end_col].iter().all(|&n| n != 0);
300+
debug_assert!(all_nonzero, "{start_line}:{start_col} to {end_line}:{end_col}");
301+
if !all_nonzero {
302+
debug!(
303+
"Skipping region with zero coordinates: {start_line}:{start_col} to {end_line}:{end_col}"
304+
);
305+
return None;
306+
}
307+
308+
// Coverage mappings use the high bit of `end_col` to indicate that a
309+
// region is actually a "gap" region, so make sure it's unset.
310+
let end_col_has_high_bit_unset = (end_col & (1 << 31)) == 0;
311+
debug_assert!(end_col_has_high_bit_unset, "{end_col}");
312+
if !end_col_has_high_bit_unset {
313+
debug!("Skipping region with high bit set in end column: {end_col}");
314+
return None;
315+
}
316+
297317
// If we ever emit a region that is improperly ordered (end < start),
298318
// `llvm-cov` will fail with `coveragemap_error::malformed`, which is
299319
// inconvenient for users and also very hard to debug.

0 commit comments

Comments
 (0)