rust-lang / rust-lang/rust

Optimization of deinitialization became worse in nightly

Open
#149,727 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-codegen C-optimization regression-from-stable-to-stable S-has-bisection T-compiler
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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.