]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/borrow_check/places_conflict.rs
Rollup merge of #69572 - matthiaskrgr:try_err_and_iter_on_ref, r=Centril
[rust.git] / src / librustc_mir / borrow_check / places_conflict.rs
1 use crate::borrow_check::ArtificialField;
2 use crate::borrow_check::Overlap;
3 use crate::borrow_check::{AccessDepth, Deep, Shallow};
4 use rustc::mir::{Body, BorrowKind, Local, Place, PlaceElem, PlaceRef, ProjectionElem};
5 use rustc::ty::{self, TyCtxt};
6 use rustc_hir as hir;
7 use std::cmp::max;
8
9 /// When checking if a place conflicts with another place, this enum is used to influence decisions
10 /// where a place might be equal or disjoint with another place, such as if `a[i] == a[j]`.
11 /// `PlaceConflictBias::Overlap` would bias toward assuming that `i` might equal `j` and that these
12 /// places overlap. `PlaceConflictBias::NoOverlap` assumes that for the purposes of the predicate
13 /// being run in the calling context, the conservative choice is to assume the compared indices
14 /// are disjoint (and therefore, do not overlap).
15 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
16 crate enum PlaceConflictBias {
17     Overlap,
18     NoOverlap,
19 }
20
21 /// Helper function for checking if places conflict with a mutable borrow and deep access depth.
22 /// This is used to check for places conflicting outside of the borrow checking code (such as in
23 /// dataflow).
24 crate fn places_conflict<'tcx>(
25     tcx: TyCtxt<'tcx>,
26     body: &Body<'tcx>,
27     borrow_place: &Place<'tcx>,
28     access_place: &Place<'tcx>,
29     bias: PlaceConflictBias,
30 ) -> bool {
31     borrow_conflicts_with_place(
32         tcx,
33         body,
34         borrow_place,
35         BorrowKind::Mut { allow_two_phase_borrow: true },
36         access_place.as_ref(),
37         AccessDepth::Deep,
38         bias,
39     )
40 }
41
42 /// Checks whether the `borrow_place` conflicts with the `access_place` given a borrow kind and
43 /// access depth. The `bias` parameter is used to determine how the unknowable (comparing runtime
44 /// array indices, for example) should be interpreted - this depends on what the caller wants in
45 /// order to make the conservative choice and preserve soundness.
46 pub(super) fn borrow_conflicts_with_place<'tcx>(
47     tcx: TyCtxt<'tcx>,
48     body: &Body<'tcx>,
49     borrow_place: &Place<'tcx>,
50     borrow_kind: BorrowKind,
51     access_place: PlaceRef<'_, 'tcx>,
52     access: AccessDepth,
53     bias: PlaceConflictBias,
54 ) -> bool {
55     debug!(
56         "borrow_conflicts_with_place({:?}, {:?}, {:?}, {:?})",
57         borrow_place, access_place, access, bias,
58     );
59
60     // This Local/Local case is handled by the more general code below, but
61     // it's so common that it's a speed win to check for it first.
62     if let Some(l1) = borrow_place.as_local() {
63         if let Some(l2) = access_place.as_local() {
64             return l1 == l2;
65         }
66     }
67
68     place_components_conflict(tcx, body, borrow_place, borrow_kind, access_place, access, bias)
69 }
70
71 fn place_components_conflict<'tcx>(
72     tcx: TyCtxt<'tcx>,
73     body: &Body<'tcx>,
74     borrow_place: &Place<'tcx>,
75     borrow_kind: BorrowKind,
76     access_place: PlaceRef<'_, 'tcx>,
77     access: AccessDepth,
78     bias: PlaceConflictBias,
79 ) -> bool {
80     // The borrowck rules for proving disjointness are applied from the "root" of the
81     // borrow forwards, iterating over "similar" projections in lockstep until
82     // we can prove overlap one way or another. Essentially, we treat `Overlap` as
83     // a monoid and report a conflict if the product ends up not being `Disjoint`.
84     //
85     // At each step, if we didn't run out of borrow or place, we know that our elements
86     // have the same type, and that they only overlap if they are the identical.
87     //
88     // For example, if we are comparing these:
89     // BORROW:  (*x1[2].y).z.a
90     // ACCESS:  (*x1[i].y).w.b
91     //
92     // Then our steps are:
93     //       x1         |   x1          -- places are the same
94     //       x1[2]      |   x1[i]       -- equal or disjoint (disjoint if indexes differ)
95     //       x1[2].y    |   x1[i].y     -- equal or disjoint
96     //      *x1[2].y    |  *x1[i].y     -- equal or disjoint
97     //     (*x1[2].y).z | (*x1[i].y).w  -- we are disjoint and don't need to check more!
98     //
99     // Because `zip` does potentially bad things to the iterator inside, this loop
100     // also handles the case where the access might be a *prefix* of the borrow, e.g.
101     //
102     // BORROW:  (*x1[2].y).z.a
103     // ACCESS:  x1[i].y
104     //
105     // Then our steps are:
106     //       x1         |   x1          -- places are the same
107     //       x1[2]      |   x1[i]       -- equal or disjoint (disjoint if indexes differ)
108     //       x1[2].y    |   x1[i].y     -- equal or disjoint
109     //
110     // -- here we run out of access - the borrow can access a part of it. If this
111     // is a full deep access, then we *know* the borrow conflicts with it. However,
112     // if the access is shallow, then we can proceed:
113     //
114     //       x1[2].y    | (*x1[i].y)    -- a deref! the access can't get past this, so we
115     //                                     are disjoint
116     //
117     // Our invariant is, that at each step of the iteration:
118     //  - If we didn't run out of access to match, our borrow and access are comparable
119     //    and either equal or disjoint.
120     //  - If we did run out of access, the borrow can access a part of it.
121
122     let borrow_local = borrow_place.local;
123     let access_local = access_place.local;
124
125     match place_base_conflict(borrow_local, access_local) {
126         Overlap::Arbitrary => {
127             bug!("Two base can't return Arbitrary");
128         }
129         Overlap::EqualOrDisjoint => {
130             // This is the recursive case - proceed to the next element.
131         }
132         Overlap::Disjoint => {
133             // We have proven the borrow disjoint - further
134             // projections will remain disjoint.
135             debug!("borrow_conflicts_with_place: disjoint");
136             return false;
137         }
138     }
139
140     // loop invariant: borrow_c is always either equal to access_c or disjoint from it.
141     for (i, (borrow_c, access_c)) in
142         borrow_place.projection.iter().zip(access_place.projection.iter()).enumerate()
143     {
144         debug!("borrow_conflicts_with_place: borrow_c = {:?}", borrow_c);
145         let borrow_proj_base = &borrow_place.projection[..i];
146
147         debug!("borrow_conflicts_with_place: access_c = {:?}", access_c);
148
149         // Borrow and access path both have more components.
150         //
151         // Examples:
152         //
153         // - borrow of `a.(...)`, access to `a.(...)`
154         // - borrow of `a.(...)`, access to `b.(...)`
155         //
156         // Here we only see the components we have checked so
157         // far (in our examples, just the first component). We
158         // check whether the components being borrowed vs
159         // accessed are disjoint (as in the second example,
160         // but not the first).
161         match place_projection_conflict(
162             tcx,
163             body,
164             borrow_local,
165             borrow_proj_base,
166             borrow_c,
167             access_c,
168             bias,
169         ) {
170             Overlap::Arbitrary => {
171                 // We have encountered different fields of potentially
172                 // the same union - the borrow now partially overlaps.
173                 //
174                 // There is no *easy* way of comparing the fields
175                 // further on, because they might have different types
176                 // (e.g., borrows of `u.a.0` and `u.b.y` where `.0` and
177                 // `.y` come from different structs).
178                 //
179                 // We could try to do some things here - e.g., count
180                 // dereferences - but that's probably not a good
181                 // idea, at least for now, so just give up and
182                 // report a conflict. This is unsafe code anyway so
183                 // the user could always use raw pointers.
184                 debug!("borrow_conflicts_with_place: arbitrary -> conflict");
185                 return true;
186             }
187             Overlap::EqualOrDisjoint => {
188                 // This is the recursive case - proceed to the next element.
189             }
190             Overlap::Disjoint => {
191                 // We have proven the borrow disjoint - further
192                 // projections will remain disjoint.
193                 debug!("borrow_conflicts_with_place: disjoint");
194                 return false;
195             }
196         }
197     }
198
199     if borrow_place.projection.len() > access_place.projection.len() {
200         for (i, elem) in borrow_place.projection[access_place.projection.len()..].iter().enumerate()
201         {
202             // Borrow path is longer than the access path. Examples:
203             //
204             // - borrow of `a.b.c`, access to `a.b`
205             //
206             // Here, we know that the borrow can access a part of
207             // our place. This is a conflict if that is a part our
208             // access cares about.
209
210             let proj_base = &borrow_place.projection[..access_place.projection.len() + i];
211             let base_ty = Place::ty_from(borrow_local, proj_base, body, tcx).ty;
212
213             match (elem, &base_ty.kind, access) {
214                 (_, _, Shallow(Some(ArtificialField::ArrayLength)))
215                 | (_, _, Shallow(Some(ArtificialField::ShallowBorrow))) => {
216                     // The array length is like  additional fields on the
217                     // type; it does not overlap any existing data there.
218                     // Furthermore, if cannot actually be a prefix of any
219                     // borrowed place (at least in MIR as it is currently.)
220                     //
221                     // e.g., a (mutable) borrow of `a[5]` while we read the
222                     // array length of `a`.
223                     debug!("borrow_conflicts_with_place: implicit field");
224                     return false;
225                 }
226
227                 (ProjectionElem::Deref, _, Shallow(None)) => {
228                     // e.g., a borrow of `*x.y` while we shallowly access `x.y` or some
229                     // prefix thereof - the shallow access can't touch anything behind
230                     // the pointer.
231                     debug!("borrow_conflicts_with_place: shallow access behind ptr");
232                     return false;
233                 }
234                 (ProjectionElem::Deref, ty::Ref(_, _, hir::Mutability::Not), _) => {
235                     // Shouldn't be tracked
236                     bug!("Tracking borrow behind shared reference.");
237                 }
238                 (ProjectionElem::Deref, ty::Ref(_, _, hir::Mutability::Mut), AccessDepth::Drop) => {
239                     // Values behind a mutable reference are not access either by dropping a
240                     // value, or by StorageDead
241                     debug!("borrow_conflicts_with_place: drop access behind ptr");
242                     return false;
243                 }
244
245                 (ProjectionElem::Field { .. }, ty::Adt(def, _), AccessDepth::Drop) => {
246                     // Drop can read/write arbitrary projections, so places
247                     // conflict regardless of further projections.
248                     if def.has_dtor(tcx) {
249                         return true;
250                     }
251                 }
252
253                 (ProjectionElem::Deref, _, Deep)
254                 | (ProjectionElem::Deref, _, AccessDepth::Drop)
255                 | (ProjectionElem::Field { .. }, _, _)
256                 | (ProjectionElem::Index { .. }, _, _)
257                 | (ProjectionElem::ConstantIndex { .. }, _, _)
258                 | (ProjectionElem::Subslice { .. }, _, _)
259                 | (ProjectionElem::Downcast { .. }, _, _) => {
260                     // Recursive case. This can still be disjoint on a
261                     // further iteration if this a shallow access and
262                     // there's a deref later on, e.g., a borrow
263                     // of `*x.y` while accessing `x`.
264                 }
265             }
266         }
267     }
268
269     // Borrow path ran out but access path may not
270     // have. Examples:
271     //
272     // - borrow of `a.b`, access to `a.b.c`
273     // - borrow of `a.b`, access to `a.b`
274     //
275     // In the first example, where we didn't run out of
276     // access, the borrow can access all of our place, so we
277     // have a conflict.
278     //
279     // If the second example, where we did, then we still know
280     // that the borrow can access a *part* of our place that
281     // our access cares about, so we still have a conflict.
282     if borrow_kind == BorrowKind::Shallow
283         && borrow_place.projection.len() < access_place.projection.len()
284     {
285         debug!("borrow_conflicts_with_place: shallow borrow");
286         false
287     } else {
288         debug!("borrow_conflicts_with_place: full borrow, CONFLICT");
289         true
290     }
291 }
292
293 // Given that the bases of `elem1` and `elem2` are always either equal
294 // or disjoint (and have the same type!), return the overlap situation
295 // between `elem1` and `elem2`.
296 fn place_base_conflict(l1: Local, l2: Local) -> Overlap {
297     if l1 == l2 {
298         // the same local - base case, equal
299         debug!("place_element_conflict: DISJOINT-OR-EQ-LOCAL");
300         Overlap::EqualOrDisjoint
301     } else {
302         // different locals - base case, disjoint
303         debug!("place_element_conflict: DISJOINT-LOCAL");
304         Overlap::Disjoint
305     }
306 }
307
308 // Given that the bases of `elem1` and `elem2` are always either equal
309 // or disjoint (and have the same type!), return the overlap situation
310 // between `elem1` and `elem2`.
311 fn place_projection_conflict<'tcx>(
312     tcx: TyCtxt<'tcx>,
313     body: &Body<'tcx>,
314     pi1_local: Local,
315     pi1_proj_base: &[PlaceElem<'tcx>],
316     pi1_elem: &PlaceElem<'tcx>,
317     pi2_elem: &PlaceElem<'tcx>,
318     bias: PlaceConflictBias,
319 ) -> Overlap {
320     match (pi1_elem, pi2_elem) {
321         (ProjectionElem::Deref, ProjectionElem::Deref) => {
322             // derefs (e.g., `*x` vs. `*x`) - recur.
323             debug!("place_element_conflict: DISJOINT-OR-EQ-DEREF");
324             Overlap::EqualOrDisjoint
325         }
326         (ProjectionElem::Field(f1, _), ProjectionElem::Field(f2, _)) => {
327             if f1 == f2 {
328                 // same field (e.g., `a.y` vs. `a.y`) - recur.
329                 debug!("place_element_conflict: DISJOINT-OR-EQ-FIELD");
330                 Overlap::EqualOrDisjoint
331             } else {
332                 let ty = Place::ty_from(pi1_local, pi1_proj_base, body, tcx).ty;
333                 match ty.kind {
334                     ty::Adt(def, _) if def.is_union() => {
335                         // Different fields of a union, we are basically stuck.
336                         debug!("place_element_conflict: STUCK-UNION");
337                         Overlap::Arbitrary
338                     }
339                     _ => {
340                         // Different fields of a struct (`a.x` vs. `a.y`). Disjoint!
341                         debug!("place_element_conflict: DISJOINT-FIELD");
342                         Overlap::Disjoint
343                     }
344                 }
345             }
346         }
347         (ProjectionElem::Downcast(_, v1), ProjectionElem::Downcast(_, v2)) => {
348             // different variants are treated as having disjoint fields,
349             // even if they occupy the same "space", because it's
350             // impossible for 2 variants of the same enum to exist
351             // (and therefore, to be borrowed) at the same time.
352             //
353             // Note that this is different from unions - we *do* allow
354             // this code to compile:
355             //
356             // ```
357             // fn foo(x: &mut Result<i32, i32>) {
358             //     let mut v = None;
359             //     if let Ok(ref mut a) = *x {
360             //         v = Some(a);
361             //     }
362             //     // here, you would *think* that the
363             //     // *entirety* of `x` would be borrowed,
364             //     // but in fact only the `Ok` variant is,
365             //     // so the `Err` variant is *entirely free*:
366             //     if let Err(ref mut a) = *x {
367             //         v = Some(a);
368             //     }
369             //     drop(v);
370             // }
371             // ```
372             if v1 == v2 {
373                 debug!("place_element_conflict: DISJOINT-OR-EQ-FIELD");
374                 Overlap::EqualOrDisjoint
375             } else {
376                 debug!("place_element_conflict: DISJOINT-FIELD");
377                 Overlap::Disjoint
378             }
379         }
380         (ProjectionElem::Index(..), ProjectionElem::Index(..))
381         | (ProjectionElem::Index(..), ProjectionElem::ConstantIndex { .. })
382         | (ProjectionElem::Index(..), ProjectionElem::Subslice { .. })
383         | (ProjectionElem::ConstantIndex { .. }, ProjectionElem::Index(..))
384         | (ProjectionElem::Subslice { .. }, ProjectionElem::Index(..)) => {
385             // Array indexes (`a[0]` vs. `a[i]`). These can either be disjoint
386             // (if the indexes differ) or equal (if they are the same).
387             match bias {
388                 PlaceConflictBias::Overlap => {
389                     // If we are biased towards overlapping, then this is the recursive
390                     // case that gives "equal *or* disjoint" its meaning.
391                     debug!("place_element_conflict: DISJOINT-OR-EQ-ARRAY-INDEX");
392                     Overlap::EqualOrDisjoint
393                 }
394                 PlaceConflictBias::NoOverlap => {
395                     // If we are biased towards no overlapping, then this is disjoint.
396                     debug!("place_element_conflict: DISJOINT-ARRAY-INDEX");
397                     Overlap::Disjoint
398                 }
399             }
400         }
401         (
402             ProjectionElem::ConstantIndex { offset: o1, min_length: _, from_end: false },
403             ProjectionElem::ConstantIndex { offset: o2, min_length: _, from_end: false },
404         )
405         | (
406             ProjectionElem::ConstantIndex { offset: o1, min_length: _, from_end: true },
407             ProjectionElem::ConstantIndex { offset: o2, min_length: _, from_end: true },
408         ) => {
409             if o1 == o2 {
410                 debug!("place_element_conflict: DISJOINT-OR-EQ-ARRAY-CONSTANT-INDEX");
411                 Overlap::EqualOrDisjoint
412             } else {
413                 debug!("place_element_conflict: DISJOINT-ARRAY-CONSTANT-INDEX");
414                 Overlap::Disjoint
415             }
416         }
417         (
418             ProjectionElem::ConstantIndex {
419                 offset: offset_from_begin,
420                 min_length: min_length1,
421                 from_end: false,
422             },
423             ProjectionElem::ConstantIndex {
424                 offset: offset_from_end,
425                 min_length: min_length2,
426                 from_end: true,
427             },
428         )
429         | (
430             ProjectionElem::ConstantIndex {
431                 offset: offset_from_end,
432                 min_length: min_length1,
433                 from_end: true,
434             },
435             ProjectionElem::ConstantIndex {
436                 offset: offset_from_begin,
437                 min_length: min_length2,
438                 from_end: false,
439             },
440         ) => {
441             // both patterns matched so it must be at least the greater of the two
442             let min_length = max(min_length1, min_length2);
443             // `offset_from_end` can be in range `[1..min_length]`, 1 indicates the last
444             // element (like -1 in Python) and `min_length` the first.
445             // Therefore, `min_length - offset_from_end` gives the minimal possible
446             // offset from the beginning
447             if *offset_from_begin >= *min_length - *offset_from_end {
448                 debug!("place_element_conflict: DISJOINT-OR-EQ-ARRAY-CONSTANT-INDEX-FE");
449                 Overlap::EqualOrDisjoint
450             } else {
451                 debug!("place_element_conflict: DISJOINT-ARRAY-CONSTANT-INDEX-FE");
452                 Overlap::Disjoint
453             }
454         }
455         (
456             ProjectionElem::ConstantIndex { offset, min_length: _, from_end: false },
457             ProjectionElem::Subslice { from, to, from_end: false },
458         )
459         | (
460             ProjectionElem::Subslice { from, to, from_end: false },
461             ProjectionElem::ConstantIndex { offset, min_length: _, from_end: false },
462         ) => {
463             if (from..to).contains(&offset) {
464                 debug!("place_element_conflict: DISJOINT-OR-EQ-ARRAY-CONSTANT-INDEX-SUBSLICE");
465                 Overlap::EqualOrDisjoint
466             } else {
467                 debug!("place_element_conflict: DISJOINT-ARRAY-CONSTANT-INDEX-SUBSLICE");
468                 Overlap::Disjoint
469             }
470         }
471         (
472             ProjectionElem::ConstantIndex { offset, min_length: _, from_end: false },
473             ProjectionElem::Subslice { from, .. },
474         )
475         | (
476             ProjectionElem::Subslice { from, .. },
477             ProjectionElem::ConstantIndex { offset, min_length: _, from_end: false },
478         ) => {
479             if offset >= from {
480                 debug!("place_element_conflict: DISJOINT-OR-EQ-SLICE-CONSTANT-INDEX-SUBSLICE");
481                 Overlap::EqualOrDisjoint
482             } else {
483                 debug!("place_element_conflict: DISJOINT-SLICE-CONSTANT-INDEX-SUBSLICE");
484                 Overlap::Disjoint
485             }
486         }
487         (
488             ProjectionElem::ConstantIndex { offset, min_length: _, from_end: true },
489             ProjectionElem::Subslice { to, from_end: true, .. },
490         )
491         | (
492             ProjectionElem::Subslice { to, from_end: true, .. },
493             ProjectionElem::ConstantIndex { offset, min_length: _, from_end: true },
494         ) => {
495             if offset > to {
496                 debug!(
497                     "place_element_conflict: \
498                        DISJOINT-OR-EQ-SLICE-CONSTANT-INDEX-SUBSLICE-FE"
499                 );
500                 Overlap::EqualOrDisjoint
501             } else {
502                 debug!("place_element_conflict: DISJOINT-SLICE-CONSTANT-INDEX-SUBSLICE-FE");
503                 Overlap::Disjoint
504             }
505         }
506         (
507             ProjectionElem::Subslice { from: f1, to: t1, from_end: false },
508             ProjectionElem::Subslice { from: f2, to: t2, from_end: false },
509         ) => {
510             if f2 >= t1 || f1 >= t2 {
511                 debug!("place_element_conflict: DISJOINT-ARRAY-SUBSLICES");
512                 Overlap::Disjoint
513             } else {
514                 debug!("place_element_conflict: DISJOINT-OR-EQ-ARRAY-SUBSLICES");
515                 Overlap::EqualOrDisjoint
516             }
517         }
518         (ProjectionElem::Subslice { .. }, ProjectionElem::Subslice { .. }) => {
519             debug!("place_element_conflict: DISJOINT-OR-EQ-SLICE-SUBSLICES");
520             Overlap::EqualOrDisjoint
521         }
522         (ProjectionElem::Deref, _)
523         | (ProjectionElem::Field(..), _)
524         | (ProjectionElem::Index(..), _)
525         | (ProjectionElem::ConstantIndex { .. }, _)
526         | (ProjectionElem::Subslice { .. }, _)
527         | (ProjectionElem::Downcast(..), _) => bug!(
528             "mismatched projections in place_element_conflict: {:?} and {:?}",
529             pi1_elem,
530             pi2_elem
531         ),
532     }
533 }