Optimization of deinitialization became worse in nightly
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
I tried this code (not from real code, but just experimenting):
use std::mem::MaybeUninit;
#[unsafe(no_mangle)]
fn demo(x: &mut MaybeUninit<u32>) -> MaybeUninit<u32> {
*x = MaybeUninit::uninit();
*x
}
In beta, this compiles to (Godbolt):
demo:
ret
In nightly, this compiles to (Godbolt):
demo:
mov eax, dword ptr [rdi]
ret
Since we've deinitialized the memory that x points to, returning a copy of that memory should result in returning uninit. The compiler should be free to return whatever garbage data it wants, so just ret is a valid compilation, and is probably the best-performing compilation result.
Version it worked on
It most recently worked on Godbolt full compiler version:
rustc 1.92.0-beta.3 (f3f12444a 2025-11-09)
binary: rustc
commit-hash: f3f12444a017add0468f683f3a10656d29438a05
commit-date: 2025-11-09
host: x86_64-unknown-linux-gnu
release: 1.92.0-beta.3
LLVM version: 21.1.3
Internal compiler ID: beta
Version with regression
Godbolt full compiler version:
rustc 1.93.0-nightly (83e49b75e 2025-12-03)
binary: rustc
commit-hash: 83e49b75e7daf827e4390ae0ccbcb0d0e2c96493
commit-date: 2025-12-03
host: x86_64-unknown-linux-gnu
release: 1.93.0-nightly
LLVM version: 21.1.5
Internal compiler ID: nightly
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
Reproduce the regression with the provided MaybeUninit example in the two linked Godbolt compiler versions, comparing the generated assembly. Trace the nightly compiler change affecting this optimization and add or update a regression test; done means the example no longer emits the unnecessary load while preserving correct behavior.
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
- 42/100