rust-analyzer/crates/hir_ty/src/traits/chalk/mapping.rs

330 lines
9.2 KiB
Rust
Raw Normal View History

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.
use chalk_ir::{cast::Cast, interner::HasInterner};
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;
use hir_def::{GenericDefId, TypeAliasId};
2020-05-22 15:14:53 +00:00
use crate::{
2021-04-07 18:40:01 +00:00
db::HirDatabase, static_lifetime, AliasTy, CallableDefId, Canonical, ConstrainedSubst,
DomainGoal, FnPointer, GenericArg, InEnvironment, OpaqueTy, ProjectionTy, ProjectionTyExt,
QuantifiedWhereClause, Substitution, TraitRef, Ty, TypeWalk, WhereClause,
2020-05-22 15:14:53 +00:00
};
use super::interner::*;
use super::*;
impl ToChalk for Ty {
type Chalk = chalk_ir::Ty<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> {
2021-04-03 15:41:14 +00:00
self
2020-05-22 15:14:53 +00:00
}
fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self {
2021-04-03 15:41:14 +00:00
chalk
2020-05-22 15:14:53 +00:00
}
}
impl ToChalk for GenericArg {
type Chalk = chalk_ir::GenericArg<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk {
2021-04-03 15:41:14 +00:00
self
}
fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self {
2021-04-03 15:41:14 +00:00
chalk
}
}
2021-03-15 20:02:34 +00:00
impl ToChalk for Substitution {
2020-05-22 15:14:53 +00:00
type Chalk = chalk_ir::Substitution<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Substitution<Interner> {
2021-04-03 15:41:14 +00:00
self
2020-05-22 15:14:53 +00:00
}
2021-03-15 20:02:34 +00:00
fn from_chalk(
db: &dyn HirDatabase,
parameters: chalk_ir::Substitution<Interner>,
) -> Substitution {
2021-04-03 15:41:14 +00:00
parameters
2020-05-22 15:14:53 +00:00
}
}
impl ToChalk for TraitRef {
type Chalk = chalk_ir::TraitRef<Interner>;
fn to_chalk(self: TraitRef, db: &dyn HirDatabase) -> chalk_ir::TraitRef<Interner> {
2021-04-03 15:41:14 +00:00
self
2020-05-22 15:14:53 +00:00
}
fn from_chalk(db: &dyn HirDatabase, trait_ref: chalk_ir::TraitRef<Interner>) -> Self {
2021-04-03 15:41:14 +00:00
trait_ref
2020-05-22 15:14:53 +00:00
}
}
impl ToChalk for hir_def::TraitId {
type Chalk = TraitId;
fn to_chalk(self, _db: &dyn HirDatabase) -> TraitId {
chalk_ir::TraitId(self.as_intern_id())
}
fn from_chalk(_db: &dyn HirDatabase, trait_id: TraitId) -> hir_def::TraitId {
InternKey::from_intern_id(trait_id.0)
}
}
2020-07-12 13:26:02 +00:00
impl ToChalk for hir_def::ImplId {
2020-05-22 15:14:53 +00:00
type Chalk = ImplId;
2020-07-12 13:26:02 +00:00
fn to_chalk(self, _db: &dyn HirDatabase) -> ImplId {
chalk_ir::ImplId(self.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, impl_id: ImplId) -> hir_def::ImplId {
InternKey::from_intern_id(impl_id.0)
2020-05-22 15:14:53 +00:00
}
}
impl ToChalk for CallableDefId {
type Chalk = FnDefId;
fn to_chalk(self, db: &dyn HirDatabase) -> FnDefId {
db.intern_callable_def(self).into()
}
fn from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDefId {
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 {
2020-05-22 15:14:53 +00:00
type Chalk = AssociatedTyValueId;
2020-07-12 13:26:02 +00:00
fn to_chalk(self, _db: &dyn HirDatabase) -> AssociatedTyValueId {
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,
assoc_ty_value_id: AssociatedTyValueId,
) -> TypeAliasAsValue {
TypeAliasAsValue(TypeAliasId::from_intern_id(assoc_ty_value_id.0))
2020-05-22 15:14:53 +00:00
}
}
2021-03-20 09:46:36 +00:00
impl ToChalk for WhereClause {
type Chalk = chalk_ir::WhereClause<Interner>;
2020-05-22 15:14:53 +00:00
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::WhereClause<Interner> {
2021-04-03 15:41:14 +00:00
self
2020-05-22 15:14:53 +00:00
}
fn from_chalk(
db: &dyn HirDatabase,
where_clause: chalk_ir::WhereClause<Interner>,
2021-03-20 09:46:36 +00:00
) -> WhereClause {
2021-04-03 15:41:14 +00:00
where_clause
2020-05-22 15:14:53 +00:00
}
}
impl ToChalk for ProjectionTy {
type Chalk = chalk_ir::ProjectionTy<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy<Interner> {
2021-04-03 15:41:14 +00:00
self
2020-05-22 15:14:53 +00:00
}
fn from_chalk(
db: &dyn HirDatabase,
projection_ty: chalk_ir::ProjectionTy<Interner>,
) -> ProjectionTy {
2021-04-03 15:41:14 +00:00
projection_ty
2020-05-22 15:14:53 +00:00
}
}
impl ToChalk for OpaqueTy {
type Chalk = chalk_ir::OpaqueTy<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk {
2021-04-03 15:41:14 +00:00
self
}
fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self {
2021-04-03 15:41:14 +00:00
chalk
}
}
impl ToChalk for AliasTy {
type Chalk = chalk_ir::AliasTy<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk {
2021-04-03 15:41:14 +00:00
self
}
fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self {
2021-04-03 15:41:14 +00:00
chalk
}
}
2020-05-22 15:14:53 +00:00
impl ToChalk for AliasEq {
2020-05-22 15:14:53 +00:00
type Chalk = chalk_ir::AliasEq<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::AliasEq<Interner> {
2021-04-03 15:41:14 +00:00
self
2020-05-22 15:14:53 +00:00
}
fn from_chalk(db: &dyn HirDatabase, alias_eq: chalk_ir::AliasEq<Interner>) -> Self {
2021-04-03 15:41:14 +00:00
alias_eq
2020-05-22 15:14:53 +00:00
}
}
impl ToChalk for DomainGoal {
2020-05-22 15:14:53 +00:00
type Chalk = chalk_ir::DomainGoal<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::DomainGoal<Interner> {
2021-04-03 15:41:14 +00:00
self
2020-05-22 15:14:53 +00:00
}
2021-04-03 15:41:14 +00:00
fn from_chalk(_db: &dyn HirDatabase, goal: chalk_ir::DomainGoal<Interner>) -> Self {
goal
2020-05-22 15:14:53 +00:00
}
}
impl<T> ToChalk for Canonical<T>
where
2021-04-03 15:41:14 +00:00
T: HasInterner<Interner = Interner>,
2020-05-22 15:14:53 +00:00
{
2021-04-03 15:41:14 +00:00
type Chalk = chalk_ir::Canonical<T>;
2020-05-22 15:14:53 +00:00
2021-04-03 15:41:14 +00:00
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical<T> {
self
2020-05-22 15:14:53 +00:00
}
2021-04-03 15:41:14 +00:00
fn from_chalk(db: &dyn HirDatabase, canonical: chalk_ir::Canonical<T>) -> Canonical<T> {
canonical
2020-05-22 15:14:53 +00:00
}
}
impl<T: ToChalk> ToChalk for InEnvironment<T>
where
2021-04-03 15:41:14 +00:00
T: HasInterner<Interner = Interner>,
2020-05-22 15:14:53 +00:00
{
2021-04-03 15:41:14 +00:00
type Chalk = chalk_ir::InEnvironment<T>;
2020-05-22 15:14:53 +00:00
2021-04-03 15:41:14 +00:00
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::InEnvironment<T> {
self
2020-05-22 15:14:53 +00:00
}
2021-04-03 15:41:14 +00:00
fn from_chalk(_db: &dyn HirDatabase, in_env: chalk_ir::InEnvironment<T>) -> InEnvironment<T> {
in_env
2020-05-22 15:14:53 +00:00
}
}
impl<T: ToChalk> ToChalk for crate::Binders<T>
where
2021-04-03 15:41:14 +00:00
T: HasInterner<Interner = Interner>,
{
2021-04-03 15:41:14 +00:00
type Chalk = chalk_ir::Binders<T>;
2021-04-03 15:41:14 +00:00
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Binders<T> {
self
}
2021-04-03 15:41:14 +00:00
fn from_chalk(db: &dyn HirDatabase, binders: chalk_ir::Binders<T>) -> crate::Binders<T> {
binders
}
}
impl ToChalk for crate::ConstrainedSubst {
type Chalk = chalk_ir::ConstrainedSubst<Interner>;
fn to_chalk(self, _db: &dyn HirDatabase) -> Self::Chalk {
2021-04-03 15:41:14 +00:00
self
}
fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self {
2021-04-03 15:41:14 +00:00
chalk
}
}
2020-05-22 15:14:53 +00:00
pub(super) fn make_binders<T>(value: T, num_vars: usize) -> chalk_ir::Binders<T>
where
T: HasInterner<Interner = Interner>,
{
chalk_ir::Binders::new(
2020-07-11 13:22:46 +00:00
chalk_ir::VariableKinds::from_iter(
2020-05-22 15:14:53 +00:00
&Interner,
std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General))
.take(num_vars),
2020-05-22 15:14:53 +00:00
),
value,
)
}
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() {
result.push(pred.clone().substitute(&Interner, substs).to_chalk(db));
2020-05-22 15:14:53 +00:00
}
result
}
pub(super) fn generic_predicate_to_inline_bound(
db: &dyn HirDatabase,
pred: &QuantifiedWhereClause,
2020-05-22 15:14:53 +00:00
self_ty: &Ty,
) -> 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.
let self_ty_shifted_in = self_ty.clone().shifted_in_from(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) => {
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;
}
let args_no_self = trait_ref.substitution.interned()[1..]
2020-05-22 15:14:53 +00:00
.iter()
.map(|ty| ty.clone().to_chalk(db).cast(&Interner))
.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 }) => {
if projection_ty.self_type_parameter(&Interner) != self_ty_shifted_in {
2020-05-22 15:14:53 +00:00
return None;
}
let trait_ = projection_ty.trait_(db);
let args_no_self = projection_ty.substitution.interned()[1..]
2020-05-22 15:14:53 +00:00
.iter()
.map(|ty| ty.clone().to_chalk(db).cast(&Interner))
.collect();
let alias_eq_bound = rust_ir::AliasEqBound {
value: ty.clone().to_chalk(db),
trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self },
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
}
_ => None,
2020-05-22 15:14:53 +00:00
}
}