Skip to content

Commit cdb6bef

Browse files
committed
Deprecate Box::into_raw_non_null
Per rust-lang#47336 (comment)
1 parent df768c5 commit cdb6bef

File tree

5 files changed

+32
-28
lines changed

5 files changed

+32
-28
lines changed

src/liballoc/boxed.rs

+21-12
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,15 @@ 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+
let b = mem::ManuallyDrop::new(b);
432+
let mut unique = b.0;
433+
// Box is kind-of a library type, but recognized as a "unique pointer" by
434+
// Stacked Borrows. This function here corresponds to "reborrowing to
435+
// a raw pointer", but there is no actual reborrow here -- so
436+
// without some care, the pointer we are returning here still carries
437+
// the tag of `b`, with `Unique` permission.
438+
// We round-trip through a mutable reference to avoid that.
439+
unsafe { unique.as_mut() as *mut T }
432440
}
433441

434442
/// Consumes the `Box`, returning the wrapped pointer as `NonNull<T>`.
@@ -451,6 +459,7 @@ impl<T: ?Sized> Box<T> {
451459
///
452460
/// ```
453461
/// #![feature(box_into_raw_non_null)]
462+
/// #![allow(deprecated)]
454463
///
455464
/// let x = Box::new(5);
456465
/// let ptr = Box::into_raw_non_null(x);
@@ -460,24 +469,24 @@ impl<T: ?Sized> Box<T> {
460469
/// let x = unsafe { Box::from_raw(ptr.as_ptr()) };
461470
/// ```
462471
#[unstable(feature = "box_into_raw_non_null", issue = "47336")]
472+
#[rustc_deprecated(
473+
since = "1.44.0",
474+
reason = "use `Box::leak(b).into()` or `NonNull::from(Box::leak(b))` instead"
475+
)]
463476
#[inline]
464477
pub fn into_raw_non_null(b: Box<T>) -> NonNull<T> {
465-
Box::into_unique(b).into()
478+
Box::leak(b).into()
466479
}
467480

468-
#[unstable(feature = "ptr_internals", issue = "none", reason = "use into_raw_non_null instead")]
481+
#[unstable(
482+
feature = "ptr_internals",
483+
issue = "none",
484+
reason = "use `Box::leak(b).into()` or `NonNull::from(Box::leak(b))` instead"
485+
)]
469486
#[inline]
470487
#[doc(hidden)]
471488
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) }
489+
Box::leak(b).into()
481490
}
482491

483492
/// Consumes and leaks the `Box`, returning a mutable reference,

src/liballoc/collections/linked_list.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl<T> LinkedList<T> {
143143
unsafe {
144144
node.next = self.head;
145145
node.prev = None;
146-
let node = Some(Box::into_raw_non_null(node));
146+
let node = Some(Box::leak(node).into());
147147

148148
match self.head {
149149
None => self.tail = node,
@@ -184,7 +184,7 @@ impl<T> LinkedList<T> {
184184
unsafe {
185185
node.next = None;
186186
node.prev = self.tail;
187-
let node = Some(Box::into_raw_non_null(node));
187+
let node = Some(Box::leak(node).into());
188188

189189
match self.tail {
190190
None => self.head = node,
@@ -1133,11 +1133,9 @@ impl<T> IterMut<'_, T> {
11331133
Some(prev) => prev,
11341134
};
11351135

1136-
let node = Some(Box::into_raw_non_null(box Node {
1137-
next: Some(head),
1138-
prev: Some(prev),
1139-
element,
1140-
}));
1136+
let node = Some(
1137+
Box::leak(box Node { next: Some(head), prev: Some(prev), element }).into(),
1138+
);
11411139

11421140
// Not creating references to entire nodes to not invalidate the
11431141
// reference to `element` we handed to the user.
@@ -1442,7 +1440,7 @@ impl<'a, T> CursorMut<'a, T> {
14421440
#[unstable(feature = "linked_list_cursors", issue = "58533")]
14431441
pub fn insert_after(&mut self, item: T) {
14441442
unsafe {
1445-
let spliced_node = Box::into_raw_non_null(Box::new(Node::new(item)));
1443+
let spliced_node = Box::leak(Box::new(Node::new(item))).into();
14461444
let node_next = match self.current {
14471445
None => self.list.head,
14481446
Some(node) => node.as_ref().next,
@@ -1462,7 +1460,7 @@ impl<'a, T> CursorMut<'a, T> {
14621460
#[unstable(feature = "linked_list_cursors", issue = "58533")]
14631461
pub fn insert_before(&mut self, item: T) {
14641462
unsafe {
1465-
let spliced_node = Box::into_raw_non_null(Box::new(Node::new(item)));
1463+
let spliced_node = Box::leak(Box::new(Node::new(item))).into();
14661464
let node_prev = match self.current {
14671465
None => self.list.tail,
14681466
Some(node) => node.as_ref().prev,

src/liballoc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
#![feature(allocator_api)]
7878
#![feature(allow_internal_unstable)]
7979
#![feature(arbitrary_self_types)]
80-
#![feature(box_into_raw_non_null)]
8180
#![feature(box_patterns)]
8281
#![feature(box_syntax)]
8382
#![feature(cfg_sanitize)]

src/liballoc/rc.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,9 @@ impl<T> Rc<T> {
324324
// pointers, which ensures that the weak destructor never frees
325325
// the allocation while the strong destructor is running, even
326326
// if the weak pointer is stored inside the strong one.
327-
Self::from_inner(Box::into_raw_non_null(box RcBox {
328-
strong: Cell::new(1),
329-
weak: Cell::new(1),
330-
value,
331-
}))
327+
Self::from_inner(
328+
Box::leak(box RcBox { strong: Cell::new(1), weak: Cell::new(1), value }).into(),
329+
)
332330
}
333331

334332
/// Constructs a new `Rc` with uninitialized contents.

src/liballoc/sync.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ impl<T> Arc<T> {
325325
weak: atomic::AtomicUsize::new(1),
326326
data,
327327
};
328-
Self::from_inner(Box::into_raw_non_null(x))
328+
Self::from_inner(Box::leak(x).into())
329329
}
330330

331331
/// Constructs a new `Arc` with uninitialized contents.

0 commit comments

Comments
 (0)