Skip to content

Commit a86fde3

Browse files
Rollup merge of rust-lang#124013 - RalfJung:box-to-raw, r=oli-obk
Box::into_raw: make Miri understand that this is a box-to-raw cast Turns out rust-lang#122647 went a bit too far in cleaning up `Box`... we still need a hack in `Box::into_raw`. The nicer fix would be to make Stacked Borrows not care about reference-to-raw-pointer casts, but it's unclear whether that will ever be possible without going to full Tree Borrows. Fixes rust-lang/miri#3473.
2 parents ffa1ca9 + 9485346 commit a86fde3

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

alloc/src/boxed.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,8 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
10581058
#[stable(feature = "box_raw", since = "1.4.0")]
10591059
#[inline]
10601060
pub fn into_raw(b: Self) -> *mut T {
1061-
Self::into_raw_with_allocator(b).0
1061+
// Make sure Miri realizes that we transition from a noalias pointer to a raw pointer here.
1062+
unsafe { addr_of_mut!(*&mut *Self::into_raw_with_allocator(b).0) }
10621063
}
10631064

10641065
/// Consumes the `Box`, returning a wrapped raw pointer and the allocator.
@@ -1112,7 +1113,10 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
11121113
pub fn into_raw_with_allocator(b: Self) -> (*mut T, A) {
11131114
let mut b = mem::ManuallyDrop::new(b);
11141115
// We carefully get the raw pointer out in a way that Miri's aliasing model understands what
1115-
// is happening: using the primitive "deref" of `Box`.
1116+
// is happening: using the primitive "deref" of `Box`. In case `A` is *not* `Global`, we
1117+
// want *no* aliasing requirements here!
1118+
// In case `A` *is* `Global`, this does not quite have the right behavior; `into_raw`
1119+
// works around that.
11161120
let ptr = addr_of_mut!(**b);
11171121
let alloc = unsafe { ptr::read(&b.1) };
11181122
(ptr, alloc)

0 commit comments

Comments
 (0)