Skip to content

Commit 51e8478

Browse files
New rule items.enum.struct-like-zst
1 parent 4e32c14 commit 51e8478

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

src/glossary.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ A type is zero sized (a ZST) if its size is 0. Such types have at most one possi
223223
- `repr(Rust)` [structs] with no fields or where all fields are zero-sized (see [layout.repr.rust.struct-zst]).
224224
- `repr(C)` [structs] with no fields or where all fields are zero-sized (see [layout.repr.c.struct.size-field-offset]).
225225
- `repr(transparent)` [structs] with no fields or where all fields are zero-sized (see [layout.repr.transparent.layout-abi]).
226+
- [Enums] with a single struct-like variant with no fields or where all fields are zero-sized (see [items.enum.struct-like-zst])
226227
- [Arrays] of zero-sized types (see [layout.array]).
227228
- [Arrays] of length zero (see [layout.array]).
228229
- [Unions] of zero-sized types (see [items.union.common-storage]).
@@ -261,6 +262,9 @@ union U {
261262
f2: [(); 10],
262263
f3: [u8; 0],
263264
}
265+
enum E2 {
266+
V1 { f1: (), f2: [(); 10 ] },
267+
}
264268
assert_eq!(0, size_of::<()>());
265269
assert_eq!(0, size_of_val(&f));
266270
assert_eq!(0, size_of_val(&S));
@@ -275,6 +279,7 @@ assert_eq!(0, size_of::<T2>());
275279
assert_eq!(0, size_of::<[(); 10]>());
276280
assert_eq!(0, size_of::<[u8; 0]>());
277281
assert_eq!(0, size_of::<U>());
282+
assert_eq!(0, size_of::<E2>());
278283
```
279284

280285
[`extern` blocks]: items.extern

src/items/enumerations.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ enum Enum {
7777
}
7878
```
7979

80+
r[items.enum.struct-like-zst]
81+
If an enum only has a single struct-like variant with no fields or where all fields are [zero-sized], the enum itself is [zero-sized].
82+
8083
r[items.enum.constructor-names]
8184
Variant constructors are similar to [struct] definitions, and can be referenced by a path from the enumeration name, including in [use declarations].
8285

@@ -364,3 +367,4 @@ enum E {
364367
[unit-only]: #unit-only-enum
365368
[use declarations]: use-declarations.md
366369
[value namespace]: ../names/namespaces.md
370+
[zero-sized]: glossary.zst

0 commit comments

Comments
 (0)