2021-04-04 13:22:00 -05:00
|
|
|
//! The `TypeWalk` trait (probably to be replaced by Chalk's `Fold` and
|
|
|
|
//! `Visit`).
|
|
|
|
|
2021-04-08 07:35:15 -05:00
|
|
|
use chalk_ir::interner::HasInterner;
|
2021-04-04 13:22:00 -05:00
|
|
|
|
|
|
|
use crate::{
|
2021-04-08 07:35:15 -05:00
|
|
|
AliasEq, AliasTy, Binders, CallableSig, FnSubst, GenericArg, GenericArgData, Interner,
|
|
|
|
OpaqueTy, ProjectionTy, Substitution, TraitRef, Ty, TyKind, WhereClause,
|
2021-04-04 13:22:00 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/// This allows walking structures that contain types to do something with those
|
|
|
|
/// types, similar to Chalk's `Fold` trait.
|
|
|
|
pub trait TypeWalk {
|
|
|
|
fn walk(&self, f: &mut impl FnMut(&Ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeWalk for Ty {
|
|
|
|
fn walk(&self, f: &mut impl FnMut(&Ty)) {
|
2021-12-19 10:58:39 -06:00
|
|
|
match self.kind(Interner) {
|
2021-04-04 13:22:00 -05:00
|
|
|
TyKind::Alias(AliasTy::Projection(p_ty)) => {
|
2021-12-19 10:58:39 -06:00
|
|
|
for t in p_ty.substitution.iter(Interner) {
|
2021-04-04 13:22:00 -05:00
|
|
|
t.walk(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TyKind::Alias(AliasTy::Opaque(o_ty)) => {
|
2021-12-19 10:58:39 -06:00
|
|
|
for t in o_ty.substitution.iter(Interner) {
|
2021-04-04 13:22:00 -05:00
|
|
|
t.walk(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TyKind::Dyn(dyn_ty) => {
|
2021-04-05 10:13:50 -05:00
|
|
|
for p in dyn_ty.bounds.skip_binders().interned().iter() {
|
2021-04-04 13:22:00 -05:00
|
|
|
p.walk(f);
|
|
|
|
}
|
|
|
|
}
|
2021-04-06 04:45:41 -05:00
|
|
|
TyKind::Slice(ty)
|
|
|
|
| TyKind::Array(ty, _)
|
|
|
|
| TyKind::Ref(_, _, ty)
|
|
|
|
| TyKind::Raw(_, ty) => {
|
2021-04-04 13:22:00 -05:00
|
|
|
ty.walk(f);
|
|
|
|
}
|
2021-04-05 15:32:58 -05:00
|
|
|
TyKind::Function(fn_pointer) => {
|
|
|
|
fn_pointer.substitution.0.walk(f);
|
|
|
|
}
|
2021-04-07 10:26:01 -05:00
|
|
|
TyKind::Adt(_, substs)
|
|
|
|
| TyKind::FnDef(_, substs)
|
|
|
|
| TyKind::Tuple(_, substs)
|
|
|
|
| TyKind::OpaqueType(_, substs)
|
|
|
|
| TyKind::AssociatedType(_, substs)
|
|
|
|
| TyKind::Closure(.., substs) => {
|
|
|
|
substs.walk(f);
|
2021-04-04 13:22:00 -05:00
|
|
|
}
|
2021-04-07 10:26:01 -05:00
|
|
|
_ => {}
|
2021-04-04 13:22:00 -05:00
|
|
|
}
|
|
|
|
f(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: TypeWalk> TypeWalk for Vec<T> {
|
|
|
|
fn walk(&self, f: &mut impl FnMut(&Ty)) {
|
|
|
|
for t in self {
|
|
|
|
t.walk(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeWalk for OpaqueTy {
|
|
|
|
fn walk(&self, f: &mut impl FnMut(&Ty)) {
|
|
|
|
self.substitution.walk(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeWalk for ProjectionTy {
|
|
|
|
fn walk(&self, f: &mut impl FnMut(&Ty)) {
|
|
|
|
self.substitution.walk(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeWalk for AliasTy {
|
|
|
|
fn walk(&self, f: &mut impl FnMut(&Ty)) {
|
|
|
|
match self {
|
|
|
|
AliasTy::Projection(it) => it.walk(f),
|
|
|
|
AliasTy::Opaque(it) => it.walk(f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeWalk for GenericArg {
|
|
|
|
fn walk(&self, f: &mut impl FnMut(&Ty)) {
|
2021-09-03 09:00:50 -05:00
|
|
|
if let GenericArgData::Ty(ty) = &self.interned() {
|
|
|
|
ty.walk(f);
|
2021-04-04 13:22:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeWalk for Substitution {
|
|
|
|
fn walk(&self, f: &mut impl FnMut(&Ty)) {
|
2021-12-19 10:58:39 -06:00
|
|
|
for t in self.iter(Interner) {
|
2021-04-04 13:22:00 -05:00
|
|
|
t.walk(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 10:49:29 -05:00
|
|
|
impl<T: TypeWalk + HasInterner<Interner = Interner>> TypeWalk for Binders<T> {
|
2021-04-04 13:22:00 -05:00
|
|
|
fn walk(&self, f: &mut impl FnMut(&Ty)) {
|
2021-04-05 10:13:50 -05:00
|
|
|
self.skip_binders().walk(f);
|
2021-04-04 13:22:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeWalk for TraitRef {
|
|
|
|
fn walk(&self, f: &mut impl FnMut(&Ty)) {
|
|
|
|
self.substitution.walk(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeWalk for WhereClause {
|
|
|
|
fn walk(&self, f: &mut impl FnMut(&Ty)) {
|
|
|
|
match self {
|
|
|
|
WhereClause::Implemented(trait_ref) => trait_ref.walk(f),
|
|
|
|
WhereClause::AliasEq(alias_eq) => alias_eq.walk(f),
|
2021-04-08 06:51:04 -05:00
|
|
|
_ => {}
|
2021-04-04 13:22:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeWalk for CallableSig {
|
|
|
|
fn walk(&self, f: &mut impl FnMut(&Ty)) {
|
|
|
|
for t in self.params_and_return.iter() {
|
|
|
|
t.walk(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-04 13:27:40 -05:00
|
|
|
|
|
|
|
impl TypeWalk for AliasEq {
|
|
|
|
fn walk(&self, f: &mut impl FnMut(&Ty)) {
|
|
|
|
self.ty.walk(f);
|
|
|
|
match &self.alias {
|
|
|
|
AliasTy::Projection(projection_ty) => projection_ty.walk(f),
|
|
|
|
AliasTy::Opaque(opaque) => opaque.walk(f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-05 15:23:16 -05:00
|
|
|
|
2021-04-03 10:40:56 -05:00
|
|
|
impl TypeWalk for FnSubst<Interner> {
|
2021-04-05 15:23:16 -05:00
|
|
|
fn walk(&self, f: &mut impl FnMut(&Ty)) {
|
|
|
|
self.0.walk(f)
|
|
|
|
}
|
|
|
|
}
|