Skip to content

Commit

Permalink
Fix line numbers for MIR inlined code
Browse files Browse the repository at this point in the history
`should_collapse_debuginfo` detects if the specified span is part of a
macro expansion however it does this by checking if the span is anything
other than a normal (non-expanded) kind, then the span sequence is
walked backwards to the root span.

This doesn't work when the MIR inliner inlines code as it creates spans
with expansion information set to `ExprKind::Inlined` and results in the
line number being attributed to the inline callsite rather than the
normal line number of the inlined code.
  • Loading branch information
wesleywiser committed Oct 14, 2022
1 parent 9363a14 commit 34d90a4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 3 additions & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2522,7 +2522,9 @@ impl<'tcx> TyCtxt<'tcx> {
&& if self.features().collapse_debuginfo {
span.in_macro_expansion_with_collapse_debuginfo()
} else {
span.from_expansion()
// Inlined spans should not be collapsed as that leads to all of the
// inlined code being attributed to the inline callsite.
span.from_expansion() && !span.is_inlined()
}
}

Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ impl Span {
self.data_untracked().is_dummy()
}

/// Returns `true` if this span comes from a macro or desugaring.
/// Returns `true` if this span comes from any kind of macro, desugaring or inlining.
#[inline]
pub fn from_expansion(self) -> bool {
self.ctxt() != SyntaxContext::root()
Expand All @@ -571,6 +571,12 @@ impl Span {
matches!(outer_expn.kind, ExpnKind::Macro(..)) && outer_expn.collapse_debuginfo
}

/// Returns `true` if this span comes from MIR inlining.
pub fn is_inlined(self) -> bool {
let outer_expn = self.ctxt().outer_expn_data();
matches!(outer_expn.kind, ExpnKind::Inlined)
}

/// Returns `true` if `span` originates in a derive-macro's expansion.
pub fn in_derive_expansion(self) -> bool {
matches!(self.ctxt().outer_expn_data().kind, ExpnKind::Macro(MacroKind::Derive, _))
Expand Down
2 changes: 1 addition & 1 deletion src/test/codegen/mir-inlined-line-numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ pub fn example() {

// CHECK-LABEL: @example
// CHECK: tail call void @bar(), !dbg [[DBG_ID:![0-9]+]]
// CHECK: [[DBG_ID]] = !DILocation(line: 18,
// CHECK: [[DBG_ID]] = !DILocation(line: 7,
// CHECK-SAME: inlinedAt: [[INLINE_ID:![0-9]+]])
// CHECK: [[INLINE_ID]] = !DILocation(line: 18,

0 comments on commit 34d90a4

Please sign in to comment.