Empty repr(Rust) enums are ZSTs#2293
Conversation
This comment has been minimized.
This comment has been minimized.
|
Error: Label T-lang can only be set by Rust team members Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #triagebot on Zulip. |
|
cc @traviscross As per #2262 (comment) , here's a followup PR to extend the ZST guarantee to zero-variant |
|
This seems obvious to me: of course an uninhabitable type with no variants and no fields needn't take up any space, so might as well be guaranteed a ZST. Especially since this doesn't say anything about alignment it seems fine. @rfcbot merge lang (Since 1-ZSTs are extra meaningful in various ways it'd probably also be fine to make these 1-ZSTs specifically, not just ZSTs, but let's accept the proposal here for just "size ≡ 0" for now since we have it in front of us.) |
|
@scottmcm has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), 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! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
|
@rfcbot reviewed |
|
These types are uninhabited, but for the purposes of layout, yes, they should not affect the size (or alignment) of whatever they're put inside. @rfcbot reviewed |
|
🔔 This is now entering its final comment period, as per the review above. 🔔 |
|
@rfcbot reviewed |
|
I would appreciate if there’s some (at least minor) indication about this case being an uninhabited type, too; because those feel different from the more typical case of a “ZST”. Also, looking up at the previous section about uninhabited types, which lists Maybe the (or part of the) callout needed would just be in that previous section about uninhabited types, noting the fact that types can be uninhabited and zero-sized at the same time. (And that uninhabited types often - but not always - are zero-sized.) |
I added a note to
I think
|
|
Hmm, maybe the I'll put the note that that types can be uninhabited and zero-sized at the same time in this PR then. |
Co-authored-by: Travis Cross <tc@traviscross.com> Co-authored-by: zachs18 <8355914+zachs18@users.noreply.github.com>
| - `repr(Rust)` [structs] with no fields or where all fields are zero sized (see [layout.repr.rust.struct-zst]). | ||
| - `repr(C)` [structs] with no fields or where all fields are zero-sized (see [layout.repr.c.struct.size-field-offset]). | ||
| - `repr(transparent)` [structs] with no fields or where all fields are zero-sized (see [layout.repr.transparent.layout-abi]). | ||
| - `repr(Rust)` [enums] (without a [primitive representation] specified) with no variants (see [layout.repr.rust.enum-empty-zst]) |
There was a problem hiding this comment.
it feels inconsistent not to list ! here as well? Or is the size of ! not guaranteed?
I think
!should be listed in the primitives section, not therepr(Rust)section. I'll make a separate PR for that, since this one is already in FCP.
I was looking at the list in the glossary, actually ^^ - sorry for any potential confusion by not including a more precise reference to what I was talking about.
There was a problem hiding this comment.
By having ! in the previous glossary entry, but not this section, one could be made to wonder whether it’s intentionally not included here as any sort of meaningful distinction. (I know these are just lists of examples, technically.)
Guarantee that enums with Rust representation with no variants are ZSTs.
Followup to #2262 , (which guaranteed the same for enums with Rust representation with a single variant whose fields are all ZSTs).