Skip to content

Commit 34d90a4

Browse files
committed
Fix line numbers for MIR inlined code
`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.
1 parent 9363a14 commit 34d90a4

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

compiler/rustc_middle/src/ty/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2522,7 +2522,9 @@ impl<'tcx> TyCtxt<'tcx> {
25222522
&& if self.features().collapse_debuginfo {
25232523
span.in_macro_expansion_with_collapse_debuginfo()
25242524
} else {
2525-
span.from_expansion()
2525+
// Inlined spans should not be collapsed as that leads to all of the
2526+
// inlined code being attributed to the inline callsite.
2527+
span.from_expansion() && !span.is_inlined()
25262528
}
25272529
}
25282530

compiler/rustc_span/src/lib.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ impl Span {
558558
self.data_untracked().is_dummy()
559559
}
560560

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

574+
/// Returns `true` if this span comes from MIR inlining.
575+
pub fn is_inlined(self) -> bool {
576+
let outer_expn = self.ctxt().outer_expn_data();
577+
matches!(outer_expn.kind, ExpnKind::Inlined)
578+
}
579+
574580
/// Returns `true` if `span` originates in a derive-macro's expansion.
575581
pub fn in_derive_expansion(self) -> bool {
576582
matches!(self.ctxt().outer_expn_data().kind, ExpnKind::Macro(MacroKind::Derive, _))

src/test/codegen/mir-inlined-line-numbers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ pub fn example() {
2020

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

0 commit comments

Comments
 (0)