From 3d34e53e3b5ac0ac0427f26f79c7aa48cd355670 Mon Sep 17 00:00:00 2001 From: Alyssa Haroldsen Date: Thu, 13 Oct 2022 11:20:30 -0700 Subject: [PATCH] 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); ``` --- src/type-layout.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/type-layout.md b/src/type-layout.md index 80a36abb8..ccb879f41 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -562,11 +562,11 @@ Like all ways to create undefined behavior in safe Rust, this is a bug. The `transparent` representation can only be used on a [`struct`][structs] or an [`enum`][enumerations] with a single variant that has: -- a single field with non-zero size, and +- a single field with non-zero size, and/or - any number of fields with size 0 and alignment 1 (e.g. [`PhantomData`]). Structs and enums with this representation have the same layout and ABI -as the single non-zero sized field. +as the single non-zero sized field, or size 0 and alignment 1 if not present. This is different than the `C` representation because a struct with the `C` representation will always have the ABI of a `C` `struct`