]> git.proxmox.com Git - rustc.git/blob - src/librustc/middle/infer/region_inference/README.md
Imported Upstream version 1.3.0+dfsg1
[rustc.git] / src / librustc / middle / infer / region_inference / README.md
1 Region inference
2
3 # Terminology
4
5 Note that we use the terms region and lifetime interchangeably,
6 though the term `lifetime` is preferred.
7
8 # Introduction
9
10 Region inference uses a somewhat more involved algorithm than type
11 inference. It is not the most efficient thing ever written though it
12 seems to work well enough in practice (famous last words). The reason
13 that we use a different algorithm is because, unlike with types, it is
14 impractical to hand-annotate with regions (in some cases, there aren't
15 even the requisite syntactic forms). So we have to get it right, and
16 it's worth spending more time on a more involved analysis. Moreover,
17 regions are a simpler case than types: they don't have aggregate
18 structure, for example.
19
20 Unlike normal type inference, which is similar in spirit to H-M and thus
21 works progressively, the region type inference works by accumulating
22 constraints over the course of a function. Finally, at the end of
23 processing a function, we process and solve the constraints all at
24 once.
25
26 The constraints are always of one of three possible forms:
27
28 - ConstrainVarSubVar(R_i, R_j) states that region variable R_i
29 must be a subregion of R_j
30 - ConstrainRegSubVar(R, R_i) states that the concrete region R
31 (which must not be a variable) must be a subregion of the variable R_i
32 - ConstrainVarSubReg(R_i, R) is the inverse
33
34 # Building up the constraints
35
36 Variables and constraints are created using the following methods:
37
38 - `new_region_var()` creates a new, unconstrained region variable;
39 - `make_subregion(R_i, R_j)` states that R_i is a subregion of R_j
40 - `lub_regions(R_i, R_j) -> R_k` returns a region R_k which is
41 the smallest region that is greater than both R_i and R_j
42 - `glb_regions(R_i, R_j) -> R_k` returns a region R_k which is
43 the greatest region that is smaller than both R_i and R_j
44
45 The actual region resolution algorithm is not entirely
46 obvious, though it is also not overly complex.
47
48 ## Snapshotting
49
50 It is also permitted to try (and rollback) changes to the graph. This
51 is done by invoking `start_snapshot()`, which returns a value. Then
52 later you can call `rollback_to()` which undoes the work.
53 Alternatively, you can call `commit()` which ends all snapshots.
54 Snapshots can be recursive---so you can start a snapshot when another
55 is in progress, but only the root snapshot can "commit".
56
57 # Resolving constraints
58
59 The constraint resolution algorithm is not super complex but also not
60 entirely obvious. Here I describe the problem somewhat abstractly,
61 then describe how the current code works. There may be other, smarter
62 ways of doing this with which I am unfamiliar and can't be bothered to
63 research at the moment. - NDM
64
65 ## The problem
66
67 Basically our input is a directed graph where nodes can be divided
68 into two categories: region variables and concrete regions. Each edge
69 `R -> S` in the graph represents a constraint that the region `R` is a
70 subregion of the region `S`.
71
72 Region variable nodes can have arbitrary degree. There is one region
73 variable node per region variable.
74
75 Each concrete region node is associated with some, well, concrete
76 region: e.g., a free lifetime, or the region for a particular scope.
77 Note that there may be more than one concrete region node for a
78 particular region value. Moreover, because of how the graph is built,
79 we know that all concrete region nodes have either in-degree 1 or
80 out-degree 1.
81
82 Before resolution begins, we build up the constraints in a hashmap
83 that maps `Constraint` keys to spans. During resolution, we construct
84 the actual `Graph` structure that we describe here.
85
86 ## Our current algorithm
87
88 We divide region variables into two groups: Expanding and Contracting.
89 Expanding region variables are those that have a concrete region
90 predecessor (direct or indirect). Contracting region variables are
91 all others.
92
93 We first resolve the values of Expanding region variables and then
94 process Contracting ones. We currently use an iterative, fixed-point
95 procedure (but read on, I believe this could be replaced with a linear
96 walk). Basically we iterate over the edges in the graph, ensuring
97 that, if the source of the edge has a value, then this value is a
98 subregion of the target value. If the target does not yet have a
99 value, it takes the value from the source. If the target already had
100 a value, then the resulting value is Least Upper Bound of the old and
101 new values. When we are done, each Expanding node will have the
102 smallest region that it could possibly have and still satisfy the
103 constraints.
104
105 We next process the Contracting nodes. Here we again iterate over the
106 edges, only this time we move values from target to source (if the
107 source is a Contracting node). For each contracting node, we compute
108 its value as the GLB of all its successors. Basically contracting
109 nodes ensure that there is overlap between their successors; we will
110 ultimately infer the largest overlap possible.
111
112 # The Region Hierarchy
113
114 ## Without closures
115
116 Let's first consider the region hierarchy without thinking about
117 closures, because they add a lot of complications. The region
118 hierarchy *basically* mirrors the lexical structure of the code.
119 There is a region for every piece of 'evaluation' that occurs, meaning
120 every expression, block, and pattern (patterns are considered to
121 "execute" by testing the value they are applied to and creating any
122 relevant bindings). So, for example:
123
124 fn foo(x: isize, y: isize) { // -+
125 // +------------+ // |
126 // | +-----+ // |
127 // | +-+ +-+ +-+ // |
128 // | | | | | | | // |
129 // v v v v v v v // |
130 let z = x + y; // |
131 ... // |
132 } // -+
133
134 fn bar() { ... }
135
136 In this example, there is a region for the fn body block as a whole,
137 and then a subregion for the declaration of the local variable.
138 Within that, there are sublifetimes for the assignment pattern and
139 also the expression `x + y`. The expression itself has sublifetimes
140 for evaluating `x` and `y`.
141
142 ## Function calls
143
144 Function calls are a bit tricky. I will describe how we handle them
145 *now* and then a bit about how we can improve them (Issue #6268).
146
147 Consider a function call like `func(expr1, expr2)`, where `func`,
148 `arg1`, and `arg2` are all arbitrary expressions. Currently,
149 we construct a region hierarchy like:
150
151 +----------------+
152 | |
153 +--+ +---+ +---+|
154 v v v v v vv
155 func(expr1, expr2)
156
157 Here you can see that the call as a whole has a region and the
158 function plus arguments are subregions of that. As a side-effect of
159 this, we get a lot of spurious errors around nested calls, in
160 particular when combined with `&mut` functions. For example, a call
161 like this one
162
163 self.foo(self.bar())
164
165 where both `foo` and `bar` are `&mut self` functions will always yield
166 an error.
167
168 Here is a more involved example (which is safe) so we can see what's
169 going on:
170
171 struct Foo { f: usize, g: usize }
172 ...
173 fn add(p: &mut usize, v: usize) {
174 *p += v;
175 }
176 ...
177 fn inc(p: &mut usize) -> usize {
178 *p += 1; *p
179 }
180 fn weird() {
181 let mut x: Box<Foo> = box Foo { ... };
182 'a: add(&mut (*x).f,
183 'b: inc(&mut (*x).f)) // (..)
184 }
185
186 The important part is the line marked `(..)` which contains a call to
187 `add()`. The first argument is a mutable borrow of the field `f`. The
188 second argument also borrows the field `f`. Now, in the current borrow
189 checker, the first borrow is given the lifetime of the call to
190 `add()`, `'a`. The second borrow is given the lifetime of `'b` of the
191 call to `inc()`. Because `'b` is considered to be a sublifetime of
192 `'a`, an error is reported since there are two co-existing mutable
193 borrows of the same data.
194
195 However, if we were to examine the lifetimes a bit more carefully, we
196 can see that this error is unnecessary. Let's examine the lifetimes
197 involved with `'a` in detail. We'll break apart all the steps involved
198 in a call expression:
199
200 'a: {
201 'a_arg1: let a_temp1: ... = add;
202 'a_arg2: let a_temp2: &'a mut usize = &'a mut (*x).f;
203 'a_arg3: let a_temp3: usize = {
204 let b_temp1: ... = inc;
205 let b_temp2: &'b = &'b mut (*x).f;
206 'b_call: b_temp1(b_temp2)
207 };
208 'a_call: a_temp1(a_temp2, a_temp3) // (**)
209 }
210
211 Here we see that the lifetime `'a` includes a number of substatements.
212 In particular, there is this lifetime I've called `'a_call` that
213 corresponds to the *actual execution of the function `add()`*, after
214 all arguments have been evaluated. There is a corresponding lifetime
215 `'b_call` for the execution of `inc()`. If we wanted to be precise
216 about it, the lifetime of the two borrows should be `'a_call` and
217 `'b_call` respectively, since the references that were created
218 will not be dereferenced except during the execution itself.
219
220 However, this model by itself is not sound. The reason is that
221 while the two references that are created will never be used
222 simultaneously, it is still true that the first reference is
223 *created* before the second argument is evaluated, and so even though
224 it will not be *dereferenced* during the evaluation of the second
225 argument, it can still be *invalidated* by that evaluation. Consider
226 this similar but unsound example:
227
228 struct Foo { f: usize, g: usize }
229 ...
230 fn add(p: &mut usize, v: usize) {
231 *p += v;
232 }
233 ...
234 fn consume(x: Box<Foo>) -> usize {
235 x.f + x.g
236 }
237 fn weird() {
238 let mut x: Box<Foo> = box Foo { ... };
239 'a: add(&mut (*x).f, consume(x)) // (..)
240 }
241
242 In this case, the second argument to `add` actually consumes `x`, thus
243 invalidating the first argument.
244
245 So, for now, we exclude the `call` lifetimes from our model.
246 Eventually I would like to include them, but we will have to make the
247 borrow checker handle this situation correctly. In particular, if
248 there is a reference created whose lifetime does not enclose
249 the borrow expression, we must issue sufficient restrictions to ensure
250 that the pointee remains valid.
251
252 ## Modeling closures
253
254 Integrating closures properly into the model is a bit of
255 work-in-progress. In an ideal world, we would model closures as
256 closely as possible after their desugared equivalents. That is, a
257 closure type would be modeled as a struct, and the region hierarchy of
258 different closure bodies would be completely distinct from all other
259 fns. We are generally moving in that direction but there are
260 complications in terms of the implementation.
261
262 In practice what we currently do is somewhat different. The basis for
263 the current approach is the observation that the only time that
264 regions from distinct fn bodies interact with one another is through
265 an upvar or the type of a fn parameter (since closures live in the fn
266 body namespace, they can in fact have fn parameters whose types
267 include regions from the surrounding fn body). For these cases, there
268 are separate mechanisms which ensure that the regions that appear in
269 upvars/parameters outlive the dynamic extent of each call to the
270 closure:
271
272 1. Types must outlive the region of any expression where they are used.
273 For a closure type `C` to outlive a region `'r`, that implies that the
274 types of all its upvars must outlive `'r`.
275 2. Parameters must outlive the region of any fn that they are passed to.
276
277 Therefore, we can -- sort of -- assume that any region from an
278 enclosing fns is larger than any region from one of its enclosed
279 fn. And that is precisely what we do: when building the region
280 hierarchy, each region lives in its own distinct subtree, but if we
281 are asked to compute the `LUB(r1, r2)` of two regions, and those
282 regions are in disjoint subtrees, we compare the lexical nesting of
283 the two regions.
284
285 *Ideas for improving the situation:* (FIXME #3696) The correctness
286 argument here is subtle and a bit hand-wavy. The ideal, as stated
287 earlier, would be to model things in such a way that it corresponds
288 more closely to the desugared code. The best approach for doing this
289 is a bit unclear: it may in fact be possible to *actually* desugar
290 before we start, but I don't think so. The main option that I've been
291 thinking through is imposing a "view shift" as we enter the fn body,
292 so that regions appearing in the types of fn parameters and upvars are
293 translated from being regions in the outer fn into free region
294 parameters, just as they would be if we applied the desugaring. The
295 challenge here is that type inference may not have fully run, so the
296 types may not be fully known: we could probably do this translation
297 lazilly, as type variables are instantiated. We would also have to
298 apply a kind of inverse translation to the return value. This would be
299 a good idea anyway, as right now it is possible for free regions
300 instantiated within the closure to leak into the parent: this
301 currently leads to type errors, since those regions cannot outlive any
302 expressions within the parent hierarchy. Much like the current
303 handling of closures, there are no known cases where this leads to a
304 type-checking accepting incorrect code (though it sometimes rejects
305 what might be considered correct code; see rust-lang/rust#22557), but
306 it still doesn't feel like the right approach.
307
308 ### Skolemization
309
310 For a discussion on skolemization and higher-ranked subtyping, please
311 see the module `middle::infer::higher_ranked::doc`.