You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This isn't intended to create controversy, but document where discussion
has settled. Please feel free to open more PRs to clear up additional
items.
Closes#156.
Copy file name to clipboardexpand all lines: reference/src/glossary.md
+31-7
Original file line number
Diff line number
Diff line change
@@ -190,8 +190,12 @@ guarantee that `Option<&mut T>` has the same size as `&mut T`.
190
190
191
191
While all niches are invalid bit-patterns, not all invalid bit-patterns are
192
192
niches. For example, the "all bits uninitialized" is an invalid bit-pattern for
193
-
`&mut T`, but this bit-pattern cannot be used by layout optimizations, and is not a
194
-
niche.
193
+
`&mut T`, but this bit-pattern cannot be used by layout optimizations, and is not a niche.
194
+
195
+
It is a surprisingly common misconception that niches can occur in [padding] bytes.
196
+
They cannot: A niche representation must be invalid for `T`.
197
+
But a padding byte must be irrelevant to the value of `T`.
198
+
A byte that participates in deciding whether or not the representation is valid cannot, therefore, be a padding byte.
195
199
196
200
#### Zero-sized type / ZST
197
201
@@ -207,6 +211,8 @@ requirement of 2.
207
211
208
212
*Padding* (of a type `T`) 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.
209
213
214
+
Padding for a type is either [interior padding], which is part of one or more fields, or [exterior padding], which is before, between, or after the fields.
215
+
210
216
Padding can be though of as `[Pad; N]` for some hypothetical type `Pad` (of size 1) with the following properties:
211
217
*`Pad` is valid for any byte, i.e., it has the same validity invariant as `MaybeUninit<u8>`.
212
218
* 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.
@@ -217,8 +223,26 @@ for all values `v` and lists of bytes `b` such that `v` and `b` are related at `
217
223
changing `b` at index `i` to any other byte yields a `b'` such `v` and `b'` are related (`Vrel_T(v, b')`).
218
224
In other words, the byte at index `i` is entirely ignored by `Vrel_T` (the value relation for `T`), and two lists of bytes that only differ in padding bytes relate to the same value(s), if any.
219
225
220
-
This definition works fine for product types (structs, tuples, arrays, ...).
221
-
The desired notion of "padding byte" for enums and unions is still unclear.
226
+
This definition works fine for product types (structs, tuples, arrays, ...) and for unions. The desired notion of "padding byte" for enums is still unclear.
227
+
228
+
#### Padding (exterior)
229
+
[exterior padding]: #exterior-padding
230
+
231
+
Exterior padding bytes are [padding] bytes that are not part of one or more fields. They are exactly the padding bytes that are not [interior padding], and therefore must be before, between, or after the fields of the type. Padding that comes after all fields is called [tail padding].
232
+
233
+
#### Padding (interior)
234
+
[interior padding]: #interior-padding
235
+
236
+
Interior padding bytes are [padding] bytes that are part of one or more fields of a type.
237
+
238
+
We can say that a field `f: F`*contains* the byte at index `i` in the type `T` if the layout of `T` places `f` at offset `j` and we have `j <= i < j + size_of::<F>()`. Then a padding byte is interior padding if and only if there exists a field `f` that contains it.
239
+
240
+
It follows that, provided `T` is not an enum, for any such `f`, the byte at index `i - j` in `F` is a padding byte of `F`. This is because all values of `f` give rise to distinct values of `T`.
241
+
242
+
#### Padding (tail)
243
+
[tail padding]: #tail-padding
244
+
245
+
Tail padding is [exterior padding] that comes after all fields of a type.
222
246
223
247
#### Place
224
248
@@ -254,8 +278,8 @@ The relation should be functional for a fixed list of bytes (i.e., every list of
254
278
It is partial in both directions: not all values have a representation (e.g. the mathematical integer `300` has no representation at type `u8`), and not all lists of bytes correspond to a value of a specific type (e.g. lists of the wrong size correspond to no value, and the list consisting of the single byte `0x10` corresponds to no value of type `bool`).
255
279
For a fixed value, there can be many representations (e.g., when considering type `#[repr(C)] Pair(u8, u16)`, the second byte is a [padding byte][padding] so changing it does not affect the value represented by a list of bytes).
256
280
257
-
See the [value domain][value-domain] for an example how values and representation relations can be made more precise.
281
+
See the [MiniRust page on values][minirust-values] for an example how values and representation relations can be made more precise.
There are applications where it is desirable that unions behave simply as a
265
+
buffer of abstract bytes, with no constraints on validity and no interior
266
+
padding bytes that can [get surprisingly reset to uninit][interior padding
267
+
footgun].
268
+
269
+
Thus, we propose that Rust support a repr, which we are tentatively calling the Raw-repr, which gives these semantics to unions. The Raw-repr may be `#[repr(Rust)]` or it may be a new repr, say `#[repr(Raw)`]. The Raw-repr will have the following properties:
270
+
271
+
* All fields are laid out at offset 0.
272
+
* The alignment of the union is the greatest alignment among fields.
273
+
* The only padding bytes are tail padding bytes, if any.
274
+
275
+
<details><summary><b>Rationale</b></summary>
276
+
277
+
We need at least one repr without the [interior mutability footgun]. This layout is extremely constrained, so it would generally be against the philosophy of `#[repr(Rust)]` to impose these constraints on the default layout instead of introducing a new one. However, without such constraints, `#[repr(Rust)]` is a just a giant, largely useless footgun, which is a rationale to simply constrain it and leave any potential relaxations, e.g. for safe transmutes and niches, to other reprs.
0 commit comments