Skip to content

Commit 4560459

Browse files
committed
explain more terms
1 parent 81410e2 commit 4560459

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

wip/stacked-borrows.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
**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].
44

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].
68
Also see some [notes from a discussion at the 2019 Rust All-Hands][all-hands].
79

810
[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
1719
Moreover, there is a per-call-frame `CallId` as well as some global tracking state.
1820

1921
```rust
22+
/// Timestamps: a monotonically increasing counter.
2023
// `nat` is the type of mathematical natural numbers, meaning we don't want to think about overflow.
2124
// NOTE: Miri just uses `u64` which, realistically, will not overflow because we only ever increment by 1.
2225
type Timestamp = nat;
2326

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.
2529
pub enum Borrow {
30+
/// A unique (mutable) reference.
2631
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.
2737
Alias(Option<Timestamp>),
2838
}
2939

40+
/// An item on the per-location stack, controlling which pointers may access this location.
3041
pub enum BorStackItem {
42+
/// Indicates the unique reference that may mutate this location.
3143
Uniq(Timestamp),
44+
/// Indicates that the location has been mutably shared. Used for raw pointers as
45+
/// well as for unfrozen shared references.
3246
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.
3349
FnBarrier(CallId),
3450
}
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.
3657
pub struct Stack {
3758
borrows: Vec<BorStackItem>, // used as a stack; never empty
3859
frozen_since: Option<Timestamp>, // virtual frozen "item" on top of the stack
3960
}
4061

41-
// Extra per-call-frame state
62+
/// Extra per-call-frame state: an ID uniquely identifying a stack frame.
4263
type CallId = nat;
4364

44-
// Extra global state
65+
/// Extra global state: the next unused `Timestamp`, as well as the next unused `CallId`.
4566
pub struct Tracking {
4667
clock: Timestamp,
4768
next_call: CallId,
@@ -55,7 +76,7 @@ The stack of a location, and the tag of a pointer stored at some location, do no
5576
## Retag statement
5677

5778
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:
5980

6081
```rust
6182
pub enum RetagKind {

0 commit comments

Comments
 (0)