-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Improve raw Box conversions #44877
Conversation
Makes use of conversions via Unique.
This change caused this error:
When running
I'm not sure how to go about fixing this. |
Would you mind taking a look at this @alexcrichton? |
Thanks for the PR @nvzqz! We'll check in now and again to make sure @alexcrichton or another reviewer gets to this soon. |
Thanks for the PR @nvzqz! As of a very long time ago (maybe this is still true today?) the |
src/liballoc/boxed.rs
Outdated
@@ -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; |
There was a problem hiding this comment.
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 T
s 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.
Remove mem::transmute used in Box<str> conversions Given that #44877 is failing, I decided to make a separate PR. This is done with the same motivation: to avoid `mem::transmute`-ing non `#[repr(C)]` types.
Heya, @nvzqz, it's been over 7 days since you last checked in; are you still working on this? |
Seems to cause this error: "Cannot handle boxed::Box<[u8]> represented as TyLayout".
@shepmaster just pushed another commit that'll hopefully make this work by reverting one of the changes. I don't know nearly enough about the compiler internals to fix the root issue. |
Same reasons as commit 904133e.
I'm interested in adding |
Provides a reasonable interface for Box::from_raw implementation. Does not get around the requirement of mem::transmute for converting back and forth between Unique and Box.
@bors: r+ Thanks! |
📌 Commit 22298b8 has been approved by |
Improve raw Box conversions This PR has two goals: - Reduce use of `mem::transmute` in `Box` conversions I understand that `mem::transmute`-ing non `#[repr(C)]` types is implementation-defined behavior. This may not matter within the reference implementation of Rust, but I believe it's important to remain consistent. For example, I noticed that `str::from_utf8_unchecked` went from using `mem::transmute` to using pointer casts. - Make `Box` pointer conversions more straightforward regarding `Unique`
☀️ Test successful - status-appveyor, status-travis |
This PR has two goals:
Reduce use of
mem::transmute
inBox
conversionsI understand that
mem::transmute
-ing non#[repr(C)]
types is implementation-defined behavior. This may not matter within the reference implementation of Rust, but I believe it's important to remain consistent. For example, I noticed thatstr::from_utf8_unchecked
went from usingmem::transmute
to using pointer casts.Make
Box
pointer conversions more straightforward regardingUnique