1 //! The `TypeWalk` trait (probably to be replaced by Chalk's `Fold` and
4 use chalk_ir::interner::HasInterner;
7 AliasEq, AliasTy, Binders, CallableSig, FnSubst, GenericArg, GenericArgData, Interner,
8 OpaqueTy, ProjectionTy, Substitution, TraitRef, Ty, TyKind, WhereClause,
11 /// This allows walking structures that contain types to do something with those
12 /// types, similar to Chalk's `Fold` trait.
14 fn walk(&self, f: &mut impl FnMut(&Ty));
17 impl TypeWalk for Ty {
18 fn walk(&self, f: &mut impl FnMut(&Ty)) {
19 match self.kind(Interner) {
20 TyKind::Alias(AliasTy::Projection(p_ty)) => {
21 for t in p_ty.substitution.iter(Interner) {
25 TyKind::Alias(AliasTy::Opaque(o_ty)) => {
26 for t in o_ty.substitution.iter(Interner) {
30 TyKind::Dyn(dyn_ty) => {
31 for p in dyn_ty.bounds.skip_binders().interned().iter() {
36 | TyKind::Array(ty, _)
37 | TyKind::Ref(_, _, ty)
38 | TyKind::Raw(_, ty) => {
41 TyKind::Function(fn_pointer) => {
42 fn_pointer.substitution.0.walk(f);
44 TyKind::Adt(_, substs)
45 | TyKind::FnDef(_, substs)
46 | TyKind::Tuple(_, substs)
47 | TyKind::OpaqueType(_, substs)
48 | TyKind::AssociatedType(_, substs)
49 | TyKind::Closure(.., substs) => {
58 impl<T: TypeWalk> TypeWalk for Vec<T> {
59 fn walk(&self, f: &mut impl FnMut(&Ty)) {
66 impl TypeWalk for OpaqueTy {
67 fn walk(&self, f: &mut impl FnMut(&Ty)) {
68 self.substitution.walk(f);
72 impl TypeWalk for ProjectionTy {
73 fn walk(&self, f: &mut impl FnMut(&Ty)) {
74 self.substitution.walk(f);
78 impl TypeWalk for AliasTy {
79 fn walk(&self, f: &mut impl FnMut(&Ty)) {
81 AliasTy::Projection(it) => it.walk(f),
82 AliasTy::Opaque(it) => it.walk(f),
87 impl TypeWalk for GenericArg {
88 fn walk(&self, f: &mut impl FnMut(&Ty)) {
89 if let GenericArgData::Ty(ty) = &self.interned() {
95 impl TypeWalk for Substitution {
96 fn walk(&self, f: &mut impl FnMut(&Ty)) {
97 for t in self.iter(Interner) {
103 impl<T: TypeWalk + HasInterner<Interner = Interner>> TypeWalk for Binders<T> {
104 fn walk(&self, f: &mut impl FnMut(&Ty)) {
105 self.skip_binders().walk(f);
109 impl TypeWalk for TraitRef {
110 fn walk(&self, f: &mut impl FnMut(&Ty)) {
111 self.substitution.walk(f);
115 impl TypeWalk for WhereClause {
116 fn walk(&self, f: &mut impl FnMut(&Ty)) {
118 WhereClause::Implemented(trait_ref) => trait_ref.walk(f),
119 WhereClause::AliasEq(alias_eq) => alias_eq.walk(f),
125 impl TypeWalk for CallableSig {
126 fn walk(&self, f: &mut impl FnMut(&Ty)) {
127 for t in self.params_and_return.iter() {
133 impl TypeWalk for AliasEq {
134 fn walk(&self, f: &mut impl FnMut(&Ty)) {
137 AliasTy::Projection(projection_ty) => projection_ty.walk(f),
138 AliasTy::Opaque(opaque) => opaque.walk(f),
143 impl TypeWalk for FnSubst<Interner> {
144 fn walk(&self, f: &mut impl FnMut(&Ty)) {