Skip to content

Commit ce3f37b

Browse files
Use new dataflow interface for initialization/borrows analyses
1 parent 702096d commit ce3f37b

File tree

2 files changed

+195
-108
lines changed

2 files changed

+195
-108
lines changed

src/librustc_mir/dataflow/impls/borrows.rs

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use crate::borrow_check::{
1010
places_conflict, BorrowData, BorrowSet, PlaceConflictBias, PlaceExt, RegionInferenceContext,
1111
ToRegionVid,
1212
};
13-
use crate::dataflow::{BitDenotation, BottomValue, GenKillSet};
13+
use crate::dataflow::generic::{self, GenKill};
14+
use crate::dataflow::BottomValue;
1415

1516
use std::rc::Rc;
1617

@@ -172,7 +173,7 @@ impl<'a, 'tcx> Borrows<'a, 'tcx> {
172173
/// That means they went out of a nonlexical scope
173174
fn kill_loans_out_of_scope_at_location(
174175
&self,
175-
trans: &mut GenKillSet<BorrowIndex>,
176+
trans: &mut impl GenKill<BorrowIndex>,
176177
location: Location,
177178
) {
178179
// NOTE: The state associated with a given `location`
@@ -187,16 +188,21 @@ impl<'a, 'tcx> Borrows<'a, 'tcx> {
187188
// region, then setting that gen-bit will override any
188189
// potential kill introduced here.
189190
if let Some(indices) = self.borrows_out_of_scope_at_location.get(&location) {
190-
trans.kill_all(indices);
191+
trans.kill_all(indices.iter().copied());
191192
}
192193
}
193194

194195
/// Kill any borrows that conflict with `place`.
195-
fn kill_borrows_on_place(&self, trans: &mut GenKillSet<BorrowIndex>, place: &Place<'tcx>) {
196+
fn kill_borrows_on_place(&self, trans: &mut impl GenKill<BorrowIndex>, place: &Place<'tcx>) {
196197
debug!("kill_borrows_on_place: place={:?}", place);
197198

198-
let other_borrows_of_local =
199-
self.borrow_set.local_map.get(&place.local).into_iter().flat_map(|bs| bs.into_iter());
199+
let other_borrows_of_local = self
200+
.borrow_set
201+
.local_map
202+
.get(&place.local)
203+
.into_iter()
204+
.flat_map(|bs| bs.into_iter())
205+
.copied();
200206

201207
// If the borrowed place is a local with no projections, all other borrows of this
202208
// local must conflict. This is purely an optimization so we don't have to call
@@ -212,7 +218,7 @@ impl<'a, 'tcx> Borrows<'a, 'tcx> {
212218
// pair of array indices are unequal, so that when `places_conflict` returns true, we
213219
// will be assured that two places being compared definitely denotes the same sets of
214220
// locations.
215-
let definitely_conflicting_borrows = other_borrows_of_local.filter(|&&i| {
221+
let definitely_conflicting_borrows = other_borrows_of_local.filter(|&i| {
216222
places_conflict(
217223
self.tcx,
218224
self.body,
@@ -226,36 +232,41 @@ impl<'a, 'tcx> Borrows<'a, 'tcx> {
226232
}
227233
}
228234

229-
impl<'a, 'tcx> BitDenotation<'tcx> for Borrows<'a, 'tcx> {
235+
impl<'tcx> generic::AnalysisDomain<'tcx> for Borrows<'_, 'tcx> {
230236
type Idx = BorrowIndex;
231-
fn name() -> &'static str {
232-
"borrows"
233-
}
234-
fn bits_per_block(&self) -> usize {
237+
238+
const NAME: &'static str = "borrows";
239+
240+
fn bits_per_block(&self, _: &mir::Body<'tcx>) -> usize {
235241
self.borrow_set.borrows.len() * 2
236242
}
237243

238-
fn start_block_effect(&self, _entry_set: &mut BitSet<Self::Idx>) {
244+
fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut BitSet<Self::Idx>) {
239245
// no borrows of code region_scopes have been taken prior to
240246
// function execution, so this method has no effect.
241247
}
242248

243-
fn before_statement_effect(&self, trans: &mut GenKillSet<Self::Idx>, location: Location) {
244-
debug!("Borrows::before_statement_effect trans: {:?} location: {:?}", trans, location);
245-
self.kill_loans_out_of_scope_at_location(trans, location);
249+
fn pretty_print_idx(&self, w: &mut impl std::io::Write, idx: Self::Idx) -> std::io::Result<()> {
250+
write!(w, "{:?}", self.location(idx))
246251
}
252+
}
247253

248-
fn statement_effect(&self, trans: &mut GenKillSet<Self::Idx>, location: Location) {
249-
debug!("Borrows::statement_effect: trans={:?} location={:?}", trans, location);
250-
251-
let block = &self.body.basic_blocks().get(location.block).unwrap_or_else(|| {
252-
panic!("could not find block at location {:?}", location);
253-
});
254-
let stmt = block.statements.get(location.statement_index).unwrap_or_else(|| {
255-
panic!("could not find statement at location {:?}");
256-
});
254+
impl<'tcx> generic::GenKillAnalysis<'tcx> for Borrows<'_, 'tcx> {
255+
fn before_statement_effect(
256+
&self,
257+
trans: &mut impl GenKill<Self::Idx>,
258+
_statement: &mir::Statement<'tcx>,
259+
location: Location,
260+
) {
261+
self.kill_loans_out_of_scope_at_location(trans, location);
262+
}
257263

258-
debug!("Borrows::statement_effect: stmt={:?}", stmt);
264+
fn statement_effect(
265+
&self,
266+
trans: &mut impl GenKill<Self::Idx>,
267+
stmt: &mir::Statement<'tcx>,
268+
location: Location,
269+
) {
259270
match stmt.kind {
260271
mir::StatementKind::Assign(box (ref lhs, ref rhs)) => {
261272
if let mir::Rvalue::Ref(_, _, ref place) = *rhs {
@@ -301,18 +312,29 @@ impl<'a, 'tcx> BitDenotation<'tcx> for Borrows<'a, 'tcx> {
301312
}
302313
}
303314

304-
fn before_terminator_effect(&self, trans: &mut GenKillSet<Self::Idx>, location: Location) {
305-
debug!("Borrows::before_terminator_effect: trans={:?} location={:?}", trans, location);
315+
fn before_terminator_effect(
316+
&self,
317+
trans: &mut impl GenKill<Self::Idx>,
318+
_terminator: &mir::Terminator<'tcx>,
319+
location: Location,
320+
) {
306321
self.kill_loans_out_of_scope_at_location(trans, location);
307322
}
308323

309-
fn terminator_effect(&self, _: &mut GenKillSet<Self::Idx>, _: Location) {}
324+
fn terminator_effect(
325+
&self,
326+
_: &mut impl GenKill<Self::Idx>,
327+
_: &mir::Terminator<'tcx>,
328+
_: Location,
329+
) {
330+
}
310331

311-
fn propagate_call_return(
332+
fn call_return_effect(
312333
&self,
313-
_in_out: &mut BitSet<BorrowIndex>,
314-
_call_bb: mir::BasicBlock,
315-
_dest_bb: mir::BasicBlock,
334+
_trans: &mut impl GenKill<Self::Idx>,
335+
_block: mir::BasicBlock,
336+
_func: &mir::Operand<'tcx>,
337+
_args: &[mir::Operand<'tcx>],
316338
_dest_place: &mir::Place<'tcx>,
317339
) {
318340
}

0 commit comments

Comments
 (0)