-
Notifications
You must be signed in to change notification settings - Fork 583
fix field-less repr(C) enum docs #2018
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
Open
RalfJung
wants to merge
3
commits into
rust-lang:master
Choose a base branch
from
RalfJung:repr-c-enum
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We were looking at this paragraph, and we were feeling like this may be packing too many things into a single rule. We were wondering about maybe splitting it up into a few separate things, perhaps along these lines:
Change the
items.enum.discriminant.repr-rustrule to also coverrepr(C). It could maybe look something like this:r[items.enum.discriminant.repr-discriminant]
Enums with the [
Rustrepresentation] or [Crepresentation] have discriminants of typeisize.With the [
Rustrepresentation], the compiler may use a smaller type (or another means of distinguishing variants) in the actual memory layout.And then in the layout section:
r[layout.repr.c.enum.discriminant]
For [field-less enums] with the
Crepresentation, all discriminant values (which are of [typeisize][items.enum.discriminant.repr-discriminant]) must be representable by either theintorunsigned inttype in the target platform's C ABI.r[layout.repr.c.enum.size-align]
A [field-less enum] with the
Crepresentation will have the same size and alignment as a C enum that has the same discriminant values and does not explicitly specify the underlying integer type.WDYT?
Uh oh!
There was an error while loading. Please reload this page.
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.
This is also true with the C representation -- the memory layout will usually be
i32, notisize, and may be even smaller (e.g. on ARM). The only difference is that with the Rust representation we leave it unspecified which layout we pick (today we pick the smallest that can fit all values), while with the C representation we pick the same layout as a C compiler would.The rest sounds good. However here...
... it may be worth mentioning (possibly as a non-normative note) that this is about the values after they have been cast to
isize. The order of operations is:isizeresult, so things get wrapped if they don't fit.intor all fit intounsigned int.Uh oh!
There was an error while loading. Please reload this page.
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.
Oh, also, this...
...is subtly wrong. The correct requirement is that either all values fit in
intor all values fit inunsigned int. This is not equivalent to what you wrote: if one value is-1and another one is 0xFFFFFFFF, then they both "fit intointorunsigned int", but the enum still gets rejected because they don't all fit intoint, and they also don't all fit intounsigned int. I specifically worded this very carefully in my PR. :)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.
Thanks, I see now.
Also, just to double check, I was uncertain if the "without a fixed underlying type" is referring specifically to the new C23
enum name: type {...}syntax, or something else (__attribute__((__packed__))?-fshort-enums?). I could have sworn this ability existed before C23, but my memory is clearly clouded.And...Would you be OK if I push these changes on top of your PR here?
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.
That is what I meant, yes. AFAIK that is the only tweak that the standard has here. GCC and other compilers have their extensions but if we start talking about all compiler extensions here we'll never be done. ;)
Yes, that's entirely fine. :)