Skip to content

Commit ba89b4f

Browse files
New rule: items.struct.zst
Guarantee that structs with no fields, or where all fields are ZSTs, are themselves ZSTs.
1 parent 92ba1b7 commit ba89b4f

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

src/glossary.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,9 @@ A type is zero sized (a ZST) if its size is 0. Such types have at most one possi
220220
- [Function items] (see [type.fn-item.intro]).
221221
- The constructors of [tuple-like structs] (see [type.fn-item.intro]).
222222
- The constructors of [tuple-like enum variants] (see [type.fn-item.intro]).
223-
- `repr(C)` [structs] with no fields or where all fields are zero-sized (see [layout.repr.c.struct.size-field-offset]).
224-
- `repr(transparent)` [structs] with no fields or where all fields are zero-sized (see [layout.repr.transparent.layout-abi]).
223+
- [structs] with no fields or where all fields are zero-sized (see [items.struct.zst])
224+
- Including those marked with `repr(C)` (see [layout.repr.c.struct.size-field-offset]).
225+
- or marked with `repr(transparent)` (see [layout.repr.transparent.layout-abi]).
225226
- [Arrays] of zero-sized types (see [layout.array]).
226227
- [Arrays] of length zero (see [layout.array]).
227228
- [Unions] of zero-sized types (see [items.union.common-storage]).
@@ -231,6 +232,13 @@ A type is zero sized (a ZST) if its size is 0. Such types have at most one possi
231232
fn f() {}
232233
struct S(u8);
233234
enum E { V(u8) }
235+
struct UnitLike;
236+
struct NoFields {}
237+
struct OnlyZST {
238+
f1: (),
239+
f2: [(); 10],
240+
f3: [u8; 0],
241+
}
234242
#[repr(C)]
235243
struct C1 {}
236244
#[repr(C)]
@@ -257,6 +265,9 @@ assert_eq!(0, size_of::<()>());
257265
assert_eq!(0, size_of_val(&f));
258266
assert_eq!(0, size_of_val(&S));
259267
assert_eq!(0, size_of_val(&E::V));
268+
assert_eq!(0, size_of::<UnitLike>());
269+
assert_eq!(0, size_of::<NoFields>());
270+
assert_eq!(0, size_of::<OnlyZST>());
260271
assert_eq!(0, size_of::<C1>());
261272
assert_eq!(0, size_of::<C2>());
262273
assert_eq!(0, size_of::<T1>());

src/items/structs.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,13 @@ let c = [Cookie, Cookie {}, Cookie, Cookie {}];
6464
r[items.struct.layout]
6565
The precise memory layout of a struct is not specified. One can specify a particular layout using the [`repr` attribute].
6666

67+
r[items.struct.zst]
68+
Structs with no fields, or where all fields are [zero-sized], are themselves guaranteed to be [zero-sized].
69+
6770
[`repr` attribute]: ../type-layout.md#representations
6871
[constant]: constant-items.md
6972
[struct type]: ../types/struct.md
7073
[tuple type]: ../types/tuple.md
7174
[type namespace]: ../names/namespaces.md
7275
[value namespace]: ../names/namespaces.md
76+
[zero-sized]: glossary.zst

0 commit comments

Comments
 (0)