rust-lang / rust-lang/rust

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

Open
#136,486 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-debuginfo A-edition-2021 A-edition-2024 A-MIR C-bug I-edition-triaged T-compiler
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

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>

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the closure_test.rs reproducer and compile it with rustc --edition 2021 -g -Copt-level=0, then inspect the closure's MIR debug info while comparing the valid edition 2018 output. Use GDB at the reported closure line to verify the captured x; done means edition 2021 reports "hello world" rather than 104.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.