1 //! Manages the dataflow bits required for borrowck.
3 //! FIXME: this might be better as a "generic" fixed-point combinator,
4 //! but is not as ugly as it is right now.
6 use rustc::mir::{BasicBlock, Local, Location};
7 use rustc::ty::RegionVid;
8 use rustc_index::bit_set::BitIter;
10 use crate::borrow_check::location::LocationIndex;
12 use polonius_engine::Output;
14 use crate::dataflow::indexes::BorrowIndex;
15 use crate::dataflow::move_paths::{HasMoveData, MovePathIndex};
16 use crate::dataflow::Borrows;
17 use crate::dataflow::EverInitializedPlaces;
18 use crate::dataflow::MaybeUninitializedPlaces;
19 use crate::dataflow::{FlowAtLocation, FlowsAtLocation};
24 crate type PoloniusOutput = Output<RegionVid, BorrowIndex, LocationIndex, Local, MovePathIndex>;
26 crate struct Flows<'b, 'tcx> {
27 borrows: FlowAtLocation<'tcx, Borrows<'b, 'tcx>>,
28 pub uninits: FlowAtLocation<'tcx, MaybeUninitializedPlaces<'b, 'tcx>>,
29 pub ever_inits: FlowAtLocation<'tcx, EverInitializedPlaces<'b, 'tcx>>,
32 pub polonius_output: Option<Rc<PoloniusOutput>>,
35 impl<'b, 'tcx> Flows<'b, 'tcx> {
37 borrows: FlowAtLocation<'tcx, Borrows<'b, 'tcx>>,
38 uninits: FlowAtLocation<'tcx, MaybeUninitializedPlaces<'b, 'tcx>>,
39 ever_inits: FlowAtLocation<'tcx, EverInitializedPlaces<'b, 'tcx>>,
40 polonius_output: Option<Rc<PoloniusOutput>>,
42 Flows { borrows, uninits, ever_inits, polonius_output }
45 crate fn borrows_in_scope(
47 location: LocationIndex,
48 ) -> impl Iterator<Item = BorrowIndex> + '_ {
49 if let Some(ref polonius) = self.polonius_output {
50 Either::Left(polonius.errors_at(location).iter().cloned())
52 Either::Right(self.borrows.iter_incoming())
56 crate fn with_outgoing_borrows(&self, op: impl FnOnce(BitIter<'_, BorrowIndex>)) {
57 self.borrows.with_iter_outgoing(op)
61 macro_rules! each_flow {
62 ($this:ident, $meth:ident($arg:ident)) => {
63 FlowAtLocation::$meth(&mut $this.borrows, $arg);
64 FlowAtLocation::$meth(&mut $this.uninits, $arg);
65 FlowAtLocation::$meth(&mut $this.ever_inits, $arg);
69 impl<'b, 'tcx> FlowsAtLocation for Flows<'b, 'tcx> {
70 fn reset_to_entry_of(&mut self, bb: BasicBlock) {
71 each_flow!(self, reset_to_entry_of(bb));
74 fn reset_to_exit_of(&mut self, bb: BasicBlock) {
75 each_flow!(self, reset_to_exit_of(bb));
78 fn reconstruct_statement_effect(&mut self, location: Location) {
79 each_flow!(self, reconstruct_statement_effect(location));
82 fn reconstruct_terminator_effect(&mut self, location: Location) {
83 each_flow!(self, reconstruct_terminator_effect(location));
86 fn apply_local_effect(&mut self, location: Location) {
87 each_flow!(self, apply_local_effect(location));
91 impl<'b, 'tcx> fmt::Display for Flows<'b, 'tcx> {
92 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
93 let mut s = String::new();
95 s.push_str("borrows in effect: [");
96 let mut saw_one = false;
97 self.borrows.each_state_bit(|borrow| {
102 let borrow_data = &self.borrows.operator().borrows()[borrow];
103 s.push_str(&borrow_data.to_string());
107 s.push_str("borrows generated: [");
108 let mut saw_one = false;
109 self.borrows.each_gen_bit(|borrow| {
114 let borrow_data = &self.borrows.operator().borrows()[borrow];
115 s.push_str(&borrow_data.to_string());
119 s.push_str("uninits: [");
120 let mut saw_one = false;
121 self.uninits.each_state_bit(|mpi_uninit| {
126 let move_path = &self.uninits.operator().move_data().move_paths[mpi_uninit];
127 s.push_str(&move_path.to_string());
131 s.push_str("ever_init: [");
132 let mut saw_one = false;
133 self.ever_inits.each_state_bit(|mpi_ever_init| {
138 let ever_init = &self.ever_inits.operator().move_data().inits[mpi_ever_init];
139 s.push_str(&format!("{:?}", ever_init));
143 fmt::Display::fmt(&s, fmt)