@@ -428,7 +428,12 @@ impl<T: ?Sized> Box<T> {
428
428
#[ stable( feature = "box_raw" , since = "1.4.0" ) ]
429
429
#[ inline]
430
430
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
432
437
}
433
438
434
439
/// Consumes the `Box`, returning the wrapped pointer as `NonNull<T>`.
@@ -451,6 +456,7 @@ impl<T: ?Sized> Box<T> {
451
456
///
452
457
/// ```
453
458
/// #![feature(box_into_raw_non_null)]
459
+ /// #![allow(deprecated)]
454
460
///
455
461
/// let x = Box::new(5);
456
462
/// let ptr = Box::into_raw_non_null(x);
@@ -460,24 +466,34 @@ impl<T: ?Sized> Box<T> {
460
466
/// let x = unsafe { Box::from_raw(ptr.as_ptr()) };
461
467
/// ```
462
468
#[ 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
+ ) ]
463
473
#[ inline]
464
474
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
+ ) ]
469
488
#[ inline]
470
489
#[ doc( hidden) ]
471
490
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 ( )
481
497
}
482
498
483
499
/// Consumes and leaks the `Box`, returning a mutable reference,
@@ -523,7 +539,7 @@ impl<T: ?Sized> Box<T> {
523
539
where
524
540
T : ' a , // Technically not needed, but kept to be explicit.
525
541
{
526
- unsafe { & mut * Box :: into_raw ( b ) }
542
+ unsafe { & mut * mem :: ManuallyDrop :: new ( b ) . 0 . as_ptr ( ) }
527
543
}
528
544
529
545
/// Converts a `Box<T>` into a `Pin<Box<T>>`
0 commit comments