Skip to content

Commit 3d34e53

Browse files
committed
Clarify that #[repr(transparent)] is valid without a non-ZST field
The following is valid, but the prior language suggests it is not: ```rust #[repr(transparent)] enum Banana { Phone, } #[repr(transparent)] struct Blooey(()); #[repr(transparent)] struct TheVicePresidentDoesNotHaveUnilateralPowerOverFederalElections; ``` Of note: it's still invalid to have a zero-sized field in a transparent struct that has an alignment requirement higher than 1: ```rust #[repr(align(2))] struct Inner; // error[E0691]: zero-sized field in transparent struct has alignment // larger than 1 #[repr(transparent)] struct Fizz(Inner); ```
1 parent f6ed74f commit 3d34e53

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/type-layout.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -562,11 +562,11 @@ Like all ways to create undefined behavior in safe Rust, this is a bug.
562562
The `transparent` representation can only be used on a [`struct`][structs]
563563
or an [`enum`][enumerations] with a single variant that has:
564564

565-
- a single field with non-zero size, and
565+
- a single field with non-zero size, and/or
566566
- any number of fields with size 0 and alignment 1 (e.g. [`PhantomData<T>`]).
567567

568568
Structs and enums with this representation have the same layout and ABI
569-
as the single non-zero sized field.
569+
as the single non-zero sized field, or size 0 and alignment 1 if not present.
570570

571571
This is different than the `C` representation because
572572
a struct with the `C` representation will always have the ABI of a `C` `struct`

0 commit comments

Comments
 (0)