Skip to content

Improve raw Box conversions #44877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 10, 2017
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ impl<T: ?Sized> Box<T> {
#[stable(feature = "box_raw", since = "1.4.0")]
#[inline]
pub unsafe fn from_raw(raw: *mut T) -> Self {
mem::transmute(raw)
Box(Unique::new_unchecked(raw))
}

/// Consumes the `Box`, returning the wrapped raw pointer.
Expand All @@ -295,7 +295,7 @@ impl<T: ?Sized> Box<T> {
#[stable(feature = "box_raw", since = "1.4.0")]
#[inline]
pub fn into_raw(b: Box<T>) -> *mut T {
unsafe { mem::transmute(b) }
Box::into_unique(b).as_ptr()
}

/// Consumes the `Box`, returning the wrapped pointer as `Unique<T>`.
Expand Down Expand Up @@ -326,7 +326,9 @@ impl<T: ?Sized> Box<T> {
issue = "27730")]
#[inline]
pub fn into_unique(b: Box<T>) -> Unique<T> {
unsafe { mem::transmute(b) }
let u = b.0;
Copy link
Member

@nagisa nagisa Sep 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error is most likely arising due to this line in particular.

If it is indeed the culprit, it is pretty easy to see why is this a case -- namely within the compiler it is considered that Box is pretty much equivalent to a non-aliasable *mut T, and you cannot just take fields of *mut Ts without deferencing first -- hence the ICE you see.

If you want, you could try fixing it in the compiler, but I’m fairly sure it would be very messy to do so properly.

Disclaimer: This is a guess.

mem::forget(b);
u
}
}

Expand Down