Skip to content

Commit ab98c59

Browse files
committed
Fix a large number of Body -> (ReadOnly)BodyCache type errors, add predecessor_locations fn to ReadOnlyBodyCache
1 parent 2eed90a commit ab98c59

File tree

15 files changed

+207
-183
lines changed

15 files changed

+207
-183
lines changed

src/librustc/mir/cache.rs

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,41 @@ pub struct Cache {
3333
// }
3434
//}
3535

36+
macro_rules! get_predecessors {
37+
(mut $self:ident, $block:expr, $body:expr) => {
38+
$self.predecessors_for($block, $body)
39+
};
40+
($self:ident, $block:expr, $body:expr) => {
41+
$self.unwrap_predecessors_for($block)
42+
};
43+
}
44+
45+
macro_rules! impl_predecessor_locations {
46+
( ( $($pub:ident)? ) $name:ident $($mutability:ident)?) => {
47+
$($pub)? fn $name<'a>(&'a $($mutability)? self, loc: Location, body: &'a Body<'a>) -> impl Iterator<Item = Location> + 'a {
48+
let if_zero_locations = if loc.statement_index == 0 {
49+
let predecessor_blocks = get_predecessors!($($mutability)? self, loc.block, body);
50+
let num_predecessor_blocks = predecessor_blocks.len();
51+
Some(
52+
(0..num_predecessor_blocks)
53+
.map(move |i| predecessor_blocks[i])
54+
.map(move |bb| body.terminator_loc(bb)),
55+
)
56+
} else {
57+
None
58+
};
59+
60+
let if_not_zero_locations = if loc.statement_index == 0 {
61+
None
62+
} else {
63+
Some(Location { block: loc.block, statement_index: loc.statement_index - 1 })
64+
};
65+
66+
if_zero_locations.into_iter().flatten().chain(if_not_zero_locations)
67+
}
68+
};
69+
}
70+
3671
impl Cache {
3772
pub fn new() -> Self {
3873
Self {
@@ -80,27 +115,9 @@ impl Cache {
80115
}
81116

82117
#[inline]
83-
pub fn predecessor_locations<'a>(&'a mut self, loc: Location, body: &'a Body<'a>) -> impl Iterator<Item = Location> + 'a {
84-
let if_zero_locations = if loc.statement_index == 0 {
85-
let predecessor_blocks = self.predecessors_for(loc.block, body);
86-
let num_predecessor_blocks = predecessor_blocks.len();
87-
Some(
88-
(0..num_predecessor_blocks)
89-
.map(move |i| predecessor_blocks[i])
90-
.map(move |bb| body.terminator_loc(bb)),
91-
)
92-
} else {
93-
None
94-
};
95-
96-
let if_not_zero_locations = if loc.statement_index == 0 {
97-
None
98-
} else {
99-
Some(Location { block: loc.block, statement_index: loc.statement_index - 1 })
100-
};
101-
102-
if_zero_locations.into_iter().flatten().chain(if_not_zero_locations)
103-
}
118+
impl_predecessor_locations!((pub) predecessor_locations mut);
119+
120+
impl_predecessor_locations!(() unwrap_predecessor_locations);
104121

105122
#[inline]
106123
pub fn basic_blocks_mut<'a, 'tcx>(&mut self, body: &'a mut Body<'tcx>) -> &'a mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
@@ -240,6 +257,11 @@ impl ReadOnlyBodyCache<'a, 'tcx> {
240257
self.cache.unwrap_predecessors_for(bb)
241258
}
242259

260+
#[inline]
261+
pub fn predecessor_locations(&self, loc: Location) -> impl Iterator<Item = Location> + '_ {
262+
self.cache.unwrap_predecessor_locations(loc, self.body)
263+
}
264+
243265
#[inline]
244266
pub fn body(&self) -> &'a Body<'tcx> {
245267
self.body

src/librustc_mir/borrow_check/borrow_set.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::dataflow::indexes::BorrowIndex;
55
use crate::dataflow::move_paths::MoveData;
66
use rustc::mir::traversal;
77
use rustc::mir::visit::{PlaceContext, Visitor, NonUseContext, MutatingUseContext};
8-
use rustc::mir::{self, Location, Body, Local};
8+
use rustc::mir::{self, Location, Body, Local, ReadOnlyBodyCache};
99
use rustc::ty::{RegionVid, TyCtxt};
1010
use rustc::util::nodemap::{FxHashMap, FxHashSet};
1111
use rustc_index::vec::IndexVec;
@@ -90,7 +90,7 @@ crate enum LocalsStateAtExit {
9090
impl LocalsStateAtExit {
9191
fn build(
9292
locals_are_invalidated_at_exit: bool,
93-
body: &Body<'tcx>,
93+
body_cache: &ReadOnlyBodyCache<'_, 'tcx>,
9494
move_data: &MoveData<'tcx>
9595
) -> Self {
9696
struct HasStorageDead(BitSet<Local>);
@@ -106,8 +106,8 @@ impl LocalsStateAtExit {
106106
if locals_are_invalidated_at_exit {
107107
LocalsStateAtExit::AllAreInvalidated
108108
} else {
109-
let mut has_storage_dead = HasStorageDead(BitSet::new_empty(body.local_decls.len()));
110-
has_storage_dead.visit_body(body);
109+
let mut has_storage_dead = HasStorageDead(BitSet::new_empty(body_cache.local_decls.len()));
110+
has_storage_dead.visit_body(body_cache);
111111
let mut has_storage_dead_or_moved = has_storage_dead.0;
112112
for move_out in &move_data.moves {
113113
if let Some(index) = move_data.base_local(move_out.path) {
@@ -123,23 +123,23 @@ impl LocalsStateAtExit {
123123
impl<'tcx> BorrowSet<'tcx> {
124124
pub fn build(
125125
tcx: TyCtxt<'tcx>,
126-
body: &Body<'tcx>,
126+
body_cache: &ReadOnlyBodyCache<'_, 'tcx>,
127127
locals_are_invalidated_at_exit: bool,
128128
move_data: &MoveData<'tcx>,
129129
) -> Self {
130130
let mut visitor = GatherBorrows {
131131
tcx,
132-
body,
132+
body: body_cache.body(),
133133
idx_vec: IndexVec::new(),
134134
location_map: Default::default(),
135135
activation_map: Default::default(),
136136
local_map: Default::default(),
137137
pending_activations: Default::default(),
138138
locals_state_at_exit:
139-
LocalsStateAtExit::build(locals_are_invalidated_at_exit, body, move_data),
139+
LocalsStateAtExit::build(locals_are_invalidated_at_exit, body_cache, move_data),
140140
};
141141

142-
for (block, block_data) in traversal::preorder(body) {
142+
for (block, block_data) in traversal::preorder(body_cache) {
143143
visitor.visit_basic_block_data(block, block_data);
144144
}
145145

src/librustc_mir/borrow_check/conflict_errors.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
206206
}
207207

208208
let ty =
209-
Place::ty_from(used_place.base, used_place.projection, self.body, self.infcx.tcx)
209+
Place::ty_from(used_place.base, used_place.projection, self.body_cache.body(), self.infcx.tcx)
210210
.ty;
211211
let needs_note = match ty.kind {
212212
ty::Closure(id, _) => {
@@ -222,7 +222,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
222222
let mpi = self.move_data.moves[move_out_indices[0]].path;
223223
let place = &self.move_data.move_paths[mpi].place;
224224

225-
let ty = place.ty(self.body, self.infcx.tcx).ty;
225+
let ty = place.ty(self.body_cache.body(), self.infcx.tcx).ty;
226226
let opt_name =
227227
self.describe_place_with_options(place.as_ref(), IncludingDowncast(true));
228228
let note_msg = match opt_name {
@@ -314,7 +314,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
314314
None,
315315
).add_explanation_to_diagnostic(
316316
self.infcx.tcx,
317-
self.body,
317+
&self.body_cache,
318318
&self.local_names,
319319
&mut err,
320320
"",
@@ -356,7 +356,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
356356
self.explain_why_borrow_contains_point(location, borrow, None)
357357
.add_explanation_to_diagnostic(
358358
self.infcx.tcx,
359-
self.body,
359+
&self.body_cache,
360360
&self.local_names,
361361
&mut err,
362362
"",
@@ -578,7 +578,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
578578

579579
explanation.add_explanation_to_diagnostic(
580580
self.infcx.tcx,
581-
self.body,
581+
&self.body_cache,
582582
&self.local_names,
583583
&mut err,
584584
first_borrow_desc,
@@ -619,7 +619,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
619619
// Define a small closure that we can use to check if the type of a place
620620
// is a union.
621621
let union_ty = |place_base, place_projection| {
622-
let ty = Place::ty_from(place_base, place_projection, self.body, self.infcx.tcx).ty;
622+
let ty = Place::ty_from(place_base, place_projection, self.body_cache.body(), self.infcx.tcx).ty;
623623
ty.ty_adt_def().filter(|adt| adt.is_union()).map(|_| ty)
624624
};
625625
let describe_place = |place| self.describe_place(place).unwrap_or_else(|| "_".to_owned());
@@ -738,7 +738,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
738738

739739
assert!(root_place.projection.is_empty());
740740
let proper_span = match root_place.base {
741-
PlaceBase::Local(local) => self.body.local_decls[*local].source_info.span,
741+
PlaceBase::Local(local) => self.body_cache.local_decls[*local].source_info.span,
742742
_ => drop_span,
743743
};
744744

@@ -965,7 +965,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
965965
} else {
966966
explanation.add_explanation_to_diagnostic(
967967
self.infcx.tcx,
968-
self.body,
968+
&self.body_cache,
969969
&self.local_names,
970970
&mut err,
971971
"",
@@ -991,7 +991,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
991991
);
992992

993993
explanation.add_explanation_to_diagnostic(
994-
self.infcx.tcx, self.body, &self.local_names, &mut err, "", None);
994+
self.infcx.tcx, &self.body_cache, &self.local_names, &mut err, "", None);
995995
}
996996

997997
err
@@ -1051,7 +1051,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10511051

10521052
explanation.add_explanation_to_diagnostic(
10531053
self.infcx.tcx,
1054-
self.body,
1054+
&self.body_cache,
10551055
&self.local_names,
10561056
&mut err,
10571057
"",
@@ -1138,7 +1138,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11381138
}
11391139
explanation.add_explanation_to_diagnostic(
11401140
self.infcx.tcx,
1141-
self.body,
1141+
&self.body_cache,
11421142
&self.local_names,
11431143
&mut err,
11441144
"",
@@ -1174,15 +1174,15 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11741174
};
11751175

11761176
// FIXME use a better heuristic than Spans
1177-
let reference_desc = if return_span == self.body.source_info(borrow.reserve_location).span {
1177+
let reference_desc = if return_span == self.body_cache.source_info(borrow.reserve_location).span {
11781178
"reference to"
11791179
} else {
11801180
"value referencing"
11811181
};
11821182

11831183
let (place_desc, note) = if let Some(place_desc) = opt_place_desc {
11841184
let local_kind = if let Some(local) = borrow.borrowed_place.as_local() {
1185-
match self.body.local_kind(local) {
1185+
match self.body_cache.local_kind(local) {
11861186
LocalKind::ReturnPointer
11871187
| LocalKind::Temp => bug!("temporary or return pointer with a name"),
11881188
LocalKind::Var => "local variable ",
@@ -1215,7 +1215,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12151215
} else {
12161216
bug!("try_report_cannot_return_reference_to_local: not a local")
12171217
};
1218-
match self.body.local_kind(*local) {
1218+
match self.body_cache.local_kind(*local) {
12191219
LocalKind::ReturnPointer | LocalKind::Temp => (
12201220
"temporary value".to_string(),
12211221
"temporary value created here".to_string(),
@@ -1372,10 +1372,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
13721372
}
13731373

13741374
fn get_moved_indexes(&mut self, location: Location, mpi: MovePathIndex) -> Vec<MoveSite> {
1375-
let body = self.body;
1376-
13771375
let mut stack = Vec::new();
1378-
stack.extend(body.predecessor_locations(location).map(|predecessor| {
1376+
stack.extend(self.body_cache.predecessor_locations(location).map(|predecessor| {
13791377
let is_back_edge = location.dominates(predecessor, &self.dominators);
13801378
(predecessor, is_back_edge)
13811379
}));
@@ -1394,7 +1392,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
13941392
}
13951393

13961394
// check for moves
1397-
let stmt_kind = body[location.block]
1395+
let stmt_kind = self.body_cache[location.block]
13981396
.statements
13991397
.get(location.statement_index)
14001398
.map(|s| &s.kind);
@@ -1449,7 +1447,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
14491447
let mut any_match = false;
14501448
drop_flag_effects::for_location_inits(
14511449
self.infcx.tcx,
1452-
self.body,
1450+
&self.body_cache,
14531451
self.move_data,
14541452
location,
14551453
|m| {
@@ -1462,7 +1460,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
14621460
continue 'dfs;
14631461
}
14641462

1465-
stack.extend(body.predecessor_locations(location).map(|predecessor| {
1463+
stack.extend(self.body_cache.predecessor_locations(location).map(|predecessor| {
14661464
let back_edge = location.dominates(predecessor, &self.dominators);
14671465
(predecessor, is_back_edge || back_edge)
14681466
}));
@@ -1514,7 +1512,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
15141512
self.explain_why_borrow_contains_point(location, loan, None)
15151513
.add_explanation_to_diagnostic(
15161514
self.infcx.tcx,
1517-
self.body,
1515+
&self.body_cache,
15181516
&self.local_names,
15191517
&mut err,
15201518
"",
@@ -1539,8 +1537,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
15391537
) {
15401538
let (from_arg, local_decl, local_name) = match err_place.as_local() {
15411539
Some(local) => (
1542-
self.body.local_kind(local) == LocalKind::Arg,
1543-
Some(&self.body.local_decls[local]),
1540+
self.body_cache.local_kind(local) == LocalKind::Arg,
1541+
Some(&self.body_cache.local_decls[local]),
15441542
self.local_names[local],
15451543
),
15461544
None => (false, None, None),
@@ -1625,15 +1623,15 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
16251623
StorageDeadOrDrop::LocalStorageDead
16261624
| StorageDeadOrDrop::BoxedStorageDead => {
16271625
assert!(
1628-
Place::ty_from(&place.base, proj_base, self.body, tcx).ty.is_box(),
1626+
Place::ty_from(&place.base, proj_base, self.body_cache.body(), tcx).ty.is_box(),
16291627
"Drop of value behind a reference or raw pointer"
16301628
);
16311629
StorageDeadOrDrop::BoxedStorageDead
16321630
}
16331631
StorageDeadOrDrop::Destructor(_) => base_access,
16341632
},
16351633
ProjectionElem::Field(..) | ProjectionElem::Downcast(..) => {
1636-
let base_ty = Place::ty_from(&place.base, proj_base, self.body, tcx).ty;
1634+
let base_ty = Place::ty_from(&place.base, proj_base, self.body_cache.body(), tcx).ty;
16371635
match base_ty.kind {
16381636
ty::Adt(def, _) if def.has_dtor(tcx) => {
16391637
// Report the outermost adt with a destructor
@@ -1721,22 +1719,22 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
17211719
location
17221720
);
17231721
if let Some(&Statement { kind: StatementKind::Assign(box(ref reservation, _)), ..})
1724-
= &self.body[location.block].statements.get(location.statement_index)
1722+
= &self.body_cache[location.block].statements.get(location.statement_index)
17251723
{
17261724
debug!(
17271725
"annotate_argument_and_return_for_borrow: reservation={:?}",
17281726
reservation
17291727
);
17301728
// Check that the initial assignment of the reserve location is into a temporary.
17311729
let mut target = match reservation.as_local() {
1732-
Some(local) if self.body.local_kind(local) == LocalKind::Temp => local,
1730+
Some(local) if self.body_cache.local_kind(local) == LocalKind::Temp => local,
17331731
_ => return None,
17341732
};
17351733

17361734
// Next, look through the rest of the block, checking if we are assigning the
17371735
// `target` (that is, the place that contains our borrow) to anything.
17381736
let mut annotated_closure = None;
1739-
for stmt in &self.body[location.block].statements[location.statement_index + 1..] {
1737+
for stmt in &self.body_cache[location.block].statements[location.statement_index + 1..] {
17401738
debug!(
17411739
"annotate_argument_and_return_for_borrow: target={:?} stmt={:?}",
17421740
target, stmt
@@ -1861,7 +1859,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
18611859
}
18621860

18631861
// Check the terminator if we didn't find anything in the statements.
1864-
let terminator = &self.body[location.block].terminator();
1862+
let terminator = &self.body_cache[location.block].terminator();
18651863
debug!(
18661864
"annotate_argument_and_return_for_borrow: target={:?} terminator={:?}",
18671865
target, terminator

0 commit comments

Comments
 (0)