Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/glossary.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ r[glossary.uninhabited]

A type is uninhabited if it has no constructors and therefore can never be instantiated. An uninhabited type is "empty" in the sense that there are no values of the type. The canonical example of an uninhabited type is the [never type] `!`, or an enum with no variants `enum Never { }`. Opposite of [Inhabited](#inhabited).

> [!NOTE]
> Uninhabited types are not necessarily [zero sized]. For example, `enum Never { }` is uninhabited and zero sized, but `(u8, Never)` is uninhabited and not zero sized.

r[glossary.zst]
### Zero-sized type (ZST)

Expand All @@ -223,6 +226,7 @@ A type is zero sized (a ZST) if its size is 0. Such types have at most one possi
- `repr(Rust)` [structs] with no fields or where all fields are zero sized (see [layout.repr.rust.struct-zst]).
- `repr(C)` [structs] with no fields or where all fields are zero-sized (see [layout.repr.c.struct.size-field-offset]).
- `repr(transparent)` [structs] with no fields or where all fields are zero-sized (see [layout.repr.transparent.layout-abi]).
- `repr(Rust)` [enums] (without a [primitive representation] specified) with no variants (see [layout.repr.rust.enum-empty-zst]).
- `repr(Rust)` [enums] (without a [primitive representation] specified) with a single [field-struct-like variant], a single [unit-struct-like variant], or a single [tuple-struct-like variant] and where the struct-like thing has no fields or where all of the fields are zero sized (see [layout.repr.rust.enum-struct-like-zst]).
- [Arrays] of zero-sized types (see [layout.array]).
- [Arrays] of length zero (see [layout.array]).
Expand Down Expand Up @@ -284,6 +288,8 @@ enum E5 {
enum E6 {
V1 (),
}
# /// An enum with no variants.
enum E7 {}

assert_eq!(0, size_of::<()>());
assert_eq!(0, size_of_val(&f));
Expand All @@ -304,6 +310,7 @@ assert_eq!(0, size_of::<E3>());
assert_eq!(0, size_of::<E4>());
assert_eq!(0, size_of::<E5>());
assert_eq!(0, size_of::<E6>());
assert_eq!(0, size_of::<E7>());
```

[`extern` blocks]: items.extern
Expand Down Expand Up @@ -358,3 +365,4 @@ assert_eq!(0, size_of::<E6>());
[unit-struct-like variant]: EnumVariant
[variable bindings]: patterns.md
[visibility rules]: visibility-and-privacy.md
[zero sized]: glossary.zst
7 changes: 7 additions & 0 deletions src/type-layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ Be aware that this guarantee does not imply that the fields have distinct addres
r[layout.repr.rust.struct-zst]
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-empty-zst]
[Enums] (without a [primitive representation] specified) with no variants are [zero sized].

> [!NOTE]
> Such enums are [uninhabited].

r[layout.repr.rust.enum-struct-like-zst]
For [enums] (without a [primitive representation] specified) with a single [field-struct-like variant], a single [unit-struct-like variant], or a single [tuple-struct-like variant] and where the struct-like thing has no fields or where all of the fields are [zero sized], the enums themselves are [zero sized].

Expand Down Expand Up @@ -648,3 +654,4 @@ Because this representation delegates type layout to another type, it cannot be
[tuple-struct-like variant]: EnumVariantTuple
[unit-struct-like variant]: EnumVariant
[`Layout`]: std::alloc::Layout
[uninhabited]: glossary.uninhabited
Loading