MIR debuginfo: Capture by value contains additional dereference with `--edition 2021`
Nobody has claimed this yet.
- 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:
Meta
rustc --version --verbose:
<version>
Backtrace
<backtrace>
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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