diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-05-24 01:30:24 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-05-24 01:30:24 +0200 |
| commit | 3388028776ef606335d0b04373323c02248e6c7e (patch) | |
| tree | 929cb9a97af24eba9d05ca8226b620cf6a25c9f9 /src/liballoc | |
| parent | ee972100b2b2a504057e6b6c37e44633f5bbcc73 (diff) | |
| parent | 8d4e7fde479e018d3caf37d1d12f47710183252e (diff) | |
| download | rust-3388028776ef606335d0b04373323c02248e6c7e.tar.gz rust-3388028776ef606335d0b04373323c02248e6c7e.zip | |
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 https://github.com/rust-lang/rust/pull/58429 to reorder the two operations.
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/boxed.rs | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 024594517d9..97c2d8e7a8e 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -253,15 +253,16 @@ 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(mut b: Box<T>) -> Unique<T> { + pub fn into_unique(b: Box<T>) -> Unique<T> { + let mut unique = b.0; + mem::forget(b); // 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); - unsafe { Unique::new_unchecked(unique) } + // the tag of `b`, with `Unique` permission. + // We round-trip through a mutable reference to avoid that. + unsafe { Unique::new_unchecked(unique.as_mut() as *mut T) } } /// Consumes and leaks the `Box`, returning a mutable reference, |
