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