Skip to content

Commit

Permalink
Unify {visit,walk}_assoc_item
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcabrajac committed Oct 15, 2024
1 parent b31b31b commit 0e75ea0
Showing 1 changed file with 80 additions and 153 deletions.
233 changes: 80 additions & 153 deletions compiler/rustc_ast/src/visitors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ macro_rules! make_ast_visitor {
/// or `ControlFlow<T>`.
type Result: VisitorResult = ();

make_visit!{AssocItem, ctxt: AssocCtxt; visit_assoc_item, walk_assoc_item}
make_visit!{Stmt; visit_stmt, walk_stmt}

/// This method is a hack to workaround unstable of `stmt_expr_attributes`.
Expand Down Expand Up @@ -372,9 +371,10 @@ macro_rules! make_ast_visitor {
make_visit!{P!(Pat); visit_pat, walk_pat}
make_visit!{P!(Ty); visit_ty, walk_ty}

// Default implementations are generic over WalkItemKind
make_visit!{ForeignItem; visit_foreign_item, walk_item}
// Item variants
make_visit!{Item; visit_item, walk_item}
make_visit!{AssocItem, ctxt: AssocCtxt; visit_assoc_item, walk_assoc_item}
make_visit!{ForeignItem; visit_foreign_item, walk_item}

fn visit_fn(&mut self, fn_kind: FnKind!(), _span: Span, _id: NodeId) -> result!() {
walk_fn(self, fn_kind)
Expand Down Expand Up @@ -1496,6 +1496,81 @@ macro_rules! make_ast_visitor {
return_result!(V)
}

pub fn walk_assoc_item<$($lt,)? V: $trait$(<$lt>)?>(
visitor: &mut V,
item: ref_t!(Item<AssocItemKind>),
ctxt: AssocCtxt
) -> result!(V) {
let Item { attrs, id, span, vis, ident, kind, tokens } = item;
try_v!(visit_id!(visitor, id));
visit_list!(visitor, visit_attribute, attrs);
try_v!(visitor.visit_vis(vis));
try_v!(visitor.visit_ident(ident));
match kind {
AssocItemKind::Const(box ConstItem { defaultness, generics, ty, expr }) => {
try_v!(visitor.visit_defaultness(defaultness));
try_v!(visitor.visit_generics(generics));
try_v!(visitor.visit_ty(ty));
visit_o!(expr, |expr| visitor.visit_expr(expr));
}
AssocItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
try_v!(visitor.visit_defaultness(defaultness));
let kind =
FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, generics, body);
try_v!(visitor.visit_fn(kind, *span, *id));
}
AssocItemKind::Type(box TyAlias {
defaultness,
generics,
where_clauses,
bounds,
ty,
}) => {
try_v!(visitor.visit_defaultness(defaultness));
try_v!(visitor.visit_generics(generics));
visit_list!(visitor, visit_param_bound, bounds; BoundKind::Bound);
visit_o!(ty, |ty| visitor.visit_ty(ty));
try_v!(visitor.visit_ty_alias_where_clauses(where_clauses));
}
AssocItemKind::MacCall(mac) => {
try_v!(visitor.visit_mac_call(mac));
}
AssocItemKind::Delegation(box Delegation {
id,
qself,
path,
rename,
body,
from_glob: _,
}) => {
try_v!(visit_id!(visitor, id));
try_v!(visitor.visit_qself(qself));
try_v!(visitor.visit_path(path, *id));
visit_o!(rename, |rename| visitor.visit_ident(rename));
visit_o!(body, |body| visitor.visit_block(body));
}
AssocItemKind::DelegationMac(box DelegationMac {
qself,
prefix,
suffixes,
body,
}) => {
try_v!(visitor.visit_qself(qself));
try_v!(visitor.visit_path(prefix, *id));
if let Some(suffixes) = suffixes {
for (ident, rename) in suffixes {
try_v!(visitor.visit_ident(ident));
visit_o!(rename, |rename| visitor.visit_ident(rename));
}
}
visit_o!(body, |body| visitor.visit_block(body));
}
}
visit_lazy_tts!(visitor, tokens);
try_v!(visit_span!(visitor, span));
return_result!(V)
}

pub fn walk_fn<$($lt,)? V: $trait$(<$lt>)?>(
visitor: &mut V,
kind: FnKind!()
Expand Down Expand Up @@ -1626,73 +1701,6 @@ pub mod visit {

make_ast_visitor!(Visitor<'ast>);

pub fn walk_assoc_item<'a, V: Visitor<'a>>(
visitor: &mut V,
item: &'a AssocItem,
ctxt: AssocCtxt,
) -> V::Result {
let Item { id, span, ident, vis, attrs, kind, tokens: _ } = item;
walk_list!(visitor, visit_attribute, attrs);
try_visit!(visitor.visit_vis(vis));
try_visit!(visitor.visit_ident(ident));
match kind {
AssocItemKind::Const(box ConstItem { defaultness, generics, ty, expr }) => {
try_visit!(visitor.visit_defaultness(defaultness));
try_visit!(visitor.visit_generics(generics));
try_visit!(visitor.visit_ty(ty));
visit_opt!(visitor, visit_expr, expr);
}
AssocItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
try_visit!(visitor.visit_defaultness(defaultness));
let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, generics, body);
try_visit!(visitor.visit_fn(kind, *span, *id));
}
AssocItemKind::Type(box TyAlias {
generics,
bounds,
ty,
defaultness,
where_clauses,
}) => {
try_visit!(visitor.visit_defaultness(defaultness));
try_visit!(visitor.visit_generics(generics));
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
visit_opt!(visitor, visit_ty, ty);
try_visit!(visitor.visit_ty_alias_where_clauses(where_clauses));
}
AssocItemKind::MacCall(mac) => {
try_visit!(visitor.visit_mac_call(mac));
}
AssocItemKind::Delegation(box Delegation {
id,
qself,
path,
rename,
body,
from_glob: _,
}) => {
try_visit!(visitor.visit_qself(qself));
try_visit!(visitor.visit_path(path, *id));
visit_opt!(visitor, visit_ident, rename);
visit_opt!(visitor, visit_block, body);
}
AssocItemKind::DelegationMac(box DelegationMac { qself, prefix, suffixes, body }) => {
try_visit!(visitor.visit_qself(qself));
try_visit!(visitor.visit_path(prefix, *id));
if let Some(suffixes) = suffixes {
for (ident, rename) in suffixes {
visitor.visit_ident(ident);
if let Some(rename) = rename {
visitor.visit_ident(rename);
}
}
}
visit_opt!(visitor, visit_block, body);
}
}
V::Result::output()
}

pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) -> V::Result {
let Stmt { id: _, kind, span: _ } = statement;
match kind {
Expand Down Expand Up @@ -1960,11 +1968,6 @@ pub mod mut_visit {
exprs.flat_map_in_place(|expr| vis.filter_map_expr(expr))
}

// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
fn visit_bounds<T: MutVisitor>(vis: &mut T, bounds: &mut GenericBounds, ctxt: BoundKind) {
visit_vec(bounds, |bound| vis.visit_param_bound(bound, ctxt));
}

// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
fn visit_delim_args<T: MutVisitor>(vis: &mut T, args: &mut DelimArgs) {
let DelimArgs { dspan, delim: _, tokens } = args;
Expand Down Expand Up @@ -2197,16 +2200,6 @@ pub mod mut_visit {
item.kind.walk(item.id, item.span, &mut item.vis, &mut item.ident, vis)
}

fn visit_const_item<T: MutVisitor>(
ConstItem { defaultness, generics, ty, expr }: &mut ConstItem,
visitor: &mut T,
) {
visitor.visit_defaultness(defaultness);
visitor.visit_generics(generics);
visitor.visit_ty(ty);
visit_opt(expr, |expr| visitor.visit_expr(expr));
}

/// Mutates one item, returning the item again.
pub fn walk_flat_map_item(
vis: &mut impl MutVisitor,
Expand All @@ -2226,77 +2219,11 @@ pub mod mut_visit {

/// Mutates one item, returning the item again.
pub fn walk_flat_map_assoc_item(
visitor: &mut impl MutVisitor,
vis: &mut impl MutVisitor,
mut item: P<AssocItem>,
ctxt: AssocCtxt,
) -> SmallVec<[P<AssocItem>; 1]> {
let Item { ident, attrs, id, kind, vis, span, tokens } = item.deref_mut();
visitor.visit_id(id);
visit_attrs(visitor, attrs);
visitor.visit_vis(vis);
visitor.visit_ident(ident);
match kind {
AssocItemKind::Const(item) => {
visit_const_item(item, visitor);
}
AssocItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
visitor.visit_defaultness(defaultness);
visitor.visit_fn(
FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, generics, body),
*span,
*id,
);
}
AssocItemKind::Type(box TyAlias {
defaultness,
generics,
where_clauses,
bounds,
ty,
}) => {
visitor.visit_defaultness(defaultness);
visitor.visit_generics(generics);
visit_bounds(visitor, bounds, BoundKind::Bound);
visit_opt(ty, |ty| visitor.visit_ty(ty));
visitor.visit_ty_alias_where_clauses(where_clauses);
}
AssocItemKind::MacCall(mac) => visitor.visit_mac_call(mac),
AssocItemKind::Delegation(box Delegation {
id,
qself,
path,
rename,
body,
from_glob: _,
}) => {
visitor.visit_id(id);
visitor.visit_qself(qself);
visitor.visit_path(path, *id);
if let Some(rename) = rename {
visitor.visit_ident(rename);
}
if let Some(body) = body {
visitor.visit_block(body);
}
}
AssocItemKind::DelegationMac(box DelegationMac { qself, prefix, suffixes, body }) => {
visitor.visit_qself(qself);
visitor.visit_path(prefix, *id);
if let Some(suffixes) = suffixes {
for (ident, rename) in suffixes {
visitor.visit_ident(ident);
if let Some(rename) = rename {
visitor.visit_ident(rename);
}
}
}
if let Some(body) = body {
visitor.visit_block(body);
}
}
}
visit_lazy_tts(visitor, tokens);
visitor.visit_span(span);
vis.visit_assoc_item(&mut item, ctxt);
smallvec![item]
}

Expand Down

0 comments on commit 0e75ea0

Please sign in to comment.