Skip to content

MIR debuginfo: Capture by value contains additional dereference with --edition 2021 #136486

Open
@prusovak2

Description

@prusovak2

I tried this code:

closure_test.rs

fn main() {
    let x = "hello world";

    foo(x);
}

#[inline(never)]
pub fn foo(x: &str) {
    bar(|z|
        if x.len() == 0 { Ok(()) } else { Ok(()) });
}

#[inline(never)]
fn bar(f: impl FnOnce(&mut dyn Debug) -> Result<(), ()>) {
    let mut x = 10i32;
    f(&mut x);
}

https://rust.godbolt.org/z/8ErnGbG9z

When compiled with --edition 2021, it produces invalid debug info for capture of variable x.

rustc --edition 2021 -g closure_test.rs -Copt-level=0
gdb ./closure_test
...
(gdb) b closure_test.rs:12
Breakpoint 1 at 0x69f7: file closure_test.rs, line 12.
(gdb) r
...
Breakpoint 1, closure_test::foo::{{closure}} (z=...) at closure_test.rs:12
12              if x.len() == 0 { Ok(()) } else { Ok(()) });
(gdb) p x
$1 = 104

When compiled with --edition 2018, debug info is valid:

$ rustc --edition 2018 -g closure_test.rs -Copt-level=0
$ gdb ./closure_test
...
(gdb) b closure_test.rs:12
Breakpoint 1 at 0x69f3: file closure_test.rs, line 12.
(gdb) r
...
Breakpoint 1, closure_test::foo::{{closure}} (z=...) at closure_test.rs:12
12              if x.len() == 0 { Ok(()) } else { Ok(()) });
(gdb) p x
$1 = "hello world"

Problem seems to be in an addition dereference in MIR debug info:
Image

Meta

rustc --version --verbose:

<version>
Backtrace

<backtrace>

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlA-debuginfoArea: Debugging information in compiled programs (DWARF, PDB, etc.)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions