2020-05-22 15:14:53 +00:00
|
|
|
//! This module contains the implementations of the `ToChalk` trait, which
|
|
|
|
//! handles conversion between our data types and their corresponding types in
|
|
|
|
//! Chalk (in both directions); plus some helper functions for more specialized
|
|
|
|
//! conversions.
|
|
|
|
|
2021-04-09 12:15:26 +00:00
|
|
|
use chalk_ir::{cast::Cast, fold::Shift, DebruijnIndex};
|
2020-05-27 19:05:21 +00:00
|
|
|
use chalk_solve::rust_ir;
|
2020-05-22 15:14:53 +00:00
|
|
|
|
2020-08-13 14:25:38 +00:00
|
|
|
use base_db::salsa::InternKey;
|
2021-03-21 12:22:22 +00:00
|
|
|
use hir_def::{GenericDefId, TypeAliasId};
|
2020-05-22 15:14:53 +00:00
|
|
|
|
2021-04-09 12:15:26 +00:00
|
|
|
use crate::{
|
|
|
|
chalk_db, db::HirDatabase, AliasEq, AliasTy, CallableDefId, FnDefId, Interner, ProjectionTyExt,
|
|
|
|
QuantifiedWhereClause, Substitution, Ty, WhereClause,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub(crate) trait ToChalk {
|
|
|
|
type Chalk;
|
|
|
|
fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk;
|
|
|
|
fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn from_chalk<T, ChalkT>(db: &dyn HirDatabase, chalk: ChalkT) -> T
|
|
|
|
where
|
|
|
|
T: ToChalk<Chalk = ChalkT>,
|
|
|
|
{
|
|
|
|
T::from_chalk(db, chalk)
|
|
|
|
}
|
2020-05-22 15:14:53 +00:00
|
|
|
|
|
|
|
impl ToChalk for hir_def::TraitId {
|
2021-04-09 12:11:37 +00:00
|
|
|
type Chalk = chalk_db::TraitId;
|
2020-05-22 15:14:53 +00:00
|
|
|
|
2021-04-09 12:11:37 +00:00
|
|
|
fn to_chalk(self, _db: &dyn HirDatabase) -> chalk_db::TraitId {
|
2020-05-22 15:14:53 +00:00
|
|
|
chalk_ir::TraitId(self.as_intern_id())
|
|
|
|
}
|
|
|
|
|
2021-04-09 12:11:37 +00:00
|
|
|
fn from_chalk(_db: &dyn HirDatabase, trait_id: chalk_db::TraitId) -> hir_def::TraitId {
|
2020-05-22 15:14:53 +00:00
|
|
|
InternKey::from_intern_id(trait_id.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 13:26:02 +00:00
|
|
|
impl ToChalk for hir_def::ImplId {
|
2021-04-09 12:11:37 +00:00
|
|
|
type Chalk = chalk_db::ImplId;
|
2020-05-22 15:14:53 +00:00
|
|
|
|
2021-04-09 12:11:37 +00:00
|
|
|
fn to_chalk(self, _db: &dyn HirDatabase) -> chalk_db::ImplId {
|
2020-07-12 13:26:02 +00:00
|
|
|
chalk_ir::ImplId(self.as_intern_id())
|
2020-05-22 15:14:53 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 12:11:37 +00:00
|
|
|
fn from_chalk(_db: &dyn HirDatabase, impl_id: chalk_db::ImplId) -> hir_def::ImplId {
|
2020-07-12 13:26:02 +00:00
|
|
|
InternKey::from_intern_id(impl_id.0)
|
2020-05-22 15:14:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 11:15:00 +00:00
|
|
|
impl ToChalk for CallableDefId {
|
2020-05-22 16:15:53 +00:00
|
|
|
type Chalk = FnDefId;
|
|
|
|
|
|
|
|
fn to_chalk(self, db: &dyn HirDatabase) -> FnDefId {
|
|
|
|
db.intern_callable_def(self).into()
|
|
|
|
}
|
|
|
|
|
2020-07-16 11:15:00 +00:00
|
|
|
fn from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDefId {
|
2020-05-22 16:15:53 +00:00
|
|
|
db.lookup_intern_callable_def(fn_def_id.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 15:31:38 +00:00
|
|
|
pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId);
|
2020-07-12 13:26:02 +00:00
|
|
|
|
|
|
|
impl ToChalk for TypeAliasAsValue {
|
2021-04-09 12:11:37 +00:00
|
|
|
type Chalk = chalk_db::AssociatedTyValueId;
|
2020-05-22 15:14:53 +00:00
|
|
|
|
2021-04-09 12:11:37 +00:00
|
|
|
fn to_chalk(self, _db: &dyn HirDatabase) -> chalk_db::AssociatedTyValueId {
|
2020-07-12 13:26:02 +00:00
|
|
|
rust_ir::AssociatedTyValueId(self.0.as_intern_id())
|
2020-05-22 15:14:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-12 13:26:02 +00:00
|
|
|
fn from_chalk(
|
|
|
|
_db: &dyn HirDatabase,
|
2021-04-09 12:11:37 +00:00
|
|
|
assoc_ty_value_id: chalk_db::AssociatedTyValueId,
|
2020-07-12 13:26:02 +00:00
|
|
|
) -> TypeAliasAsValue {
|
|
|
|
TypeAliasAsValue(TypeAliasId::from_intern_id(assoc_ty_value_id.0))
|
2020-05-22 15:14:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn convert_where_clauses(
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
def: GenericDefId,
|
2021-03-15 20:02:34 +00:00
|
|
|
substs: &Substitution,
|
2020-05-22 15:14:53 +00:00
|
|
|
) -> Vec<chalk_ir::QuantifiedWhereClause<Interner>> {
|
|
|
|
let generic_predicates = db.generic_predicates(def);
|
|
|
|
let mut result = Vec::with_capacity(generic_predicates.len());
|
|
|
|
for pred in generic_predicates.iter() {
|
2021-04-08 12:16:05 +00:00
|
|
|
result.push(pred.clone().substitute(&Interner, substs));
|
2020-05-22 15:14:53 +00:00
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn generic_predicate_to_inline_bound(
|
|
|
|
db: &dyn HirDatabase,
|
2021-03-21 12:22:22 +00:00
|
|
|
pred: &QuantifiedWhereClause,
|
2020-05-22 15:14:53 +00:00
|
|
|
self_ty: &Ty,
|
2021-03-21 12:22:22 +00:00
|
|
|
) -> Option<chalk_ir::Binders<rust_ir::InlineBound<Interner>>> {
|
2020-05-22 15:14:53 +00:00
|
|
|
// An InlineBound is like a GenericPredicate, except the self type is left out.
|
|
|
|
// We don't have a special type for this, but Chalk does.
|
2021-04-07 19:26:24 +00:00
|
|
|
let self_ty_shifted_in = self_ty.clone().shifted_in_from(&Interner, DebruijnIndex::ONE);
|
2021-04-05 15:13:50 +00:00
|
|
|
let (pred, binders) = pred.as_ref().into_value_and_skipped_binders();
|
|
|
|
match pred {
|
2021-03-20 09:46:36 +00:00
|
|
|
WhereClause::Implemented(trait_ref) => {
|
2021-04-07 18:41:52 +00:00
|
|
|
if trait_ref.self_type_parameter(&Interner) != self_ty_shifted_in {
|
2020-05-22 15:14:53 +00:00
|
|
|
// we can only convert predicates back to type bounds if they
|
|
|
|
// have the expected self type
|
|
|
|
return None;
|
|
|
|
}
|
2021-04-08 16:25:18 +00:00
|
|
|
let args_no_self = trait_ref.substitution.as_slice(&Interner)[1..]
|
2020-05-22 15:14:53 +00:00
|
|
|
.iter()
|
2021-04-08 12:16:05 +00:00
|
|
|
.map(|ty| ty.clone().cast(&Interner))
|
2020-05-22 15:14:53 +00:00
|
|
|
.collect();
|
2021-03-18 20:53:19 +00:00
|
|
|
let trait_bound = rust_ir::TraitBound { trait_id: trait_ref.trait_id, args_no_self };
|
2021-04-05 15:45:18 +00:00
|
|
|
Some(chalk_ir::Binders::new(binders, rust_ir::InlineBound::TraitBound(trait_bound)))
|
2020-05-22 15:14:53 +00:00
|
|
|
}
|
2021-03-20 09:46:36 +00:00
|
|
|
WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(projection_ty), ty }) => {
|
2021-04-07 18:41:52 +00:00
|
|
|
if projection_ty.self_type_parameter(&Interner) != self_ty_shifted_in {
|
2020-05-22 15:14:53 +00:00
|
|
|
return None;
|
|
|
|
}
|
2021-03-21 12:22:22 +00:00
|
|
|
let trait_ = projection_ty.trait_(db);
|
2021-04-08 16:25:18 +00:00
|
|
|
let args_no_self = projection_ty.substitution.as_slice(&Interner)[1..]
|
2020-05-22 15:14:53 +00:00
|
|
|
.iter()
|
2021-04-08 12:16:05 +00:00
|
|
|
.map(|ty| ty.clone().cast(&Interner))
|
2020-05-22 15:14:53 +00:00
|
|
|
.collect();
|
2020-05-27 19:05:21 +00:00
|
|
|
let alias_eq_bound = rust_ir::AliasEqBound {
|
2021-04-08 12:16:05 +00:00
|
|
|
value: ty.clone(),
|
2020-05-27 19:05:21 +00:00
|
|
|
trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self },
|
2021-03-19 01:07:15 +00:00
|
|
|
associated_ty_id: projection_ty.associated_ty_id,
|
2020-05-22 15:14:53 +00:00
|
|
|
parameters: Vec::new(), // FIXME we don't support generic associated types yet
|
|
|
|
};
|
2021-04-05 15:45:18 +00:00
|
|
|
Some(chalk_ir::Binders::new(
|
|
|
|
binders,
|
|
|
|
rust_ir::InlineBound::AliasEqBound(alias_eq_bound),
|
|
|
|
))
|
2020-05-22 15:14:53 +00:00
|
|
|
}
|
2021-03-19 01:07:15 +00:00
|
|
|
_ => None,
|
2020-05-22 15:14:53 +00:00
|
|
|
}
|
|
|
|
}
|