Clean up Chalk mapping a bit

This commit is contained in:
Florian Diebold 2021-04-06 20:51:21 +02:00
parent 5df65e5066
commit d280538174

View file

@ -10,10 +10,9 @@ use base_db::salsa::InternKey;
use hir_def::{GenericDefId, TypeAliasId}; use hir_def::{GenericDefId, TypeAliasId};
use crate::{ use crate::{
chalk_ext::ProjectionTyExt, db::HirDatabase, dummy_usize_const, static_lifetime, AliasTy, chalk_ext::ProjectionTyExt, db::HirDatabase, static_lifetime, AliasTy, CallableDefId,
CallableDefId, Canonical, Const, DomainGoal, FnPointer, GenericArg, InEnvironment, Lifetime, Canonical, DomainGoal, FnPointer, GenericArg, InEnvironment, OpaqueTy, ProjectionTy,
OpaqueTy, ProjectionTy, QuantifiedWhereClause, Substitution, TraitRef, Ty, TypeWalk, QuantifiedWhereClause, Substitution, TraitRef, Ty, TypeWalk, WhereClause,
WhereClause,
}; };
use super::interner::*; use super::interner::*;
@ -23,15 +22,15 @@ impl ToChalk for Ty {
type Chalk = chalk_ir::Ty<Interner>; type Chalk = chalk_ir::Ty<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> { fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> {
match self.into_inner() { match self.into_inner() {
TyKind::Ref(m, lt, ty) => ref_to_chalk(db, m, lt, ty), TyKind::Ref(m, lt, ty) => {
TyKind::Array(ty, size) => array_to_chalk(db, ty, size), chalk_ir::TyKind::Ref(m, lt, ty.to_chalk(db)).intern(&Interner)
TyKind::Function(FnPointer { sig, substitution: substs, .. }) => { }
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)); let substitution = chalk_ir::FnSubst(substs.0.to_chalk(db));
chalk_ir::TyKind::Function(chalk_ir::FnPointer { chalk_ir::TyKind::Function(chalk_ir::FnPointer { num_binders, sig, substitution })
num_binders: 0,
sig,
substitution,
})
.intern(&Interner) .intern(&Interner)
} }
TyKind::AssociatedType(assoc_type_id, substs) => { TyKind::AssociatedType(assoc_type_id, substs) => {
@ -74,21 +73,12 @@ impl ToChalk for Ty {
chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner) chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner)
} }
TyKind::Alias(AliasTy::Projection(proj_ty)) => { TyKind::Alias(AliasTy::Projection(proj_ty)) => {
let associated_ty_id = proj_ty.associated_ty_id; chalk_ir::AliasTy::Projection(proj_ty.to_chalk(db))
let substitution = proj_ty.substitution.to_chalk(db);
chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy {
associated_ty_id,
substitution,
})
.cast(&Interner) .cast(&Interner)
.intern(&Interner) .intern(&Interner)
} }
TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { TyKind::Alias(AliasTy::Opaque(opaque_ty)) => {
let opaque_ty_id = opaque_ty.opaque_ty_id; chalk_ir::AliasTy::Opaque(opaque_ty.to_chalk(db)).cast(&Interner).intern(&Interner)
let substitution = opaque_ty.substitution.to_chalk(db);
chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy { opaque_ty_id, substitution })
.cast(&Interner)
.intern(&Interner)
} }
TyKind::Placeholder(idx) => idx.to_ty::<Interner>(&Interner), TyKind::Placeholder(idx) => idx.to_ty::<Interner>(&Interner),
TyKind::BoundVar(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner), TyKind::BoundVar(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner),
@ -101,7 +91,7 @@ impl ToChalk for Ty {
); );
let bounded_ty = chalk_ir::DynTy { let bounded_ty = chalk_ir::DynTy {
bounds: chalk_ir::Binders::new(binders, where_clauses), bounds: chalk_ir::Binders::new(binders, where_clauses),
lifetime: static_lifetime(), lifetime: dyn_ty.lifetime,
}; };
chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner) chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner)
} }
@ -114,17 +104,10 @@ impl ToChalk for Ty {
chalk_ir::TyKind::Array(ty, size) => TyKind::Array(from_chalk(db, ty), size), chalk_ir::TyKind::Array(ty, size) => TyKind::Array(from_chalk(db, ty), size),
chalk_ir::TyKind::Placeholder(idx) => TyKind::Placeholder(idx), chalk_ir::TyKind::Placeholder(idx) => TyKind::Placeholder(idx),
chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => { chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => {
let associated_ty = proj.associated_ty_id; TyKind::Alias(AliasTy::Projection(from_chalk(db, proj)))
let parameters = from_chalk(db, proj.substitution);
TyKind::Alias(AliasTy::Projection(ProjectionTy {
associated_ty_id: associated_ty,
substitution: parameters,
}))
} }
chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(opaque_ty)) => { chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(opaque_ty)) => {
let opaque_ty_id = opaque_ty.opaque_ty_id; TyKind::Alias(AliasTy::Opaque(from_chalk(db, opaque_ty)))
let parameters = from_chalk(db, opaque_ty.substitution);
TyKind::Alias(AliasTy::Opaque(OpaqueTy { opaque_ty_id, substitution: parameters }))
} }
chalk_ir::TyKind::Function(chalk_ir::FnPointer { chalk_ir::TyKind::Function(chalk_ir::FnPointer {
num_binders, num_binders,
@ -138,18 +121,19 @@ impl ToChalk for Ty {
} }
chalk_ir::TyKind::BoundVar(idx) => TyKind::BoundVar(idx), chalk_ir::TyKind::BoundVar(idx) => TyKind::BoundVar(idx),
chalk_ir::TyKind::InferenceVar(_iv, _kind) => TyKind::Error, chalk_ir::TyKind::InferenceVar(_iv, _kind) => TyKind::Error,
chalk_ir::TyKind::Dyn(where_clauses) => { chalk_ir::TyKind::Dyn(dyn_ty) => {
assert_eq!(where_clauses.bounds.binders.len(&Interner), 1); assert_eq!(dyn_ty.bounds.binders.len(&Interner), 1);
let bounds = where_clauses let (bounds, binders) = dyn_ty.bounds.into_value_and_skipped_binders();
.bounds let where_clauses = crate::QuantifiedWhereClauses::from_iter(
.skip_binders() &Interner,
.iter(&Interner) bounds.interned().iter().cloned().map(|p| from_chalk(db, p)),
.map(|c| from_chalk(db, c.clone())); );
TyKind::Dyn(crate::DynTy { TyKind::Dyn(crate::DynTy {
bounds: crate::Binders::new( bounds: crate::Binders::new(binders, where_clauses),
where_clauses.bounds.binders.clone(), // HACK: we sometimes get lifetime variables back in solutions
crate::QuantifiedWhereClauses::from_iter(&Interner, bounds), // 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(), lifetime: static_lifetime(),
}) })
} }
@ -169,8 +153,12 @@ impl ToChalk for Ty {
} }
chalk_ir::TyKind::Raw(mutability, ty) => TyKind::Raw(mutability, from_chalk(db, ty)), chalk_ir::TyKind::Raw(mutability, ty) => TyKind::Raw(mutability, from_chalk(db, ty)),
chalk_ir::TyKind::Slice(ty) => TyKind::Slice(from_chalk(db, ty)), chalk_ir::TyKind::Slice(ty) => TyKind::Slice(from_chalk(db, ty)),
chalk_ir::TyKind::Ref(mutability, lifetime, ty) => { chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => {
TyKind::Ref(mutability, lifetime, from_chalk(db, 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::Str => TyKind::Str,
chalk_ir::TyKind::Never => TyKind::Never, chalk_ir::TyKind::Never => TyKind::Never,
@ -189,26 +177,6 @@ impl ToChalk for Ty {
} }
} }
/// 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,
mutability: chalk_ir::Mutability,
_lifetime: Lifetime,
ty: Ty,
) -> chalk_ir::Ty<Interner> {
let arg = ty.to_chalk(db);
let lifetime = static_lifetime();
chalk_ir::TyKind::Ref(mutability, lifetime, arg).intern(&Interner)
}
/// 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.
fn array_to_chalk(db: &dyn HirDatabase, ty: Ty, _: Const) -> chalk_ir::Ty<Interner> {
let arg = ty.to_chalk(db);
chalk_ir::TyKind::Array(arg, dummy_usize_const()).intern(&Interner)
}
impl ToChalk for GenericArg { impl ToChalk for GenericArg {
type Chalk = chalk_ir::GenericArg<Interner>; type Chalk = chalk_ir::GenericArg<Interner>;