-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Transmute trait #1891
Transmute trait #1891
Conversation
I don't get why you want to make transmute more useable in Rust. We should be trying to get rid of transmute, not making it better. Transmute is one of the worst unsafeties in Rust, and new users (and cough old users) use it far too much. Giving more support to it would make it even more popular, while we want it to be less popular. |
"The point", as I take it, is to clean up a wart in the language definition. Why have this kind of magical special-cased condition in the type system for |
@glaebhoerl |
@ubsan There's no reason why we couldn't do this then deprecate |
The way I see it, using |
Well I don't see how one for coercions would be useful, you can always use
What other ones are there? You could argue for any operation like this having a trait but |
Well the transmutability check would just move into trait selection. But it would make rust kind-safe like it's supposed to be. Edit: Also, |
@canndrew I must confess my ignorance of what kind-safeness is and why we want it. Could you expound please? |
@canndrew It's not broken now. It's just not an operator that should be used in a generic context. In fact, I would argue that all such uses, excepting implementation of |
I don't understand the motivation here. Today, std::mem::transmute<T, U>() has some compiler magic associated with it that ensures a compilation error if T and U are different sizes. Under this RFC, the Transmute trait would have that magic instead. But it's still magic. If we had--or were anywhere close to getting--trait bounds like
I don't think you'd ever want to do that. Many types can be transmuted to usize, but without even knowing which type you're transmuting it seems virtually guaranteed to invoke some sort of undefined behavior.
What does "kind-safe" mean? |
If you want to change Also, I suspect the warnings against transmute should include better explanations about how conversions should be done. I'm thinking this issue, fix, etc to the arrayref crate. At present, the transmute section of the nomicon has a link to the unbounded lifetimes section and a note that "you can get most of the functionality of these functions using pointer casts", but says relatively little. And some examples in the docs for |
@Ericson2314: would this help your usize-stuffing implementation of threads? |
Part of what makes the But what if it was more restricted? (And thus not actually related to Today, Having a built-in trait for that would be nice, as it could understand alignment and recurse through appropriate- Maybe it'd be easier to discourage |
@scottmcm I've argued in the past for (unsafe) |
We discussed this RFC at this mornings lang team meeting. We decided while it does have some advantages, this RFC would be effectively impossible to implement and so we should not keep it open. To summarise our feelings:
However, the above is basically moot because of the implementation issues: the size of types is not known until the 'trans' phase of the compiler (in fact in the past this required using LLVM, and there is some chance we might again require input from backends like LLVM in some circumstances). It would be a major change to the compiler to move this work earlier - consider, for example, the case of enums where the size depends on the optimisations we apply to the layout of the enum. It may even be impossible to compute sizes in all cases before trans. Since we would need this information to determine if In summary, the implementation burden here seems to vastly outweigh the usefulness of the feature, if it is possible to implement at all. |
|
Thanks @eddyb. The (rare) decision to close out of hand was based on assumptions about the implementation that appear to be incorrect, so I'm going to reopen for the time being. Personally, while I'm open to cleaning up the definition of |
For the record, I'm not too comfortable with this, and even if I care about expressing the requirements of |
My opinion is that we ought to close for now -- not because of technical infeasibility, but because of low priority and unclear motivation. Also I'd prefer to make more progress on the refinements to trait system impl before adding more complexity to it; this will inevitably involve some special cases (until such time as we support something like UPDATE: (I also think we should just avoid closing out of hand as a rule.) |
@rfcbot fcp close I propose we close this RFC for the reasons given here. In particular, while adding a trait seems like the correct way to permit generic code that uses transmute, we are not sure if we would want to do that, and fairly sure we won't want to do that now. So this is not a "close forever" sort of thing, but a "close for now" sort of thing. |
Team member @nikomatsakis has proposed to close this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period is now complete. |
The FCP has completed, with no additional comments. I'm going to close the RFC, with the stated rationale. |
Add a
Transmute<T>
trait for representing types that can be transmuted toT
.Rendered