Skip to content

Commit 6116f19

Browse files
committed
Box::into_unique: do the reborrow-to-raw *after* destroying the Box
1 parent f688ba6 commit 6116f19

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/liballoc/boxed.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,15 @@ impl<T: ?Sized> Box<T> {
253253
#[unstable(feature = "ptr_internals", issue = "0", reason = "use into_raw_non_null instead")]
254254
#[inline]
255255
#[doc(hidden)]
256-
pub fn into_unique(mut b: Box<T>) -> Unique<T> {
256+
pub fn into_unique(b: Box<T>) -> Unique<T> {
257+
let mut unique = b.0;
258+
mem::forget(b);
257259
// Box is kind-of a library type, but recognized as a "unique pointer" by
258260
// Stacked Borrows. This function here corresponds to "reborrowing to
259261
// a raw pointer", but there is no actual reborrow here -- so
260262
// without some care, the pointer we are returning here still carries
261263
// the `Uniq` tag. We round-trip through a mutable reference to avoid that.
262-
let unique = unsafe { b.0.as_mut() as *mut T };
263-
mem::forget(b);
264-
unsafe { Unique::new_unchecked(unique) }
264+
unsafe { Unique::new_unchecked(unique.as_mut() as *mut T) }
265265
}
266266

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

0 commit comments

Comments
 (0)