CELL_PERMISSIONS 6.8 KiB raw
1
================
2
CELL PERMISSIONS
3
================
4
5
This document defines permission-associated cells. The words MUST, MUST NOT,
6
SHOULD, and MAY are normative.
7
8
TERMS
9
=====
10
11
A storage region controls the lifetime and reclamation of a cell's storage.
12
13
A permission owner is a non-Copy value. One explicit exclusive borrow of that
14
value backs one permission identity. A named permission region denotes that
15
identity. A permission controls access to all cells that have its identity.
16
It does not own or reclaim their storage.
17
18
A loan region is the lexical region of a payload reference and of the matching
19
borrow of the permission owner. Storage, permission, and loan regions are
20
separate regions.
21
22
An associated cell is a cell type that names a permission region. Every alias
23
of the cell has the same permission association.
24
25
TYPE AND CONSTRUCTION SYNTAX
26
============================
27
28
The associated cell type is:
29
30
    &'storage cell 'permission T
31
32
The first region is the storage region. The optional region after `cell` is
33
the permission region. Existing unassociated forms, such as `&cell T` and
34
`&'storage cell T`, keep their current meaning.
35
36
The associated construction form is:
37
38
    &cell 'permission place
39
40
`place` MUST be initialized and exclusively accessible when this expression
41
executes. Its storage region becomes the cell's storage region. The named
42
permission identity becomes the cell's permission association. Construction
43
MUST have the matching permission authority in scope.
44
45
An unassociated cell MAY provide value access when `T` is Copy. It MUST NOT
46
produce a shared or mutable reference to its payload.
47
48
PERMISSION AUTHORITY
49
====================
50
51
A lexical permission region MUST contain one explicit exclusive borrow of its
52
non-Copy owner. For example:
53
54
    let permission: 'permission = &mut owner in {
55
        // `permission` is the authority for `'permission`.
56
    }
57
58
A function that uses `'permission` in an associated cell type MUST also take
59
an explicit authority parameter of the form:
60
61
    permission: &'permission mut Owner
62
63
The function MUST use the same region in the cell type and the authority
64
parameter. Permission access is therefore an explicit call argument. The
65
language MUST NOT infer a transitive permission effect.
66
67
ACCESS AND LOANS
68
================
69
70
Direct Copy reads and whole-value writes through an associated cell require
71
available matching permission authority.
72
73
A payload reference MUST be created in an explicit lexical region block. The
74
same block MUST borrow the matching permission owner into the same loan
75
region.
76
77
For a shared payload reference, the block MUST borrow the permission owner
78
with `&`. Any number of shared payload references for that permission MAY be
79
live together. While one is live, mutation through every cell with that
80
permission MUST be rejected.
81
82
For a mutable payload reference, the block MUST borrow the permission owner
83
with `&mut`. While it is live, every other read, write, or payload borrow
84
through every cell with that permission MUST be rejected.
85
86
The payload reference MUST end at the lexical end of its loan region. It MUST
87
NOT escape the matching permission-owner borrow. It also MUST NOT outlive its
88
cell's storage region. Reclamation or reset of the storage region MUST remain
89
invalid while a payload reference into that storage is live. A payload loan
90
does not itself borrow the storage owner, so allocation that does not reclaim
91
or reset existing storage MAY continue.
92
93
Aliases, field extraction, assignment, substitution, and storage-region
94
shortening MUST preserve the permission association. If storage region
95
`'storage` is shortened to `'short`, the result is
96
`&'short cell 'permission T`; it is not relabeled to another permission.
97
Permission associations are invariant.
98
99
GENERICS AND CALLS
100
==================
101
102
A generic function MUST declare each permission region that occurs in an
103
associated cell parameter and MUST receive the matching explicit authority
104
parameter. Region inference and substitution MUST propagate the permission
105
association through cell types.
106
107
A call MUST use the same permission identity for an associated cell and its
108
authority argument. It MUST reject cross-permission access, relabeling, and
109
any call or callback that conflicts with a live loan. Two distinct permission
110
parameters in one signature MUST NOT map to the same actual permission
111
identity at a call.
112
113
After a payload reference is passed as an ordinary `&T` or `&mut T`, a helper
114
uses the ordinary reference rules. The helper does not need a cell permission
115
argument unless it also accesses an associated cell.
116
117
NON-COPY PAYLOADS
118
=================
119
120
An associated cell MAY contain a non-Copy payload only when that payload is
121
eligible for bulk discard under the existing ownership rules. Such a payload
122
MAY be borrowed by reference under the access and loan rules above.
123
124
An implicit payload value read MUST require Copy. A move of the payload or of
125
a payload field that would leave shared storage partly or wholly uninitialized
126
MUST be rejected.
127
128
Whole-value replacement is permitted. The operation MUST evaluate the new
129
value before it changes the cell. If evaluation fails, the cell MUST remain
130
unchanged. After successful evaluation, the operation MUST replace the old
131
value and bulk-discard the old value. It MUST NOT run a destructor.
132
133
Storage reclamation remains a bulk operation controlled only by the storage
134
region. Permission expiration does not reclaim storage. Freezing remains a
135
library-level state transition.
136
137
EXAMPLE
138
========
139
140
This example uses distinct storage, permission, and loan regions:
141
142
    record Permit {}
143
    record Item { value: u32 }
144
145
    fn addOne(item: &mut Item) {
146
        set item.value += 1;
147
    }
148
149
    fn update 'storage 'permission (
150
        item: &'storage cell 'permission Item,
151
        permission: &'permission mut Permit,
152
    ) {
153
        let authority: 'loan = &mut *permission,
154
            payload = &mut *item in {
155
            addOne(payload);
156
        }
157
    }
158
159
    fn example 'storage (storage: &Session 'storage) throws (AllocError) {
160
        let mut owner = Permit {};
161
        let permission: 'permission = &mut owner in {
162
            let place = try storage.new(Item { value: 0 });
163
            let item: &'storage cell 'permission Item =
164
                &cell 'permission *place;
165
            update(item, permission);
166
        }
167
    }
168
169
COMPILE-TIME AND RUNTIME CONTRACT
170
=================================
171
172
Permission identities, cell associations, and loan conflicts are compile-time
173
facts. They MUST NOT add garbage collection, runtime identity values, runtime
174
borrow checks, hidden arguments, hidden effects, or general alias analysis.
175
176
An associated cell MUST have the same pointer layout and runtime operations as
177
an unassociated cell. Permission metadata MUST NOT change emitted pointer
178
representation. This feature MUST NOT add destructors. Ordinary `&mut`,
179
`&cell`, field access, and `set` remain the access operations; the feature adds
180
no read or write block syntax.