Skip to content

Commit

Permalink
Auto merge of rust-lang#137371 - matthiaskrgr:rollup-3qkdqar, r=matth…
Browse files Browse the repository at this point in the history
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#128080 (Specify scope in `out_of_scope_macro_calls` lint)
 - rust-lang#135630 (add more `s390x` target features)
 - rust-lang#136089 (Reduce `Box::default` stack copies in debug mode)
 - rust-lang#137204 (Clarify MIR dialects and phases)
 - rust-lang#137299 (Simplify `Postorder` customization.)
 - rust-lang#137302 (Use a probe to avoid registering stray region obligations when re-checking drops in MIR typeck)
 - rust-lang#137305 (Tweaks in and around `rustc_middle`)
 - rust-lang#137313 (Some codegen_llvm cleanups)

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors authored and gitbot committed Mar 11, 2025
2 parents 093b2dd + 42e5099 commit fb387be
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1689,7 +1689,20 @@ impl<T: Default> Default for Box<T> {
/// Creates a `Box<T>`, with the `Default` value for T.
#[inline]
fn default() -> Self {
Box::write(Box::new_uninit(), T::default())
let mut x: Box<mem::MaybeUninit<T>> = Box::new_uninit();
unsafe {
// SAFETY: `x` is valid for writing and has the same layout as `T`.
// If `T::default()` panics, dropping `x` will just deallocate the Box as `MaybeUninit<T>`
// does not have a destructor.
//
// We use `ptr::write` as `MaybeUninit::write` creates
// extra stack copies of `T` in debug mode.
//
// See https://github.com/rust-lang/rust/issues/136043 for more context.
ptr::write(&raw mut *x as *mut T, T::default());
// SAFETY: `x` was just initialized above.
x.assume_init()
}
}
}

Expand Down

0 comments on commit fb387be

Please sign in to comment.