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