-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Allow limited transmuting between types involving type parameters #86281
Conversation
This allows code like: ```rust fn my_array_transmute<T>(t1: [T; 10]) -> [T; 10] { unsafe { std::mem::transmute(t1) } } ```
This allows code like: ```rust struct T1<T>([T; 10]); fn my_transmute<T>(t1: T1<T>) -> [T; 10] { unsafe { std::mem::transmute(t1) } } ```
r? @varkor (rust-highfive has picked a reviewer for you, use r? to override) |
@RalfJung , you seemed to be in favor of allowing such transmutes in #61956 , are you still of the opinion that it should be sound to expand transmutation with the rules described above? |
Seems reasonable to me but I'd recommend t-lang FCP for changes like this. |
Note: Strictly speaking |
Reassigning to r? @RalfJung, who is more familiar with the nuances here. |
@varkor I might be familiar with the nuances of at the bigh level here, but I am completely unfamiliar with the nuances of the code that is being changed here.^^ I don't think I can review that, I am afraid. But I'll nominate this for lang team discussion. Cc @rust-lang/lang |
/// * one's size is a const generic parameter | ||
/// * the other's is an const value which happens to be equal to that const generic parameter | ||
/// will not be considered equal. | ||
UnresolvedConstant { |
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.
Maybe something like RepeatedTy
or so would make more sense? The current name doesn't really convey a lot of meaning.
Personally I'm torn here. On one hand, I think this would be quite useful. On the other hand, I worry that this might make it harder to make transmute non-magic. Maybe someone from the const WG has an idea about whether this would make it harder to encode the "sizes are the same" check as a "real" type system predicate instead of intrinsic magic? For the specific example in the OP, one way forward would be to just stabilize https://doc.rust-lang.org/nightly/std/mem/union.MaybeUninit.html#method.array_assume_init (or something like it). |
r? @nagisa yeah, I can review the implementation but before I spend time on this please make a decision at T-lang level that this is a desirable change in the first place. |
Dropping I-nominated; @scottmcm to write up a consensus from lang-team discussion. |
We discussed this in the 07-20 lang team meeting, and the consensus was to not move forward with this. The core of the concern here is that it felt like it was overlapping with the safe transmute work. Not a 100% overlap, of course, but that there are primitive features which that project will be adding that could be leveraged here in a way that's less ad-hoc. And this change brings up a bunch of the same visibility/coherence questions that that project is tackling, like how changing a private field from For now we'd like to see #61956 fixed without encouraging (Spitballing without my lang hat on: safe transmute will know that |
Before this change, functions could never perform transmutes between types which involve type parameters.
This opens up slightly less limited analysis whereby certain types involving
Sized
type parameters can be considered relative to each other, even if the exact size of that type parameter isn't yet known because monomorphization hasn't happened yet. Specifically it allows:repr(transparent)
types to be considered to have the same size as theSized
type they wrap.repr(transaprent)
wrappers thereof) to be considered to have the same size as other arrays of the same type parameters (orrepr(transparent)
wrappers thereof), as long as their element count is the same.It only supports very limited comparisons, but it opens up more safe and legal transmutes than were previously allowed.
In particular, it allows for transmutes like:
which are currently not allowed because
[T; N]
isn't considered to have the same size as[MaybeUninit<T>; N]
Fixes #61956.