1 use crate::borrow_check::ArtificialField;
2 use crate::borrow_check::Overlap;
3 use crate::borrow_check::{Deep, Shallow, AccessDepth};
6 Body, BorrowKind, Place, PlaceBase, PlaceRef, Projection, ProjectionElem, ProjectionsIter,
9 use rustc::ty::{self, TyCtxt};
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 {
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
27 crate fn places_conflict<'tcx>(
29 param_env: ty::ParamEnv<'tcx>,
31 borrow_place: &Place<'tcx>,
32 access_place: &Place<'tcx>,
33 bias: PlaceConflictBias,
35 borrow_conflicts_with_place(
40 BorrowKind::Mut { allow_two_phase_borrow: true },
41 access_place.as_ref(),
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>(
53 param_env: ty::ParamEnv<'tcx>,
55 borrow_place: &Place<'tcx>,
56 borrow_kind: BorrowKind,
57 access_place: PlaceRef<'_, 'tcx>,
59 bias: PlaceConflictBias,
62 "borrow_conflicts_with_place({:?}, {:?}, {:?}, {:?})",
63 borrow_place, access_place, access, bias,
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.
69 base: PlaceBase::Local(l1),
73 base: PlaceBase::Local(l2),
80 borrow_place.iterate(|borrow_base, borrow_projections| {
81 access_place.iterate(|access_base, access_projections| {
82 place_components_conflict(
86 (borrow_base, borrow_projections),
88 (access_base, access_projections),
96 fn place_components_conflict<'tcx>(
98 param_env: ty::ParamEnv<'tcx>,
100 borrow_projections: (&PlaceBase<'tcx>, ProjectionsIter<'_, 'tcx>),
101 borrow_kind: BorrowKind,
102 access_projections: (&PlaceBase<'tcx>, ProjectionsIter<'_, 'tcx>),
104 bias: PlaceConflictBias,
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`.
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.
114 // For example, if we are comparing these:
115 // BORROW: (*x1[2].y).z.a
116 // ACCESS: (*x1[i].y).w.b
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!
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.
128 // BORROW: (*x1[2].y).z.a
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
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:
140 // x1[2].y | (*x1[i].y) -- a deref! the access can't get past this, so we
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.
148 let borrow_base = borrow_projections.0;
149 let access_base = access_projections.0;
151 match place_base_conflict(tcx, param_env, borrow_base, access_base) {
152 Overlap::Arbitrary => {
153 bug!("Two base can't return Arbitrary");
155 Overlap::EqualOrDisjoint => {
156 // This is the recursive case - proceed to the next element.
158 Overlap::Disjoint => {
159 // We have proven the borrow disjoint - further
160 // projections will remain disjoint.
161 debug!("borrow_conflicts_with_place: disjoint");
166 let mut borrow_projections = borrow_projections.1;
167 let mut access_projections = access_projections.1;
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);
174 if let Some(access_c) = access_projections.next() {
175 debug!("borrow_conflicts_with_place: access_c = {:?}", access_c);
177 // Borrow and access path both have more components.
181 // - borrow of `a.(...)`, access to `a.(...)`
182 // - borrow of `a.(...)`, access to `b.(...)`
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.
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).
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");
207 Overlap::EqualOrDisjoint => {
208 // This is the recursive case - proceed to the next element.
210 Overlap::Disjoint => {
211 // We have proven the borrow disjoint - further
212 // projections will remain disjoint.
213 debug!("borrow_conflicts_with_place: disjoint");
218 // Borrow path is longer than the access path. Examples:
220 // - borrow of `a.b.c`, access to `a.b`
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.
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;
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.)
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");
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
248 debug!("borrow_conflicts_with_place: shallow access behind ptr");
251 (ProjectionElem::Deref, ty::Ref(_, _, hir::MutImmutable), _) => {
252 // Shouldn't be tracked
253 bug!("Tracking borrow behind shared reference.");
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");
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) {
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`.
285 // Borrow path ran out but access path may not
288 // - borrow of `a.b`, access to `a.b.c`
289 // - borrow of `a.b`, access to `a.b`
291 // In the first example, where we didn't run out of
292 // access, the borrow can access all of our place, so we
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");
302 debug!("borrow_conflicts_with_place: full borrow, CONFLICT");
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>(
314 param_env: ty::ParamEnv<'tcx>,
315 elem1: &PlaceBase<'tcx>,
316 elem2: &PlaceBase<'tcx>,
318 match (elem1, elem2) {
319 (PlaceBase::Local(l1), PlaceBase::Local(l2)) => {
321 // the same local - base case, equal
322 debug!("place_element_conflict: DISJOINT-OR-EQ-LOCAL");
323 Overlap::EqualOrDisjoint
325 // different locals - base case, disjoint
326 debug!("place_element_conflict: DISJOINT-LOCAL");
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");
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");
341 debug!("place_element_conflict: DISJOINT-OR-EQ-STATIC");
342 Overlap::EqualOrDisjoint
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;
354 // the same promoted - base case, equal
355 debug!("place_element_conflict: DISJOINT-OR-EQ-PROMOTED");
356 Overlap::EqualOrDisjoint
358 // different promoteds - base case, disjoint
359 debug!("place_element_conflict: DISJOINT-PROMOTED");
364 debug!("place_element_conflict: DISJOINT-STATIC-PROMOTED");
369 (PlaceBase::Local(_), PlaceBase::Static(_)) |
370 (PlaceBase::Static(_), PlaceBase::Local(_)) => {
371 debug!("place_element_conflict: DISJOINT-STATIC-LOCAL-PROMOTED");
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>(
383 pi1_base: &PlaceBase<'tcx>,
384 pi1: &Projection<'tcx>,
385 pi2: &Projection<'tcx>,
386 bias: PlaceConflictBias,
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
394 (ProjectionElem::Field(f1, _), ProjectionElem::Field(f2, _)) => {
396 // same field (e.g., `a.y` vs. `a.y`) - recur.
397 debug!("place_element_conflict: DISJOINT-OR-EQ-FIELD");
398 Overlap::EqualOrDisjoint
400 let ty = Place::ty_from(pi1_base, &pi1.base, body, tcx).ty;
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");
408 // Different fields of a struct (`a.x` vs. `a.y`). Disjoint!
409 debug!("place_element_conflict: DISJOINT-FIELD");
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.
421 // Note that this is different from unions - we *do* allow
422 // this code to compile:
425 // fn foo(x: &mut Result<i32, i32>) {
427 // if let Ok(ref mut a) = *x {
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 {
441 debug!("place_element_conflict: DISJOINT-OR-EQ-FIELD");
442 Overlap::EqualOrDisjoint
444 debug!("place_element_conflict: DISJOINT-FIELD");
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).
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
462 PlaceConflictBias::NoOverlap => {
463 // If we are biased towards no overlapping, then this is disjoint.
464 debug!("place_element_conflict: DISJOINT-ARRAY-INDEX");
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 }) => {
475 debug!("place_element_conflict: DISJOINT-OR-EQ-ARRAY-CONSTANT-INDEX");
476 Overlap::EqualOrDisjoint
478 debug!("place_element_conflict: DISJOINT-ARRAY-CONSTANT-INDEX");
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
500 debug!("place_element_conflict: DISJOINT-ARRAY-CONSTANT-INDEX-FE");
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 }) => {
510 "place_element_conflict: DISJOINT-OR-EQ-ARRAY-CONSTANT-INDEX-SUBSLICE");
511 Overlap::EqualOrDisjoint
513 debug!("place_element_conflict: DISJOINT-ARRAY-CONSTANT-INDEX-SUBSLICE");
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 }) => {
522 debug!("place_element_conflict: \
523 DISJOINT-OR-EQ-ARRAY-CONSTANT-INDEX-SUBSLICE-FE");
524 Overlap::EqualOrDisjoint
526 debug!("place_element_conflict: DISJOINT-ARRAY-CONSTANT-INDEX-SUBSLICE-FE");
530 (ProjectionElem::Subslice { .. }, ProjectionElem::Subslice { .. }) => {
531 debug!("place_element_conflict: DISJOINT-OR-EQ-ARRAY-SUBSLICES");
532 Overlap::EqualOrDisjoint
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 {:?}",