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

547 lines
20 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> {
match self.into_inner() {
2021-04-06 18:51:21 +00:00
TyKind::Ref(m, lt, ty) => {
chalk_ir::TyKind::Ref(m, lt, ty.to_chalk(db)).intern(&Interner)
}
TyKind::Array(ty, size) => {
chalk_ir::TyKind::Array(ty.to_chalk(db), size).intern(&Interner)
}
TyKind::Function(FnPointer { sig, substitution: substs, num_binders }) => {
let substitution = chalk_ir::FnSubst(substs.0.to_chalk(db));
2021-04-06 18:51:21 +00:00
chalk_ir::TyKind::Function(chalk_ir::FnPointer { num_binders, sig, substitution })
.intern(&Interner)
2021-02-28 18:13:37 +00:00
}
2021-03-13 16:36:07 +00:00
TyKind::AssociatedType(assoc_type_id, substs) => {
2021-02-28 18:13:37 +00:00
let substitution = substs.to_chalk(db);
chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner)
}
2021-03-13 19:05:47 +00:00
TyKind::OpaqueType(id, substs) => {
2021-02-28 18:13:37 +00:00
let substitution = substs.to_chalk(db);
chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner)
}
2021-04-05 12:38:28 +00:00
TyKind::Foreign(id) => chalk_ir::TyKind::Foreign(id).intern(&Interner),
TyKind::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner),
TyKind::Tuple(cardinality, substs) => {
2021-02-28 18:13:37 +00:00
let substitution = substs.to_chalk(db);
chalk_ir::TyKind::Tuple(cardinality, substitution).intern(&Interner)
2021-02-28 18:13:37 +00:00
}
TyKind::Raw(mutability, ty) => {
let ty = ty.to_chalk(db);
2021-03-01 18:30:34 +00:00
chalk_ir::TyKind::Raw(mutability, ty).intern(&Interner)
2021-02-28 18:13:37 +00:00
}
TyKind::Slice(ty) => chalk_ir::TyKind::Slice(ty.to_chalk(db)).intern(&Interner),
TyKind::Str => chalk_ir::TyKind::Str.intern(&Interner),
2021-03-13 16:55:50 +00:00
TyKind::FnDef(id, substs) => {
2021-02-28 18:13:37 +00:00
let substitution = substs.to_chalk(db);
chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner)
}
TyKind::Never => chalk_ir::TyKind::Never.intern(&Interner),
2021-03-13 18:27:09 +00:00
TyKind::Closure(closure_id, substs) => {
2021-02-28 18:13:37 +00:00
let substitution = substs.to_chalk(db);
2021-03-13 18:27:09 +00:00
chalk_ir::TyKind::Closure(closure_id, substitution).intern(&Interner)
2021-02-28 18:13:37 +00:00
}
TyKind::Adt(adt_id, substs) => {
2021-02-28 18:13:37 +00:00
let substitution = substs.to_chalk(db);
2021-03-01 20:57:39 +00:00
chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner)
2021-02-28 18:13:37 +00:00
}
TyKind::Alias(AliasTy::Projection(proj_ty)) => {
2021-04-06 18:51:21 +00:00
chalk_ir::AliasTy::Projection(proj_ty.to_chalk(db))
2021-03-14 15:33:27 +00:00
.cast(&Interner)
.intern(&Interner)
}
2021-04-06 18:51:21 +00:00
TyKind::Alias(AliasTy::Opaque(opaque_ty)) => {
chalk_ir::AliasTy::Opaque(opaque_ty.to_chalk(db)).cast(&Interner).intern(&Interner)
}
2021-03-13 18:47:34 +00:00
TyKind::Placeholder(idx) => idx.to_ty::<Interner>(&Interner),
TyKind::BoundVar(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner),
TyKind::InferenceVar(..) => panic!("uncanonicalized infer ty"),
TyKind::Dyn(dyn_ty) => {
2021-04-05 15:13:50 +00:00
let (bounds, binders) = dyn_ty.bounds.into_value_and_skipped_binders();
2020-07-11 13:22:46 +00:00
let where_clauses = chalk_ir::QuantifiedWhereClauses::from_iter(
2020-05-22 15:14:53 +00:00
&Interner,
2021-04-05 15:13:50 +00:00
bounds.interned().iter().cloned().map(|p| p.to_chalk(db)),
2020-05-22 15:14:53 +00:00
);
2020-06-22 11:18:10 +00:00
let bounded_ty = chalk_ir::DynTy {
2021-04-05 15:45:18 +00:00
bounds: chalk_ir::Binders::new(binders, where_clauses),
2021-04-06 18:51:21 +00:00
lifetime: dyn_ty.lifetime,
2020-06-22 11:18:10 +00:00
};
chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner)
2020-05-22 15:14:53 +00:00
}
2021-04-05 12:37:11 +00:00
TyKind::Error => chalk_ir::TyKind::Error.intern(&Interner),
2020-05-22 15:14:53 +00:00
}
}
fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self {
match chalk.data(&Interner).kind.clone() {
2021-04-05 12:37:11 +00:00
chalk_ir::TyKind::Error => TyKind::Error,
2021-04-06 09:45:41 +00:00
chalk_ir::TyKind::Array(ty, size) => TyKind::Array(from_chalk(db, ty), size),
2021-03-13 18:47:34 +00:00
chalk_ir::TyKind::Placeholder(idx) => TyKind::Placeholder(idx),
chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => {
2021-04-06 18:51:21 +00:00
TyKind::Alias(AliasTy::Projection(from_chalk(db, proj)))
2020-05-22 15:14:53 +00:00
}
chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(opaque_ty)) => {
2021-04-06 18:51:21 +00:00
TyKind::Alias(AliasTy::Opaque(from_chalk(db, opaque_ty)))
}
chalk_ir::TyKind::Function(chalk_ir::FnPointer {
2020-07-30 17:37:28 +00:00
num_binders,
2021-03-14 15:30:02 +00:00
sig,
2020-07-30 17:37:28 +00:00
substitution,
..
}) => {
assert_eq!(num_binders, 0);
let substs = crate::FnSubst(from_chalk(db, substitution.0));
2021-04-05 20:23:16 +00:00
TyKind::Function(FnPointer { num_binders, sig, substitution: substs })
}
chalk_ir::TyKind::BoundVar(idx) => TyKind::BoundVar(idx),
2021-04-05 12:37:11 +00:00
chalk_ir::TyKind::InferenceVar(_iv, _kind) => TyKind::Error,
2021-04-06 18:51:21 +00:00
chalk_ir::TyKind::Dyn(dyn_ty) => {
assert_eq!(dyn_ty.bounds.binders.len(&Interner), 1);
let (bounds, binders) = dyn_ty.bounds.into_value_and_skipped_binders();
let where_clauses = crate::QuantifiedWhereClauses::from_iter(
&Interner,
bounds.interned().iter().cloned().map(|p| from_chalk(db, p)),
);
TyKind::Dyn(crate::DynTy {
2021-04-06 18:51:21 +00:00
bounds: crate::Binders::new(binders, where_clauses),
// HACK: we sometimes get lifetime variables back in solutions
// from Chalk, and don't have the infrastructure to substitute
// them yet. So for now we just turn them into 'static right
// when we get them
lifetime: static_lifetime(),
})
2020-05-22 15:14:53 +00:00
}
chalk_ir::TyKind::Adt(adt_id, subst) => TyKind::Adt(adt_id, from_chalk(db, subst)),
2021-03-13 16:36:07 +00:00
chalk_ir::TyKind::AssociatedType(type_id, subst) => {
TyKind::AssociatedType(type_id, from_chalk(db, subst))
}
2021-02-28 18:13:37 +00:00
chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => {
2021-03-13 19:05:47 +00:00
TyKind::OpaqueType(opaque_type_id, from_chalk(db, subst))
}
chalk_ir::TyKind::Scalar(scalar) => TyKind::Scalar(scalar),
chalk_ir::TyKind::Tuple(cardinality, subst) => {
TyKind::Tuple(cardinality, from_chalk(db, subst))
}
chalk_ir::TyKind::Raw(mutability, ty) => TyKind::Raw(mutability, from_chalk(db, ty)),
chalk_ir::TyKind::Slice(ty) => TyKind::Slice(from_chalk(db, ty)),
2021-04-06 18:51:21 +00:00
chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => {
// HACK: we sometimes get lifetime variables back in solutions
// from Chalk, and don't have the infrastructure to substitute
// them yet. So for now we just turn them into 'static right
// when we get them
TyKind::Ref(mutability, static_lifetime(), from_chalk(db, ty))
}
chalk_ir::TyKind::Str => TyKind::Str,
chalk_ir::TyKind::Never => TyKind::Never,
chalk_ir::TyKind::FnDef(fn_def_id, subst) => {
2021-03-13 16:55:50 +00:00
TyKind::FnDef(fn_def_id, from_chalk(db, subst))
}
2021-03-13 18:27:09 +00:00
chalk_ir::TyKind::Closure(id, subst) => TyKind::Closure(id, from_chalk(db, subst)),
2021-04-05 12:38:28 +00:00
chalk_ir::TyKind::Foreign(foreign_def_id) => TyKind::Foreign(foreign_def_id),
chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME
chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME
2020-05-22 15:14:53 +00:00
}
.intern(&Interner)
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 {
match self.interned() {
crate::GenericArgData::Ty(ty) => ty.clone().to_chalk(db).cast(&Interner),
}
}
fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self {
match chalk.interned() {
chalk_ir::GenericArgData::Ty(ty) => Ty::from_chalk(db, ty.clone()).cast(&Interner),
chalk_ir::GenericArgData::Lifetime(_) => unimplemented!(),
chalk_ir::GenericArgData::Const(_) => unimplemented!(),
}
}
}
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> {
chalk_ir::Substitution::from_iter(
&Interner,
self.iter(&Interner).map(|ty| ty.clone().to_chalk(db)),
)
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 {
let tys = parameters.iter(&Interner).map(|p| from_chalk(db, p.clone())).collect();
Substitution::intern(tys)
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-03-18 20:53:19 +00:00
let trait_id = self.trait_id;
let substitution = self.substitution.to_chalk(db);
2020-05-22 15:14:53 +00:00
chalk_ir::TraitRef { trait_id, substitution }
}
fn from_chalk(db: &dyn HirDatabase, trait_ref: chalk_ir::TraitRef<Interner>) -> Self {
2021-03-18 20:53:19 +00:00
let trait_id = trait_ref.trait_id;
2020-05-22 15:14:53 +00:00
let substs = from_chalk(db, trait_ref.substitution);
2021-03-18 20:53:19 +00:00
TraitRef { trait_id, substitution: substs }
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> {
2020-05-22 15:14:53 +00:00
match self {
2021-03-20 09:46:36 +00:00
WhereClause::Implemented(trait_ref) => {
chalk_ir::WhereClause::Implemented(trait_ref.to_chalk(db))
2020-05-22 15:14:53 +00:00
}
WhereClause::AliasEq(alias_eq) => chalk_ir::WhereClause::AliasEq(alias_eq.to_chalk(db)),
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 {
match where_clause {
2021-03-20 09:46:36 +00:00
chalk_ir::WhereClause::Implemented(tr) => WhereClause::Implemented(from_chalk(db, tr)),
chalk_ir::WhereClause::AliasEq(alias_eq) => {
2021-03-20 09:46:36 +00:00
WhereClause::AliasEq(from_chalk(db, alias_eq))
2020-05-22 15:14:53 +00:00
}
2020-06-05 15:06:07 +00:00
chalk_ir::WhereClause::LifetimeOutlives(_) => {
// we shouldn't get these from Chalk
panic!("encountered LifetimeOutlives from Chalk")
}
2020-07-11 13:22:46 +00:00
chalk_ir::WhereClause::TypeOutlives(_) => {
// we shouldn't get these from Chalk
panic!("encountered TypeOutlives from Chalk")
}
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> {
chalk_ir::ProjectionTy {
associated_ty_id: self.associated_ty_id,
substitution: self.substitution.to_chalk(db),
2020-05-22 15:14:53 +00:00
}
}
fn from_chalk(
db: &dyn HirDatabase,
projection_ty: chalk_ir::ProjectionTy<Interner>,
) -> ProjectionTy {
ProjectionTy {
associated_ty_id: projection_ty.associated_ty_id,
substitution: from_chalk(db, projection_ty.substitution),
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 {
chalk_ir::OpaqueTy {
opaque_ty_id: self.opaque_ty_id,
substitution: self.substitution.to_chalk(db),
}
}
fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self {
OpaqueTy {
opaque_ty_id: chalk.opaque_ty_id,
substitution: from_chalk(db, chalk.substitution),
}
}
}
impl ToChalk for AliasTy {
type Chalk = chalk_ir::AliasTy<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk {
match self {
AliasTy::Projection(projection_ty) => {
chalk_ir::AliasTy::Projection(projection_ty.to_chalk(db))
}
AliasTy::Opaque(opaque_ty) => chalk_ir::AliasTy::Opaque(opaque_ty.to_chalk(db)),
}
}
fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self {
match chalk {
chalk_ir::AliasTy::Projection(projection_ty) => {
AliasTy::Projection(from_chalk(db, projection_ty))
}
chalk_ir::AliasTy::Opaque(opaque_ty) => AliasTy::Opaque(from_chalk(db, opaque_ty)),
}
}
}
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> {
chalk_ir::AliasEq { alias: self.alias.to_chalk(db), ty: self.ty.to_chalk(db) }
2020-05-22 15:14:53 +00:00
}
fn from_chalk(db: &dyn HirDatabase, alias_eq: chalk_ir::AliasEq<Interner>) -> Self {
AliasEq { alias: from_chalk(db, alias_eq.alias), ty: from_chalk(db, alias_eq.ty) }
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> {
match self {
DomainGoal::Holds(WhereClause::Implemented(tr)) => tr.to_chalk(db).cast(&Interner),
DomainGoal::Holds(WhereClause::AliasEq(alias_eq)) => {
alias_eq.to_chalk(db).cast(&Interner)
}
2020-05-22 15:14:53 +00:00
}
}
fn from_chalk(_db: &dyn HirDatabase, _goal: chalk_ir::DomainGoal<Interner>) -> Self {
unimplemented!()
}
}
impl<T> ToChalk for Canonical<T>
where
T: ToChalk,
T::Chalk: HasInterner<Interner = Interner>,
{
type Chalk = chalk_ir::Canonical<T::Chalk>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical<T::Chalk> {
let value = self.value.to_chalk(db);
chalk_ir::Canonical { value, binders: self.binders }
2020-05-22 15:14:53 +00:00
}
fn from_chalk(db: &dyn HirDatabase, canonical: chalk_ir::Canonical<T::Chalk>) -> Canonical<T> {
Canonical { binders: canonical.binders, value: from_chalk(db, canonical.value) }
2020-05-22 15:14:53 +00:00
}
}
impl<T: ToChalk> ToChalk for InEnvironment<T>
where
T::Chalk: chalk_ir::interner::HasInterner<Interner = Interner>,
{
type Chalk = chalk_ir::InEnvironment<T::Chalk>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::InEnvironment<T::Chalk> {
chalk_ir::InEnvironment { environment: self.environment, goal: self.goal.to_chalk(db) }
2020-05-22 15:14:53 +00:00
}
fn from_chalk(
2021-03-12 18:12:17 +00:00
_db: &dyn HirDatabase,
_in_env: chalk_ir::InEnvironment<T::Chalk>,
2020-05-22 15:14:53 +00:00
) -> InEnvironment<T> {
2021-03-12 18:12:17 +00:00
unimplemented!()
2020-05-22 15:14:53 +00:00
}
}
impl<T: ToChalk> ToChalk for crate::Binders<T>
where
T::Chalk: chalk_ir::interner::HasInterner<Interner = Interner>,
{
type Chalk = chalk_ir::Binders<T::Chalk>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Binders<T::Chalk> {
2021-04-05 15:13:50 +00:00
let (value, binders) = self.into_value_and_skipped_binders();
2021-04-05 15:45:18 +00:00
chalk_ir::Binders::new(binders, value.to_chalk(db))
}
fn from_chalk(db: &dyn HirDatabase, binders: chalk_ir::Binders<T::Chalk>) -> crate::Binders<T> {
let (v, b) = binders.into_value_and_skipped_binders();
2021-04-05 15:45:18 +00:00
crate::Binders::new(b, from_chalk(db, v))
}
}
impl ToChalk for crate::ConstrainedSubst {
type Chalk = chalk_ir::ConstrainedSubst<Interner>;
fn to_chalk(self, _db: &dyn HirDatabase) -> Self::Chalk {
unimplemented!()
}
fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self {
ConstrainedSubst { subst: from_chalk(db, chalk.subst) }
}
}
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
}
}