2019-01-08 17:47:12 -06:00
|
|
|
//! In certain situations, rust automatically inserts derefs as necessary: for
|
2019-01-06 12:51:42 -06:00
|
|
|
//! example, field accesses `foo.bar` still work when `foo` is actually a
|
|
|
|
//! reference to a type with the field `bar`. This is an approximation of the
|
|
|
|
//! logic in rustc (which lives in librustc_typeck/check/autoderef.rs).
|
|
|
|
|
2019-04-13 09:43:49 -05:00
|
|
|
use std::iter::successors;
|
2019-01-06 12:51:42 -06:00
|
|
|
|
2020-08-13 09:25:38 -05:00
|
|
|
use base_db::CrateId;
|
2021-04-08 16:34:05 -05:00
|
|
|
use chalk_ir::{cast::Cast, fold::Fold, interner::HasInterner, VariableKind};
|
2019-11-25 04:10:26 -06:00
|
|
|
use hir_def::lang_item::LangItemTarget;
|
2019-12-13 15:01:06 -06:00
|
|
|
use hir_expand::name::name;
|
2021-07-10 15:49:17 -05:00
|
|
|
use limit::Limit;
|
2021-08-15 07:46:13 -05:00
|
|
|
use tracing::{info, warn};
|
2019-01-06 12:51:42 -06:00
|
|
|
|
2019-12-07 04:50:36 -06:00
|
|
|
use crate::{
|
2021-04-08 16:34:05 -05:00
|
|
|
db::HirDatabase, static_lifetime, AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds,
|
2021-08-09 04:30:05 -05:00
|
|
|
ConstrainedSubst, DebruijnIndex, Environment, Guidance, InEnvironment, Interner,
|
|
|
|
ProjectionTyExt, Solution, Substitution, Ty, TyBuilder, TyKind,
|
2019-11-25 03:45:45 -06:00
|
|
|
};
|
2019-05-12 11:33:47 -05:00
|
|
|
|
2021-07-10 15:49:17 -05:00
|
|
|
const AUTODEREF_RECURSION_LIMIT: Limit = Limit::new(10);
|
|
|
|
|
2021-07-09 12:12:56 -05:00
|
|
|
pub(crate) enum AutoderefKind {
|
|
|
|
Builtin,
|
|
|
|
Overloaded,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct Autoderef<'db> {
|
|
|
|
db: &'db dyn HirDatabase,
|
|
|
|
ty: Canonical<Ty>,
|
|
|
|
at_start: bool,
|
|
|
|
krate: Option<CrateId>,
|
|
|
|
environment: Environment,
|
|
|
|
steps: Vec<(AutoderefKind, Ty)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'db> Autoderef<'db> {
|
|
|
|
pub(crate) fn new(
|
|
|
|
db: &'db dyn HirDatabase,
|
|
|
|
krate: Option<CrateId>,
|
|
|
|
ty: InEnvironment<Canonical<Ty>>,
|
|
|
|
) -> Self {
|
|
|
|
let InEnvironment { goal: ty, environment } = ty;
|
|
|
|
Autoderef { db, ty, at_start: true, environment, krate, steps: Vec::new() }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn step_count(&self) -> usize {
|
|
|
|
self.steps.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn steps(&self) -> &[(AutoderefKind, chalk_ir::Ty<Interner>)] {
|
|
|
|
&self.steps
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn final_ty(&self) -> Ty {
|
|
|
|
self.ty.value.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for Autoderef<'_> {
|
|
|
|
type Item = (Canonical<Ty>, usize);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.at_start {
|
|
|
|
self.at_start = false;
|
|
|
|
return Some((self.ty.clone(), 0));
|
|
|
|
}
|
|
|
|
|
2021-07-10 15:49:17 -05:00
|
|
|
if AUTODEREF_RECURSION_LIMIT.check(self.steps.len() + 1).is_err() {
|
2021-07-09 12:12:56 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let (kind, new_ty) = if let Some(derefed) = builtin_deref(&self.ty.value) {
|
|
|
|
(AutoderefKind::Builtin, Canonical { value: derefed, binders: self.ty.binders.clone() })
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
AutoderefKind::Overloaded,
|
|
|
|
deref_by_trait(
|
|
|
|
self.db,
|
|
|
|
self.krate?,
|
|
|
|
InEnvironment { goal: &self.ty, environment: self.environment.clone() },
|
|
|
|
)?,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
self.steps.push((kind, self.ty.value.clone()));
|
|
|
|
self.ty = new_ty;
|
|
|
|
|
|
|
|
Some((self.ty.clone(), self.step_count()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: replace uses of this with Autoderef above
|
2019-11-27 08:46:02 -06:00
|
|
|
pub fn autoderef<'a>(
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &'a dyn HirDatabase,
|
2019-11-25 04:10:26 -06:00
|
|
|
krate: Option<CrateId>,
|
|
|
|
ty: InEnvironment<Canonical<Ty>>,
|
2019-05-12 11:33:47 -05:00
|
|
|
) -> impl Iterator<Item = Canonical<Ty>> + 'a {
|
2021-03-21 14:19:07 -05:00
|
|
|
let InEnvironment { goal: ty, environment } = ty;
|
2019-11-25 04:10:26 -06:00
|
|
|
successors(Some(ty), move |ty| {
|
2021-03-21 14:19:07 -05:00
|
|
|
deref(db, krate?, InEnvironment { goal: ty, environment: environment.clone() })
|
2019-11-25 04:10:26 -06:00
|
|
|
})
|
2021-07-10 15:49:17 -05:00
|
|
|
.take(AUTODEREF_RECURSION_LIMIT.inner())
|
2019-05-12 11:33:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn deref(
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn HirDatabase,
|
2019-11-25 04:10:26 -06:00
|
|
|
krate: CrateId,
|
|
|
|
ty: InEnvironment<&Canonical<Ty>>,
|
2019-05-12 11:33:47 -05:00
|
|
|
) -> Option<Canonical<Ty>> {
|
2021-04-14 09:15:37 -05:00
|
|
|
let _p = profile::span("deref");
|
2021-04-07 06:06:48 -05:00
|
|
|
if let Some(derefed) = builtin_deref(&ty.goal.value) {
|
2021-03-21 14:19:07 -05:00
|
|
|
Some(Canonical { value: derefed, binders: ty.goal.binders.clone() })
|
2019-05-12 11:33:47 -05:00
|
|
|
} else {
|
2019-11-25 04:10:26 -06:00
|
|
|
deref_by_trait(db, krate, ty)
|
2019-01-06 12:51:42 -06:00
|
|
|
}
|
2019-05-12 11:33:47 -05:00
|
|
|
}
|
|
|
|
|
2021-04-07 06:06:48 -05:00
|
|
|
fn builtin_deref(ty: &Ty) -> Option<Ty> {
|
|
|
|
match ty.kind(&Interner) {
|
|
|
|
TyKind::Ref(.., ty) => Some(ty.clone()),
|
|
|
|
TyKind::Raw(.., ty) => Some(ty.clone()),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 11:33:47 -05:00
|
|
|
fn deref_by_trait(
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn HirDatabase,
|
2019-11-25 03:45:45 -06:00
|
|
|
krate: CrateId,
|
|
|
|
ty: InEnvironment<&Canonical<Ty>>,
|
2019-05-12 11:33:47 -05:00
|
|
|
) -> Option<Canonical<Ty>> {
|
2021-04-14 08:59:08 -05:00
|
|
|
let _p = profile::span("deref_by_trait");
|
2019-12-20 14:14:30 -06:00
|
|
|
let deref_trait = match db.lang_item(krate, "deref".into())? {
|
2019-11-26 08:21:29 -06:00
|
|
|
LangItemTarget::TraitId(it) => it,
|
2019-05-12 11:33:47 -05:00
|
|
|
_ => return None,
|
|
|
|
};
|
2019-12-13 15:01:06 -06:00
|
|
|
let target = db.trait_data(deref_trait).associated_type_by_name(&name![Target])?;
|
2019-05-12 11:33:47 -05:00
|
|
|
|
2021-04-03 14:56:18 -05:00
|
|
|
let projection = {
|
|
|
|
let b = TyBuilder::assoc_type_projection(db, target);
|
|
|
|
if b.remaining() != 1 {
|
|
|
|
// the Target type + Deref trait should only have one generic parameter,
|
|
|
|
// namely Deref's Self type
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
b.push(ty.goal.value.clone()).build()
|
|
|
|
};
|
2019-06-15 11:33:30 -05:00
|
|
|
|
2020-04-05 11:24:18 -05:00
|
|
|
// FIXME make the Canonical / bound var handling nicer
|
2019-05-12 11:33:47 -05:00
|
|
|
|
2020-04-10 10:44:43 -05:00
|
|
|
// Check that the type implements Deref at all
|
2021-04-03 14:56:18 -05:00
|
|
|
let trait_ref = projection.trait_ref(db);
|
2020-06-28 14:17:27 -05:00
|
|
|
let implements_goal = Canonical {
|
2021-03-21 14:19:07 -05:00
|
|
|
binders: ty.goal.binders.clone(),
|
2020-04-10 10:44:43 -05:00
|
|
|
value: InEnvironment {
|
2021-03-21 14:19:07 -05:00
|
|
|
goal: trait_ref.cast(&Interner),
|
2020-04-10 10:44:43 -05:00
|
|
|
environment: ty.environment.clone(),
|
|
|
|
},
|
|
|
|
};
|
2021-04-14 11:11:17 -05:00
|
|
|
if db.trait_solve(krate, implements_goal).is_none() {
|
2020-04-10 10:44:43 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now do the assoc type projection
|
2021-04-03 14:56:18 -05:00
|
|
|
let alias_eq = AliasEq {
|
|
|
|
alias: AliasTy::Projection(projection),
|
2021-03-21 14:05:38 -05:00
|
|
|
ty: TyKind::BoundVar(BoundVar::new(
|
|
|
|
DebruijnIndex::INNERMOST,
|
2021-03-21 14:19:07 -05:00
|
|
|
ty.goal.binders.len(&Interner),
|
2021-03-21 14:05:38 -05:00
|
|
|
))
|
|
|
|
.intern(&Interner),
|
2019-05-12 11:33:47 -05:00
|
|
|
};
|
|
|
|
|
2021-04-03 14:56:18 -05:00
|
|
|
let in_env = InEnvironment { goal: alias_eq.cast(&Interner), environment: ty.environment };
|
2019-07-07 11:14:56 -05:00
|
|
|
|
2021-03-21 14:05:38 -05:00
|
|
|
let canonical = Canonical {
|
|
|
|
value: in_env,
|
|
|
|
binders: CanonicalVarKinds::from_iter(
|
|
|
|
&Interner,
|
2021-03-21 14:19:07 -05:00
|
|
|
ty.goal.binders.iter(&Interner).cloned().chain(Some(chalk_ir::WithKind::new(
|
2021-04-08 16:34:05 -05:00
|
|
|
VariableKind::Ty(chalk_ir::TyVariableKind::General),
|
2021-03-21 14:05:38 -05:00
|
|
|
chalk_ir::UniverseIndex::ROOT,
|
|
|
|
))),
|
|
|
|
),
|
|
|
|
};
|
2019-05-12 11:33:47 -05:00
|
|
|
|
2019-12-20 14:14:30 -06:00
|
|
|
let solution = db.trait_solve(krate, canonical)?;
|
2019-01-06 12:51:42 -06:00
|
|
|
|
2019-05-12 11:33:47 -05:00
|
|
|
match &solution {
|
2021-08-09 04:30:05 -05:00
|
|
|
Solution::Unique(Canonical { value: ConstrainedSubst { subst, .. }, binders })
|
|
|
|
| Solution::Ambig(Guidance::Definite(Canonical { value: subst, binders })) => {
|
2019-06-15 11:20:59 -05:00
|
|
|
// FIXME: vars may contain solutions for any inference variables
|
|
|
|
// that happened to be inside ty. To correctly handle these, we
|
|
|
|
// would have to pass the solution up to the inference context, but
|
|
|
|
// that requires a larger refactoring (especially if the deref
|
|
|
|
// happens during method resolution). So for the moment, we just
|
2021-09-15 22:47:01 -05:00
|
|
|
// check that we're not in the situation where we would actually
|
2019-06-15 11:20:59 -05:00
|
|
|
// need to handle the values of the additional variables, i.e.
|
|
|
|
// they're just being 'passed through'. In the 'standard' case where
|
|
|
|
// we have `impl<T> Deref for Foo<T> { Target = T }`, that should be
|
|
|
|
// the case.
|
2020-04-10 10:44:43 -05:00
|
|
|
|
|
|
|
// FIXME: if the trait solver decides to truncate the type, these
|
|
|
|
// assumptions will be broken. We would need to properly introduce
|
|
|
|
// new variables in that case
|
|
|
|
|
2021-08-09 04:30:05 -05:00
|
|
|
for i in 1..binders.len(&Interner) {
|
|
|
|
if subst.at(&Interner, i - 1).assert_ty_ref(&Interner).kind(&Interner)
|
2021-03-13 07:44:51 -06:00
|
|
|
!= &TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, i - 1))
|
2020-04-05 11:24:18 -05:00
|
|
|
{
|
2021-03-21 14:19:07 -05:00
|
|
|
warn!("complex solution for derefing {:?}: {:?}, ignoring", ty.goal, solution);
|
2019-06-15 11:20:59 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
2021-04-08 16:34:05 -05:00
|
|
|
// FIXME: we remove lifetime variables here since they can confuse
|
|
|
|
// the method resolution code later
|
|
|
|
Some(fixup_lifetime_variables(Canonical {
|
2021-08-09 04:30:05 -05:00
|
|
|
value: subst
|
|
|
|
.at(&Interner, subst.len(&Interner) - 1)
|
2021-04-01 14:04:02 -05:00
|
|
|
.assert_ty_ref(&Interner)
|
|
|
|
.clone(),
|
2021-08-09 04:30:05 -05:00
|
|
|
binders: binders.clone(),
|
2021-04-08 16:34:05 -05:00
|
|
|
}))
|
2019-05-12 11:33:47 -05:00
|
|
|
}
|
|
|
|
Solution::Ambig(_) => {
|
2021-03-21 14:19:07 -05:00
|
|
|
info!("Ambiguous solution for derefing {:?}: {:?}", ty.goal, solution);
|
2019-05-12 11:33:47 -05:00
|
|
|
None
|
|
|
|
}
|
2019-01-06 12:51:42 -06:00
|
|
|
}
|
|
|
|
}
|
2021-04-08 16:34:05 -05:00
|
|
|
|
|
|
|
fn fixup_lifetime_variables<T: Fold<Interner, Result = T> + HasInterner<Interner = Interner>>(
|
|
|
|
c: Canonical<T>,
|
|
|
|
) -> Canonical<T> {
|
|
|
|
// Removes lifetime variables from the Canonical, replacing them by static lifetimes.
|
|
|
|
let mut i = 0;
|
|
|
|
let subst = Substitution::from_iter(
|
|
|
|
&Interner,
|
|
|
|
c.binders.iter(&Interner).map(|vk| match vk.kind {
|
|
|
|
VariableKind::Ty(_) => {
|
|
|
|
let index = i;
|
|
|
|
i += 1;
|
|
|
|
BoundVar::new(DebruijnIndex::INNERMOST, index).to_ty(&Interner).cast(&Interner)
|
|
|
|
}
|
|
|
|
VariableKind::Lifetime => static_lifetime().cast(&Interner),
|
|
|
|
VariableKind::Const(_) => unimplemented!(),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
let binders = CanonicalVarKinds::from_iter(
|
|
|
|
&Interner,
|
|
|
|
c.binders.iter(&Interner).filter(|vk| match vk.kind {
|
|
|
|
VariableKind::Ty(_) => true,
|
|
|
|
VariableKind::Lifetime => false,
|
|
|
|
VariableKind::Const(_) => true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
let value = subst.apply(c.value, &Interner);
|
|
|
|
Canonical { binders, value }
|
|
|
|
}
|