@@ -428,7 +428,12 @@ impl<T: ?Sized> Box<T> {
428428 #[ stable( feature = "box_raw" , since = "1.4.0" ) ]
429429 #[ inline]
430430 pub fn into_raw ( b : Box < T > ) -> * mut T {
431- Box :: into_raw_non_null ( b) . as_ptr ( )
431+ // Box is recognized as a "unique pointer" by Stacked Borrows, but internally it is a
432+ // raw pointer for the type system. Turning it directly into a raw pointer would not be
433+ // recognized as "releasing" the unique pointer to permit aliased raw accesses,
434+ // so all raw pointer methods go through `leak` which creates a (unique)
435+ // mutable reference. Turning *that* to a raw pointer behaves correctly.
436+ Box :: leak ( b) as * mut T
432437 }
433438
434439 /// Consumes the `Box`, returning the wrapped pointer as `NonNull<T>`.
@@ -451,6 +456,7 @@ impl<T: ?Sized> Box<T> {
451456 ///
452457 /// ```
453458 /// #![feature(box_into_raw_non_null)]
459+ /// #![allow(deprecated)]
454460 ///
455461 /// let x = Box::new(5);
456462 /// let ptr = Box::into_raw_non_null(x);
@@ -460,24 +466,34 @@ impl<T: ?Sized> Box<T> {
460466 /// let x = unsafe { Box::from_raw(ptr.as_ptr()) };
461467 /// ```
462468 #[ unstable( feature = "box_into_raw_non_null" , issue = "47336" ) ]
469+ #[ rustc_deprecated(
470+ since = "1.44.0" ,
471+ reason = "use `Box::leak(b).into()` or `NonNull::from(Box::leak(b))` instead"
472+ ) ]
463473 #[ inline]
464474 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" ) ]
475+ // Box is recognized as a "unique pointer" by Stacked Borrows, but internally it is a
476+ // raw pointer for the type system. Turning it directly into a raw pointer would not be
477+ // recognized as "releasing" the unique pointer to permit aliased raw accesses,
478+ // so all raw pointer methods go through `leak` which creates a (unique)
479+ // mutable reference. Turning *that* to a raw pointer behaves correctly.
480+ Box :: leak ( b) . into ( )
481+ }
482+
483+ #[ unstable(
484+ feature = "ptr_internals" ,
485+ issue = "none" ,
486+ reason = "use `Box::leak(b).into()` or `Unique::from(Box::leak(b))` instead"
487+ ) ]
469488 #[ inline]
470489 #[ doc( hidden) ]
471490 pub fn into_unique ( b : Box < T > ) -> Unique < T > {
472- let b = mem:: ManuallyDrop :: new ( b) ;
473- let mut unique = b. 0 ;
474- // Box is kind-of a library type, but recognized as a "unique pointer" by
475- // Stacked Borrows. This function here corresponds to "reborrowing to
476- // a raw pointer", but there is no actual reborrow here -- so
477- // without some care, the pointer we are returning here still carries
478- // the tag of `b`, with `Unique` permission.
479- // We round-trip through a mutable reference to avoid that.
480- unsafe { Unique :: new_unchecked ( unique. as_mut ( ) as * mut T ) }
491+ // Box is recognized as a "unique pointer" by Stacked Borrows, but internally it is a
492+ // raw pointer for the type system. Turning it directly into a raw pointer would not be
493+ // recognized as "releasing" the unique pointer to permit aliased raw accesses,
494+ // so all raw pointer methods go through `leak` which creates a (unique)
495+ // mutable reference. Turning *that* to a raw pointer behaves correctly.
496+ Box :: leak ( b) . into ( )
481497 }
482498
483499 /// Consumes and leaks the `Box`, returning a mutable reference,
@@ -523,7 +539,7 @@ impl<T: ?Sized> Box<T> {
523539 where
524540 T : ' a , // Technically not needed, but kept to be explicit.
525541 {
526- unsafe { & mut * Box :: into_raw ( b ) }
542+ unsafe { & mut * mem :: ManuallyDrop :: new ( b ) . 0 . as_ptr ( ) }
527543 }
528544
529545 /// Converts a `Box<T>` into a `Pin<Box<T>>`
0 commit comments