2019-05-01 10:06:11 -05:00
|
|
|
//! Trait solving using Chalk.
|
2019-06-01 02:57:44 -05:00
|
|
|
use std::sync::Arc;
|
2019-03-31 13:02:16 -05:00
|
|
|
|
2019-05-01 10:06:11 -05:00
|
|
|
use chalk_ir::cast::Cast;
|
2019-07-04 15:05:17 -05:00
|
|
|
use log::debug;
|
|
|
|
use parking_lot::Mutex;
|
2019-07-05 09:45:57 -05:00
|
|
|
use ra_db::salsa;
|
2019-05-21 08:04:17 -05:00
|
|
|
use ra_prof::profile;
|
2019-07-04 15:05:17 -05:00
|
|
|
use rustc_hash::FxHashSet;
|
2019-03-31 13:02:16 -05:00
|
|
|
|
2019-09-03 06:10:00 -05:00
|
|
|
use super::{Canonical, GenericPredicate, HirDisplay, ProjectionTy, TraitRef, Ty, TypeWalk};
|
2019-07-04 15:05:17 -05:00
|
|
|
use crate::{db::HirDatabase, Crate, ImplBlock, Trait};
|
2019-05-01 10:06:11 -05:00
|
|
|
|
2019-07-04 15:05:17 -05:00
|
|
|
use self::chalk::{from_chalk, ToChalk};
|
2019-05-01 10:06:11 -05:00
|
|
|
|
2019-06-26 04:54:13 -05:00
|
|
|
pub(crate) mod chalk;
|
2019-03-31 13:02:16 -05:00
|
|
|
|
2019-07-05 09:45:57 -05:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct TraitSolver {
|
|
|
|
krate: Crate,
|
|
|
|
inner: Arc<Mutex<chalk_solve::Solver>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// We need eq for salsa
|
|
|
|
impl PartialEq for TraitSolver {
|
|
|
|
fn eq(&self, other: &TraitSolver) -> bool {
|
|
|
|
Arc::ptr_eq(&self.inner, &other.inner)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for TraitSolver {}
|
|
|
|
|
|
|
|
impl TraitSolver {
|
|
|
|
fn solve(
|
|
|
|
&self,
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
goal: &chalk_ir::UCanonical<chalk_ir::InEnvironment<chalk_ir::Goal>>,
|
|
|
|
) -> Option<chalk_solve::Solution> {
|
|
|
|
let context = ChalkContext { db, krate: self.krate };
|
|
|
|
debug!("solve goal: {:?}", goal);
|
2019-06-26 01:12:46 -05:00
|
|
|
let solution = self.inner.lock().solve(&context, goal);
|
2019-07-05 09:45:57 -05:00
|
|
|
debug!("solve({:?}) => {:?}", goal, solution);
|
|
|
|
solution
|
|
|
|
}
|
|
|
|
}
|
2019-05-01 10:13:33 -05:00
|
|
|
|
2019-05-07 10:35:45 -05:00
|
|
|
/// This controls the maximum size of types Chalk considers. If we set this too
|
|
|
|
/// high, we can run into slow edge cases; if we set it too low, Chalk won't
|
|
|
|
/// find some solutions.
|
2019-05-13 15:20:39 -05:00
|
|
|
const CHALK_SOLVER_MAX_SIZE: usize = 4;
|
2019-05-07 10:35:45 -05:00
|
|
|
|
2019-04-20 05:34:36 -05:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
struct ChalkContext<'a, DB> {
|
|
|
|
db: &'a DB,
|
|
|
|
krate: Crate,
|
|
|
|
}
|
2019-03-31 13:02:16 -05:00
|
|
|
|
2019-07-05 09:45:57 -05:00
|
|
|
pub(crate) fn trait_solver_query(
|
|
|
|
db: &(impl HirDatabase + salsa::Database),
|
|
|
|
krate: Crate,
|
|
|
|
) -> TraitSolver {
|
|
|
|
db.salsa_runtime().report_untracked_read();
|
2019-04-20 05:34:36 -05:00
|
|
|
// krate parameter is just so we cache a unique solver per crate
|
2019-05-07 10:35:45 -05:00
|
|
|
let solver_choice = chalk_solve::SolverChoice::SLG { max_size: CHALK_SOLVER_MAX_SIZE };
|
2019-07-05 09:45:57 -05:00
|
|
|
debug!("Creating new solver for crate {:?}", krate);
|
|
|
|
TraitSolver { krate, inner: Arc::new(Mutex::new(solver_choice.into_solver())) }
|
2019-04-20 05:34:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Collects impls for the given trait in the whole dependency tree of `krate`.
|
2019-05-21 04:44:08 -05:00
|
|
|
pub(crate) fn impls_for_trait_query(
|
2019-04-20 05:34:36 -05:00
|
|
|
db: &impl HirDatabase,
|
|
|
|
krate: Crate,
|
|
|
|
trait_: Trait,
|
|
|
|
) -> Arc<[ImplBlock]> {
|
2019-05-07 05:09:57 -05:00
|
|
|
let mut impls = FxHashSet::default();
|
2019-04-20 05:34:36 -05:00
|
|
|
// We call the query recursively here. On the one hand, this means we can
|
|
|
|
// reuse results from queries for different crates; on the other hand, this
|
|
|
|
// will only ever get called for a few crates near the root of the tree (the
|
|
|
|
// ones the user is editing), so this may actually be a waste of memory. I'm
|
|
|
|
// doing it like this mainly for simplicity for now.
|
|
|
|
for dep in krate.dependencies(db) {
|
|
|
|
impls.extend(db.impls_for_trait(dep.krate, trait_).iter());
|
|
|
|
}
|
|
|
|
let crate_impl_blocks = db.impls_in_crate(krate);
|
2019-07-05 11:02:32 -05:00
|
|
|
impls.extend(crate_impl_blocks.lookup_impl_blocks_for_trait(trait_));
|
2019-05-07 05:09:57 -05:00
|
|
|
impls.into_iter().collect::<Vec<_>>().into()
|
2019-04-20 05:34:36 -05:00
|
|
|
}
|
|
|
|
|
2019-06-29 10:40:00 -05:00
|
|
|
/// A set of clauses that we assume to be true. E.g. if we are inside this function:
|
|
|
|
/// ```rust
|
|
|
|
/// fn foo<T: Default>(t: T) {}
|
|
|
|
/// ```
|
|
|
|
/// we assume that `T: Default`.
|
2019-06-29 12:14:52 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
2019-07-09 14:34:23 -05:00
|
|
|
pub struct TraitEnvironment {
|
2019-06-29 12:14:52 -05:00
|
|
|
pub predicates: Vec<GenericPredicate>,
|
|
|
|
}
|
2019-06-29 10:40:00 -05:00
|
|
|
|
|
|
|
/// Something (usually a goal), along with an environment.
|
2019-06-29 12:14:52 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
2019-06-29 10:40:00 -05:00
|
|
|
pub struct InEnvironment<T> {
|
2019-07-09 14:34:23 -05:00
|
|
|
pub environment: Arc<TraitEnvironment>,
|
2019-06-29 10:40:00 -05:00
|
|
|
pub value: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> InEnvironment<T> {
|
2019-07-09 14:34:23 -05:00
|
|
|
pub fn new(environment: Arc<TraitEnvironment>, value: T) -> InEnvironment<T> {
|
2019-06-29 10:40:00 -05:00
|
|
|
InEnvironment { environment, value }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-31 13:02:16 -05:00
|
|
|
/// Something that needs to be proven (by Chalk) during type checking, e.g. that
|
|
|
|
/// a certain type implements a certain trait. Proving the Obligation might
|
|
|
|
/// result in additional information about inference variables.
|
2019-07-08 14:43:52 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
2019-03-31 13:02:16 -05:00
|
|
|
pub enum Obligation {
|
|
|
|
/// Prove that a certain type implements a trait (the type is the `Self` type
|
|
|
|
/// parameter to the `TraitRef`).
|
|
|
|
Trait(TraitRef),
|
2019-07-07 02:31:09 -05:00
|
|
|
Projection(ProjectionPredicate),
|
2019-05-12 10:53:44 -05:00
|
|
|
}
|
|
|
|
|
2019-07-06 09:41:04 -05:00
|
|
|
impl Obligation {
|
|
|
|
pub fn from_predicate(predicate: GenericPredicate) -> Option<Obligation> {
|
|
|
|
match predicate {
|
|
|
|
GenericPredicate::Implemented(trait_ref) => Some(Obligation::Trait(trait_ref)),
|
2019-08-23 10:19:37 -05:00
|
|
|
GenericPredicate::Projection(projection_pred) => {
|
|
|
|
Some(Obligation::Projection(projection_pred))
|
|
|
|
}
|
2019-07-06 09:41:04 -05:00
|
|
|
GenericPredicate::Error => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 10:53:44 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub struct ProjectionPredicate {
|
2019-05-12 11:33:47 -05:00
|
|
|
pub projection_ty: ProjectionTy,
|
|
|
|
pub ty: Ty,
|
2019-03-31 13:02:16 -05:00
|
|
|
}
|
|
|
|
|
2019-09-03 06:10:00 -05:00
|
|
|
impl TypeWalk for ProjectionPredicate {
|
|
|
|
fn walk(&self, f: &mut impl FnMut(&Ty)) {
|
2019-08-23 10:19:37 -05:00
|
|
|
self.projection_ty.walk(f);
|
|
|
|
self.ty.walk(f);
|
|
|
|
}
|
|
|
|
|
2019-09-03 06:10:00 -05:00
|
|
|
fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) {
|
2019-08-23 10:19:37 -05:00
|
|
|
self.projection_ty.walk_mut(f);
|
|
|
|
self.ty.walk_mut(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 14:43:52 -05:00
|
|
|
/// Solve a trait goal using Chalk.
|
2019-07-09 14:34:23 -05:00
|
|
|
pub(crate) fn trait_solve_query(
|
2019-05-21 12:57:36 -05:00
|
|
|
db: &impl HirDatabase,
|
|
|
|
krate: Crate,
|
2019-08-10 05:13:39 -05:00
|
|
|
goal: Canonical<InEnvironment<Obligation>>,
|
2019-04-20 05:34:36 -05:00
|
|
|
) -> Option<Solution> {
|
2019-07-09 14:34:23 -05:00
|
|
|
let _p = profile("trait_solve_query");
|
2019-08-10 05:13:39 -05:00
|
|
|
debug!("trait_solve_query({})", goal.value.value.display(db));
|
|
|
|
let canonical = goal.to_chalk(db).cast();
|
2019-05-21 12:57:36 -05:00
|
|
|
// We currently don't deal with universes (I think / hope they're not yet
|
|
|
|
// relevant for our use cases?)
|
|
|
|
let u_canonical = chalk_ir::UCanonical { canonical, universes: 1 };
|
2019-07-05 09:45:57 -05:00
|
|
|
let solution = db.trait_solver(krate).solve(db, &u_canonical);
|
2019-05-21 12:57:36 -05:00
|
|
|
solution.map(|solution| solution_from_chalk(db, solution))
|
2019-03-31 13:02:16 -05:00
|
|
|
}
|
|
|
|
|
2019-05-01 10:57:56 -05:00
|
|
|
fn solution_from_chalk(db: &impl HirDatabase, solution: chalk_solve::Solution) -> Solution {
|
2019-04-20 05:34:36 -05:00
|
|
|
let convert_subst = |subst: chalk_ir::Canonical<chalk_ir::Substitution>| {
|
|
|
|
let value = subst
|
|
|
|
.value
|
|
|
|
.parameters
|
|
|
|
.into_iter()
|
|
|
|
.map(|p| {
|
|
|
|
let ty = match p {
|
|
|
|
chalk_ir::Parameter(chalk_ir::ParameterKind::Ty(ty)) => from_chalk(db, ty),
|
|
|
|
chalk_ir::Parameter(chalk_ir::ParameterKind::Lifetime(_)) => unimplemented!(),
|
|
|
|
};
|
|
|
|
ty
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let result = Canonical { value, num_vars: subst.binders.len() };
|
|
|
|
SolutionVariables(result)
|
|
|
|
};
|
|
|
|
match solution {
|
2019-05-01 09:37:52 -05:00
|
|
|
chalk_solve::Solution::Unique(constr_subst) => {
|
2019-04-20 05:34:36 -05:00
|
|
|
let subst = chalk_ir::Canonical {
|
|
|
|
value: constr_subst.value.subst,
|
|
|
|
binders: constr_subst.binders,
|
|
|
|
};
|
2019-05-01 09:37:52 -05:00
|
|
|
Solution::Unique(convert_subst(subst))
|
2019-04-20 05:34:36 -05:00
|
|
|
}
|
2019-05-01 09:37:52 -05:00
|
|
|
chalk_solve::Solution::Ambig(chalk_solve::Guidance::Definite(subst)) => {
|
|
|
|
Solution::Ambig(Guidance::Definite(convert_subst(subst)))
|
2019-04-20 05:34:36 -05:00
|
|
|
}
|
2019-05-01 09:37:52 -05:00
|
|
|
chalk_solve::Solution::Ambig(chalk_solve::Guidance::Suggested(subst)) => {
|
|
|
|
Solution::Ambig(Guidance::Suggested(convert_subst(subst)))
|
2019-03-31 13:02:16 -05:00
|
|
|
}
|
2019-05-01 09:37:52 -05:00
|
|
|
chalk_solve::Solution::Ambig(chalk_solve::Guidance::Unknown) => {
|
|
|
|
Solution::Ambig(Guidance::Unknown)
|
2019-04-20 05:34:36 -05:00
|
|
|
}
|
2019-03-31 13:02:16 -05:00
|
|
|
}
|
2019-04-20 05:34:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2019-05-05 09:04:31 -05:00
|
|
|
pub struct SolutionVariables(pub Canonical<Vec<Ty>>);
|
2019-04-20 05:34:36 -05:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
/// A (possible) solution for a proposed goal.
|
2019-05-05 09:04:31 -05:00
|
|
|
pub enum Solution {
|
2019-04-20 05:34:36 -05:00
|
|
|
/// The goal indeed holds, and there is a unique value for all existential
|
|
|
|
/// variables.
|
|
|
|
Unique(SolutionVariables),
|
|
|
|
|
|
|
|
/// The goal may be provable in multiple ways, but regardless we may have some guidance
|
|
|
|
/// for type inference. In this case, we don't return any lifetime
|
|
|
|
/// constraints, since we have not "committed" to any particular solution
|
|
|
|
/// yet.
|
|
|
|
Ambig(Guidance),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
/// When a goal holds ambiguously (e.g., because there are multiple possible
|
|
|
|
/// solutions), we issue a set of *guidance* back to type inference.
|
2019-05-05 09:04:31 -05:00
|
|
|
pub enum Guidance {
|
2019-04-20 05:34:36 -05:00
|
|
|
/// The existential variables *must* have the given values if the goal is
|
|
|
|
/// ever to hold, but that alone isn't enough to guarantee the goal will
|
|
|
|
/// actually hold.
|
|
|
|
Definite(SolutionVariables),
|
|
|
|
|
|
|
|
/// There are multiple plausible values for the existentials, but the ones
|
|
|
|
/// here are suggested as the preferred choice heuristically. These should
|
|
|
|
/// be used for inference fallback only.
|
|
|
|
Suggested(SolutionVariables),
|
|
|
|
|
|
|
|
/// There's no useful information to feed back to type inference
|
|
|
|
Unknown,
|
2019-03-31 13:02:16 -05:00
|
|
|
}
|