Skip to content

Commit 3388028

Browse files
authored
Rollup merge of #61086 - RalfJung:box, r=alexcrichton
Box::into_unique: do the reborrow-to-raw *after* destroying the Box Currently we first "reborrow" the box to a raw pointer, and then `forget` it. When tracking raw pointers more strictly (something I am experimenting with locally in Miri), the "use" induced by passing the box to `forget` invalidates the previously created raw pointer. So adjust my hack from #58429 to reorder the two operations.
2 parents ee97210 + 8d4e7fd commit 3388028

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/liballoc/boxed.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,16 @@ 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
261-
// 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) }
263+
// the tag of `b`, with `Unique` permission.
264+
// We round-trip through a mutable reference to avoid that.
265+
unsafe { Unique::new_unchecked(unique.as_mut() as *mut T) }
265266
}
266267

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

0 commit comments

Comments
 (0)