1 #![deny(rustc::untranslatable_diagnostic)]
2 #![deny(rustc::diagnostic_outside_of_impl)]
3 use crate::location::{LocationIndex, LocationTable};
4 use crate::BorrowIndex;
5 use polonius_engine::AllFacts as PoloniusFacts;
6 use polonius_engine::Atom;
7 use rustc_index::vec::Idx;
8 use rustc_middle::mir::Local;
9 use rustc_middle::ty::{RegionVid, TyCtxt};
10 use rustc_mir_dataflow::move_paths::MovePathIndex;
11 use std::error::Error;
13 use std::fs::{self, File};
14 use std::io::{BufWriter, Write};
17 #[derive(Copy, Clone, Debug)]
18 pub struct RustcFacts;
20 impl polonius_engine::FactTypes for RustcFacts {
21 type Origin = RegionVid;
22 type Loan = BorrowIndex;
23 type Point = LocationIndex;
24 type Variable = Local;
25 type Path = MovePathIndex;
28 pub type AllFacts = PoloniusFacts<RustcFacts>;
30 pub(crate) trait AllFactsExt {
31 /// Returns `true` if there is a need to gather `AllFacts` given the
32 /// current `-Z` flags.
33 fn enabled(tcx: TyCtxt<'_>) -> bool;
37 dir: impl AsRef<Path>,
38 location_table: &LocationTable,
39 ) -> Result<(), Box<dyn Error>>;
42 impl AllFactsExt for AllFacts {
44 fn enabled(tcx: TyCtxt<'_>) -> bool {
45 tcx.sess.opts.unstable_opts.nll_facts || tcx.sess.opts.unstable_opts.polonius
50 dir: impl AsRef<Path>,
51 location_table: &LocationTable,
52 ) -> Result<(), Box<dyn Error>> {
53 let dir: &Path = dir.as_ref();
54 fs::create_dir_all(dir)?;
55 let wr = FactWriter { location_table, dir };
56 macro_rules! write_facts_to_path {
57 ($wr:ident . write_facts_to_path($this:ident . [
61 $wr.write_facts_to_path(
63 &format!("{}.facts", stringify!($field))
68 write_facts_to_path! {
69 wr.write_facts_to_path(self.[
79 use_of_var_derefs_origin,
80 drop_of_var_derefs_origin,
83 path_assigned_at_base,
85 path_accessed_at_base,
86 known_placeholder_subset,
94 impl Atom for BorrowIndex {
95 fn index(self) -> usize {
100 impl Atom for LocationIndex {
101 fn index(self) -> usize {
106 struct FactWriter<'w> {
107 location_table: &'w LocationTable,
111 impl<'w> FactWriter<'w> {
112 fn write_facts_to_path<T>(&self, rows: &[T], file_name: &str) -> Result<(), Box<dyn Error>>
116 let file = &self.dir.join(file_name);
117 let mut file = BufWriter::new(File::create(file)?);
119 row.write(&mut file, self.location_table)?;
129 location_table: &LocationTable,
130 ) -> Result<(), Box<dyn Error>>;
133 impl FactRow for RegionVid {
137 location_table: &LocationTable,
138 ) -> Result<(), Box<dyn Error>> {
139 write_row(out, location_table, &[self])
143 impl<A, B> FactRow for (A, B)
151 location_table: &LocationTable,
152 ) -> Result<(), Box<dyn Error>> {
153 write_row(out, location_table, &[&self.0, &self.1])
157 impl<A, B, C> FactRow for (A, B, C)
166 location_table: &LocationTable,
167 ) -> Result<(), Box<dyn Error>> {
168 write_row(out, location_table, &[&self.0, &self.1, &self.2])
172 impl<A, B, C, D> FactRow for (A, B, C, D)
182 location_table: &LocationTable,
183 ) -> Result<(), Box<dyn Error>> {
184 write_row(out, location_table, &[&self.0, &self.1, &self.2, &self.3])
190 location_table: &LocationTable,
191 columns: &[&dyn FactCell],
192 ) -> Result<(), Box<dyn Error>> {
193 for (index, c) in columns.iter().enumerate() {
194 let tail = if index == columns.len() - 1 { "\n" } else { "\t" };
195 write!(out, "{:?}{}", c.to_string(location_table), tail)?;
201 fn to_string(&self, location_table: &LocationTable) -> String;
204 impl<A: Debug> FactCell for A {
205 default fn to_string(&self, _location_table: &LocationTable) -> String {
206 format!("{:?}", self)
210 impl FactCell for LocationIndex {
211 fn to_string(&self, location_table: &LocationTable) -> String {
212 format!("{:?}", location_table.to_location(*self))