From be8acd082e6a304ce154f9066aebb8d6ee420b7f Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Wed, 11 Feb 2026 21:42:38 +0000 Subject: [PATCH 1/3] Fields must fit in the struct --- src/type-layout.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/type-layout.md b/src/type-layout.md index 16dd006e2d..167d386f04 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -168,6 +168,7 @@ The only data layout guarantees made by this representation are those required f 1. The offset of a field is divisible by that field's alignment. 2. The alignment of the type is at least the maximum alignment of its fields. + 3. For any field, its offset plus its size is at most the size of the type. r[layout.repr.rust.layout.struct] For [structs], it is further guaranteed that the fields do not overlap. That is, the fields can be ordered such that the offset plus the size of any field is less than or equal to the offset of the next field in the ordering. The ordering does not have to be the same as the order in which the fields are specified in the declaration of the type. From 47a5caf7422f2d6e94b1cdbfc4b62acd8626a283 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Wed, 11 Mar 2026 08:57:53 +0000 Subject: [PATCH 2/3] Restrict guarantees to constructible fields --- src/type-layout.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/type-layout.md b/src/type-layout.md index 167d386f04..4193f8abc0 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -166,9 +166,15 @@ The `Rust` representation is the default representation for nominal types withou r[layout.repr.rust.layout] The only data layout guarantees made by this representation are those required for soundness. These are: - 1. The offset of a field is divisible by that field's alignment. - 2. The alignment of the type is at least the maximum alignment of its fields. - 3. For any field, its offset plus its size is at most the size of the type. + 1. The offset of a constructible field is divisible by that field's alignment. + 2. The alignment of the type is at least the maximum alignment of its constructible fields. + 3. For any constructible field, its offset plus its size is at most the size of the type. + +r[layout.repr.rust.layout.constructible] + +A field is considered constructible if it is possible to create a value of the type containing the field. + +For example, given `enum E { A { x: u32 }, B { y: u32, z: ! } }`, the field `x` is constructible because you can create the value `E::A { x: 0 }`, but the fields `y` and `z` are not constructible because the type of `z` is uninhabited, so it is impossible to create an `E::B` value. r[layout.repr.rust.layout.struct] For [structs], it is further guaranteed that the fields do not overlap. That is, the fields can be ordered such that the offset plus the size of any field is less than or equal to the offset of the next field in the ordering. The ordering does not have to be the same as the order in which the fields are specified in the declaration of the type. From b1f9d0596f54ca58d0e683a09cb6cd5a49335daa Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Wed, 11 Mar 2026 22:06:43 +0000 Subject: [PATCH 3/3] Make it more clear that uninhabited exception is only for enums --- src/type-layout.md | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/type-layout.md b/src/type-layout.md index 4193f8abc0..6b969201c5 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -166,21 +166,20 @@ The `Rust` representation is the default representation for nominal types withou r[layout.repr.rust.layout] The only data layout guarantees made by this representation are those required for soundness. These are: - 1. The offset of a constructible field is divisible by that field's alignment. - 2. The alignment of the type is at least the maximum alignment of its constructible fields. - 3. For any constructible field, its offset plus its size is at most the size of the type. + 1. The offset of a field is divisible by that field's alignment. + 2. The alignment of the type is at least the maximum alignment of its fields. + 3. For any field, its offset plus its size is at most the size of the type. -r[layout.repr.rust.layout.constructible] - -A field is considered constructible if it is possible to create a value of the type containing the field. - -For example, given `enum E { A { x: u32 }, B { y: u32, z: ! } }`, the field `x` is constructible because you can create the value `E::A { x: 0 }`, but the fields `y` and `z` are not constructible because the type of `z` is uninhabited, so it is impossible to create an `E::B` value. +For enums, the above guarantees only apply to fields of inhabited variants. r[layout.repr.rust.layout.struct] -For [structs], it is further guaranteed that the fields do not overlap. That is, the fields can be ordered such that the offset plus the size of any field is less than or equal to the offset of the next field in the ordering. The ordering does not have to be the same as the order in which the fields are specified in the declaration of the type. +For [structs], it is further guaranteed that the fields do not overlap. That is, the fields can be ordered such that the offset plus the size of any field is less than or equal to the offset of the next field in the ordering, or for the last field, the size of the struct. The ordering does not have to be the same as the order in which the fields are specified in the declaration of the type. Be aware that this guarantee does not imply that the fields have distinct addresses: zero-sized types may have the same address as other fields in the same struct. +r[layout.repr.rust.layout.enum] +For enums, a variant of the enum is said to be inhabited if all of its fields are [inhabited](glossary.html#inhabited). When an enum variant is inhabited, its fields are laid out according to the same guarantees as a struct. Otherwise, nothing is guaranteed about the fields. This allows the enum to be smaller than fields of uninhabited variants. + r[layout.repr.rust.unspecified] There are no other guarantees of data layout made by this representation.