Skip to content

Empty repr(Rust) enums are ZSTs - #2293

Merged
traviscross merged 7 commits into
rust-lang:masterfrom
zachs18:zst-empty-enum
Aug 11, 2026
Merged

Empty repr(Rust) enums are ZSTs#2293
traviscross merged 7 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.

@rust-rfcbot

Copy link
Copy Markdown
Collaborator

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 23, 2026
… r=davidtwco

Update comments and add tests for `-Zrandomize-layout` for some guaranteed ZSTs

See rust-lang/reference#2262 . T-lang wants to make some additional guarantees about zero-sized `repr(Rust)` structs and enums, that changes to `-Zrandomize-layout` could theoretically break in the future. This PR adds comments to `-Zrandomize-layout`'s implementation and tests to `tests/ui/layout/randomize.rs` to prevent breaking those guarantees.

First commit is the guarantees that T-lang already FCP'd in rust-lang/reference#2262 .
Second commit is an additional guarantee (that zero-variant repr(Rust) enums are ZSTs) that's at rust-lang/reference#2293 (FCP now completed)
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 23, 2026
… r=davidtwco

Update comments and add tests for `-Zrandomize-layout` for some guaranteed ZSTs

See rust-lang/reference#2262 . T-lang wants to make some additional guarantees about zero-sized `repr(Rust)` structs and enums, that changes to `-Zrandomize-layout` could theoretically break in the future. This PR adds comments to `-Zrandomize-layout`'s implementation and tests to `tests/ui/layout/randomize.rs` to prevent breaking those guarantees.

First commit is the guarantees that T-lang already FCP'd in rust-lang/reference#2262 .
Second commit is an additional guarantee (that zero-variant repr(Rust) enums are ZSTs) that's at rust-lang/reference#2293 (FCP now completed)
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 23, 2026
… r=davidtwco

Update comments and add tests for `-Zrandomize-layout` for some guaranteed ZSTs

See rust-lang/reference#2262 . T-lang wants to make some additional guarantees about zero-sized `repr(Rust)` structs and enums, that changes to `-Zrandomize-layout` could theoretically break in the future. This PR adds comments to `-Zrandomize-layout`'s implementation and tests to `tests/ui/layout/randomize.rs` to prevent breaking those guarantees.

First commit is the guarantees that T-lang already FCP'd in rust-lang/reference#2262 .
Second commit is an additional guarantee (that zero-variant repr(Rust) enums are ZSTs) that's at rust-lang/reference#2293 (FCP now completed)
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 23, 2026
… r=davidtwco

Update comments and add tests for `-Zrandomize-layout` for some guaranteed ZSTs

See rust-lang/reference#2262 . T-lang wants to make some additional guarantees about zero-sized `repr(Rust)` structs and enums, that changes to `-Zrandomize-layout` could theoretically break in the future. This PR adds comments to `-Zrandomize-layout`'s implementation and tests to `tests/ui/layout/randomize.rs` to prevent breaking those guarantees.

First commit is the guarantees that T-lang already FCP'd in rust-lang/reference#2262 .
Second commit is an additional guarantee (that zero-variant repr(Rust) enums are ZSTs) that's at rust-lang/reference#2293 (FCP now completed)
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 23, 2026
… r=davidtwco

Update comments and add tests for `-Zrandomize-layout` for some guaranteed ZSTs

See rust-lang/reference#2262 . T-lang wants to make some additional guarantees about zero-sized `repr(Rust)` structs and enums, that changes to `-Zrandomize-layout` could theoretically break in the future. This PR adds comments to `-Zrandomize-layout`'s implementation and tests to `tests/ui/layout/randomize.rs` to prevent breaking those guarantees.

First commit is the guarantees that T-lang already FCP'd in rust-lang/reference#2262 .
Second commit is an additional guarantee (that zero-variant repr(Rust) enums are ZSTs) that's at rust-lang/reference#2293 (FCP now completed)
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 23, 2026
… r=davidtwco

Update comments and add tests for `-Zrandomize-layout` for some guaranteed ZSTs

See rust-lang/reference#2262 . T-lang wants to make some additional guarantees about zero-sized `repr(Rust)` structs and enums, that changes to `-Zrandomize-layout` could theoretically break in the future. This PR adds comments to `-Zrandomize-layout`'s implementation and tests to `tests/ui/layout/randomize.rs` to prevent breaking those guarantees.

First commit is the guarantees that T-lang already FCP'd in rust-lang/reference#2262 .
Second commit is an additional guarantee (that zero-variant repr(Rust) enums are ZSTs) that's at rust-lang/reference#2293 (FCP now completed)
rust-timer added a commit to rust-lang/rust that referenced this pull request Jul 24, 2026
Rollup merge of #157905 - zachs18:randomize-layout-zst-note, r=davidtwco

Update comments and add tests for `-Zrandomize-layout` for some guaranteed ZSTs

See rust-lang/reference#2262 . T-lang wants to make some additional guarantees about zero-sized `repr(Rust)` structs and enums, that changes to `-Zrandomize-layout` could theoretically break in the future. This PR adds comments to `-Zrandomize-layout`'s implementation and tests to `tests/ui/layout/randomize.rs` to prevent breaking those guarantees.

First commit is the guarantees that T-lang already FCP'd in rust-lang/reference#2262 .
Second commit is an additional guarantee (that zero-variant repr(Rust) enums are ZSTs) that's at rust-lang/reference#2293 (FCP now completed)
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Jul 25, 2026
Update comments and add tests for `-Zrandomize-layout` for some guaranteed ZSTs

See rust-lang/reference#2262 . T-lang wants to make some additional guarantees about zero-sized `repr(Rust)` structs and enums, that changes to `-Zrandomize-layout` could theoretically break in the future. This PR adds comments to `-Zrandomize-layout`'s implementation and tests to `tests/ui/layout/randomize.rs` to prevent breaking those guarantees.

First commit is the guarantees that T-lang already FCP'd in rust-lang/reference#2262 .
Second commit is an additional guarantee (that zero-variant repr(Rust) enums are ZSTs) that's at rust-lang/reference#2293 (FCP now completed)
@DanielEScherzer

Copy link
Copy Markdown
Contributor

@rustbot label -S-waiting-on-author
Seems to have been present from the start when this was a draft PR, no longer waiting

@rustbot rustbot removed the S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. label Aug 11, 2026
When "zero sized" is used in predicate position, we don't hyphenate
it.  Let's also move the link definition to the bottom.
The phrasing of this rule was a bit indirect due to language being
carried over from other rules that's less applicable here.  Let's make
this clearer.
@traviscross traviscross removed the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Aug 11, 2026
@traviscross
traviscross enabled auto-merge August 11, 2026 18:25
@traviscross

Copy link
Copy Markdown
Contributor

Thanks @zachs18.

@traviscross traviscross added the relnotes Marks issues that should be documented in the release notes of the next release. label Aug 11, 2026
@traviscross
traviscross added this pull request to the merge queue Aug 11, 2026
Merged via the queue into rust-lang:master with commit 6a5392a Aug 11, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge finished-final-comment-period I-lang-radar Items that are on lang's radar and will need eventual work or consideration. relnotes Marks issues that should be documented in the release notes of the next release. T-lang Relevant to the language team. to-announce

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants