Skip to content

Commit dfea230

Browse files
authored
Rollup merge of #160902 - dianne:preserve-shallow, r=oli-obk
borrowck dataflow: respect borrow shallowness when killing borrows that conflict with assignments It's not a conflict to write to a sub-place of a shallowly borrowed place, so it's unsound to kill shallow borrows when we encounter assignments to their sub-places. Fixes #160599 on the default borrow checker and under `-Zpolonius=next`. I haven't touched `-Zpolonius=legacy` since I'm not sure how much we're maintaining it, but at a glance the fix looks like it would be the same.
2 parents 8e73e2e + 4952963 commit dfea230

3 files changed

Lines changed: 59 additions & 4 deletions

File tree

‎compiler/rustc_borrowck/src/dataflow.rs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use rustc_mir_dataflow::impls::{
1010
use rustc_mir_dataflow::{Analysis, GenKill, JoinSemiLattice};
1111
use tracing::debug;
1212

13-
use crate::{BorrowSet, PlaceConflictBias, PlaceExt, RegionInferenceContext, places_conflict};
13+
use crate::{
14+
AccessDepth, BorrowSet, PlaceConflictBias, PlaceExt, RegionInferenceContext, places_conflict,
15+
};
1416

1517
// This analysis is different to most others. Its results aren't computed with
1618
// `iterate_to_fixpoint`, but are instead composed from the results of three sub-analyses that are
@@ -448,7 +450,7 @@ impl<'a, 'tcx> Borrows<'a, 'tcx> {
448450

449451
// If the borrowed place is a local with no projections, all other borrows of this
450452
// local must conflict. This is purely an optimization so we don't have to call
451-
// `places_conflict` for every borrow.
453+
// `places_conflict::borrow_conflicts_with_place` for every borrow.
452454
if place.projection.is_empty() {
453455
if !self.body.local_decls[place.local].is_ref_to_static() {
454456
state.kill_all(other_borrows_of_local);
@@ -461,11 +463,13 @@ impl<'a, 'tcx> Borrows<'a, 'tcx> {
461463
// will be assured that two places being compared definitely denotes the same sets of
462464
// locations.
463465
let definitely_conflicting_borrows = other_borrows_of_local.filter(|&i| {
464-
places_conflict(
466+
places_conflict::borrow_conflicts_with_place(
465467
self.tcx,
466468
self.body,
467469
self.borrow_set[i].borrowed_place,
468-
place,
470+
self.borrow_set[i].kind,
471+
place.as_ref(),
472+
AccessDepth::Deep,
469473
PlaceConflictBias::NoOverlap,
470474
)
471475
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//! Regression test for <https://github.com/rust-lang/rust/issues/160599>. On assignment statements,
2+
//! borrowck's dataflow analysis kills borrows that it knows conflict with the assigment: a borrow
3+
//! of a place can't be live anymore after that place is assigned over. Previously, this didn't
4+
//! account for fake borrows being shallow: it would kill any shallow borrows that would have
5+
//! conflicted if they were normal borrows. This made it possible to circumvent fake borrows for
6+
//! match guards and indexing expressions.
7+
8+
fn test_match_guard() {
9+
let mut a = (Some(&42u64), 0u8);
10+
let mut b = (None::<&u64>, 0u8);
11+
let mut p = &mut a;
12+
// Writing to `(*p).1` in the match guard previously killed the fake borrow of `p` in the guard,
13+
// making it possible to mutate `p` despite `(*p).0` being matched on. This would reach the
14+
// `Some(r)` branch with `(*p).0` being `None`, so the `r` binding was invalid.
15+
match p.0 {
16+
Some(_) if { p.1 = 1; p = &mut b; false } => unreachable!(),
17+
//~^ ERROR: cannot assign `p` in match guard
18+
Some(r) => println!("{r}"),
19+
None => unreachable!(),
20+
}
21+
}
22+
23+
fn test_indexing() {
24+
let mut x: &mut [&mut [u32]] = &mut [&mut [0]];
25+
let y: &mut [&mut [u32]] = &mut [];
26+
// Writing to `x[0][0]` previously killed the fake borrow of `x` in the index expression, making
27+
// it possible to access `y[0]` without a bounds-check.
28+
x[0][{ x[0][0] = 1; x = y; 0 }];
29+
//~^ ERROR: cannot assign `x` in indexing expression
30+
}
31+
32+
fn main() {}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0510]: cannot assign `p` in match guard
2+
--> $DIR/dont-kill-shallow-borrows-on-child-writes.rs:16:31
3+
|
4+
LL | match p.0 {
5+
| --- value is immutable in match guard
6+
LL | Some(_) if { p.1 = 1; p = &mut b; false } => unreachable!(),
7+
| ^^^^^^^^^^ cannot assign
8+
9+
error[E0510]: cannot assign `x` in indexing expression
10+
--> $DIR/dont-kill-shallow-borrows-on-child-writes.rs:28:25
11+
|
12+
LL | x[0][{ x[0][0] = 1; x = y; 0 }];
13+
| ---- ^^^^^ cannot assign
14+
| |
15+
| value is immutable in indexing expression
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0510`.

0 commit comments

Comments
 (0)