Skip to content

Commit

Permalink
fix Box::into_unique
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Oct 27, 2020
1 parent 56d288f commit ab374dc
Showing 1 changed file with 2 additions and 7 deletions.
9 changes: 2 additions & 7 deletions library/alloc/src/boxed.rs
Expand Up @@ -724,15 +724,10 @@ impl<T: ?Sized, A: AllocRef> Box<T, A> {
// Box is recognized as a "unique pointer" by Stacked Borrows, but internally it is a
// raw pointer for the type system. Turning it directly into a raw pointer would not be
// recognized as "releasing" the unique pointer to permit aliased raw accesses,
// so all raw pointer methods have to leak the box. Turning *that* to a raw pointer
// so all raw pointer methods have to go through `Box::leak`. Turning *that* to a raw pointer
// behaves correctly.
let b = mem::ManuallyDrop::new(b);

// The box is unitiliazed later when moving out the allocator. The pointer is stored
// beforehand.
let ptr = b.0;
let alloc = unsafe { ptr::read(&b.1) };
(ptr, alloc)
(Unique::from(Box::leak(b)), alloc)
}

/// Returns a reference to the underlying allocator.
Expand Down

0 comments on commit ab374dc

Please sign in to comment.