Skip to content

Empty repr(Rust) enums are ZSTs#2293

Open
zachs18 wants to merge 4 commits into
rust-lang:masterfrom
zachs18:zst-empty-enum
Open

Empty repr(Rust) enums are ZSTs#2293
zachs18 wants to merge 4 commits into
rust-lang:masterfrom
zachs18:zst-empty-enum

Conversation

@zachs18

@zachs18 zachs18 commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

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).

@rustbot

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. label Jul 7, 2026
@zachs18 zachs18 marked this pull request as ready for review July 8, 2026 01:46
@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Jul 8, 2026
@zachs18

zachs18 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot ready

Followup to #2262 to guarantee that zero-variant repr(Rust) enums are zero-sized. Rebased since #2262 merged, using similar phrasing as in that PR.

@rustbot

rustbot commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

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.

@zachs18

zachs18 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

cc @traviscross As per #2262 (comment) , here's a followup PR to extend the ZST guarantee to zero-variant repr(Rust) enums.

@traviscross traviscross added I-lang-nominated Nominated for discussion during a lang team meeting. T-lang Relevant to the language team. needs-fcp P-lang-drag-1 Lang team prioritization drag level 1. labels Jul 8, 2026
@scottmcm

scottmcm commented Jul 8, 2026

Copy link
Copy Markdown
Member

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.)

@rust-rfcbot

rust-rfcbot commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

@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.
See this document for info about what commands tagged team members can give me.

@traviscross

Copy link
Copy Markdown
Contributor

@rfcbot reviewed

@joshtriplett

Copy link
Copy Markdown
Member

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

@rust-rfcbot

Copy link
Copy Markdown
Collaborator

🔔 This is now entering its final comment period, as per the review above. 🔔

@nikomatsakis

Copy link
Copy Markdown
Contributor

@rfcbot reviewed

@steffahn

steffahn commented Jul 8, 2026

Copy link
Copy Markdown
Member

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 ! and variant-less enums, it feels inconsistent not to list ! here as well? Or is the size of ! not guaranteed?

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.)

@zachs18

zachs18 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

I would appreciate if there’s some (at least minor) indication about this case being an uninhabited type, too

I added a note to layout.repr.rust.enum-empty-zst with a link to the "uninhabited" glossary section.

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 the repr(Rust) section. I'll make a separate PR for that, since this one is already in FCP.

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'll do that in the ! PR

@zachs18

zachs18 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

Hmm, maybe the ! layout guarantees should be in or after #2283 ?

I'll put the note that that types can be uninhabited and zero-sized at the same time in this PR then.

Comment thread src/type-layout.md Outdated
Comment thread src/glossary.md Outdated
Co-authored-by: Travis Cross <tc@traviscross.com>
Co-authored-by: zachs18 <8355914+zachs18@users.noreply.github.com>
Comment thread src/glossary.md
- `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])

@steffahn steffahn Jul 8, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zachs18

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 the repr(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.

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in #2309

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge final-comment-period I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. S-waiting-on-review Status: The marked PR is awaiting review from a maintainer T-lang Relevant to the language team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants