2020-05-22 10:14:53 -05: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-03-13 12:47:34 -06:00
|
|
|
use chalk_ir::{cast::Cast, fold::shift::Shift, interner::HasInterner, LifetimeData};
|
2020-05-27 14:05:21 -05:00
|
|
|
use chalk_solve::rust_ir;
|
2020-05-22 10:14:53 -05:00
|
|
|
|
2020-08-13 09:25:38 -05:00
|
|
|
use base_db::salsa::InternKey;
|
2021-03-21 07:22:22 -05:00
|
|
|
use hir_def::{GenericDefId, TypeAliasId};
|
2020-05-22 10:14:53 -05:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
db::HirDatabase,
|
2021-02-28 03:58:34 -06:00
|
|
|
primitive::UintTy,
|
2021-03-20 05:23:59 -05:00
|
|
|
traits::{Canonical, DomainGoal},
|
2021-03-21 07:22:22 -05:00
|
|
|
AliasTy, CallableDefId, FnPointer, InEnvironment, OpaqueTy, ProjectionTy,
|
|
|
|
QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TypeWalk, WhereClause,
|
2020-05-22 10:14:53 -05: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-03-14 11:25:29 -05:00
|
|
|
match self.into_inner() {
|
2021-03-14 11:40:55 -05:00
|
|
|
TyKind::Ref(m, ty) => ref_to_chalk(db, m, ty),
|
|
|
|
TyKind::Array(ty) => array_to_chalk(db, ty),
|
2021-03-14 10:30:02 -05:00
|
|
|
TyKind::Function(FnPointer { sig, substs, .. }) => {
|
2021-02-28 12:13:37 -06:00
|
|
|
let substitution = chalk_ir::FnSubst(substs.to_chalk(db).shifted_in(&Interner));
|
|
|
|
chalk_ir::TyKind::Function(chalk_ir::FnPointer {
|
|
|
|
num_binders: 0,
|
2021-03-14 10:30:02 -05:00
|
|
|
sig,
|
2021-02-28 12:13:37 -06:00
|
|
|
substitution,
|
|
|
|
})
|
|
|
|
.intern(&Interner)
|
|
|
|
}
|
2021-03-13 10:36:07 -06:00
|
|
|
TyKind::AssociatedType(assoc_type_id, substs) => {
|
2021-02-28 12:13:37 -06:00
|
|
|
let substitution = substs.to_chalk(db);
|
|
|
|
chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner)
|
|
|
|
}
|
2020-10-30 12:57:16 -05:00
|
|
|
|
2021-03-13 13:05:47 -06:00
|
|
|
TyKind::OpaqueType(id, substs) => {
|
2021-02-28 12:13:37 -06:00
|
|
|
let substitution = substs.to_chalk(db);
|
|
|
|
chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner)
|
|
|
|
}
|
2020-10-30 12:57:16 -05:00
|
|
|
|
2021-03-13 10:23:19 -06:00
|
|
|
TyKind::ForeignType(id) => chalk_ir::TyKind::Foreign(id).intern(&Interner),
|
2020-10-30 12:57:16 -05:00
|
|
|
|
2021-03-13 07:44:51 -06:00
|
|
|
TyKind::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner),
|
2020-10-30 12:57:16 -05:00
|
|
|
|
2021-03-13 07:44:51 -06:00
|
|
|
TyKind::Tuple(cardinality, substs) => {
|
2021-02-28 12:13:37 -06:00
|
|
|
let substitution = substs.to_chalk(db);
|
2021-03-16 19:27:56 -05:00
|
|
|
chalk_ir::TyKind::Tuple(cardinality, substitution).intern(&Interner)
|
2021-02-28 12:13:37 -06:00
|
|
|
}
|
2021-03-14 11:40:55 -05:00
|
|
|
TyKind::Raw(mutability, ty) => {
|
|
|
|
let ty = ty.to_chalk(db);
|
2021-03-01 12:30:34 -06:00
|
|
|
chalk_ir::TyKind::Raw(mutability, ty).intern(&Interner)
|
2021-02-28 12:13:37 -06:00
|
|
|
}
|
2021-03-14 11:40:55 -05:00
|
|
|
TyKind::Slice(ty) => chalk_ir::TyKind::Slice(ty.to_chalk(db)).intern(&Interner),
|
2021-03-13 07:44:51 -06:00
|
|
|
TyKind::Str => chalk_ir::TyKind::Str.intern(&Interner),
|
2021-03-13 10:55:50 -06:00
|
|
|
TyKind::FnDef(id, substs) => {
|
2021-02-28 12:13:37 -06:00
|
|
|
let substitution = substs.to_chalk(db);
|
|
|
|
chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner)
|
|
|
|
}
|
2021-03-13 07:44:51 -06:00
|
|
|
TyKind::Never => chalk_ir::TyKind::Never.intern(&Interner),
|
2020-10-30 12:57:16 -05:00
|
|
|
|
2021-03-13 12:27:09 -06:00
|
|
|
TyKind::Closure(closure_id, substs) => {
|
2021-02-28 12:13:37 -06:00
|
|
|
let substitution = substs.to_chalk(db);
|
2021-03-13 12:27:09 -06:00
|
|
|
chalk_ir::TyKind::Closure(closure_id, substitution).intern(&Interner)
|
2021-02-28 12:13:37 -06:00
|
|
|
}
|
2020-10-30 12:57:16 -05:00
|
|
|
|
2021-03-13 07:44:51 -06:00
|
|
|
TyKind::Adt(adt_id, substs) => {
|
2021-02-28 12:13:37 -06:00
|
|
|
let substitution = substs.to_chalk(db);
|
2021-03-01 14:57:39 -06:00
|
|
|
chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner)
|
2021-02-28 12:13:37 -06:00
|
|
|
}
|
2021-03-13 07:44:51 -06:00
|
|
|
TyKind::Alias(AliasTy::Projection(proj_ty)) => {
|
2021-03-14 10:26:12 -05:00
|
|
|
let associated_ty_id = proj_ty.associated_ty_id;
|
|
|
|
let substitution = proj_ty.substitution.to_chalk(db);
|
2020-05-22 10:14:53 -05:00
|
|
|
chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy {
|
|
|
|
associated_ty_id,
|
|
|
|
substitution,
|
|
|
|
})
|
|
|
|
.cast(&Interner)
|
|
|
|
.intern(&Interner)
|
|
|
|
}
|
2021-03-14 10:33:27 -05:00
|
|
|
TyKind::Alias(AliasTy::Opaque(opaque_ty)) => {
|
|
|
|
let opaque_ty_id = opaque_ty.opaque_ty_id;
|
|
|
|
let substitution = opaque_ty.substitution.to_chalk(db);
|
|
|
|
chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy { opaque_ty_id, substitution })
|
|
|
|
.cast(&Interner)
|
|
|
|
.intern(&Interner)
|
|
|
|
}
|
2021-03-13 12:47:34 -06:00
|
|
|
TyKind::Placeholder(idx) => idx.to_ty::<Interner>(&Interner),
|
2021-03-13 07:44:51 -06:00
|
|
|
TyKind::BoundVar(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner),
|
|
|
|
TyKind::InferenceVar(..) => panic!("uncanonicalized infer ty"),
|
2021-03-21 07:22:22 -05:00
|
|
|
TyKind::Dyn(dyn_ty) => {
|
2020-07-11 08:22:46 -05:00
|
|
|
let where_clauses = chalk_ir::QuantifiedWhereClauses::from_iter(
|
2020-05-22 10:14:53 -05:00
|
|
|
&Interner,
|
2021-03-21 07:22:22 -05:00
|
|
|
dyn_ty.bounds.value.interned().iter().cloned().map(|p| p.to_chalk(db)),
|
2020-05-22 10:14:53 -05:00
|
|
|
);
|
2020-06-22 06:18:10 -05:00
|
|
|
let bounded_ty = chalk_ir::DynTy {
|
|
|
|
bounds: make_binders(where_clauses, 1),
|
2020-10-23 08:45:15 -05:00
|
|
|
lifetime: LifetimeData::Static.intern(&Interner),
|
2020-06-22 06:18:10 -05:00
|
|
|
};
|
2020-10-30 12:57:16 -05:00
|
|
|
chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner)
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
2021-03-13 07:44:51 -06:00
|
|
|
TyKind::Unknown => chalk_ir::TyKind::Error.intern(&Interner),
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self {
|
2020-10-30 12:57:16 -05:00
|
|
|
match chalk.data(&Interner).kind.clone() {
|
2021-03-13 07:44:51 -06:00
|
|
|
chalk_ir::TyKind::Error => TyKind::Unknown,
|
2021-03-14 11:40:55 -05:00
|
|
|
chalk_ir::TyKind::Array(ty, _size) => TyKind::Array(from_chalk(db, ty)),
|
2021-03-13 12:47:34 -06:00
|
|
|
chalk_ir::TyKind::Placeholder(idx) => TyKind::Placeholder(idx),
|
2020-10-30 12:57:16 -05:00
|
|
|
chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => {
|
2021-03-13 10:36:07 -06:00
|
|
|
let associated_ty = proj.associated_ty_id;
|
2020-05-22 10:14:53 -05:00
|
|
|
let parameters = from_chalk(db, proj.substitution);
|
2021-03-14 10:30:02 -05:00
|
|
|
TyKind::Alias(AliasTy::Projection(ProjectionTy {
|
|
|
|
associated_ty_id: associated_ty,
|
|
|
|
substitution: parameters,
|
|
|
|
}))
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
2020-10-30 12:57:16 -05:00
|
|
|
chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(opaque_ty)) => {
|
2021-03-13 13:05:47 -06:00
|
|
|
let opaque_ty_id = opaque_ty.opaque_ty_id;
|
2020-03-04 16:00:44 -06:00
|
|
|
let parameters = from_chalk(db, opaque_ty.substitution);
|
2021-03-14 10:33:27 -05:00
|
|
|
TyKind::Alias(AliasTy::Opaque(OpaqueTy { opaque_ty_id, substitution: parameters }))
|
2020-03-04 16:00:44 -06:00
|
|
|
}
|
2020-10-30 12:57:16 -05:00
|
|
|
chalk_ir::TyKind::Function(chalk_ir::FnPointer {
|
2020-07-30 12:37:28 -05:00
|
|
|
num_binders,
|
2021-03-14 10:30:02 -05:00
|
|
|
sig,
|
2020-07-30 12:37:28 -05:00
|
|
|
substitution,
|
|
|
|
..
|
|
|
|
}) => {
|
2020-07-10 12:14:33 -05:00
|
|
|
assert_eq!(num_binders, 0);
|
2021-03-15 15:02:34 -05:00
|
|
|
let substs: Substitution = from_chalk(
|
2020-07-10 12:14:33 -05:00
|
|
|
db,
|
2020-11-20 11:00:34 -06:00
|
|
|
substitution.0.shifted_out(&Interner).expect("fn ptr should have no binders"),
|
2020-07-10 12:14:33 -05:00
|
|
|
);
|
2021-03-14 10:30:02 -05:00
|
|
|
TyKind::Function(FnPointer { num_args: (substs.len() - 1), sig, substs })
|
2020-05-22 12:13:17 -05:00
|
|
|
}
|
2021-03-13 07:44:51 -06:00
|
|
|
chalk_ir::TyKind::BoundVar(idx) => TyKind::BoundVar(idx),
|
|
|
|
chalk_ir::TyKind::InferenceVar(_iv, _kind) => TyKind::Unknown,
|
2020-10-30 12:57:16 -05:00
|
|
|
chalk_ir::TyKind::Dyn(where_clauses) => {
|
2020-05-22 10:14:53 -05:00
|
|
|
assert_eq!(where_clauses.bounds.binders.len(&Interner), 1);
|
2021-03-21 07:22:22 -05:00
|
|
|
let bounds = where_clauses
|
2020-05-22 10:14:53 -05:00
|
|
|
.bounds
|
|
|
|
.skip_binders()
|
|
|
|
.iter(&Interner)
|
2021-03-21 07:22:22 -05:00
|
|
|
.map(|c| from_chalk(db, c.clone()));
|
|
|
|
TyKind::Dyn(crate::DynTy {
|
|
|
|
bounds: crate::Binders::new(
|
|
|
|
1,
|
|
|
|
crate::QuantifiedWhereClauses::from_iter(&Interner, bounds),
|
|
|
|
),
|
|
|
|
})
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
2020-10-30 12:57:16 -05:00
|
|
|
|
2021-03-13 07:44:51 -06:00
|
|
|
chalk_ir::TyKind::Adt(adt_id, subst) => TyKind::Adt(adt_id, from_chalk(db, subst)),
|
2021-03-13 10:36:07 -06:00
|
|
|
chalk_ir::TyKind::AssociatedType(type_id, subst) => {
|
|
|
|
TyKind::AssociatedType(type_id, from_chalk(db, subst))
|
|
|
|
}
|
2021-02-28 12:13:37 -06:00
|
|
|
|
2020-10-30 12:57:16 -05:00
|
|
|
chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => {
|
2021-03-13 13:05:47 -06:00
|
|
|
TyKind::OpaqueType(opaque_type_id, from_chalk(db, subst))
|
2020-10-30 12:57:16 -05:00
|
|
|
}
|
|
|
|
|
2021-03-13 07:44:51 -06:00
|
|
|
chalk_ir::TyKind::Scalar(scalar) => TyKind::Scalar(scalar),
|
2020-10-30 12:57:16 -05:00
|
|
|
chalk_ir::TyKind::Tuple(cardinality, subst) => {
|
2021-03-13 07:44:51 -06:00
|
|
|
TyKind::Tuple(cardinality, from_chalk(db, subst))
|
2020-10-30 12:57:16 -05:00
|
|
|
}
|
2021-03-14 11:40:55 -05:00
|
|
|
chalk_ir::TyKind::Raw(mutability, ty) => TyKind::Raw(mutability, from_chalk(db, ty)),
|
|
|
|
chalk_ir::TyKind::Slice(ty) => TyKind::Slice(from_chalk(db, ty)),
|
2020-10-30 12:57:16 -05:00
|
|
|
chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => {
|
2021-03-14 11:40:55 -05:00
|
|
|
TyKind::Ref(mutability, from_chalk(db, ty))
|
2020-10-30 12:57:16 -05:00
|
|
|
}
|
2021-03-13 07:44:51 -06:00
|
|
|
chalk_ir::TyKind::Str => TyKind::Str,
|
|
|
|
chalk_ir::TyKind::Never => TyKind::Never,
|
2020-10-30 12:57:16 -05:00
|
|
|
|
|
|
|
chalk_ir::TyKind::FnDef(fn_def_id, subst) => {
|
2021-03-13 10:55:50 -06:00
|
|
|
TyKind::FnDef(fn_def_id, from_chalk(db, subst))
|
2020-10-30 12:57:16 -05:00
|
|
|
}
|
|
|
|
|
2021-03-13 12:27:09 -06:00
|
|
|
chalk_ir::TyKind::Closure(id, subst) => TyKind::Closure(id, from_chalk(db, subst)),
|
2020-10-30 12:57:16 -05:00
|
|
|
|
2021-03-13 10:23:19 -06:00
|
|
|
chalk_ir::TyKind::Foreign(foreign_def_id) => TyKind::ForeignType(foreign_def_id),
|
2020-10-30 12:57:16 -05:00
|
|
|
chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME
|
|
|
|
chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
2021-03-13 07:44:51 -06:00
|
|
|
.intern(&Interner)
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// We currently don't model lifetimes, but Chalk does. So, we have to insert a
|
|
|
|
/// fake lifetime here, because Chalks built-in logic may expect it to be there.
|
|
|
|
fn ref_to_chalk(
|
|
|
|
db: &dyn HirDatabase,
|
2021-03-01 12:30:34 -06:00
|
|
|
mutability: chalk_ir::Mutability,
|
2021-03-14 11:40:55 -05:00
|
|
|
ty: Ty,
|
2020-05-22 10:14:53 -05:00
|
|
|
) -> chalk_ir::Ty<Interner> {
|
2021-03-14 11:40:55 -05:00
|
|
|
let arg = ty.to_chalk(db);
|
2020-10-23 08:45:15 -05:00
|
|
|
let lifetime = LifetimeData::Static.intern(&Interner);
|
2021-03-01 12:30:34 -06:00
|
|
|
chalk_ir::TyKind::Ref(mutability, lifetime, arg).intern(&Interner)
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
|
2020-07-11 11:33:50 -05:00
|
|
|
/// We currently don't model constants, but Chalk does. So, we have to insert a
|
|
|
|
/// fake constant here, because Chalks built-in logic may expect it to be there.
|
2021-03-14 11:40:55 -05:00
|
|
|
fn array_to_chalk(db: &dyn HirDatabase, ty: Ty) -> chalk_ir::Ty<Interner> {
|
|
|
|
let arg = ty.to_chalk(db);
|
2021-02-28 03:58:34 -06:00
|
|
|
let usize_ty = chalk_ir::TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(&Interner);
|
2020-10-23 08:45:15 -05:00
|
|
|
let const_ = chalk_ir::ConstData {
|
|
|
|
ty: usize_ty,
|
|
|
|
value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst { interned: () }),
|
|
|
|
}
|
|
|
|
.intern(&Interner);
|
2020-10-30 12:57:16 -05:00
|
|
|
chalk_ir::TyKind::Array(arg, const_).intern(&Interner)
|
2020-07-11 11:33:50 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 15:02:34 -05:00
|
|
|
impl ToChalk for Substitution {
|
2020-05-22 10:14:53 -05:00
|
|
|
type Chalk = chalk_ir::Substitution<Interner>;
|
|
|
|
|
|
|
|
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Substitution<Interner> {
|
2020-07-11 08:22:46 -05:00
|
|
|
chalk_ir::Substitution::from_iter(&Interner, self.iter().map(|ty| ty.clone().to_chalk(db)))
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 15:02:34 -05:00
|
|
|
fn from_chalk(
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
parameters: chalk_ir::Substitution<Interner>,
|
|
|
|
) -> Substitution {
|
2020-05-22 10:14:53 -05:00
|
|
|
let tys = parameters
|
|
|
|
.iter(&Interner)
|
|
|
|
.map(|p| match p.ty(&Interner) {
|
|
|
|
Some(ty) => from_chalk(db, ty.clone()),
|
|
|
|
None => unimplemented!(),
|
|
|
|
})
|
|
|
|
.collect();
|
2021-03-15 15:02:34 -05:00
|
|
|
Substitution(tys)
|
2020-05-22 10:14:53 -05: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 15:53:19 -05:00
|
|
|
let trait_id = self.trait_id;
|
|
|
|
let substitution = self.substitution.to_chalk(db);
|
2020-05-22 10:14:53 -05:00
|
|
|
chalk_ir::TraitRef { trait_id, substitution }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_chalk(db: &dyn HirDatabase, trait_ref: chalk_ir::TraitRef<Interner>) -> Self {
|
2021-03-18 15:53:19 -05:00
|
|
|
let trait_id = trait_ref.trait_id;
|
2020-05-22 10:14:53 -05:00
|
|
|
let substs = from_chalk(db, trait_ref.substitution);
|
2021-03-18 15:53:19 -05:00
|
|
|
TraitRef { trait_id, substitution: substs }
|
2020-05-22 10:14:53 -05: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 08:26:02 -05:00
|
|
|
impl ToChalk for hir_def::ImplId {
|
2020-05-22 10:14:53 -05:00
|
|
|
type Chalk = ImplId;
|
|
|
|
|
2020-07-12 08:26:02 -05:00
|
|
|
fn to_chalk(self, _db: &dyn HirDatabase) -> ImplId {
|
|
|
|
chalk_ir::ImplId(self.as_intern_id())
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
|
2020-07-12 08:26:02 -05:00
|
|
|
fn from_chalk(_db: &dyn HirDatabase, impl_id: ImplId) -> hir_def::ImplId {
|
|
|
|
InternKey::from_intern_id(impl_id.0)
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 06:15:00 -05:00
|
|
|
impl ToChalk for CallableDefId {
|
2020-05-22 11:15:53 -05:00
|
|
|
type Chalk = FnDefId;
|
|
|
|
|
|
|
|
fn to_chalk(self, db: &dyn HirDatabase) -> FnDefId {
|
|
|
|
db.intern_callable_def(self).into()
|
|
|
|
}
|
|
|
|
|
2020-07-16 06:15:00 -05:00
|
|
|
fn from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDefId {
|
2020-05-22 11:15:53 -05:00
|
|
|
db.lookup_intern_callable_def(fn_def_id.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 09:31:38 -06:00
|
|
|
pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId);
|
2020-07-12 08:26:02 -05:00
|
|
|
|
|
|
|
impl ToChalk for TypeAliasAsValue {
|
2020-05-22 10:14:53 -05:00
|
|
|
type Chalk = AssociatedTyValueId;
|
|
|
|
|
2020-07-12 08:26:02 -05:00
|
|
|
fn to_chalk(self, _db: &dyn HirDatabase) -> AssociatedTyValueId {
|
|
|
|
rust_ir::AssociatedTyValueId(self.0.as_intern_id())
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
|
2020-07-12 08:26:02 -05: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 10:14:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 04:46:36 -05:00
|
|
|
impl ToChalk for WhereClause {
|
2021-03-21 07:22:22 -05:00
|
|
|
type Chalk = chalk_ir::WhereClause<Interner>;
|
2020-05-22 10:14:53 -05:00
|
|
|
|
2021-03-21 07:22:22 -05:00
|
|
|
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::WhereClause<Interner> {
|
2020-05-22 10:14:53 -05:00
|
|
|
match self {
|
2021-03-20 04:46:36 -05:00
|
|
|
WhereClause::Implemented(trait_ref) => {
|
2021-03-21 07:22:22 -05:00
|
|
|
chalk_ir::WhereClause::Implemented(trait_ref.to_chalk(db))
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
2021-03-21 07:22:22 -05:00
|
|
|
WhereClause::AliasEq(alias_eq) => chalk_ir::WhereClause::AliasEq(alias_eq.to_chalk(db)),
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_chalk(
|
|
|
|
db: &dyn HirDatabase,
|
2021-03-21 07:22:22 -05:00
|
|
|
where_clause: chalk_ir::WhereClause<Interner>,
|
2021-03-20 04:46:36 -05:00
|
|
|
) -> WhereClause {
|
2021-03-21 07:22:22 -05:00
|
|
|
match where_clause {
|
2021-03-20 04:46:36 -05:00
|
|
|
chalk_ir::WhereClause::Implemented(tr) => WhereClause::Implemented(from_chalk(db, tr)),
|
2021-03-18 20:07:15 -05:00
|
|
|
chalk_ir::WhereClause::AliasEq(alias_eq) => {
|
2021-03-20 04:46:36 -05:00
|
|
|
WhereClause::AliasEq(from_chalk(db, alias_eq))
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
2020-06-05 10:06:07 -05:00
|
|
|
|
|
|
|
chalk_ir::WhereClause::LifetimeOutlives(_) => {
|
|
|
|
// we shouldn't get these from Chalk
|
|
|
|
panic!("encountered LifetimeOutlives from Chalk")
|
|
|
|
}
|
2020-07-11 08:22:46 -05:00
|
|
|
|
|
|
|
chalk_ir::WhereClause::TypeOutlives(_) => {
|
|
|
|
// we shouldn't get these from Chalk
|
|
|
|
panic!("encountered TypeOutlives from Chalk")
|
|
|
|
}
|
2020-05-22 10:14:53 -05: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 {
|
2021-03-14 10:26:12 -05:00
|
|
|
associated_ty_id: self.associated_ty_id,
|
|
|
|
substitution: self.substitution.to_chalk(db),
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_chalk(
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
projection_ty: chalk_ir::ProjectionTy<Interner>,
|
|
|
|
) -> ProjectionTy {
|
|
|
|
ProjectionTy {
|
2021-03-14 10:26:12 -05:00
|
|
|
associated_ty_id: projection_ty.associated_ty_id,
|
|
|
|
substitution: from_chalk(db, projection_ty.substitution),
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 20:07:15 -05: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 10:14:53 -05:00
|
|
|
|
2021-03-18 20:07:15 -05:00
|
|
|
impl ToChalk for AliasEq {
|
2020-05-22 10:14:53 -05:00
|
|
|
type Chalk = chalk_ir::AliasEq<Interner>;
|
|
|
|
|
|
|
|
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::AliasEq<Interner> {
|
2021-03-18 20:07:15 -05:00
|
|
|
chalk_ir::AliasEq { alias: self.alias.to_chalk(db), ty: self.ty.to_chalk(db) }
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
|
2021-03-18 20:07:15 -05: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 10:14:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 05:23:59 -05:00
|
|
|
impl ToChalk for DomainGoal {
|
2020-05-22 10:14:53 -05:00
|
|
|
type Chalk = chalk_ir::DomainGoal<Interner>;
|
|
|
|
|
|
|
|
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::DomainGoal<Interner> {
|
|
|
|
match self {
|
2021-03-20 05:23:59 -05:00
|
|
|
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 10:14:53 -05: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> {
|
2021-03-01 05:35:11 -06:00
|
|
|
let kinds = self.kinds.iter().map(|&tk| {
|
|
|
|
chalk_ir::CanonicalVarKind::new(
|
|
|
|
chalk_ir::VariableKind::Ty(tk),
|
|
|
|
chalk_ir::UniverseIndex::ROOT,
|
|
|
|
)
|
|
|
|
});
|
2020-05-22 10:14:53 -05:00
|
|
|
let value = self.value.to_chalk(db);
|
2020-07-10 11:30:32 -05:00
|
|
|
chalk_ir::Canonical {
|
|
|
|
value,
|
|
|
|
binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds),
|
|
|
|
}
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn from_chalk(db: &dyn HirDatabase, canonical: chalk_ir::Canonical<T::Chalk>) -> Canonical<T> {
|
2020-06-28 14:17:27 -05:00
|
|
|
let kinds = canonical
|
|
|
|
.binders
|
|
|
|
.iter(&Interner)
|
|
|
|
.map(|k| match k.kind {
|
2021-03-01 05:35:11 -06:00
|
|
|
chalk_ir::VariableKind::Ty(tk) => tk,
|
2020-11-20 11:00:34 -06:00
|
|
|
// HACK: Chalk can sometimes return new lifetime variables. We
|
|
|
|
// want to just skip them, but to not mess up the indices of
|
|
|
|
// other variables, we'll just create a new type variable in
|
|
|
|
// their place instead. This should not matter (we never see the
|
|
|
|
// actual *uses* of the lifetime variable).
|
2021-03-01 05:35:11 -06:00
|
|
|
chalk_ir::VariableKind::Lifetime => chalk_ir::TyVariableKind::General,
|
2020-06-28 14:17:27 -05:00
|
|
|
chalk_ir::VariableKind::Const(_) => panic!("unexpected const from Chalk"),
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
Canonical { kinds, value: from_chalk(db, canonical.value) }
|
2020-05-22 10:14:53 -05: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 {
|
2021-03-12 12:12:17 -06:00
|
|
|
environment: self.environment.env.clone(),
|
2020-05-22 10:14:53 -05:00
|
|
|
goal: self.value.to_chalk(db),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_chalk(
|
2021-03-12 12:12:17 -06:00
|
|
|
_db: &dyn HirDatabase,
|
|
|
|
_in_env: chalk_ir::InEnvironment<T::Chalk>,
|
2020-05-22 10:14:53 -05:00
|
|
|
) -> InEnvironment<T> {
|
2021-03-12 12:12:17 -06:00
|
|
|
unimplemented!()
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 07:22:22 -05: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> {
|
|
|
|
chalk_ir::Binders::new(
|
|
|
|
chalk_ir::VariableKinds::from_iter(
|
|
|
|
&Interner,
|
|
|
|
std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General))
|
|
|
|
.take(self.num_binders),
|
|
|
|
),
|
|
|
|
self.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();
|
|
|
|
crate::Binders::new(b.len(&Interner), from_chalk(db, v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 10:14:53 -05: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 08:22:46 -05:00
|
|
|
chalk_ir::VariableKinds::from_iter(
|
2020-05-22 10:14:53 -05:00
|
|
|
&Interner,
|
2020-10-30 12:57:16 -05:00
|
|
|
std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General))
|
|
|
|
.take(num_vars),
|
2020-05-22 10:14:53 -05:00
|
|
|
),
|
|
|
|
value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn convert_where_clauses(
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
def: GenericDefId,
|
2021-03-15 15:02:34 -05:00
|
|
|
substs: &Substitution,
|
2020-05-22 10:14:53 -05: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-03-21 11:40:14 -05:00
|
|
|
result.push(pred.clone().subst(substs).to_chalk(db));
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn generic_predicate_to_inline_bound(
|
|
|
|
db: &dyn HirDatabase,
|
2021-03-21 07:22:22 -05:00
|
|
|
pred: &QuantifiedWhereClause,
|
2020-05-22 10:14:53 -05:00
|
|
|
self_ty: &Ty,
|
2021-03-21 07:22:22 -05:00
|
|
|
) -> Option<chalk_ir::Binders<rust_ir::InlineBound<Interner>>> {
|
2020-05-22 10:14:53 -05: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-03-21 07:22:22 -05:00
|
|
|
let self_ty_shifted_in = self_ty.clone().shift_bound_vars(DebruijnIndex::ONE);
|
|
|
|
match &pred.value {
|
2021-03-20 04:46:36 -05:00
|
|
|
WhereClause::Implemented(trait_ref) => {
|
2021-03-21 07:22:22 -05:00
|
|
|
if trait_ref.self_type_parameter() != &self_ty_shifted_in {
|
2020-05-22 10:14:53 -05:00
|
|
|
// we can only convert predicates back to type bounds if they
|
|
|
|
// have the expected self type
|
|
|
|
return None;
|
|
|
|
}
|
2021-03-18 15:53:19 -05:00
|
|
|
let args_no_self = trait_ref.substitution[1..]
|
2020-05-22 10:14:53 -05:00
|
|
|
.iter()
|
|
|
|
.map(|ty| ty.clone().to_chalk(db).cast(&Interner))
|
|
|
|
.collect();
|
2021-03-18 15:53:19 -05:00
|
|
|
let trait_bound = rust_ir::TraitBound { trait_id: trait_ref.trait_id, args_no_self };
|
2021-03-21 07:22:22 -05:00
|
|
|
Some(make_binders(rust_ir::InlineBound::TraitBound(trait_bound), pred.num_binders))
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
2021-03-20 04:46:36 -05:00
|
|
|
WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(projection_ty), ty }) => {
|
2021-03-21 07:22:22 -05:00
|
|
|
if projection_ty.self_type_parameter() != &self_ty_shifted_in {
|
2020-05-22 10:14:53 -05:00
|
|
|
return None;
|
|
|
|
}
|
2021-03-21 07:22:22 -05:00
|
|
|
let trait_ = projection_ty.trait_(db);
|
2021-03-18 20:07:15 -05:00
|
|
|
let args_no_self = projection_ty.substitution[1..]
|
2020-05-22 10:14:53 -05:00
|
|
|
.iter()
|
|
|
|
.map(|ty| ty.clone().to_chalk(db).cast(&Interner))
|
|
|
|
.collect();
|
2020-05-27 14:05:21 -05:00
|
|
|
let alias_eq_bound = rust_ir::AliasEqBound {
|
2021-03-18 20:07:15 -05:00
|
|
|
value: ty.clone().to_chalk(db),
|
2020-05-27 14:05:21 -05:00
|
|
|
trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self },
|
2021-03-18 20:07:15 -05:00
|
|
|
associated_ty_id: projection_ty.associated_ty_id,
|
2020-05-22 10:14:53 -05:00
|
|
|
parameters: Vec::new(), // FIXME we don't support generic associated types yet
|
|
|
|
};
|
2021-03-21 07:22:22 -05:00
|
|
|
Some(make_binders(rust_ir::InlineBound::AliasEqBound(alias_eq_bound), pred.num_binders))
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
2021-03-18 20:07:15 -05:00
|
|
|
_ => None,
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
}
|