]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/borrow_check/flows.rs
Fix remaining compilation issues
[rust.git] / src / librustc_mir / borrow_check / flows.rs
1 //! Manages the dataflow bits required for borrowck.
2 //!
3 //! FIXME: this might be better as a "generic" fixed-point combinator,
4 //! but is not as ugly as it is right now.
5
6 use rustc::mir::{BasicBlock, Local, Location};
7 use rustc::ty::RegionVid;
8 use rustc_index::bit_set::BitIter;
9
10 use crate::borrow_check::location::LocationIndex;
11
12 use polonius_engine::Output;
13
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};
20 use either::Either;
21 use std::fmt;
22 use std::rc::Rc;
23
24 crate type PoloniusOutput = Output<RegionVid, BorrowIndex, LocationIndex, Local, MovePathIndex>;
25
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>>,
30
31     /// Polonius Output
32     pub polonius_output: Option<Rc<PoloniusOutput>>,
33 }
34
35 impl<'b, 'tcx> Flows<'b, 'tcx> {
36     crate fn new(
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>>,
41     ) -> Self {
42         Flows { borrows, uninits, ever_inits, polonius_output }
43     }
44
45     crate fn borrows_in_scope(
46         &self,
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())
51         } else {
52             Either::Right(self.borrows.iter_incoming())
53         }
54     }
55
56     crate fn with_outgoing_borrows(&self, op: impl FnOnce(BitIter<'_, BorrowIndex>)) {
57         self.borrows.with_iter_outgoing(op)
58     }
59 }
60
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);
66     };
67 }
68
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));
72     }
73
74     fn reset_to_exit_of(&mut self, bb: BasicBlock) {
75         each_flow!(self, reset_to_exit_of(bb));
76     }
77
78     fn reconstruct_statement_effect(&mut self, location: Location) {
79         each_flow!(self, reconstruct_statement_effect(location));
80     }
81
82     fn reconstruct_terminator_effect(&mut self, location: Location) {
83         each_flow!(self, reconstruct_terminator_effect(location));
84     }
85
86     fn apply_local_effect(&mut self, location: Location) {
87         each_flow!(self, apply_local_effect(location));
88     }
89 }
90
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();
94
95         s.push_str("borrows in effect: [");
96         let mut saw_one = false;
97         self.borrows.each_state_bit(|borrow| {
98             if saw_one {
99                 s.push_str(", ");
100             };
101             saw_one = true;
102             let borrow_data = &self.borrows.operator().borrows()[borrow];
103             s.push_str(&borrow_data.to_string());
104         });
105         s.push_str("] ");
106
107         s.push_str("borrows generated: [");
108         let mut saw_one = false;
109         self.borrows.each_gen_bit(|borrow| {
110             if saw_one {
111                 s.push_str(", ");
112             };
113             saw_one = true;
114             let borrow_data = &self.borrows.operator().borrows()[borrow];
115             s.push_str(&borrow_data.to_string());
116         });
117         s.push_str("] ");
118
119         s.push_str("uninits: [");
120         let mut saw_one = false;
121         self.uninits.each_state_bit(|mpi_uninit| {
122             if saw_one {
123                 s.push_str(", ");
124             };
125             saw_one = true;
126             let move_path = &self.uninits.operator().move_data().move_paths[mpi_uninit];
127             s.push_str(&move_path.to_string());
128         });
129         s.push_str("] ");
130
131         s.push_str("ever_init: [");
132         let mut saw_one = false;
133         self.ever_inits.each_state_bit(|mpi_ever_init| {
134             if saw_one {
135                 s.push_str(", ");
136             };
137             saw_one = true;
138             let ever_init = &self.ever_inits.operator().move_data().inits[mpi_ever_init];
139             s.push_str(&format!("{:?}", ever_init));
140         });
141         s.push_str("]");
142
143         fmt::Display::fmt(&s, fmt)
144     }
145 }