1
Fork 0

rename to BoundKind and add comments

This commit is contained in:
SparrowLii 2022-04-21 10:09:32 +08:00
parent 4375b36117
commit 38bd4fbdbd
5 changed files with 41 additions and 29 deletions

View file

@ -33,9 +33,21 @@ pub enum FnCtxt {
}
#[derive(Copy, Clone, Debug)]
pub enum BoundCtxt {
Normal,
pub enum BoundKind {
/// Trait bounds in generics bounds and type/trait alias.
/// E.g., `<T: Bound>`, `type A: Bound`, or `where T: Bound`.
Bound,
/// Trait bounds in `impl` type.
/// E.g., `type Foo = impl Bound1 + Bound2 + Bound3`.
Impl,
/// Trait bounds in trait object type.
/// E.g., `dyn Bound1 + Bound2 + Bound3`.
TraitObject,
/// Super traits of a trait.
/// E.g., `trait A: B`
SuperTraits,
}
@ -146,7 +158,7 @@ pub trait Visitor<'ast>: Sized {
fn visit_trait_ref(&mut self, t: &'ast TraitRef) {
walk_trait_ref(self, t)
}
fn visit_param_bound(&mut self, bounds: &'ast GenericBound, _ctxt: BoundCtxt) {
fn visit_param_bound(&mut self, bounds: &'ast GenericBound, _ctxt: BoundKind) {
walk_param_bound(self, bounds)
}
fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
@ -318,7 +330,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
ItemKind::GlobalAsm(ref asm) => walk_inline_asm(visitor, asm),
ItemKind::TyAlias(box TyAlias { ref generics, ref bounds, ref ty, .. }) => {
visitor.visit_generics(generics);
walk_list!(visitor, visit_param_bound, bounds, BoundCtxt::Normal);
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
walk_list!(visitor, visit_ty, ty);
}
ItemKind::Enum(ref enum_definition, ref generics) => {
@ -353,12 +365,12 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
ref items,
}) => {
visitor.visit_generics(generics);
walk_list!(visitor, visit_param_bound, bounds, BoundCtxt::SuperTraits);
walk_list!(visitor, visit_param_bound, bounds, BoundKind::SuperTraits);
walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);
}
ItemKind::TraitAlias(ref generics, ref bounds) => {
visitor.visit_generics(generics);
walk_list!(visitor, visit_param_bound, bounds, BoundCtxt::Normal);
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
}
ItemKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
ItemKind::MacroDef(ref ts) => visitor.visit_mac_def(ts, item.id),
@ -424,10 +436,10 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
visitor.visit_anon_const(length)
}
TyKind::TraitObject(ref bounds, ..) => {
walk_list!(visitor, visit_param_bound, bounds, BoundCtxt::TraitObject);
walk_list!(visitor, visit_param_bound, bounds, BoundKind::TraitObject);
}
TyKind::ImplTrait(_, ref bounds) => {
walk_list!(visitor, visit_param_bound, bounds, BoundCtxt::Normal);
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Impl);
}
TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
@ -513,7 +525,7 @@ pub fn walk_assoc_constraint<'a, V: Visitor<'a>>(visitor: &mut V, constraint: &'
Term::Const(c) => visitor.visit_anon_const(c),
},
AssocConstraintKind::Bound { ref bounds } => {
walk_list!(visitor, visit_param_bound, bounds, BoundCtxt::Normal);
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
}
}
}
@ -576,7 +588,7 @@ pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignI
}
ForeignItemKind::TyAlias(box TyAlias { generics, bounds, ty, .. }) => {
visitor.visit_generics(generics);
walk_list!(visitor, visit_param_bound, bounds, BoundCtxt::Normal);
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
walk_list!(visitor, visit_ty, ty);
}
ForeignItemKind::MacCall(mac) => {
@ -595,7 +607,7 @@ pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericB
pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a GenericParam) {
visitor.visit_ident(param.ident);
walk_list!(visitor, visit_attribute, param.attrs.iter());
walk_list!(visitor, visit_param_bound, &param.bounds, BoundCtxt::Normal);
walk_list!(visitor, visit_param_bound, &param.bounds, BoundKind::Bound);
match param.kind {
GenericParamKind::Lifetime => (),
GenericParamKind::Type { ref default } => walk_list!(visitor, visit_ty, default),
@ -622,14 +634,14 @@ pub fn walk_where_predicate<'a, V: Visitor<'a>>(visitor: &mut V, predicate: &'a
..
}) => {
visitor.visit_ty(bounded_ty);
walk_list!(visitor, visit_param_bound, bounds, BoundCtxt::Normal);
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
walk_list!(visitor, visit_generic_param, bound_generic_params);
}
WherePredicate::RegionPredicate(WhereRegionPredicate {
ref lifetime, ref bounds, ..
}) => {
visitor.visit_lifetime(lifetime);
walk_list!(visitor, visit_param_bound, bounds, BoundCtxt::Normal);
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
}
WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty, .. }) => {
visitor.visit_ty(lhs_ty);
@ -682,7 +694,7 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem,
}
AssocItemKind::TyAlias(box TyAlias { generics, bounds, ty, .. }) => {
visitor.visit_generics(generics);
walk_list!(visitor, visit_param_bound, bounds, BoundCtxt::Normal);
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
walk_list!(visitor, visit_ty, ty);
}
AssocItemKind::MacCall(mac) => {