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

Improve raw Box conversions #44877

merged 7 commits into from
Oct 10, 2017

Conversation

nvzqz
Copy link
Contributor

@nvzqz nvzqz commented Sep 27, 2017

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

@nvzqz
Copy link
Contributor Author

nvzqz commented Sep 27, 2017

This change caused this error:

[00:03:49] error: internal compiler error: /checkout/src/librustc_trans/adt.rs:431: Cannot handle boxed::Box<[u8]> represented as TyLayout {
[00:03:49]     ty: boxed::Box<[u8]>,
[00:03:49]     layout: FatPointer {
[00:03:49]         metadata: Int(
[00:03:49]             I64
[00:03:49]         ),
[00:03:49]         non_zero: true
[00:03:49]     },
[00:03:49]     variant_index: None
[00:03:49] }

When running

stamp sh -x -c "$RUN_SCRIPT"

I'm not sure how to go about fixing this.

@nvzqz nvzqz changed the title Remove use of mem::transmute in some Box conversions Remove use of mem::transmute in raw Box conversions Sep 27, 2017
@nvzqz
Copy link
Contributor Author

nvzqz commented Sep 28, 2017

Would you mind taking a look at this @alexcrichton?

@aidanhs
Copy link
Member

aidanhs commented Sep 28, 2017

Thanks for the PR @nvzqz! We'll check in now and again to make sure @alexcrichton or another reviewer gets to this soon.

@aidanhs aidanhs added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 28, 2017
@alexcrichton
Copy link
Member

Thanks for the PR @nvzqz! As of a very long time ago (maybe this is still true today?) the Box type was defined in the compiler and the type definition here in the libraries mainly only sufficed for a few analysis passes in rustc. I think that may be where the error is coming from here? Maybe the tyep is still defined in the compiler and as a result trying to interpret it is causing issues?

@@ -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.

@carols10cents carols10cents added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 2, 2017
bors added a commit that referenced this pull request Oct 4, 2017
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.
@shepmaster
Copy link
Member

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".
@nvzqz
Copy link
Contributor Author

nvzqz commented Oct 6, 2017

@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.

@nvzqz
Copy link
Contributor Author

nvzqz commented Oct 6, 2017

I'm interested in adding Box::from_unique and making it unstable under #27730.

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.
@nvzqz nvzqz changed the title Remove use of mem::transmute in raw Box conversions Improve raw Box conversions Oct 6, 2017
@alexcrichton
Copy link
Member

@bors: r+

Thanks!

@bors
Copy link
Collaborator

bors commented Oct 9, 2017

📌 Commit 22298b8 has been approved by alexcrichton

@bors
Copy link
Collaborator

bors commented Oct 10, 2017

⌛ Testing commit 22298b8 with merge 0217315...

bors added a commit that referenced this pull request Oct 10, 2017
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`
@bors
Copy link
Collaborator

bors commented Oct 10, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: alexcrichton
Pushing 0217315 to master...

@bors bors merged commit 22298b8 into rust-lang:master Oct 10, 2017
@nvzqz nvzqz deleted the box-conversions branch December 25, 2017 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants