Skip to content

Commit 491411b

Browse files
authored
Rollup merge of rust-lang#56388 - matthewjasper:more-lexical-mir-cleanup, r=nikomatsakis
More MIR borrow check cleanup * Fix some rustc doc links * Remove the `region_map` field from `BorrowSet` * Use `visit_local` to find 2PB activations r? @nikomatsakis
2 parents 8d4f83e + 6000c2e commit 491411b

File tree

3 files changed

+74
-81
lines changed

3 files changed

+74
-81
lines changed

src/librustc_mir/borrow_check/borrow_set.rs

Lines changed: 56 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc::mir::traversal;
1616
use rustc::mir::visit::{
1717
PlaceContext, Visitor, NonUseContext, MutatingUseContext, NonMutatingUseContext
1818
};
19-
use rustc::mir::{self, Location, Mir, Place, Local};
19+
use rustc::mir::{self, Location, Mir, Local};
2020
use rustc::ty::{RegionVid, TyCtxt};
2121
use rustc::util::nodemap::{FxHashMap, FxHashSet};
2222
use rustc_data_structures::indexed_vec::IndexVec;
@@ -41,10 +41,6 @@ crate struct BorrowSet<'tcx> {
4141
/// only need to store one borrow index
4242
crate activation_map: FxHashMap<Location, Vec<BorrowIndex>>,
4343

44-
/// Every borrow has a region; this maps each such regions back to
45-
/// its borrow-indexes.
46-
crate region_map: FxHashMap<RegionVid, FxHashSet<BorrowIndex>>,
47-
4844
/// Map from local to all the borrows on that local
4945
crate local_map: FxHashMap<mir::Local, FxHashSet<BorrowIndex>>,
5046

@@ -149,7 +145,6 @@ impl<'tcx> BorrowSet<'tcx> {
149145
idx_vec: IndexVec::new(),
150146
location_map: Default::default(),
151147
activation_map: Default::default(),
152-
region_map: Default::default(),
153148
local_map: Default::default(),
154149
pending_activations: Default::default(),
155150
locals_state_at_exit:
@@ -164,7 +159,6 @@ impl<'tcx> BorrowSet<'tcx> {
164159
borrows: visitor.idx_vec,
165160
location_map: visitor.location_map,
166161
activation_map: visitor.activation_map,
167-
region_map: visitor.region_map,
168162
local_map: visitor.local_map,
169163
locals_state_at_exit: visitor.locals_state_at_exit,
170164
}
@@ -184,7 +178,6 @@ struct GatherBorrows<'a, 'gcx: 'tcx, 'tcx: 'a> {
184178
idx_vec: IndexVec<BorrowIndex, BorrowData<'tcx>>,
185179
location_map: FxHashMap<Location, BorrowIndex>,
186180
activation_map: FxHashMap<Location, Vec<BorrowIndex>>,
187-
region_map: FxHashMap<RegionVid, FxHashSet<BorrowIndex>>,
188181
local_map: FxHashMap<mir::Local, FxHashSet<BorrowIndex>>,
189182

190183
/// When we encounter a 2-phase borrow statement, it will always
@@ -229,7 +222,6 @@ impl<'a, 'gcx, 'tcx> Visitor<'tcx> for GatherBorrows<'a, 'gcx, 'tcx> {
229222

230223
self.insert_as_pending_if_two_phase(location, &assigned_place, kind, idx);
231224

232-
self.region_map.entry(region).or_default().insert(idx);
233225
if let Some(local) = borrowed_place.root_local() {
234226
self.local_map.entry(local).or_default().insert(idx);
235227
}
@@ -238,68 +230,68 @@ impl<'a, 'gcx, 'tcx> Visitor<'tcx> for GatherBorrows<'a, 'gcx, 'tcx> {
238230
self.super_assign(block, assigned_place, rvalue, location)
239231
}
240232

