Skip to content

Commit 09cd115

Browse files
ZalatharMark-Simulacrum
authored andcommitted
coverage: Avoid creating malformed macro name spans
This method is trying to detect macro invocations, so that it can split a span into two parts just after the `!` of the invocation. Under some circumstances (probably involving nested macros), it gets confused and produces a span that is larger than the original span, and possibly extends outside its enclosing function and even into an adjacent file. In extreme cases, that can result in malformed coverage mappings that cause `llvm-cov` to fail. For now, we at least want to detect these egregious cases and avoid them, so that coverage reports can still be produced.
1 parent 034f8bb commit 09cd115

File tree

1 file changed

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

1 file changed

+6
-0
lines changed

compiler/rustc_mir_transform/src/coverage/spans.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,12 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
427427
let merged_prefix_len = self.curr_original_span.lo() - self.curr().span.lo();
428428
let after_macro_bang =
429429
merged_prefix_len + BytePos(visible_macro.as_str().len() as u32 + 1);
430+
if self.curr().span.lo() + after_macro_bang > self.curr().span.hi() {
431+
// Something is wrong with the macro name span;
432+
// return now to avoid emitting malformed mappings.
433+
// FIXME(#117788): Track down why this happens.
434+
return;
435+
}
430436
let mut macro_name_cov = self.curr().clone();
431437
self.curr_mut().span =
432438
self.curr().span.with_lo(self.curr().span.lo() + after_macro_bang);

0 commit comments

Comments
 (0)