8000 Address discovered problems · sarvex/rust-memory-model@dbc0a55 · GitHub
[go: up one dir, main page]

Skip to content

Commit dbc0a55

Browse files
committed
Address discovered problems
1 parent 88f9439 commit dbc0a55

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

specification/terms.md

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,25 @@ If a vexpr is used where an lexpr is expected (other than the LHS of an assignme
7777
A typed expression that evaluates to an value, as opposed to an object. The RHS of an assignment and the arguments of a function are examples of vexprs.
7878

7979
NOTE: Because of evaluation order concerns, after being potentially wrapped to a
80-
vexpr, subexpressions are often wrapped in a `VExpr::Use(LExpr::Temp(vexpr))`. The
80+
vexpr, subexpressions are often wrapped in a `Use(Temp(vexpr))`. The
8181
exact rules here should be documented.
8282

8383
If a lexpr is used where an vexpr is expected, it is wrapped in an implicit use
8484
vexpr, the rules of which are given in §2.3.
8585

8686
### §2.3 Temporary lexpr
8787

88-
```Rust
89-
LExpr::Temp(VExpr)
88+
```
89+
Temp:
90+
Γ ⊢ vexp VExpr
91+
--------------
92+
Γ ⊢ Temp(vexp) LExpr
9093
```
9194

9295
An implicit lexpr that wraps a vexpr when an lexpr is expected.
9396
When evaluated, it:
9497

95-
* creates a temporary object, scheduled for destruction at the end of the current temporary scope.
98+
* creates a temporary object within the arena selected by the arena specification.
9699
* evaluates the vexpr and stores it within the object.
97100
* evaluates to the temporary object.
98101

@@ -111,8 +114,11 @@ fn main() {
111114

112115
### §2.4 Use vexpr
113116

114-
```Rust
115-
VExpr::Use(LExpr)
117+
```
118+
Use:
119+
Γ ⊢ vexp VExpr
120+
--------------
121+
Γ ⊢ Use(vexp) LExpr
116122
```
117123

118124
An implicit vexpr that wraps a lexpr when a vexpr is expected.
@@ -126,31 +132,12 @@ If the value read is an `undef`, behavior is undefined unless the expression's
126132
type is an integer, in which case behavior is unspecified (FIXME: this is intended
127133
to allow reads from padding - what can we say here?).
128134

129-
The read-from object must be aligned to `align_of::<T>()` or behavior is undefined.
135+
The read-from object must be aligned to `AlignOf(T)` or behavior is undefined.
130136

131137
If the expression's type does not implement `Copy`, the value is moved out of the
132138
object, preventing further accesses.
133139

134-
### §2.5 Destruction scopes
135-
136-
A Rust function always keeps a chain of destruction scopes. Each destruction scope contains a list of objects - temporaries and variables - that are to be destroyed when that scope is exited.
137-
138-
The objects in a scope are dropped in the reverse order of their insertion. If such an object is in an uninitialized state (either because it was never initialized or because it was moved out of), it is not destroyed again.
139-
140-
If one of the destructors panics, the current scope as well as the remaining scopes
141-
are exited and the panic continues to unwind.
142-
143-
### §2.5.1 Temporary scopes
144-
145-
A set of baroque rules assigns a destruction scope, called the *temporary scope*,
146-
to each expression.
147-
148-
FIXME: explain these rules.
149-
150-
The temporary created by `LExpr::Temp` is added to that expression's temporary
151-
scope's destruction list.
152-
153-
### §2.6 Note on the terms lvalue and rvalue
140+
### §2.5 Note on the terms lvalue and rvalue
154141

155142
These terms aren't used in this document, because they are used in conflicting ways in various places. However, lvalue is equivalent to lexpr, while rvalue is equivalent to vexpr.
156143

@@ -162,37 +149,55 @@ An object is an untyped, contiguous block of memory with a certain size and a ce
162149

163150
All objects are able to be read, but you may not write to all objects.
164151

165-
A lexpr associated with an object, as a vexpr, will evaluate to a value (see §4) that is equivalent to the memory of the object, interpreted as the type of the lexpr.
152+
A `Use` vexpr, applied to a lexpr that evaluates to an object, will evaluate to a value (see §4) that is equivalent to the memory of the object, interpreted as the type of the vexpr.
166153

167-
A write to an object will set the memory of that object equivalent to the value of the write, without the interpretation of typing.
154+
A write to an object will set the memory of that object equivalent to the value of the write, without the interpretation of typing. (XXX: padding on assignment).
168155

169156
### §3.2 Allocation.
170157

171-
An object may be allocated in one of three ways:
158+
Objects may be created in the following ways, which create objects that do not overlap as long as they are valid.
159+
160+
There may be other (implementation-defined and/or unspecified) ways to create objects.
161+
162+
### §3.2.1 Statics
172163

173-
In static memory:
164+
Statics are valid until the end of the program. For example,
174165

175166
```rust
176167
static x: i32 = 0i32; // a static, read-only object of size >= 4, alignment >= 4
177168
static mut x: i32 = 0i32; // a static, read-write object of size >= 4, alignment >= 4
178169
static x: AtomicI32 = 0i32; // a static, read-write object of size >= 4, alignment >= 4
179170
```
180171

181-
on the stack:
172+
### §3.2.2 Locals
173+
174+
Locals include argument buffers, temporaries, and named bindings. They are always allocated into an arena (§3.3) and are valid until destroyed by it.
175+
176+
For example:
182177

183178
```rust
184179
let x: i32 = 0i32; // a stack, read-only object of size >= 4, alignment >= 4
185180
// you get the point
186181
```
187182

188-
or on the heap:
183+
### §3.2.3 Heap Allocations
184+
185+
Each call to `Box::new` creates a heap allocation, which is valid until it is destroyed when the box is dropped.
186+
187+
For example:
189188

190189
```rust
191190
let x: Box<i32> = Box::new(0i32); // a stack, read-only object pointing to a heap,
192191
// read-write object of size >= 4, alignment >= 4
193192
```
194193

195-
### §3.3 Note on types:
194+
### §3.3 Arenas
195+
196+
A function's stack of arenas is used to manage the orderly destruction of the locals within that function.
197+
198+
See [the arena specification](https://github.com/nikomatsakis/rust-memory-model/issues/17) for more details (XXX: write it somewhere more permanent).
199+
200+
### §3.4 Note on types:
196201

197202
Objects *are not* typed. No, not even effective types ^-^
198203

0 commit comments

Comments
 (0)
0