]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/borrow_check/facts.rs
get rid of nll submod
[rust.git] / src / librustc_mir / borrow_check / facts.rs
1 use crate::borrow_check::location::{LocationIndex, LocationTable};
2 use crate::dataflow::indexes::{BorrowIndex, MovePathIndex};
3 use polonius_engine::AllFacts as PoloniusFacts;
4 use polonius_engine::Atom;
5 use rustc::mir::Local;
6 use rustc::ty::{RegionVid, TyCtxt};
7 use rustc_index::vec::Idx;
8 use std::error::Error;
9 use std::fmt::Debug;
10 use std::fs::{self, File};
11 use std::io::Write;
12 use std::path::Path;
13
14 #[derive(Copy, Clone, Debug)]
15 crate struct RustcFacts;
16
17 impl polonius_engine::FactTypes for RustcFacts {
18     type Origin = RegionVid;
19     type Loan = BorrowIndex;
20     type Point = LocationIndex;
21     type Variable = Local;
22     type Path = MovePathIndex;
23 }
24
25 crate type AllFacts = PoloniusFacts<RustcFacts>;
26
27 crate trait AllFactsExt {
28     /// Returns `true` if there is a need to gather `AllFacts` given the
29     /// current `-Z` flags.
30     fn enabled(tcx: TyCtxt<'_>) -> bool;
31
32     fn write_to_dir(
33         &self,
34         dir: impl AsRef<Path>,
35         location_table: &LocationTable,
36     ) -> Result<(), Box<dyn Error>>;
37 }
38
39 impl AllFactsExt for AllFacts {
40     /// Return
41     fn enabled(tcx: TyCtxt<'_>) -> bool {
42         tcx.sess.opts.debugging_opts.nll_facts || tcx.sess.opts.debugging_opts.polonius
43     }
44
45     fn write_to_dir(
46         &self,
47         dir: impl AsRef<Path>,
48         location_table: &LocationTable,
49     ) -> Result<(), Box<dyn Error>> {
50         let dir: &Path = dir.as_ref();
51         fs::create_dir_all(dir)?;
52         let wr = FactWriter { location_table, dir };
53         macro_rules! write_facts_to_path {
54             ($wr:ident . write_facts_to_path($this:ident . [
55                 $($field:ident,)*
56             ])) => {
57                 $(
58                     $wr.write_facts_to_path(
59                         &$this.$field,
60                         &format!("{}.facts", stringify!($field))
61                     )?;
62                 )*
63             }
64         }
65         write_facts_to_path! {
66             wr.write_facts_to_path(self.[
67                 borrow_region,
68                 universal_region,
69                 placeholder,
70                 cfg_edge,
71                 killed,
72                 outlives,
73                 invalidates,
74                 var_used,
75                 var_defined,
76                 var_drop_used,
77                 var_uses_region,
78                 var_drops_region,
79                 child,
80                 path_belongs_to_var,
81                 initialized_at,
82                 moved_out_at,
83                 path_accessed_at,
84                 known_subset,
85             ])
86         }
87         Ok(())
88     }
89 }
90
91 impl Atom for BorrowIndex {
92     fn index(self) -> usize {
93         Idx::index(self)
94     }
95 }
96
97 impl Atom for LocationIndex {
98     fn index(self) -> usize {
99         Idx::index(self)
100     }
101 }
102
103 impl Atom for MovePathIndex {
104     fn index(self) -> usize {
105         Idx::index(self)
106     }
107 }
108
109 struct FactWriter<'w> {
110     location_table: &'w LocationTable,
111     dir: &'w Path,
112 }
113
114 impl<'w> FactWriter<'w> {
115     fn write_facts_to_path<T>(&self, rows: &[T], file_name: &str) -> Result<(), Box<dyn Error>>
116     where
117         T: FactRow,
118     {
119         let file = &self.dir.join(file_name);
120         let mut file = File::create(file)?;
121         for row in rows {
122             row.write(&mut file, self.location_table)?;
123         }
124         Ok(())
125     }
126 }
127
128 trait FactRow {
129     fn write(&self, out: &mut File, location_table: &LocationTable) -> Result<(), Box<dyn Error>>;
130 }
131
132 impl FactRow for RegionVid {
133     fn write(&self, out: &mut File, location_table: &LocationTable) -> Result<(), Box<dyn Error>> {
134         write_row(out, location_table, &[self])
135     }
136 }
137
138 impl<A, B> FactRow for (A, B)
139 where
140     A: FactCell,
141     B: FactCell,
142 {
143     fn write(&self, out: &mut File, location_table: &LocationTable) -> Result<(), Box<dyn Error>> {
144         write_row(out, location_table, &[&self.0, &self.1])
145     }
146 }
147
148 impl<A, B, C> FactRow for (A, B, C)
149 where
150     A: FactCell,
151     B: FactCell,
152     C: FactCell,
153 {
154     fn write(&self, out: &mut File, location_table: &LocationTable) -> Result<(), Box<dyn Error>> {
155         write_row(out, location_table, &[&self.0, &self.1, &self.2])
156     }
157 }
158
159 impl<A, B, C, D> FactRow for (A, B, C, D)
160 where
161     A: FactCell,
162     B: FactCell,
163     C: FactCell,
164     D: FactCell,
165 {
166     fn write(&self, out: &mut File, location_table: &LocationTable) -> Result<(), Box<dyn Error>> {
167         write_row(out, location_table, &[&self.0, &self.1, &self.2, &self.3])
168     }
169 }
170
171 fn write_row(
172     out: &mut dyn Write,
173     location_table: &LocationTable,
174     columns: &[&dyn FactCell],
175 ) -> Result<(), Box<dyn Error>> {
176     for (index, c) in columns.iter().enumerate() {
177         let tail = if index == columns.len() - 1 { "\n" } else { "\t" };
178         write!(out, "{:?}{}", c.to_string(location_table), tail)?;
179     }
180     Ok(())
181 }
182
183 trait FactCell {
184     fn to_string(&self, location_table: &LocationTable) -> String;
185 }
186
187 impl<A: Debug> FactCell for A {
188     default fn to_string(&self, _location_table: &LocationTable) -> String {
189         format!("{:?}", self)
190     }
191 }
192
193 impl FactCell for LocationIndex {
194     fn to_string(&self, location_table: &LocationTable) -> String {
195         format!("{:?}", location_table.to_location(*self))
196     }
197 }