@@ -233,14 +233,14 @@ pub struct Box<
233233 #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator = Global ,
234234> ( Unique < T > , A ) ;
235235
236- /// Constructs a `Box<T>` by calling the `exchange_malloc` lang item and moving the argument into
237- /// the newly allocated memory. This is an intrinsic to avoid unnecessary copies.
238- ///
239- /// This is the surface syntax for `box <expr>` expressions.
236+ /// This function is not actually safe, it has the preconditions of `Box::from_raw`.
237+ /// It is meant for the `vec!` macro where we can't use an unsafe block as that would also
238+ /// wrap the user-defined `$x`.
240239#[ doc( hidden) ]
241- #[ rustc_intrinsic]
242240#[ unstable( feature = "liballoc_internals" , issue = "none" ) ]
243- pub fn box_new < T > ( x : T ) -> Box < T > ;
241+ pub fn vec_macro_slice_helper_unsafe < T , const N : usize > ( b : & mut [ T ; N ] ) -> Box < [ T ] > {
242+ unsafe { Box :: from_raw ( b) }
243+ }
244244
245245impl < T > Box < T > {
246246 /// Allocates memory on the heap and then places `x` into it.
@@ -261,8 +261,10 @@ impl<T> Box<T> {
261261 pub fn new ( x : T ) -> Self {
262262 let mut b = Box :: new_uninit ( ) ;
263263 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) } ;
264+ // SAFETY: We rely on the unstable property of the Rust semantics that references don't
265+ // require the pointee to be valid (if the pointee is inhabited, which we know it is).
266+ let dest = unsafe { & mut * ptr } ;
267+ core:: intrinsics:: write_via_move ( dest, x) ;
266268 // SAFETY: we just initialized `b`.
267269 unsafe { b. assume_init ( ) }
268270 }
0 commit comments