Skip to content

Commit fb387be

Browse files
borsgitbot
authored and
gitbot
committed
Auto merge of rust-lang#137371 - matthiaskrgr:rollup-3qkdqar, r=matthiaskrgr
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
2 parents 093b2dd + 42e5099 commit fb387be

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

Diff for: alloc/src/boxed.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1689,7 +1689,20 @@ impl<T: Default> Default for Box<T> {
16891689
/// Creates a `Box<T>`, with the `Default` value for T.
16901690
#[inline]
16911691
fn default() -> Self {
1692-
Box::write(Box::new_uninit(), T::default())
1692+
let mut x: Box<mem::MaybeUninit<T>> = Box::new_uninit();
1693+
unsafe {
1694+
// SAFETY: `x` is valid for writing and has the same layout as `T`.
1695+
// If `T::default()` panics, dropping `x` will just deallocate the Box as `MaybeUninit<T>`
1696+
// does not have a destructor.
1697+
//
1698+
// We use `ptr::write` as `MaybeUninit::write` creates
1699+
// extra stack copies of `T` in debug mode.
1700+
//
1701+
// See https://github.com/rust-lang/rust/issues/136043 for more context.
1702+
ptr::write(&raw mut *x as *mut T, T::default());
1703+
// SAFETY: `x` was just initialized above.
1704+
x.assume_init()
1705+
}
16931706
}
16941707
}
16951708

0 commit comments

Comments
 (0)