Skip to content

Structs with no fields or all-ZST fields are ZSTs#2262

Open
DanielEScherzer wants to merge 2 commits intorust-lang:masterfrom
DanielEScherzer:zst-structs
Open

Structs with no fields or all-ZST fields are ZSTs#2262
DanielEScherzer wants to merge 2 commits intorust-lang:masterfrom
DanielEScherzer:zst-structs

Conversation

@DanielEScherzer
Copy link
Copy Markdown
Contributor

@DanielEScherzer DanielEScherzer commented May 5, 2026

Guarantee that structs with Rust representation and with no fields, or where all fields are ZSTs, are themselves ZSTs.

Guarantee that enums with Rust representation with a single struct-like variant with no fields, or where all fields are ZSTs, are themselves ZSTs.

@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label May 5, 2026
@DanielEScherzer DanielEScherzer changed the title New rule: items.struct.zst New rules for ZSTs May 5, 2026
@traviscross traviscross changed the title New rules for ZSTs New rules for structs with no fields May 5, 2026
@traviscross traviscross changed the title New rules for structs with no fields Structs with no fields are ZSTs May 5, 2026
@traviscross traviscross changed the title Structs with no fields are ZSTs Structs with no fields or all-ZST fields are ZSTs May 5, 2026
@traviscross traviscross added I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. T-lang Relevant to the language team. labels May 5, 2026
@traviscross
Copy link
Copy Markdown
Contributor

cc @RalfJung @rust-lang/opsem

Comment thread src/type-layout.md
For [structs] with no fields, or where all fields are [zero-sized], it is further guaranteed that the structs are themselves [zero-sized].

r[layout.repr.rust.enum-struct-like-zst]
For [enums] (without a primitive representation specified) with a single struct-like variant with no fields or where all fields are [zero-sized], the enum itself is [zero-sized].
Copy link
Copy Markdown
Contributor

@theemathas theemathas May 6, 2026

Choose a reason for hiding this comment

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

Seems bizarre to guarantee this for struct-like variants but not unit-like variants or tuple-like variants.

View changes since the review

@RalfJung
Copy link
Copy Markdown
Member

RalfJung commented May 6, 2026

That seems reasonable to me (specifically for repr(Rust); not necessarily for other reprs). Someone should check whether it is honored by -Zrandomize-layout.

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

Labels

I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. 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.

5 participants