Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MIR inlining causes incorrect line numbers for inlined code #103068

Closed
wesleywiser opened this issue Oct 14, 2022 · 1 comment · Fixed by #103071
Closed

MIR inlining causes incorrect line numbers for inlined code #103068

wesleywiser opened this issue Oct 14, 2022 · 1 comment · Fixed by #103071
Assignees
Labels
A-debuginfo Area: Debugging information in compiled programs (DWARF, PDB, etc.) A-mir-opt-inlining Area: MIR inlining C-bug Category: This is a bug.

Comments

@wesleywiser
Copy link
Member

wesleywiser commented Oct 14, 2022

The following program generates a backtrace with incorrect line numbers:

#[inline(always)]
fn foo() {
    bar();  // line 3
}

#[inline(never)]
fn bar() {
    panic!();  // line 8
}

fn main() {
    foo();  // line 12
}
$ rustc +nightly -g --crate-name mir_inlining -O mir_inlining.rs
$ RUST_BACKTRACE=1 ./mir_inlining
thread 'main' panicked at 'explicit panic', mir_inlining.rs:8:5
stack backtrace:
   0: std::panicking::begin_panic
             at /rustc/6b3ede3f7bc502eba7bbd202b4b9312d812adcd7/library/std/src/panicking.rs:607:12
   1: mir_inlining::bar
             at ./mir_inlining.rs:8:5
   2: mir_inlining::foo
             at ./mir_inlining.rs:12:5
   3: mir_inlining::main
             at ./mir_inlining.rs:12:5
   4: core::ops::function::FnOnce::call_once
             at /rustc/6b3ede3f7bc502eba7bbd202b4b9312d812adcd7/library/core/src/ops/function.rs:251:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Notice that frames 2 and 3 both indicate they come from line 12 (frame 2 should be at line 3).

This seems to be a result of MIR inlining because the LLVM IR (pre LLVM optimization) has the same issue:

; mir_inlining::main
; Function Attrs: nonlazybind uwtable
define internal void @_ZN12mir_inlining4main17h04ea6567a81cf010E() unnamed_addr #0 !dbg !1610 {
start:
; call mir_inlining::bar
  call void @_ZN12mir_inlining3bar17hfea3a56d969a7806E(), !dbg !1611
  br label %bb1, !dbg !1611

bb1:                                              ; preds = %start
  ret void, !dbg !1614
}

!1611 = !DILocation(line: 12, column: 5, scope: !1612, inlinedAt: !1613)
!1613 = !DILocation(line: 12, column: 5, scope: !1610)

Meta

rustc --version --verbose:

rustc 1.66.0-nightly (6b3ede3f7 2022-10-13)
binary: rustc
commit-hash: 6b3ede3f7bc502eba7bbd202b4b9312d812adcd7
commit-date: 2022-10-13
host: x86_64-unknown-linux-gnu
release: 1.66.0-nightly
LLVM version: 15.0.2
@wesleywiser wesleywiser added A-debuginfo Area: Debugging information in compiled programs (DWARF, PDB, etc.) C-bug Category: This is a bug. A-mir-opt-inlining Area: MIR inlining labels Oct 14, 2022
@wesleywiser wesleywiser self-assigned this Oct 14, 2022
@wesleywiser
Copy link
Member Author

I believe I have a fix and will post it shortly.

@bors bors closed this as completed in 77e7b74 Oct 28, 2022
Aaron1011 pushed a commit to Aaron1011/rust that referenced this issue Jan 6, 2023
…, 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 rust-lang#103068
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-debuginfo Area: Debugging information in compiled programs (DWARF, PDB, etc.) A-mir-opt-inlining Area: MIR inlining C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant