Skip to content

Commit 77e7b74

Browse files
committed
Auto merge of #103071 - wesleywiser:fix_inlined_line_numbers, r=davidtwco
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. Fixes #103068
2 parents 5237c4d + 34d90a4 commit 77e7b74

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

compiler/rustc_middle/src/ty/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2604,7 +2604,9 @@ impl<'tcx> TyCtxt<'tcx> {
26042604
&& if self.features().collapse_debuginfo {
26052605
span.in_macro_expansion_with_collapse_debuginfo()
26062606
} else {
2607-
span.from_expansion()
2607+
// Inlined spans should not be collapsed as that leads to all of the
2608+
// inlined code being attributed to the inline callsite.
2609+
span.from_expansion() && !span.is_inlined()
26082610
}
26092611
}
26102612

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, _))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// compile-flags: -O -g
2+
3+
#![crate_type = "lib"]
4+
5+
#[inline(always)]
6+
fn foo() {
7+
bar();
8+
}
9+
10+
#[inline(never)]
11+
#[no_mangle]
12+
fn bar() {
13+
panic!();
14+
}
15+
16+
#[no_mangle]
17+
pub fn example() {
18+
foo();
19+
}
20+
21+
// CHECK-LABEL: @example
22+
// CHECK: tail call void @bar(), !dbg [[DBG_ID:![0-9]+]]
23+
// CHECK: [[DBG_ID]] = !DILocation(line: 7,
24+
// CHECK-SAME: inlinedAt: [[INLINE_ID:![0-9]+]])
25+
// CHECK: [[INLINE_ID]] = !DILocation(line: 18,

0 commit comments

Comments
 (0)