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
Copy file name to clipboardExpand all lines: wip/stacked-borrows.md
+27-6Lines changed: 27 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,9 @@
2
2
3
3
**Note:** This document is not normative nor endorsed by the UCG WG. It is maintained by @RalfJung to reflect what is currently implemented in [Miri].
4
4
5
-
For more background on the how and why of Stacked Borrows, see this [blog post on the first implementation][stacked-borrows-1] and this [follow-up][stacked-borrows-2].
5
+
This is not a guide!
6
+
It is more of a reference.
7
+
For a more guide-like introduction of Stacked Borrows, see this [blog post on the first implementation][stacked-borrows-1] and this [follow-up][stacked-borrows-2].
6
8
Also see some [notes from a discussion at the 2019 Rust All-Hands][all-hands].
7
9
8
10
[Miri]: https://github.com/solson/miri/
@@ -17,31 +19,50 @@ Every pointer value has a *tag* (in addition to the location in memory that the
17
19
Moreover, there is a per-call-frame `CallId` as well as some global tracking state.
18
20
19
21
```rust
22
+
/// Timestamps: a monotonically increasing counter.
20
23
// `nat` is the type of mathematical natural numbers, meaning we don't want to think about overflow.
21
24
// NOTE: Miri just uses `u64` which, realistically, will not overflow because we only ever increment by 1.
22
25
typeTimestamp=nat;
23
26
24
-
// Extra per-pointer state (the "tag")
27
+
/// Extra per-pointer state (the "tag"), which must match the stack of the locations that
28
+
/// are accessed with this pointer.
25
29
pubenumBorrow {
30
+
/// A unique (mutable) reference.
26
31
Uniq(Timestamp),
32
+
/// An aliasing reference. This is also used by raw pointers, which do not track details
33
+
/// of how or when they were created, hence the timestamp is optional.
34
+
/// Shr(Some(_)) does NOT mean that the destination of this reference is frozen;
35
+
/// that depends on the type! Only those parts outside of an `UnsafeCell` are actually
36
+
/// frozen.
27
37
Alias(Option<Timestamp>),
28
38
}
29
39
40
+
/// An item on the per-location stack, controlling which pointers may access this location.
30
41
pubenumBorStackItem {
42
+
/// Indicates the unique reference that may mutate this location.
31
43
Uniq(Timestamp),
44
+
/// Indicates that the location has been mutably shared. Used for raw pointers as
45
+
/// well as for unfrozen shared references.
32
46
Raw,
47
+
/// A barrier, tracking the function it belongs to. As long as that function executes,
48
+
/// the barrier may not be popped off the stack.
33
49
FnBarrier(CallId),
34
50
}
35
-
// Extra per-location state
51
+
/// Per-location stack of borrow items, with the most recently created item on top.
52
+
/// Making use of an item towards the bottom of the stack pops the top items,
53
+
/// meaning the corresponding references may not be used any more.
54
+
/// The stack may have exactly one "frozen" item on top, which is stored separately.
55
+
/// The stack can also contain function barriers, which may not be popped while the
56
+
/// function they belong to still runs.
36
57
pubstructStack {
37
58
borrows:Vec<BorStackItem>, // used as a stack; never empty
38
59
frozen_since:Option<Timestamp>, // virtual frozen "item" on top of the stack
39
60
}
40
61
41
-
// Extra per-call-frame state
62
+
/// Extra per-call-frame state: an ID uniquely identifying a stack frame.
42
63
typeCallId=nat;
43
64
44
-
// Extra global state
65
+
/// Extra global state: the next unused `Timestamp`, as well as the next unused `CallId`.
45
66
pubstructTracking {
46
67
clock:Timestamp,
47
68
next_call:CallId,
@@ -55,7 +76,7 @@ The stack of a location, and the tag of a pointer stored at some location, do no
55
76
## Retag statement
56
77
57
78
Stacked Borrows introduces a new operation (a new MIR statement) on the Rust abstract machine.
58
-
*Retagging* operates on a place, and it also carries a flag indicating the kind of retag that is being performed:
79
+
*Retagging* operates on a place (Rust's equivalent of a C lvalue; see [the glossary](../reference/src/glossary.md)), and it also carries a flag indicating the kind of retag that is being performed:
0 commit comments