Skip to content

Commit

Permalink
fix Box::into_unique effecitvely transmuting to a raw ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Feb 13, 2019
1 parent e544947 commit 719be24
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,15 @@ impl<T: ?Sized> Box<T> {
#[unstable(feature = "ptr_internals", issue = "0", reason = "use into_raw_non_null instead")]
#[inline]
#[doc(hidden)]
pub fn into_unique(b: Box<T>) -> Unique<T> {
let unique = b.0;
pub fn into_unique(mut b: Box<T>) -> Unique<T> {
// Box is kind-of a library type, but recognized as a "unique pointer" by
// Stacked Borrows. This function here corresponds to "reborrowing to
// a raw pointer", but there is no actual reborrow here -- so
// without some care, the pointer we are returning here still carries
// the `Uniq` tag. We round-trip through a mutable reference to avoid that.
let unique = unsafe { b.0.as_mut() as *mut T };
mem::forget(b);
unique
unsafe { Unique::new_unchecked(unique) }
}

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

0 comments on commit 719be24

Please sign in to comment.