From 9363a1401e2d0061c810504c7be169badd5a910b Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Fri, 14 Oct 2022 14:09:30 -0400 Subject: [PATCH 1/2] Add test case for MIR inlining debuginfo line numbers --- src/test/codegen/mir-inlined-line-numbers.rs | 25 ++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/test/codegen/mir-inlined-line-numbers.rs diff --git a/src/test/codegen/mir-inlined-line-numbers.rs b/src/test/codegen/mir-inlined-line-numbers.rs new file mode 100644 index 0000000000000..53efe0f5be5b1 --- /dev/null +++ b/src/test/codegen/mir-inlined-line-numbers.rs @@ -0,0 +1,25 @@ +// compile-flags: -O -g + +#![crate_type = "lib"] + +#[inline(always)] +fn foo() { + bar(); +} + +#[inline(never)] +#[no_mangle] +fn bar() { + panic!(); +} + +#[no_mangle] +pub fn example() { + foo(); +} + +// CHECK-LABEL: @example +// CHECK: tail call void @bar(), !dbg [[DBG_ID:![0-9]+]] +// CHECK: [[DBG_ID]] = !DILocation(line: 18, +// CHECK-SAME: inlinedAt: [[INLINE_ID:![0-9]+]]) +// CHECK: [[INLINE_ID]] = !DILocation(line: 18, From 34d90a46dac95d6f09cd8851b71b43350eecda4d Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Fri, 14 Oct 2022 18:44:30 -0400 Subject: [PATCH 2/2] 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. --- compiler/rustc_middle/src/ty/mod.rs | 4 +++- compiler/rustc_span/src/lib.rs | 8 +++++++- src/test/codegen/mir-inlined-line-numbers.rs | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 0d7d4054bb3cd..34938ab05cb95 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -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() } } diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 91eef647713db..ec5491116e87e 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -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() @@ -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, _)) diff --git a/src/test/codegen/mir-inlined-line-numbers.rs b/src/test/codegen/mir-inlined-line-numbers.rs index 53efe0f5be5b1..19d83f0eee7c4 100644 --- a/src/test/codegen/mir-inlined-line-numbers.rs +++ b/src/test/codegen/mir-inlined-line-numbers.rs @@ -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,