Skip to content

Commit cec5671

Browse files
committed
define padding with hypothetical Pad type
1 parent 09c0f97 commit cec5671

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Diff for: reference/src/glossary.md

+8
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ zero-sized type", to refer to zero-sized types with an alignment requirement of
177177
For example, `()` is a "1-ZST" but `[u16; 0]` is not because it has an alignment
178178
requirement of 2.
179179

180+
#### Padding
181+
182+
*Padding* refers to the space that the compiler leaves between fields of a struct or enum variant to satisfy alignment requirements, and before/after variants of a union or enum to make all variants equally sized.
183+
184+
Padding can be though of as `[Pad; N]` for some hypothetical type `Pad` (of size 1) with the following properties:
185+
* `Pad` is valid for any byte, i.e., it has the same validity invariant as `MaybeUninit<u8>`.
186+
* Copying `Pad` ignores the source byte, and writes *any* value to the target byte. Or, equivalently (in terms of Abstract Machine behavior), copying `Pad` marks the target byte as uninitialized.
187+
180188
#### Place
181189

182190
A *place* (called "lvalue" in C and "glvalue" in C++) is the result of computing a [*place expression*][place-value-expr].

Diff for: wip/value-domain.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ The value relation for `bool` relates `Bool(b)` with `[r]` if and only if `r.as_
5555

5656
The value relation for the `()` type relates the empty tuple `Tuple([])` (assuming we can use array notation to "match" on `Vec`) with the empty byte list `[]`, and that's it.
5757

58+
### `Pad`
59+
60+
The [hypothetical `Pad` type][pad] relates the empty tuple `Tuple([])` with any byte list of length 1.
61+
62+
[pad]: https://github.com/rust-lang/unsafe-code-guidelines/blob/master/reference/src/glossary.md#padding
63+
5864
### `!`
5965

6066
The value relation for the `!` type is empty: nothing is related to anything at this type.
@@ -121,7 +127,7 @@ We can implement `TypedMemory` for any `Memory` as follows:
121127
In particular, this defines the semantics of a "typed copy": when an assignment is executed in Rust (at some type `T`), or when an argument of type `T` is passed to a function or a return value of type `T` returned from a function, this is called a "typed copy".
122128
Its semantics is basically to do a `typed_read` at `T` followed by a `typed_write` at `T`.
123129
In particular, this means doing a "typed copy" at `T` of some memory that has no valid representation at `T` is undefined behavior!
124-
This also means that for types that have padding, the "typed copy" does not preserve the padding bytes.
130+
This also means that for `Pad` and more generally for types that have padding, the "typed copy" does not preserve the padding bytes.
125131

126132
## Relation to validity invariant
127133

0 commit comments

Comments
 (0)