241-
fn visit_place(
233+
fn visit_local(
242234
&mut self,
243-
place: &mir::Place<'tcx>,
235+
temp: &Local,
244236
context: PlaceContext<'tcx>,
245237
location: Location,
246238
) {
247-
self.super_place(place, context, location);
248-
249-
// We found a use of some temporary TEMP...
250-
if let Place::Local(temp) = place {
251-
// ... check whether we (earlier) saw a 2-phase borrow like
252-
//
253-
// TMP = &mut place
254-
if let Some(&borrow_index) = self.pending_activations.get(temp) {
255-
let borrow_data = &mut self.idx_vec[borrow_index];
256-
257-
// Watch out: the use of TMP in the borrow itself
258-
// doesn't count as an activation. =)
259-
if borrow_data.reserve_location == location &&
260-
context == PlaceContext::MutatingUse(MutatingUseContext::Store)
261-
{
262-
return;
263-
}
239+
if !context.is_use() {
240+
return;
241+
}
264242

265-
if let TwoPhaseActivation::ActivatedAt(other_location) =
266-
borrow_data.activation_location {
267-
span_bug!(
268-
self.mir.source_info(location).span,
269-
"found two uses for 2-phase borrow temporary {:?}: \
270-
{:?} and {:?}",
271-
temp,
272-
location,
273-
other_location,
274-
);
275-
}
243+
// We found a use of some temporary TMP
244+
// check whether we (earlier) saw a 2-phase borrow like
245+
//
246+
// TMP = &mut place
247+
if let Some(&borrow_index) = self.pending_activations.get(temp) {
248+
let borrow_data = &mut self.idx_vec[borrow_index];
276249

277-
// Otherwise, this is the unique later use
278-
// that we expect.
279-
borrow_data.activation_location = match context {
280-
// The use of TMP in a shared borrow does not
281-
// count as an actual activation.
282-
PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow(..)) |
283-
PlaceContext::NonMutatingUse(NonMutatingUseContext::ShallowBorrow(..)) =>
284-
TwoPhaseActivation::NotActivated,
285-
_ => {
286-
// Double check: This borrow is indeed a two-phase borrow (that is,
287-
// we are 'transitioning' from `NotActivated` to `ActivatedAt`) and
288-
// we've not found any other activations (checked above).
289-
assert_eq!(
290-
borrow_data.activation_location,
291-
TwoPhaseActivation::NotActivated,
292-
"never found an activation for this borrow!",
293-
);
294-
295-
self.activation_map
296-
.entry(location)
297-
.or_default()
298-
.push(borrow_index);
299-
TwoPhaseActivation::ActivatedAt(location)
300-
}
301-
};
250+
// Watch out: the use of TMP in the borrow itself
251+
// doesn't count as an activation. =)
252+
if borrow_data.reserve_location == location &&
253+
context == PlaceContext::MutatingUse(MutatingUseContext::Store)
254+
{
255+
return;
256+
}
257+
258+
if let TwoPhaseActivation::ActivatedAt(other_location) =
259+
borrow_data.activation_location {
260+
span_bug!(
261+
self.mir.source_info(location).span,
262+
"found two uses for 2-phase borrow temporary {:?}: \
263+
{:?} and {:?}",
264+
temp,
265+
location,
266+
other_location,
267+
);
302268
}
269+
270+
// Otherwise, this is the unique later use
271+
// that we expect.
272+
borrow_data.activation_location = match context {
273+
// The use of TMP in a shared borrow does not
274+
// count as an actual activation.
275+
PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow(..)) |
276+
PlaceContext::NonMutatingUse(NonMutatingUseContext::ShallowBorrow(..)) =>
277+
TwoPhaseActivation::NotActivated,
278+
_ => {
279+
// Double check: This borrow is indeed a two-phase borrow (that is,
280+
// we are 'transitioning' from `NotActivated` to `ActivatedAt`) and
281+
// we've not found any other activations (checked above).
282+
assert_eq!(
283+
borrow_data.activation_location,
284+
TwoPhaseActivation::NotActivated,
285+
"never found an activation for this borrow!",
286+
);
287+
288+
self.activation_map
289+
.entry(location)
290+
.or_default()
291+
.push(borrow_index);
292+
TwoPhaseActivation::ActivatedAt(location)
293+
}
294+
};
303295
}
304296
}
305297

src/librustc_mir/dataflow/impls/borrows.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
246246
// re-consider the current implementations of the
247247
// propagate_call_return method.
248248

249-
if let mir::Rvalue::Ref(region, _, ref place) = **rhs {
249+
if let mir::Rvalue::Ref(_, _, ref place) = **rhs {
250250
if place.ignore_borrow(
251251
self.tcx,
252252
self.mir,
@@ -258,13 +258,6 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
258258
panic!("could not find BorrowIndex for location {:?}", location);
259259
});
260260

261-
assert!(self.borrow_set.region_map
262-
.get(&region.to_region_vid())
263-
.unwrap_or_else(|| {
264-
panic!("could not find BorrowIndexs for RegionVid {:?}", region);
265-
})
266-
.contains(&index)
267-
);
268261
sets.gen(*index);
269262

270263
// Issue #46746: Two-phase borrows handles

src/librustc_mir/transform/cleanup_post_borrowck.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,25 @@
1010

1111
//! This module provides two passes:
1212
//!
13-
//! - [CleanAscribeUserType], that replaces all
14-
//! [StatementKind::AscribeUserType] statements with [StatementKind::Nop].
15-
//! - [CleanFakeReadsAndBorrows], that replaces all [FakeRead] statements and
16-
//! borrows that are read by [FakeReadCause::ForMatchGuard] fake reads with
17-
//! [StatementKind::Nop].
13+
//! - [`CleanAscribeUserType`], that replaces all [`AscribeUserType`]
14+
//! statements with [`Nop`].
15+
//! - [`CleanFakeReadsAndBorrows`], that replaces all [`FakeRead`] statements
16+
//! and borrows that are read by [`ForMatchGuard`] fake reads with [`Nop`].
1817
//!
19-
//! The [CleanFakeReadsAndBorrows] "pass" is actually implemented as two
18+
//! The `CleanFakeReadsAndBorrows` "pass" is actually implemented as two
2019
//! traversals (aka visits) of the input MIR. The first traversal,
21-
//! [DeleteAndRecordFakeReads], deletes the fake reads and finds the temporaries
22-
//! read by [ForMatchGuard] reads, and [DeleteFakeBorrows] deletes the
23-
//! initialization of those temporaries.
20+
//! [`DeleteAndRecordFakeReads`], deletes the fake reads and finds the
21+
//! temporaries read by [`ForMatchGuard`] reads, and [`DeleteFakeBorrows`]
22+
//! deletes the initialization of those temporaries.
23+
//!
24+
//! [`CleanAscribeUserType`]: cleanup_post_borrowck::CleanAscribeUserType
25+
//! [`CleanFakeReadsAndBorrows`]: cleanup_post_borrowck::CleanFakeReadsAndBorrows
26+
//! [`DeleteAndRecordFakeReads`]: cleanup_post_borrowck::DeleteAndRecordFakeReads
27+
//! [`DeleteFakeBorrows`]: cleanup_post_borrowck::DeleteFakeBorrows
28+
//! [`AscribeUserType`]: rustc::mir::StatementKind::AscribeUserType
29+
//! [`Nop`]: rustc::mir::StatementKind::Nop
30+
//! [`FakeRead`]: rustc::mir::StatementKind::FakeRead
31+
//! [`ForMatchGuard`]: rustc::mir::FakeReadCause::ForMatchGuard
2432
2533
use rustc_data_structures::fx::FxHashSet;
2634

0 commit comments

Comments
 (0)