Skip to content

Commit 3241b8f

Browse files
committed
Change to JUST the aliasing entry
other formatting cleanup will be other PRs
1 parent a121660 commit 3241b8f

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

reference/src/glossary.md

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,22 @@ If data immediately pointed to by a `*const T` or `&*const T` is mutated, that's
5252
*Interior mutability* refers to the ability to perform interior mutation without causing UB.
5353
All interior mutation in Rust has to happen inside an [`UnsafeCell`](https://doc.rust-lang.org/core/cell/struct.UnsafeCell.html), so all data structures that have interior mutability must (directly or indirectly) use `UnsafeCell` for this purpose.
5454

55-
#### Layout
55+
#### Validity and safety invariant
5656

57-
The *layout* of a type defines its size and alignment as well as the offsets of its subobjects (e.g. fields of structs/unions/enum/... or elements of arrays).
58-
Moreover, the layout of a type records its *function call ABI* (or just *ABI* for short): how the type is passed *by value* across a function boundary.
57+
The *validity invariant* is an invariant that all data must uphold any time it is accessed or copied in a typed manner.
58+
This invariant is known to the compiler and exploited by optimizations such as improved enum layout or eliding in-bounds checks.
5959

60-
Note: Originally, *layout* and *representation* were treated as synonyms, and Rust language features like the `#[repr]` attribute reflect this.
61-
In this document, *layout* and *representation* are not synonyms.
60+
In terms of MIR statements, "accessed or copied" means whenever an assignment statement is executed.
61+
That statement has a type (LHS and RHS must have the same type), and the data being assigned must be valid at that type.
62+
Moreover, arguments passed to a function must be valid at the type given in the callee signature, and the return value of a function must be valid at the type given in the caller signature.
63+
OPEN QUESTION: Are there more cases where data must be valid?
6264

63-
#### Safety Invariant
65+
In terms of code, some data computed by `TERM` is valid at type `T` if and only if the following program does not have UB:
66+
```rust,ignore
67+
fn main() { unsafe {
68+
let t: T = std::mem::transmute(TERM);
69+
} }
70+
```
6471

6572
The *safety* invariant is an invariant that safe code may assume all data to uphold.
6673
This invariant is used to justify which operations safe code can perform.
@@ -82,6 +89,14 @@ Moreover, such unsafe code must not return a non-UTF-8 string to the "outside" o
8289
To summarize: *Data must always be valid, but it only must be safe in safe code.*
8390
For some more information, see [this blog post](https://www.ralfj.de/blog/2018/08/22/two-kinds-of-invariants.html).
8491

92+
#### Layout
93+
94+
The *layout* of a type defines its size and alignment as well as the offsets of its subobjects (e.g. fields of structs/unions/enum/... or elements of arrays).
95+
Moreover, the layout of a type records its *function call ABI* (or just *ABI* for short): how the type is passed *by value* across a function boundary.
96+
97+
Note: Originally, *layout* and *representation* were treated as synonyms, and Rust language features like the `#[repr]` attribute reflect this.
98+
In this document, *layout* and *representation* are not synonyms.
99+
85100
#### Niche
86101

87102
The *niche* of a type determines invalid bit-patterns that will be used by layout optimizations.
@@ -96,26 +111,10 @@ niches. For example, the "all bits uninitialized" is an invalid bit-pattern for
96111
`&mut T`, but this bit-pattern cannot be used by layout optimizations, and is not a
97112
niche.
98113

99-
#### Validity Invariant
100-
101-
The *validity invariant* is an invariant that all data must uphold any time it is accessed or copied in a typed manner.
102-
This invariant is known to the compiler and exploited by optimizations such as improved enum layout or eliding in-bounds checks.
103-
104-
In terms of MIR statements, "accessed or copied" means whenever an assignment statement is executed.
105-
That statement has a type (LHS and RHS must have the same type), and the data being assigned must be valid at that type.
106-
Moreover, arguments passed to a function must be valid at the type given in the callee signature, and the return value of a function must be valid at the type given in the caller signature.
107-
OPEN QUESTION: Are there more cases where data must be valid?
108-
109-
In terms of code, some data computed by `TERM` is valid at type `T` if and only if the following program does not have UB:
110-
```rust,ignore
111-
fn main() { unsafe {
112-
let t: T = std::mem::transmute(TERM);
113-
} }
114-
```
115114

116115
### TODO
117116

118117
* *tag*
119118
* *rvalue*
120119
* *lvalue*
121-
* *representation*
120+
* *representation*

0 commit comments

Comments
 (0)