File tree Expand file tree Collapse file tree 3 files changed +27
-9
lines changed Expand file tree Collapse file tree 3 files changed +27
-9
lines changed Original file line number Diff line number Diff line change @@ -237,10 +237,10 @@ pub struct Box<
237237/// the newly allocated memory. This is an intrinsic to avoid unnecessary copies.
238238///
239239/// This is the surface syntax for `box <expr>` expressions.
240- #[ doc( hidden) ]
241- #[ rustc_intrinsic]
242- #[ unstable( feature = "liballoc_internals" , issue = "none" ) ]
243- pub fn box_new < T > ( x : T ) -> Box < T > ;
240+ // #[doc(hidden)]
241+ // #[rustc_intrinsic]
242+ // #[unstable(feature = "liballoc_internals", issue = "none")]
243+ // pub fn box_new<T>(x: T) -> Box<T>;
244244
245245impl < T > Box < T > {
246246 /// Allocates memory on the heap and then places `x` into it.
@@ -259,7 +259,12 @@ impl<T> Box<T> {
259259 #[ rustc_diagnostic_item = "box_new" ]
260260 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
261261 pub fn new ( x : T ) -> Self {
262- return box_new ( x) ;
262+ let mut b = Box :: new_uninit ( ) ;
263+ let ptr = mem:: MaybeUninit :: as_mut_ptr ( & mut * b) ;
264+ // SAFETY: we just allocated the box to store `x`.
265+ unsafe { core:: intrinsics:: write_via_move ( ptr, x) } ;
266+ // SAFETY: we just initialized `b`.
267+ unsafe { b. assume_init ( ) }
263268 }
264269
265270 /// Constructs a new box with uninitialized contents.
Original file line number Diff line number Diff line change @@ -235,4 +235,6 @@ pub mod wtf8;
235235pub mod __export {
236236 pub use core:: format_args;
237237 pub use core:: hint:: must_use;
238+ pub use core:: intrinsics:: write_via_move;
239+ pub use core:: mem:: MaybeUninit ;
238240}
Original file line number Diff line number Diff line change 3838#[ macro_export]
3939#[ stable( feature = "rust1" , since = "1.0.0" ) ]
4040#[ rustc_diagnostic_item = "vec_macro" ]
41- #[ allow_internal_unstable( rustc_attrs, liballoc_internals) ]
41+ #[ allow_internal_unstable( rustc_attrs, liballoc_internals, core_intrinsics ) ]
4242macro_rules! vec {
4343 ( ) => (
4444 $crate:: vec:: Vec :: new( )
@@ -48,9 +48,20 @@ macro_rules! vec {
4848 ) ;
4949 ( $( $x: expr) ,+ $( , ) ?) => (
5050 <[ _] >:: into_vec(
51- // Using the intrinsic produces a dramatic improvement in stack usage for
52- // unoptimized programs using this code path to construct large Vecs.
53- $crate:: boxed:: box_new( [ $( $x) ,+] )
51+ if false {
52+ // This arm exists only to control type inference, and to ensure
53+ // `$x` can be written outside an unsafe block.
54+ $crate:: boxed:: Box :: new( [ $( $x) ,+] )
55+ } else {
56+ unsafe {
57+ // Using the intrinsic produces a dramatic improvement in stack usage for
58+ // unoptimized programs using this code path to construct large Vecs.
59+ let mut b = $crate:: boxed:: Box :: new_uninit( ) ;
60+ let ptr = $crate:: __export:: MaybeUninit :: as_mut_ptr( & mut * b) ;
61+ $crate:: __export:: write_via_move( ptr, [ $( $x) ,+] ) ;
62+ b. assume_init( )
63+ }
64+ }
5465 )
5566 ) ;
5667}
You can’t perform that action at this time.
0 commit comments