2019-01-08 23:47:12 +00:00
|
|
|
//! In certain situations, rust automatically inserts derefs as necessary: for
|
2019-01-06 18:51:42 +00: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 14:43:49 +00:00
|
|
|
use std::iter::successors;
|
2019-01-06 18:51:42 +00:00
|
|
|
|
2020-08-13 14:25:38 +00:00
|
|
|
use base_db::CrateId;
|
2021-03-20 10:23:59 +00:00
|
|
|
use chalk_ir::cast::Cast;
|
2019-11-25 10:10:26 +00:00
|
|
|
use hir_def::lang_item::LangItemTarget;
|
2019-12-13 21:01:06 +00:00
|
|
|
use hir_expand::name::name;
|
2019-06-15 16:20:59 +00:00
|
|
|
use log::{info, warn};
|
2019-01-06 18:51:42 +00:00
|
|
|
|
2019-12-07 10:50:36 +00:00
|
|
|
use crate::{
|
2021-04-04 18:27:40 +00:00
|
|
|
db::HirDatabase, AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex,
|
2021-04-05 19:14:49 +00:00
|
|
|
InEnvironment, Interner, ProjectionTyExt, Solution, Ty, TyBuilder, TyKind,
|
2019-11-25 09:45:45 +00:00
|
|
|
};
|
2019-05-12 16:33:47 +00:00
|
|
|
|
2019-06-16 10:04:08 +00:00
|
|
|
const AUTODEREF_RECURSION_LIMIT: usize = 10;
|
|
|
|
|
2019-11-27 14:46:02 +00:00
|
|
|
pub fn autoderef<'a>(
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &'a dyn HirDatabase,
|
2019-11-25 10:10:26 +00:00
|
|
|
krate: Option<CrateId>,
|
|
|
|
ty: InEnvironment<Canonical<Ty>>,
|
2019-05-12 16:33:47 +00:00
|
|
|
) -> impl Iterator<Item = Canonical<Ty>> + 'a {
|
2021-03-21 19:19:07 +00:00
|
|
|
let InEnvironment { goal: ty, environment } = ty;
|
2019-11-25 10:10:26 +00:00
|
|
|
successors(Some(ty), move |ty| {
|
2021-03-21 19:19:07 +00:00
|
|
|
deref(db, krate?, InEnvironment { goal: ty, environment: environment.clone() })
|
2019-11-25 10:10:26 +00:00
|
|
|
})
|
|
|
|
.take(AUTODEREF_RECURSION_LIMIT)
|
2019-05-12 16:33:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn deref(
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn HirDatabase,
|
2019-11-25 10:10:26 +00:00
|
|
|
krate: CrateId,
|
|
|
|
ty: InEnvironment<&Canonical<Ty>>,
|
2019-05-12 16:33:47 +00:00
|
|
|
) -> Option<Canonical<Ty>> {
|
2021-03-21 19:19:07 +00:00
|
|
|
if let Some(derefed) = ty.goal.value.builtin_deref() {
|
|
|
|
Some(Canonical { value: derefed, binders: ty.goal.binders.clone() })
|
2019-05-12 16:33:47 +00:00
|
|
|
} else {
|
2019-11-25 10:10:26 +00:00
|
|
|
deref_by_trait(db, krate, ty)
|
2019-01-06 18:51:42 +00:00
|
|
|
}
|
2019-05-12 16:33:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn deref_by_trait(
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn HirDatabase,
|
2019-11-25 09:45:45 +00:00
|
|
|
krate: CrateId,
|
|
|
|
ty: InEnvironment<&Canonical<Ty>>,
|
2019-05-12 16:33:47 +00:00
|
|
|
) -> Option<Canonical<Ty>> {
|
2019-12-20 20:14:30 +00:00
|
|
|
let deref_trait = match db.lang_item(krate, "deref".into())? {
|
2019-11-26 14:21:29 +00:00
|
|
|
LangItemTarget::TraitId(it) => it,
|
2019-05-12 16:33:47 +00:00
|
|
|
_ => return None,
|
|
|
|
};
|
2019-12-13 21:01:06 +00:00
|
|
|
let target = db.trait_data(deref_trait).associated_type_by_name(&name![Target])?;
|
2019-05-12 16:33:47 +00:00
|
|
|
|
2021-04-03 19:56:18 +00: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 16:33:30 +00:00
|
|
|
|
2020-04-05 16:24:18 +00:00
|
|
|
// FIXME make the Canonical / bound var handling nicer
|
2019-05-12 16:33:47 +00:00
|
|
|
|
2020-04-10 15:44:43 +00:00
|
|
|
// Check that the type implements Deref at all
|
2021-04-03 19:56:18 +00:00
|
|
|
let trait_ref = projection.trait_ref(db);
|
2020-06-28 19:17:27 +00:00
|
|
|
let implements_goal = Canonical {
|
2021-03-21 19:19:07 +00:00
|
|
|
binders: ty.goal.binders.clone(),
|
2020-04-10 15:44:43 +00:00
|
|
|
value: InEnvironment {
|
2021-03-21 19:19:07 +00:00
|
|
|
goal: trait_ref.cast(&Interner),
|
2020-04-10 15:44:43 +00:00
|
|
|
environment: ty.environment.clone(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
if db.trait_solve(krate, implements_goal).is_none() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now do the assoc type projection
|
2021-04-03 19:56:18 +00:00
|
|
|
let alias_eq = AliasEq {
|
|
|
|
alias: AliasTy::Projection(projection),
|
2021-03-21 19:05:38 +00:00
|
|
|
ty: TyKind::BoundVar(BoundVar::new(
|
|
|
|
DebruijnIndex::INNERMOST,
|
2021-03-21 19:19:07 +00:00
|
|
|
ty.goal.binders.len(&Interner),
|
2021-03-21 19:05:38 +00:00
|
|
|
))
|
|
|
|
.intern(&Interner),
|
2019-05-12 16:33:47 +00:00
|
|
|
};
|
|
|
|
|
2021-04-03 19:56:18 +00:00
|
|
|
let in_env = InEnvironment { goal: alias_eq.cast(&Interner), environment: ty.environment };
|
2019-07-07 16:14:56 +00:00
|
|
|
|
2021-03-21 19:05:38 +00:00
|
|
|
let canonical = Canonical {
|
|
|
|
value: in_env,
|
|
|
|
binders: CanonicalVarKinds::from_iter(
|
|
|
|
&Interner,
|
2021-03-21 19:19:07 +00:00
|
|
|
ty.goal.binders.iter(&Interner).cloned().chain(Some(chalk_ir::WithKind::new(
|
2021-03-21 19:05:38 +00:00
|
|
|
chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General),
|
|
|
|
chalk_ir::UniverseIndex::ROOT,
|
|
|
|
))),
|
|
|
|
),
|
|
|
|
};
|
2019-05-12 16:33:47 +00:00
|
|
|
|
2019-12-20 20:14:30 +00:00
|
|
|
let solution = db.trait_solve(krate, canonical)?;
|
2019-01-06 18:51:42 +00:00
|
|
|
|
2019-05-12 16:33:47 +00:00
|
|
|
match &solution {
|
|
|
|
Solution::Unique(vars) => {
|
2019-06-15 16:20:59 +00: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
|
|
|
|
// check that we're not in the situation we're we would actually
|
|
|
|
// 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 15:44:43 +00: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-04-06 21:46:32 +00:00
|
|
|
for i in 1..vars.binders.len(&Interner) {
|
|
|
|
if vars.value.subst.at(&Interner, i - 1).assert_ty_ref(&Interner).kind(&Interner)
|
2021-03-13 13:44:51 +00:00
|
|
|
!= &TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, i - 1))
|
2020-04-05 16:24:18 +00:00
|
|
|
{
|
2021-03-21 19:19:07 +00:00
|
|
|
warn!("complex solution for derefing {:?}: {:?}, ignoring", ty.goal, solution);
|
2019-06-15 16:20:59 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
2020-04-05 16:24:18 +00:00
|
|
|
Some(Canonical {
|
2021-04-01 19:04:02 +00:00
|
|
|
value: vars
|
|
|
|
.value
|
2021-04-06 21:46:32 +00:00
|
|
|
.subst
|
|
|
|
.at(&Interner, vars.value.subst.len(&Interner) - 1)
|
2021-04-01 19:04:02 +00:00
|
|
|
.assert_ty_ref(&Interner)
|
|
|
|
.clone(),
|
2021-04-06 21:46:32 +00:00
|
|
|
binders: vars.binders.clone(),
|
2020-04-05 16:24:18 +00:00
|
|
|
})
|
2019-05-12 16:33:47 +00:00
|
|
|
}
|
|
|
|
Solution::Ambig(_) => {
|
2021-03-21 19:19:07 +00:00
|
|
|
info!("Ambiguous solution for derefing {:?}: {:?}", ty.goal, solution);
|
2019-05-12 16:33:47 +00:00
|
|
|
None
|
|
|
|
}
|
2019-01-06 18:51:42 +00:00
|
|
|
}
|
|
|
|
}
|