@@ -136,13 +136,13 @@ use core::fmt;
136136use core:: future:: Future ;
137137use core:: hash:: { Hash , Hasher } ;
138138use core:: iter:: { FromIterator , FusedIterator , Iterator } ;
139- use core:: marker:: { Unpin , Unsize } ;
139+ use core:: marker:: { PhantomData , Unpin , Unsize } ;
140140use core:: mem;
141141use core:: ops:: {
142142 CoerceUnsized , Deref , DerefMut , DispatchFromDyn , Generator , GeneratorState , Receiver ,
143143} ;
144144use core:: pin:: Pin ;
145- use core:: ptr:: { self , NonNull , Unique } ;
145+ use core:: ptr:: { self , NonNull } ;
146146use core:: task:: { Context , Poll } ;
147147
148148use crate :: alloc:: { self , AllocInit , AllocRef , Global } ;
@@ -156,7 +156,26 @@ use crate::vec::Vec;
156156#[ lang = "owned_box" ]
157157#[ fundamental]
158158#[ stable( feature = "rust1" , since = "1.0.0" ) ]
159- pub struct Box < T : ?Sized > ( Unique < T > ) ;
159+ pub struct Box < T : ?Sized > ( BoxPtr < T > ) ;
160+
161+ // FIXME: remove this struct and give `Box` two fields.
162+ // Currently this causes an ICE with 'assertion failed: sig.c_variadic || extra_args.is_empty()'
163+ struct BoxPtr < T : ?Sized > {
164+ ptr : NonNull < T > ,
165+
166+ // NOTE: this marker has no consequences for variance, but is necessary
167+ // for dropck to understand that we logically own a `T`.
168+ //
169+ // For details, see:
170+ // https://github.com/rust-lang/rfcs/blob/master/text/0769-sound-generic-drop.md#phantom-data
171+ _phantom : PhantomData < T > ,
172+ }
173+
174+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
175+ unsafe impl < T : Send + ?Sized > Send for Box < T > { }
176+
177+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
178+ unsafe impl < T : Sync + ?Sized > Sync for Box < T > { }
160179
161180impl < T > Box < T > {
162181 /// Allocates memory on the heap and then places `x` into it.
@@ -382,7 +401,7 @@ impl<T: ?Sized> Box<T> {
382401 #[ stable( feature = "box_raw" , since = "1.4.0" ) ]
383402 #[ inline]
384403 pub unsafe fn from_raw ( raw : * mut T ) -> Self {
385- Box ( Unique :: new_unchecked ( raw) )
404+ Box ( BoxPtr { ptr : NonNull :: new_unchecked ( raw) , _phantom : PhantomData } )
386405 }
387406
388407 /// Consumes the `Box`, returning a wrapped raw pointer.
@@ -462,22 +481,14 @@ impl<T: ?Sized> Box<T> {
462481 #[ unstable( feature = "box_into_raw_non_null" , issue = "47336" ) ]
463482 #[ inline]
464483 pub fn into_raw_non_null ( b : Box < T > ) -> NonNull < T > {
465- Box :: into_unique ( b) . into ( )
466- }
467-
468- #[ unstable( feature = "ptr_internals" , issue = "none" , reason = "use into_raw_non_null instead" ) ]
469- #[ inline]
470- #[ doc( hidden) ]
471- pub fn into_unique ( b : Box < T > ) -> Unique < T > {
472- let b = mem:: ManuallyDrop :: new ( b) ;
473- let mut unique = b. 0 ;
484+ let mut b = mem:: ManuallyDrop :: new ( b) ;
474485 // Box is kind-of a library type, but recognized as a "unique pointer" by
475486 // Stacked Borrows. This function here corresponds to "reborrowing to
476487 // a raw pointer", but there is no actual reborrow here -- so
477488 // without some care, the pointer we are returning here still carries
478489 // the tag of `b`, with `Unique` permission.
479490 // We round-trip through a mutable reference to avoid that.
480- unsafe { Unique :: new_unchecked ( unique . as_mut ( ) as * mut T ) }
491+ unsafe { NonNull :: new_unchecked ( b . 0 . ptr . as_mut ( ) as * mut T ) }
481492 }
482493
483494 /// Consumes and leaks the `Box`, returning a mutable reference,
@@ -910,7 +921,7 @@ impl<T: fmt::Debug + ?Sized> fmt::Debug for Box<T> {
910921impl < T : ?Sized > fmt:: Pointer for Box < T > {
911922 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
912923 // It's not possible to extract the inner Uniq directly from the Box,
913- // instead we cast it to a *const which aliases the Unique
924+ // instead we cast it to a *const which aliases the NonNull
914925 let ptr: * const T = & * * self ;
915926 fmt:: Pointer :: fmt ( & ptr, f)
916927 }
@@ -1025,9 +1036,11 @@ impl<A, F: Fn<A> + ?Sized> Fn<A> for Box<F> {
10251036
10261037#[ unstable( feature = "coerce_unsized" , issue = "27732" ) ]
10271038impl < T : ?Sized + Unsize < U > , U : ?Sized > CoerceUnsized < Box < U > > for Box < T > { }
1039+ impl < T : ?Sized + Unsize < U > , U : ?Sized > CoerceUnsized < BoxPtr < U > > for BoxPtr < T > { }
10281040
10291041#[ unstable( feature = "dispatch_from_dyn" , issue = "none" ) ]
10301042impl < T : ?Sized + Unsize < U > , U : ?Sized > DispatchFromDyn < Box < U > > for Box < T > { }
1043+ impl < T : ?Sized + Unsize < U > , U : ?Sized > DispatchFromDyn < BoxPtr < U > > for BoxPtr < T > { }
10311044
10321045#[ stable( feature = "boxed_slice_from_iter" , since = "1.32.0" ) ]
10331046impl < A > FromIterator < A > for Box < [ A ] > {
0 commit comments