-
Notifications
You must be signed in to change notification settings - Fork 59
add a chapter on enum representation #57
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
Conversation
974fdf8
to
b14035f
Compare
Comments added, mostly nits. The big one is that it would be nice to have the defn of "niche value" in this section rather than hidden away in another "representation invariant" section. This would make the "representation" section self-contained, and get rid of the need for "bitstring validity". |
OK, pushed a few updates to resolve comments so far. |
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.
I think this is a good start and I agree with everything here.
I wish we would guarantee more niche optimizations though. I have some code using Result
in C FFI to map a c_int == 0
to an Ok(())
and a c_int != 0
to an Err(NonZero<c_int>)
that relies on size_of::<Result> == size_of::<c_int>
. But we can add more guaranteed discriminant elision optimizations to this document in the future. These do not need to be part of the first version of this RFC.
Addressed outstanding feedback I believe. =) |
I'm marking this with FINAL COMMENT PERIOD, it's been idle for quite a while. |
Still LGTM. My only nits are that Rust code in the wild already relies on many enum optimizations that are not guaranteed, and that makes the unsafe code guidelines less useful and more complicated than what they could be. For example, the answer to the question: "Can I use We don't have to provide better answers to these questions right now, but we should strive to do so in 2019, e.g., via an RFC about guaranteed enum layout optimizations or something like that. |
Doesn't this guarantee the optimization?
|
If it were normative, it would, but a couple of lines above the document states that everything that follows isn't normative :/ We discussed this briefly in the meeting, and @nikomatsakis wanted to open an issue to track all non-normative things that we'd like to RFC at some point and why. |
I agree it should be made normative through an RFC, but you made it sound like a change in this document could make you happy ("my only nits are ..."). I don't understand how that can be the case. Nothing we write here can change the fact that this is not normative. |
Ah no, sorry! I didn't meant to say that, only that we should fill some issues about this and once we do that hyperlink them here, but that can be done as a follow up PR, once this is merged and those issues are filled. |
- only guarantee with one unit variant - fieldless enums - remove potentially confusing discussion of what compiler does today - remove discussion of niche values from enums, not important (yet) - generally reorganize layout rules to be by category of enum
Co-Authored-By: nikomatsakis <[email protected]>
fix a few other nits too
a7c7ef3
to
f09d7e5
Compare
I opened #79 but I didn't open an issue pertaining to the normative aspects of repr layout, since I don't think it's specific to this PR. We could do that separately if desired. |
This is a proposed summary chapter on enum representation. I made a best effort to represent what seemed to be the consensus from #10 but I would definitely appreciate review, as I confess I kind of wrote this in a hurry and could easily have missed some details or objections. 😛
Here are the important details:
!
#[repr]
enums from RFC #2195It does permit enums with more than one unit variant.only one unit variant nowNone
, and hence this definition excludesResult<T, ()>
I left a few (non-normative, caveated) notes about niche values.mostly removedHere are a few questions:
Option<&T>
as having a guaranteed optimization, but am somewhat flexible beyond thatFixes #10
cc @eddyb in particular for technical review