Skip to content

Commit f045008

Browse files
Kill conflicting borrows of places with projections.
Resolves rust-lang#62007. Due to a bug, the previous version of this check did not actually kill any conflicting borrows unless the borrowed place had no projections. Specifically, `entry_set` will always be empty when `statement_effect` is called. It does not contain the set of borrows which are live at this point in the program.
1 parent f693d33 commit f045008

File tree

2 files changed

+46
-30
lines changed

2 files changed

+46
-30
lines changed

src/librustc_mir/dataflow/impls/borrows.rs

+25-30
Original file line numberDiff line numberDiff line change
@@ -193,43 +193,38 @@ impl<'a, 'tcx> Borrows<'a, 'tcx> {
193193
place: &Place<'tcx>
194194
) {
195195
debug!("kill_borrows_on_place: place={:?}", place);
196-
// Handle the `Place::Local(..)` case first and exit early.
197-
if let Place::Base(PlaceBase::Local(local)) = place {
198-
if let Some(borrow_indices) = self.borrow_set.local_map.get(&local) {
199-
debug!("kill_borrows_on_place: borrow_indices={:?}", borrow_indices);
200-
sets.kill_all(borrow_indices);
196+
197+
if let Some(local) = place.base_local() {
198+
let other_borrows_of_local = self
199+
.borrow_set
200+
.local_map
201+
.get(&local)
202+
.into_iter()
203+
.flat_map(|bs| bs.into_iter());
204+
205+
// If the borrowed place is a local with no projections, all other borrows of this
206+
// local must conflict. This is purely an optimization so we don't have to call
207+
// `places_conflict` for every borrow.
208+
if let Place::Base(PlaceBase::Local(_)) = place {
209+
sets.kill_all(other_borrows_of_local);
201210
return;
202211
}
203-
}
204-
205-
// Otherwise, look at all borrows that are live and if they conflict with the assignment
206-
// into our place then we can kill them.
207-
let mut borrows = sets.on_entry.clone();
208-
let _ = borrows.union(sets.gen_set);
209-
for borrow_index in borrows.iter() {
210-
let borrow_data = &self.borrows()[borrow_index];
211-
debug!(
212-
"kill_borrows_on_place: borrow_index={:?} borrow_data={:?}",
213-
borrow_index, borrow_data,
214-
);
215212

216213
// By passing `PlaceConflictBias::NoOverlap`, we conservatively assume that any given
217214
// pair of array indices are unequal, so that when `places_conflict` returns true, we
218215
// will be assured that two places being compared definitely denotes the same sets of
219216
// locations.
220-
if places_conflict::places_conflict(
221-
self.tcx,
222-
self.body,
223-
&borrow_data.borrowed_place,
224-
place,
225-
places_conflict::PlaceConflictBias::NoOverlap,
226-
) {
227-
debug!(
228-
"kill_borrows_on_place: (kill) borrow_index={:?} borrow_data={:?}",
229-
borrow_index, borrow_data,
230-
);
231-
sets.kill(borrow_index);
232-
}
217+
let definitely_conflicting_borrows = other_borrows_of_local
218+
.filter(|&&i| {
219+
places_conflict::places_conflict(
220+
self.tcx,
221+
self.body,
222+
&self.borrow_set.borrows[i].borrowed_place,
223+
place,
224+
places_conflict::PlaceConflictBias::NoOverlap)
225+
});
226+
227+
sets.kill_all(definitely_conflicting_borrows);
233228
}
234229
}
235230
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// run-pass
2+
#![allow(dead_code)]
3+
4+
struct List<T> {
5+
value: T,
6+
next: Option<Box<List<T>>>,
7+
}
8+
9+
fn to_refs<T>(mut list: (&mut List<T>,)) -> Vec<&mut T> {
10+
let mut result = vec![];
11+
loop {
12+
result.push(&mut (list.0).value);
13+
if let Some(n) = (list.0).next.as_mut() {
14+
list.0 = n;
15+
} else {
16+
return result;
17+
}
18+
}
19+
}
20+
21+
fn main() {}

0 commit comments

Comments
 (0)