Skip to content

Rewrite polonius-engine #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 132 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions polonius-engine/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
[package]
name = "polonius-engine"
version = "0.13.0"
version = "0.14.0"
authors = ["The Rust Project Developers", "Polonius Developers"]
description = "Core definition for the Rust borrow checker"
license = "Apache-2.0/MIT"
repository = "https://github.com/rust-lang-nursery/polonius"
readme = "README.md"
keywords = ["compiler", "borrowck", "datalog"]
edition = "2018"

[dependencies]
contracts = "0.6.2"
datafrog = "2.0.0"
rustc-hash = "1.0.0"
dyn-clone = "1.0.4"
log = "0.4"
paste = "1.0.6"
pretty_assertions = "1.0.0"
rustc-hash = "1.0.0"
smallvec = "1.8.0"
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt::Debug;
use std::hash::Hash;
//! An adapter for the existing `polonius-engine` interface.

use crate::{Db, Dump, FactTypes, StoreTo};

/// The "facts" which are the basis of the NLL borrow analysis.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -114,16 +115,51 @@ impl<T: FactTypes> Default for AllFacts<T> {
}
}

pub trait Atom:
From<usize> + Into<usize> + Copy + Clone + Debug + Eq + Ord + Hash + 'static
{
fn index(self) -> usize;
}

pub trait FactTypes: Copy + Clone + Debug {
type Origin: Atom;
type Loan: Atom;
type Point: Atom;
type Variable: Atom;
type Path: Atom;
impl<T: FactTypes> StoreTo<T> for AllFacts<T> {
const RELATIONS: crate::Rels = &[
"loan_issued_at",
"universal_region",
"cfg_edge",
"loan_killed_at",
"subset_base",
"loan_invalidated_at",
"var_used_at",
"var_defined_at",
"var_dropped_at",
"use_of_var_derefs_origin",
"drop_of_var_derefs_origin",
"child_path",
"path_is_var",
"path_assigned_at_base",
"path_moved_at_base",
"path_accessed_at_base",
"known_placeholder_subset_base",
"placeholder",
];

fn store_to_db(self, db: &mut Db<T>, _: &mut Dump<'_>) {
db.loan_issued_at = Some(self.loan_issued_at.into());
db.universal_region = Some(self.universal_region.into_iter().map(|x| (x,)).collect());
db.cfg_edge = Some(self.cfg_edge.into());
db.loan_killed_at = Some(self.loan_killed_at.into());
db.subset_base = Some(self.subset_base.into());
db.loan_invalidated_at = Some(
self.loan_invalidated_at
.into_iter()
.map(|(a, b)| (b, a))
.collect(),
);
db.var_used_at = Some(self.var_used_at.into());
db.var_defined_at = Some(self.var_defined_at.into());
db.var_dropped_at = Some(self.var_dropped_at.into());
db.use_of_var_derefs_origin = Some(self.use_of_var_derefs_origin.into());
db.drop_of_var_derefs_origin = Some(self.drop_of_var_derefs_origin.into());
db.child_path = Some(self.child_path.into());
db.path_is_var = Some(self.path_is_var.into());
db.path_assigned_at_base = Some(self.path_assigned_at_base.into());
db.path_moved_at_base = Some(self.path_moved_at_base.into());
db.path_accessed_at_base = Some(self.path_accessed_at_base.into());
db.known_placeholder_subset_base = Some(self.known_placeholder_subset.into());
db.placeholder = Some(self.placeholder.into());
}
}
69 changes: 69 additions & 0 deletions polonius-engine/src/compat/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::{FactTypes, Pipeline};

mod all_facts;
mod output;

pub use self::all_facts::AllFacts;
pub use self::output::Output;

#[derive(Debug, Clone, Copy)]
pub enum Algorithm {
/// Simple rules, but slower to execute
Naive,

/// Optimized variant of the rules
DatafrogOpt,

/// Fast to compute, but imprecise: there can be false-positives
/// but no false-negatives. Tailored for quick "early return" situations.
LocationInsensitive,

/// Compares the `Naive` and `DatafrogOpt` variants to ensure they indeed
/// compute the same errors.
Compare,

/// Combination of the fast `LocationInsensitive` pre-pass, followed by
/// the more expensive `DatafrogOpt` variant.
Hybrid,
}

impl Algorithm {
/// Optimized variants that ought to be equivalent to "naive"
pub const OPTIMIZED: &'static [Algorithm] = &[Algorithm::DatafrogOpt];

pub fn variants() -> [&'static str; 5] {
[
"Naive",
"DatafrogOpt",
"LocationInsensitive",
"Compare",
"Hybrid",
]
}

fn pipeline<T: FactTypes>(&self) -> Pipeline<T> {
match self {
Algorithm::Naive => Pipeline::naive(),
Algorithm::DatafrogOpt => Pipeline::opt(),
Algorithm::LocationInsensitive => Pipeline::location_insensitive(),
Algorithm::Compare => Pipeline::compare(),
Algorithm::Hybrid => Pipeline::hybrid(),
}
}
}

impl ::std::str::FromStr for Algorithm {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_ref() {
"naive" => Ok(Algorithm::Naive),
"datafrogopt" => Ok(Algorithm::DatafrogOpt),
"locationinsensitive" => Ok(Algorithm::LocationInsensitive),
"compare" => Ok(Algorithm::Compare),
"hybrid" => Ok(Algorithm::Hybrid),
_ => Err(String::from(
"valid values: Naive, DatafrogOpt, LocationInsensitive, Compare, Hybrid",
)),
}
}
}
Loading