rust/crates/hir-ty/src/traits.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

229 lines
7.2 KiB
Rust
Raw Normal View History

//! Trait solving using Chalk.
2021-04-09 07:39:07 -05:00
use std::env::var;
use chalk_ir::{fold::TypeFoldable, DebruijnIndex, GoalData};
2021-08-07 06:11:58 -05:00
use chalk_recursive::Cache;
use chalk_solve::{logging_db::LoggingRustIrDatabase, rust_ir, Solver};
2021-04-09 07:39:07 -05:00
use base_db::CrateId;
2023-02-13 05:55:14 -06:00
use hir_def::{
lang_item::{LangItem, LangItemTarget},
BlockId, TraitId,
2023-02-13 05:55:14 -06:00
};
use hir_expand::name::{name, Name};
use stdx::panic_context;
use triomphe::Arc;
use crate::{
db::HirDatabase, infer::unify::InferenceTable, utils::UnevaluatedConstEvaluatorFolder, AliasEq,
AliasTy, Canonical, DomainGoal, Goal, Guidance, InEnvironment, Interner, ProjectionTy,
ProjectionTyExt, Solution, TraitRefExt, Ty, TyKind, WhereClause,
};
2020-01-17 15:12:15 -06:00
/// This controls how much 'time' we give the Chalk solver before giving up.
const CHALK_SOLVER_FUEL: i32 = 1000;
#[derive(Debug, Copy, Clone)]
pub(crate) struct ChalkContext<'a> {
pub(crate) db: &'a dyn HirDatabase,
pub(crate) krate: CrateId,
pub(crate) block: Option<BlockId>,
}
2020-07-11 08:22:46 -05:00
fn create_chalk_solver() -> chalk_recursive::RecursiveSolver<Interner> {
let overflow_depth =
var("CHALK_OVERFLOW_DEPTH").ok().and_then(|s| s.parse().ok()).unwrap_or(500);
let max_size = var("CHALK_SOLVER_MAX_SIZE").ok().and_then(|s| s.parse().ok()).unwrap_or(150);
2021-08-07 06:11:58 -05:00
chalk_recursive::RecursiveSolver::new(overflow_depth, max_size, Some(Cache::new()))
}
/// 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`.
2021-03-12 12:12:17 -06:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2019-07-09 14:34:23 -05:00
pub struct TraitEnvironment {
pub krate: CrateId,
pub block: Option<BlockId>,
// FIXME make this a BTreeMap
2021-03-12 12:12:17 -06:00
pub(crate) traits_from_clauses: Vec<(Ty, TraitId)>,
pub env: chalk_ir::Environment<Interner>,
}
impl TraitEnvironment {
pub fn empty(krate: CrateId) -> Self {
TraitEnvironment {
krate,
block: None,
traits_from_clauses: Vec::new(),
2021-12-19 10:58:39 -06:00
env: chalk_ir::Environment::new(Interner),
}
}
pub fn traits_in_scope_from_clauses(&self, ty: Ty) -> impl Iterator<Item = TraitId> + '_ {
self.traits_from_clauses
.iter()
.filter_map(move |(self_ty, trait_id)| (*self_ty == ty).then_some(*trait_id))
}
}
2022-09-20 09:39:17 -05:00
pub(crate) fn normalize_projection_query(
db: &dyn HirDatabase,
projection: ProjectionTy,
env: Arc<TraitEnvironment>,
) -> Ty {
let mut table = InferenceTable::new(db, env);
let ty = table.normalize_projection_ty(projection);
table.resolve_completely(ty)
}
/// Solve a trait goal using Chalk.
2019-07-09 14:34:23 -05:00
pub(crate) fn trait_solve_query(
db: &dyn HirDatabase,
2019-11-27 00:42:55 -06:00
krate: CrateId,
block: Option<BlockId>,
goal: Canonical<InEnvironment<Goal>>,
) -> Option<Solution> {
2021-12-19 10:58:39 -06:00
let _p = profile::span("trait_solve_query").detail(|| match &goal.value.goal.data(Interner) {
GoalData::DomainGoal(DomainGoal::Holds(WhereClause::Implemented(it))) => {
db.trait_data(it.hir_trait_id()).name.display(db.upcast()).to_string()
}
GoalData::DomainGoal(DomainGoal::Holds(WhereClause::AliasEq(_))) => "alias_eq".to_string(),
2021-04-08 06:51:04 -05:00
_ => "??".to_string(),
});
2021-08-15 07:46:13 -05:00
tracing::info!("trait_solve_query({:?})", goal.value.goal);
2019-09-24 12:04:53 -05:00
if let GoalData::DomainGoal(DomainGoal::Holds(WhereClause::AliasEq(AliasEq {
alias: AliasTy::Projection(projection_ty),
..
2021-12-19 10:58:39 -06:00
}))) = &goal.value.goal.data(Interner)
{
2022-10-18 01:12:49 -05:00
if let TyKind::BoundVar(_) = projection_ty.self_type_parameter(db).kind(Interner) {
2019-09-24 12:04:53 -05:00
// Hack: don't ask Chalk to normalize with an unknown self type, it'll say that's impossible
return Some(Solution::Ambig(Guidance::Unknown));
}
}
// Chalk see `UnevaluatedConst` as a unique concrete value, but we see it as an alias for another const. So
// we should get rid of it when talking to chalk.
let goal = goal
.try_fold_with(&mut UnevaluatedConstEvaluatorFolder { db }, DebruijnIndex::INNERMOST)
.unwrap();
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: goal, universes: 1 };
solve(db, krate, block, &u_canonical)
}
fn solve(
db: &dyn HirDatabase,
krate: CrateId,
block: Option<BlockId>,
goal: &chalk_ir::UCanonical<chalk_ir::InEnvironment<chalk_ir::Goal<Interner>>>,
) -> Option<chalk_solve::Solution<Interner>> {
let context = ChalkContext { db, krate, block };
2021-08-15 07:46:13 -05:00
tracing::debug!("solve goal: {:?}", goal);
let mut solver = create_chalk_solver();
let fuel = std::cell::Cell::new(CHALK_SOLVER_FUEL);
let should_continue = || {
2021-05-17 12:07:10 -05:00
db.unwind_if_cancelled();
let remaining = fuel.get();
fuel.set(remaining - 1);
if remaining == 0 {
2021-08-15 07:46:13 -05:00
tracing::debug!("fuel exhausted");
}
remaining > 0
};
let mut solve = || {
let _ctx = if is_chalk_debug() || is_chalk_print() {
Some(panic_context::enter(format!("solving {goal:?}")))
} else {
None
};
let solution = if is_chalk_print() {
let logging_db =
LoggingRustIrDatabaseLoggingOnDrop(LoggingRustIrDatabase::new(context));
2021-09-03 09:00:50 -05:00
solver.solve_limited(&logging_db.0, goal, &should_continue)
} else {
2020-08-16 11:15:44 -05:00
solver.solve_limited(&context, goal, &should_continue)
};
2021-08-15 07:46:13 -05:00
tracing::debug!("solve({:?}) => {:?}", goal, solution);
solution
};
// don't set the TLS for Chalk unless Chalk debugging is active, to make
// extra sure we only use it for debugging
2021-09-03 09:00:50 -05:00
if is_chalk_debug() {
crate::tls::set_current_program(db, solve)
} else {
solve()
}
}
struct LoggingRustIrDatabaseLoggingOnDrop<'a>(LoggingRustIrDatabase<Interner, ChalkContext<'a>>);
impl Drop for LoggingRustIrDatabaseLoggingOnDrop<'_> {
fn drop(&mut self) {
eprintln!("chalk program:\n{}", self.0);
}
}
fn is_chalk_debug() -> bool {
std::env::var("CHALK_DEBUG").is_ok()
}
fn is_chalk_print() -> bool {
std::env::var("CHALK_PRINT").is_ok()
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
2019-09-09 15:10:58 -05:00
pub enum FnTrait {
// Warning: Order is important. If something implements `x` it should also implement
// `y` if `y <= x`.
2019-09-09 15:10:58 -05:00
FnOnce,
FnMut,
Fn,
}
impl FnTrait {
2023-02-13 05:55:14 -06:00
const fn lang_item(self) -> LangItem {
2019-09-09 15:10:58 -05:00
match self {
2023-02-13 05:55:14 -06:00
FnTrait::FnOnce => LangItem::FnOnce,
FnTrait::FnMut => LangItem::FnMut,
FnTrait::Fn => LangItem::Fn,
2019-09-09 15:10:58 -05:00
}
}
2020-06-20 01:42:35 -05:00
pub const fn to_chalk_ir(self) -> rust_ir::ClosureKind {
match self {
FnTrait::FnOnce => rust_ir::ClosureKind::FnOnce,
FnTrait::FnMut => rust_ir::ClosureKind::FnMut,
FnTrait::Fn => rust_ir::ClosureKind::Fn,
}
}
pub fn method_name(self) -> Name {
match self {
FnTrait::FnOnce => name!(call_once),
FnTrait::FnMut => name!(call_mut),
FnTrait::Fn => name!(call),
}
}
pub fn get_id(self, db: &dyn HirDatabase, krate: CrateId) -> Option<TraitId> {
2023-02-13 05:55:14 -06:00
let target = db.lang_item(krate, self.lang_item())?;
2020-06-20 01:42:35 -05:00
match target {
2023-02-13 05:55:14 -06:00
LangItemTarget::Trait(t) => Some(t),
2020-06-20 01:42:35 -05:00
_ => None,
}
}
2019-09-09 15:10:58 -05:00
}