1 //! AST walker. Each overridden visit method has full control over what
2 //! happens with its node, it can do its own traversal of the node's children,
3 //! call `visit::walk_*` to apply the default traversal algorithm, or prevent
4 //! deeper traversal by doing nothing.
6 //! Note: it is an important invariant that the default visitor walks the body
7 //! of a function in "execution order" (more concretely, reverse post-order
8 //! with respect to the CFG implied by the AST), meaning that if AST node A may
9 //! execute before AST node B, then A is visited first. The borrow checker in
10 //! particular relies on this property.
12 //! Note: walking an AST before macro expansion is probably a bad idea. For
13 //! instance, a walker looking for item names in a module will miss all of
14 //! those that are created by the expansion of a macro.
18 use rustc_span::symbol::Ident;
21 #[derive(Copy, Clone, Debug, PartialEq)]
27 #[derive(Copy, Clone, Debug, PartialEq)]
34 #[derive(Copy, Clone, Debug)]
36 /// Trait bounds in generics bounds and type/trait alias.
37 /// E.g., `<T: Bound>`, `type A: Bound`, or `where T: Bound`.
40 /// Trait bounds in `impl` type.
41 /// E.g., `type Foo = impl Bound1 + Bound2 + Bound3`.
44 /// Trait bounds in trait object type.
45 /// E.g., `dyn Bound1 + Bound2 + Bound3`.
48 /// Super traits of a trait.
49 /// E.g., `trait A: B`
53 #[derive(Copy, Clone, Debug)]
55 /// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
56 Fn(FnCtxt, Ident, &'a FnSig, &'a Visibility, &'a Generics, Option<&'a Block>),
58 /// E.g., `|x, y| body`.
59 Closure(&'a ClosureBinder, &'a FnDecl, &'a Expr),
63 pub fn header(&self) -> Option<&'a FnHeader> {
65 FnKind::Fn(_, _, sig, _, _, _) => Some(&sig.header),
66 FnKind::Closure(_, _, _) => None,
70 pub fn ident(&self) -> Option<&Ident> {
72 FnKind::Fn(_, ident, ..) => Some(ident),
77 pub fn decl(&self) -> &'a FnDecl {
79 FnKind::Fn(_, _, sig, _, _, _) => &sig.decl,
80 FnKind::Closure(_, decl, _) => decl,
84 pub fn ctxt(&self) -> Option<FnCtxt> {
86 FnKind::Fn(ctxt, ..) => Some(*ctxt),
87 FnKind::Closure(..) => None,
92 #[derive(Copy, Clone, Debug)]
93 pub enum LifetimeCtxt {
94 /// Appears in a reference type.
96 /// Appears as a bound on a type or another lifetime.
98 /// Appears as a generic argument.
102 /// Each method of the `Visitor` trait is a hook to be potentially
103 /// overridden. Each method's default implementation recursively visits
104 /// the substructure of the input via the corresponding `walk` method;
105 /// e.g., the `visit_item` method by default calls `visit::walk_item`.
107 /// If you want to ensure that your code handles every variant
108 /// explicitly, you need to override each method. (And you also need
109 /// to monitor future changes to `Visitor` in case a new method with a
110 /// new default implementation gets introduced.)
111 pub trait Visitor<'ast>: Sized {
112 fn visit_ident(&mut self, _ident: Ident) {}
113 fn visit_foreign_item(&mut self, i: &'ast ForeignItem) {
114 walk_foreign_item(self, i)
116 fn visit_item(&mut self, i: &'ast Item) {
119 fn visit_local(&mut self, l: &'ast Local) {
122 fn visit_block(&mut self, b: &'ast Block) {
125 fn visit_stmt(&mut self, s: &'ast Stmt) {
128 fn visit_param(&mut self, param: &'ast Param) {
129 walk_param(self, param)
131 fn visit_arm(&mut self, a: &'ast Arm) {
134 fn visit_pat(&mut self, p: &'ast Pat) {
137 fn visit_anon_const(&mut self, c: &'ast AnonConst) {
138 walk_anon_const(self, c)
140 fn visit_expr(&mut self, ex: &'ast Expr) {
143 /// This method is a hack to workaround unstable of `stmt_expr_attributes`.
144 /// It can be removed once that feature is stabilized.
145 fn visit_method_receiver_expr(&mut self, ex: &'ast Expr) {
148 fn visit_expr_post(&mut self, _ex: &'ast Expr) {}
149 fn visit_ty(&mut self, t: &'ast Ty) {
152 fn visit_generic_param(&mut self, param: &'ast GenericParam) {
153 walk_generic_param(self, param)
155 fn visit_generics(&mut self, g: &'ast Generics) {
156 walk_generics(self, g)
158 fn visit_closure_binder(&mut self, b: &'ast ClosureBinder) {
159 walk_closure_binder(self, b)
161 fn visit_where_predicate(&mut self, p: &'ast WherePredicate) {
162 walk_where_predicate(self, p)
164 fn visit_fn(&mut self, fk: FnKind<'ast>, _: Span, _: NodeId) {
167 fn visit_assoc_item(&mut self, i: &'ast AssocItem, ctxt: AssocCtxt) {
168 walk_assoc_item(self, i, ctxt)
170 fn visit_trait_ref(&mut self, t: &'ast TraitRef) {
171 walk_trait_ref(self, t)
173 fn visit_param_bound(&mut self, bounds: &'ast GenericBound, _ctxt: BoundKind) {
174 walk_param_bound(self, bounds)
176 fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef) {
177 walk_poly_trait_ref(self, t)
179 fn visit_variant_data(&mut self, s: &'ast VariantData) {
180 walk_struct_def(self, s)
182 fn visit_field_def(&mut self, s: &'ast FieldDef) {
183 walk_field_def(self, s)
185 fn visit_enum_def(&mut self, enum_definition: &'ast EnumDef) {
186 walk_enum_def(self, enum_definition)
188 fn visit_variant(&mut self, v: &'ast Variant) {
189 walk_variant(self, v)
191 fn visit_label(&mut self, label: &'ast Label) {
192 walk_label(self, label)
194 fn visit_lifetime(&mut self, lifetime: &'ast Lifetime, _: LifetimeCtxt) {
195 walk_lifetime(self, lifetime)
197 fn visit_mac_call(&mut self, mac: &'ast MacCall) {
200 fn visit_mac_def(&mut self, _mac: &'ast MacroDef, _id: NodeId) {
203 fn visit_path(&mut self, path: &'ast Path, _id: NodeId) {
204 walk_path(self, path)
206 fn visit_use_tree(&mut self, use_tree: &'ast UseTree, id: NodeId, _nested: bool) {
207 walk_use_tree(self, use_tree, id)
209 fn visit_path_segment(&mut self, path_segment: &'ast PathSegment) {
210 walk_path_segment(self, path_segment)
212 fn visit_generic_args(&mut self, generic_args: &'ast GenericArgs) {
213 walk_generic_args(self, generic_args)
215 fn visit_generic_arg(&mut self, generic_arg: &'ast GenericArg) {
216 walk_generic_arg(self, generic_arg)
218 fn visit_assoc_constraint(&mut self, constraint: &'ast AssocConstraint) {
219 walk_assoc_constraint(self, constraint)
221 fn visit_attribute(&mut self, attr: &'ast Attribute) {
222 walk_attribute(self, attr)
224 fn visit_vis(&mut self, vis: &'ast Visibility) {
227 fn visit_fn_ret_ty(&mut self, ret_ty: &'ast FnRetTy) {
228 walk_fn_ret_ty(self, ret_ty)
230 fn visit_fn_header(&mut self, _header: &'ast FnHeader) {
233 fn visit_expr_field(&mut self, f: &'ast ExprField) {
234 walk_expr_field(self, f)
236 fn visit_pat_field(&mut self, fp: &'ast PatField) {
237 walk_pat_field(self, fp)
239 fn visit_crate(&mut self, krate: &'ast Crate) {
240 walk_crate(self, krate)
242 fn visit_inline_asm(&mut self, asm: &'ast InlineAsm) {
243 walk_inline_asm(self, asm)
245 fn visit_format_args(&mut self, fmt: &'ast FormatArgs) {
246 walk_format_args(self, fmt)
248 fn visit_inline_asm_sym(&mut self, sym: &'ast InlineAsmSym) {
249 walk_inline_asm_sym(self, sym)
254 macro_rules! walk_list {
255 ($visitor: expr, $method: ident, $list: expr $(, $($extra_args: expr),* )?) => {
257 #[allow(for_loops_over_fallibles)]
259 $visitor.$method(elem $(, $($extra_args,)* )?)
265 pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) {
266 walk_list!(visitor, visit_item, &krate.items);
267 walk_list!(visitor, visit_attribute, &krate.attrs);
270 pub fn walk_local<'a, V: Visitor<'a>>(visitor: &mut V, local: &'a Local) {
271 for attr in local.attrs.iter() {
272 visitor.visit_attribute(attr);
274 visitor.visit_pat(&local.pat);
275 walk_list!(visitor, visit_ty, &local.ty);
276 if let Some((init, els)) = local.kind.init_else_opt() {
277 visitor.visit_expr(init);
278 walk_list!(visitor, visit_block, els);
282 pub fn walk_label<'a, V: Visitor<'a>>(visitor: &mut V, label: &'a Label) {
283 visitor.visit_ident(label.ident);
286 pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime) {
287 visitor.visit_ident(lifetime.ident);
290 pub fn walk_poly_trait_ref<'a, V>(visitor: &mut V, trait_ref: &'a PolyTraitRef)
294 walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
295 visitor.visit_trait_ref(&trait_ref.trait_ref);
298 pub fn walk_trait_ref<'a, V: Visitor<'a>>(visitor: &mut V, trait_ref: &'a TraitRef) {
299 visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
302 pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
303 visitor.visit_vis(&item.vis);
304 visitor.visit_ident(item.ident);
306 ItemKind::ExternCrate(_) => {}
307 ItemKind::Use(use_tree) => visitor.visit_use_tree(use_tree, item.id, false),
308 ItemKind::Static(typ, _, expr) | ItemKind::Const(_, typ, expr) => {
309 visitor.visit_ty(typ);
310 walk_list!(visitor, visit_expr, expr);
312 ItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => {
314 FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, generics, body.as_deref());
315 visitor.visit_fn(kind, item.span, item.id)
317 ItemKind::Mod(_unsafety, mod_kind) => match mod_kind {
318 ModKind::Loaded(items, _inline, _inner_span) => {
319 walk_list!(visitor, visit_item, items)
321 ModKind::Unloaded => {}
323 ItemKind::ForeignMod(foreign_module) => {
324 walk_list!(visitor, visit_foreign_item, &foreign_module.items);
326 ItemKind::GlobalAsm(asm) => visitor.visit_inline_asm(asm),
327 ItemKind::TyAlias(box TyAlias { generics, bounds, ty, .. }) => {
328 visitor.visit_generics(generics);
329 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
330 walk_list!(visitor, visit_ty, ty);
332 ItemKind::Enum(enum_definition, generics) => {
333 visitor.visit_generics(generics);
334 visitor.visit_enum_def(enum_definition)
336 ItemKind::Impl(box Impl {
346 visitor.visit_generics(generics);
347 walk_list!(visitor, visit_trait_ref, of_trait);
348 visitor.visit_ty(self_ty);
349 walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Impl);
351 ItemKind::Struct(struct_definition, generics)
352 | ItemKind::Union(struct_definition, generics) => {
353 visitor.visit_generics(generics);
354 visitor.visit_variant_data(struct_definition);
356 ItemKind::Trait(box Trait { unsafety: _, is_auto: _, generics, bounds, items }) => {
357 visitor.visit_generics(generics);
358 walk_list!(visitor, visit_param_bound, bounds, BoundKind::SuperTraits);
359 walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);
361 ItemKind::TraitAlias(generics, bounds) => {
362 visitor.visit_generics(generics);
363 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
365 ItemKind::MacCall(mac) => visitor.visit_mac_call(mac),
366 ItemKind::MacroDef(ts) => visitor.visit_mac_def(ts, item.id),
368 walk_list!(visitor, visit_attribute, &item.attrs);
371 pub fn walk_enum_def<'a, V: Visitor<'a>>(visitor: &mut V, enum_definition: &'a EnumDef) {
372 walk_list!(visitor, visit_variant, &enum_definition.variants);
375 pub fn walk_variant<'a, V: Visitor<'a>>(visitor: &mut V, variant: &'a Variant)
379 visitor.visit_ident(variant.ident);
380 visitor.visit_vis(&variant.vis);
381 visitor.visit_variant_data(&variant.data);
382 walk_list!(visitor, visit_anon_const, &variant.disr_expr);
383 walk_list!(visitor, visit_attribute, &variant.attrs);
386 pub fn walk_expr_field<'a, V: Visitor<'a>>(visitor: &mut V, f: &'a ExprField) {
387 visitor.visit_expr(&f.expr);
388 visitor.visit_ident(f.ident);
389 walk_list!(visitor, visit_attribute, f.attrs.iter());
392 pub fn walk_pat_field<'a, V: Visitor<'a>>(visitor: &mut V, fp: &'a PatField) {
393 visitor.visit_ident(fp.ident);
394 visitor.visit_pat(&fp.pat);
395 walk_list!(visitor, visit_attribute, fp.attrs.iter());
398 pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
400 TyKind::Slice(ty) | TyKind::Paren(ty) => visitor.visit_ty(ty),
401 TyKind::Ptr(mutable_type) => visitor.visit_ty(&mutable_type.ty),
402 TyKind::Ref(opt_lifetime, mutable_type) => {
403 walk_list!(visitor, visit_lifetime, opt_lifetime, LifetimeCtxt::Ref);
404 visitor.visit_ty(&mutable_type.ty)
406 TyKind::Tup(tys) => {
407 walk_list!(visitor, visit_ty, tys);
409 TyKind::BareFn(function_declaration) => {
410 walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
411 walk_fn_decl(visitor, &function_declaration.decl);
413 TyKind::Path(maybe_qself, path) => {
414 if let Some(qself) = maybe_qself {
415 visitor.visit_ty(&qself.ty);
417 visitor.visit_path(path, typ.id);
419 TyKind::Array(ty, length) => {
420 visitor.visit_ty(ty);
421 visitor.visit_anon_const(length)
423 TyKind::TraitObject(bounds, ..) => {
424 walk_list!(visitor, visit_param_bound, bounds, BoundKind::TraitObject);
426 TyKind::ImplTrait(_, bounds) => {
427 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Impl);
429 TyKind::Typeof(expression) => visitor.visit_anon_const(expression),
430 TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
431 TyKind::MacCall(mac) => visitor.visit_mac_call(mac),
432 TyKind::Never | TyKind::CVarArgs => {}
436 pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) {
437 for segment in &path.segments {
438 visitor.visit_path_segment(segment);
442 pub fn walk_use_tree<'a, V: Visitor<'a>>(visitor: &mut V, use_tree: &'a UseTree, id: NodeId) {
443 visitor.visit_path(&use_tree.prefix, id);
444 match &use_tree.kind {
445 UseTreeKind::Simple(rename) => {
446 // The extra IDs are handled during HIR lowering.
447 if let &Some(rename) = rename {
448 visitor.visit_ident(rename);
451 UseTreeKind::Glob => {}
452 UseTreeKind::Nested(use_trees) => {
453 for &(ref nested_tree, nested_id) in use_trees {
454 visitor.visit_use_tree(nested_tree, nested_id, true);
460 pub fn walk_path_segment<'a, V: Visitor<'a>>(visitor: &mut V, segment: &'a PathSegment) {
461 visitor.visit_ident(segment.ident);
462 if let Some(args) = &segment.args {
463 visitor.visit_generic_args(args);
467 pub fn walk_generic_args<'a, V>(visitor: &mut V, generic_args: &'a GenericArgs)
472 GenericArgs::AngleBracketed(data) => {
473 for arg in &data.args {
475 AngleBracketedArg::Arg(a) => visitor.visit_generic_arg(a),
476 AngleBracketedArg::Constraint(c) => visitor.visit_assoc_constraint(c),
480 GenericArgs::Parenthesized(data) => {
481 walk_list!(visitor, visit_ty, &data.inputs);
482 walk_fn_ret_ty(visitor, &data.output);
487 pub fn walk_generic_arg<'a, V>(visitor: &mut V, generic_arg: &'a GenericArg)
492 GenericArg::Lifetime(lt) => visitor.visit_lifetime(lt, LifetimeCtxt::GenericArg),
493 GenericArg::Type(ty) => visitor.visit_ty(ty),
494 GenericArg::Const(ct) => visitor.visit_anon_const(ct),
498 pub fn walk_assoc_constraint<'a, V: Visitor<'a>>(visitor: &mut V, constraint: &'a AssocConstraint) {
499 visitor.visit_ident(constraint.ident);
500 if let Some(gen_args) = &constraint.gen_args {
501 visitor.visit_generic_args(gen_args);
503 match &constraint.kind {
504 AssocConstraintKind::Equality { term } => match term {
505 Term::Ty(ty) => visitor.visit_ty(ty),
506 Term::Const(c) => visitor.visit_anon_const(c),
508 AssocConstraintKind::Bound { bounds } => {
509 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
514 pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) {
515 match &pattern.kind {
516 PatKind::TupleStruct(opt_qself, path, elems) => {
517 if let Some(qself) = opt_qself {
518 visitor.visit_ty(&qself.ty);
520 visitor.visit_path(path, pattern.id);
521 walk_list!(visitor, visit_pat, elems);
523 PatKind::Path(opt_qself, path) => {
524 if let Some(qself) = opt_qself {
525 visitor.visit_ty(&qself.ty);
527 visitor.visit_path(path, pattern.id)
529 PatKind::Struct(opt_qself, path, fields, _) => {
530 if let Some(qself) = opt_qself {
531 visitor.visit_ty(&qself.ty);
533 visitor.visit_path(path, pattern.id);
534 walk_list!(visitor, visit_pat_field, fields);
536 PatKind::Box(subpattern) | PatKind::Ref(subpattern, _) | PatKind::Paren(subpattern) => {
537 visitor.visit_pat(subpattern)
539 PatKind::Ident(_, ident, optional_subpattern) => {
540 visitor.visit_ident(*ident);
541 walk_list!(visitor, visit_pat, optional_subpattern);
543 PatKind::Lit(expression) => visitor.visit_expr(expression),
544 PatKind::Range(lower_bound, upper_bound, _) => {
545 walk_list!(visitor, visit_expr, lower_bound);
546 walk_list!(visitor, visit_expr, upper_bound);
548 PatKind::Wild | PatKind::Rest => {}
549 PatKind::Tuple(elems) | PatKind::Slice(elems) | PatKind::Or(elems) => {
550 walk_list!(visitor, visit_pat, elems);
552 PatKind::MacCall(mac) => visitor.visit_mac_call(mac),
556 pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignItem) {
557 let &Item { id, span, ident, ref vis, ref attrs, ref kind, tokens: _ } = item;
558 visitor.visit_vis(vis);
559 visitor.visit_ident(ident);
560 walk_list!(visitor, visit_attribute, attrs);
562 ForeignItemKind::Static(ty, _, expr) => {
563 visitor.visit_ty(ty);
564 walk_list!(visitor, visit_expr, expr);
566 ForeignItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => {
567 let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, generics, body.as_deref());
568 visitor.visit_fn(kind, span, id);
570 ForeignItemKind::TyAlias(box TyAlias { generics, bounds, ty, .. }) => {
571 visitor.visit_generics(generics);
572 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
573 walk_list!(visitor, visit_ty, ty);
575 ForeignItemKind::MacCall(mac) => {
576 visitor.visit_mac_call(mac);
581 pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericBound) {
583 GenericBound::Trait(typ, _modifier) => visitor.visit_poly_trait_ref(typ),
584 GenericBound::Outlives(lifetime) => visitor.visit_lifetime(lifetime, LifetimeCtxt::Bound),
588 pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a GenericParam) {
589 visitor.visit_ident(param.ident);
590 walk_list!(visitor, visit_attribute, param.attrs.iter());
591 walk_list!(visitor, visit_param_bound, ¶m.bounds, BoundKind::Bound);
593 GenericParamKind::Lifetime => (),
594 GenericParamKind::Type { default } => walk_list!(visitor, visit_ty, default),
595 GenericParamKind::Const { ty, default, .. } => {
596 visitor.visit_ty(ty);
597 if let Some(default) = default {
598 visitor.visit_anon_const(default);
604 pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) {
605 walk_list!(visitor, visit_generic_param, &generics.params);
606 walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
609 pub fn walk_closure_binder<'a, V: Visitor<'a>>(visitor: &mut V, binder: &'a ClosureBinder) {
611 ClosureBinder::NotPresent => {}
612 ClosureBinder::For { generic_params, span: _ } => {
613 walk_list!(visitor, visit_generic_param, generic_params)
618 pub fn walk_where_predicate<'a, V: Visitor<'a>>(visitor: &mut V, predicate: &'a WherePredicate) {
620 WherePredicate::BoundPredicate(WhereBoundPredicate {
623 bound_generic_params,
626 visitor.visit_ty(bounded_ty);
627 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
628 walk_list!(visitor, visit_generic_param, bound_generic_params);
630 WherePredicate::RegionPredicate(WhereRegionPredicate { lifetime, bounds, .. }) => {
631 visitor.visit_lifetime(lifetime, LifetimeCtxt::Bound);
632 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
634 WherePredicate::EqPredicate(WhereEqPredicate { lhs_ty, rhs_ty, .. }) => {
635 visitor.visit_ty(lhs_ty);
636 visitor.visit_ty(rhs_ty);
641 pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FnRetTy) {
642 if let FnRetTy::Ty(output_ty) = ret_ty {
643 visitor.visit_ty(output_ty)
647 pub fn walk_fn_decl<'a, V: Visitor<'a>>(visitor: &mut V, function_declaration: &'a FnDecl) {
648 for param in &function_declaration.inputs {
649 visitor.visit_param(param);
651 visitor.visit_fn_ret_ty(&function_declaration.output);
654 pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>) {
656 FnKind::Fn(_, _, sig, _, generics, body) => {
657 visitor.visit_generics(generics);
658 visitor.visit_fn_header(&sig.header);
659 walk_fn_decl(visitor, &sig.decl);
660 walk_list!(visitor, visit_block, body);
662 FnKind::Closure(binder, decl, body) => {
663 visitor.visit_closure_binder(binder);
664 walk_fn_decl(visitor, decl);
665 visitor.visit_expr(body);
670 pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem, ctxt: AssocCtxt) {
671 let &Item { id, span, ident, ref vis, ref attrs, ref kind, tokens: _ } = item;
672 visitor.visit_vis(vis);
673 visitor.visit_ident(ident);
674 walk_list!(visitor, visit_attribute, attrs);
676 AssocItemKind::Const(_, ty, expr) => {
677 visitor.visit_ty(ty);
678 walk_list!(visitor, visit_expr, expr);
680 AssocItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => {
681 let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, generics, body.as_deref());
682 visitor.visit_fn(kind, span, id);
684 AssocItemKind::Type(box TyAlias { generics, bounds, ty, .. }) => {
685 visitor.visit_generics(generics);
686 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
687 walk_list!(visitor, visit_ty, ty);
689 AssocItemKind::MacCall(mac) => {
690 visitor.visit_mac_call(mac);
695 pub fn walk_struct_def<'a, V: Visitor<'a>>(visitor: &mut V, struct_definition: &'a VariantData) {
696 walk_list!(visitor, visit_field_def, struct_definition.fields());
699 pub fn walk_field_def<'a, V: Visitor<'a>>(visitor: &mut V, field: &'a FieldDef) {
700 visitor.visit_vis(&field.vis);
701 if let Some(ident) = field.ident {
702 visitor.visit_ident(ident);
704 visitor.visit_ty(&field.ty);
705 walk_list!(visitor, visit_attribute, &field.attrs);
708 pub fn walk_block<'a, V: Visitor<'a>>(visitor: &mut V, block: &'a Block) {
709 walk_list!(visitor, visit_stmt, &block.stmts);
712 pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) {
713 match &statement.kind {
714 StmtKind::Local(local) => visitor.visit_local(local),
715 StmtKind::Item(item) => visitor.visit_item(item),
716 StmtKind::Expr(expr) | StmtKind::Semi(expr) => visitor.visit_expr(expr),
717 StmtKind::Empty => {}
718 StmtKind::MacCall(mac) => {
719 let MacCallStmt { mac, attrs, style: _, tokens: _ } = &**mac;
720 visitor.visit_mac_call(mac);
721 for attr in attrs.iter() {
722 visitor.visit_attribute(attr);
728 pub fn walk_mac<'a, V: Visitor<'a>>(visitor: &mut V, mac: &'a MacCall) {
729 visitor.visit_path(&mac.path, DUMMY_NODE_ID);
732 pub fn walk_anon_const<'a, V: Visitor<'a>>(visitor: &mut V, constant: &'a AnonConst) {
733 visitor.visit_expr(&constant.value);
736 pub fn walk_inline_asm<'a, V: Visitor<'a>>(visitor: &mut V, asm: &'a InlineAsm) {
737 for (op, _) in &asm.operands {
739 InlineAsmOperand::In { expr, .. }
740 | InlineAsmOperand::Out { expr: Some(expr), .. }
741 | InlineAsmOperand::InOut { expr, .. } => visitor.visit_expr(expr),
742 InlineAsmOperand::Out { expr: None, .. } => {}
743 InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
744 visitor.visit_expr(in_expr);
745 if let Some(out_expr) = out_expr {
746 visitor.visit_expr(out_expr);
749 InlineAsmOperand::Const { anon_const, .. } => visitor.visit_anon_const(anon_const),
750 InlineAsmOperand::Sym { sym } => visitor.visit_inline_asm_sym(sym),
755 pub fn walk_inline_asm_sym<'a, V: Visitor<'a>>(visitor: &mut V, sym: &'a InlineAsmSym) {
756 if let Some(qself) = &sym.qself {
757 visitor.visit_ty(&qself.ty);
759 visitor.visit_path(&sym.path, sym.id);
762 pub fn walk_format_args<'a, V: Visitor<'a>>(visitor: &mut V, fmt: &'a FormatArgs) {
763 for arg in fmt.arguments.all_args() {
764 if let FormatArgumentKind::Named(name) = arg.kind {
765 visitor.visit_ident(name);
767 visitor.visit_expr(&arg.expr);
771 pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
772 walk_list!(visitor, visit_attribute, expression.attrs.iter());
774 match &expression.kind {
775 ExprKind::Box(subexpression) => visitor.visit_expr(subexpression),
776 ExprKind::Array(subexpressions) => {
777 walk_list!(visitor, visit_expr, subexpressions);
779 ExprKind::ConstBlock(anon_const) => visitor.visit_anon_const(anon_const),
780 ExprKind::Repeat(element, count) => {
781 visitor.visit_expr(element);
782 visitor.visit_anon_const(count)
784 ExprKind::Struct(se) => {
785 if let Some(qself) = &se.qself {
786 visitor.visit_ty(&qself.ty);
788 visitor.visit_path(&se.path, expression.id);
789 walk_list!(visitor, visit_expr_field, &se.fields);
791 StructRest::Base(expr) => visitor.visit_expr(expr),
792 StructRest::Rest(_span) => {}
793 StructRest::None => {}
796 ExprKind::Tup(subexpressions) => {
797 walk_list!(visitor, visit_expr, subexpressions);
799 ExprKind::Call(callee_expression, arguments) => {
800 visitor.visit_expr(callee_expression);
801 walk_list!(visitor, visit_expr, arguments);
803 ExprKind::MethodCall(box MethodCall { seg, receiver, args, span: _ }) => {
804 visitor.visit_path_segment(seg);
805 visitor.visit_expr(receiver);
806 walk_list!(visitor, visit_expr, args);
808 ExprKind::Binary(_, left_expression, right_expression) => {
809 visitor.visit_expr(left_expression);
810 visitor.visit_expr(right_expression)
812 ExprKind::AddrOf(_, _, subexpression) | ExprKind::Unary(_, subexpression) => {
813 visitor.visit_expr(subexpression)
815 ExprKind::Cast(subexpression, typ) | ExprKind::Type(subexpression, typ) => {
816 visitor.visit_expr(subexpression);
817 visitor.visit_ty(typ)
819 ExprKind::Let(pat, expr, _) => {
820 visitor.visit_pat(pat);
821 visitor.visit_expr(expr);
823 ExprKind::If(head_expression, if_block, optional_else) => {
824 visitor.visit_expr(head_expression);
825 visitor.visit_block(if_block);
826 walk_list!(visitor, visit_expr, optional_else);
828 ExprKind::While(subexpression, block, opt_label) => {
829 walk_list!(visitor, visit_label, opt_label);
830 visitor.visit_expr(subexpression);
831 visitor.visit_block(block);
833 ExprKind::ForLoop(pattern, subexpression, block, opt_label) => {
834 walk_list!(visitor, visit_label, opt_label);
835 visitor.visit_pat(pattern);
836 visitor.visit_expr(subexpression);
837 visitor.visit_block(block);
839 ExprKind::Loop(block, opt_label, _) => {
840 walk_list!(visitor, visit_label, opt_label);
841 visitor.visit_block(block);
843 ExprKind::Match(subexpression, arms) => {
844 visitor.visit_expr(subexpression);
845 walk_list!(visitor, visit_arm, arms);
847 ExprKind::Closure(box Closure {
858 visitor.visit_fn(FnKind::Closure(binder, fn_decl, body), expression.span, expression.id)
860 ExprKind::Block(block, opt_label) => {
861 walk_list!(visitor, visit_label, opt_label);
862 visitor.visit_block(block);
864 ExprKind::Async(_, _, body) => {
865 visitor.visit_block(body);
867 ExprKind::Await(expr) => visitor.visit_expr(expr),
868 ExprKind::Assign(lhs, rhs, _) => {
869 visitor.visit_expr(lhs);
870 visitor.visit_expr(rhs);
872 ExprKind::AssignOp(_, left_expression, right_expression) => {
873 visitor.visit_expr(left_expression);
874 visitor.visit_expr(right_expression);
876 ExprKind::Field(subexpression, ident) => {
877 visitor.visit_expr(subexpression);
878 visitor.visit_ident(*ident);
880 ExprKind::Index(main_expression, index_expression) => {
881 visitor.visit_expr(main_expression);
882 visitor.visit_expr(index_expression)
884 ExprKind::Range(start, end, _) => {
885 walk_list!(visitor, visit_expr, start);
886 walk_list!(visitor, visit_expr, end);
888 ExprKind::Underscore => {}
889 ExprKind::Path(maybe_qself, path) => {
890 if let Some(qself) = maybe_qself {
891 visitor.visit_ty(&qself.ty);
893 visitor.visit_path(path, expression.id)
895 ExprKind::Break(opt_label, opt_expr) => {
896 walk_list!(visitor, visit_label, opt_label);
897 walk_list!(visitor, visit_expr, opt_expr);
899 ExprKind::Continue(opt_label) => {
900 walk_list!(visitor, visit_label, opt_label);
902 ExprKind::Ret(optional_expression) => {
903 walk_list!(visitor, visit_expr, optional_expression);
905 ExprKind::Yeet(optional_expression) => {
906 walk_list!(visitor, visit_expr, optional_expression);
908 ExprKind::MacCall(mac) => visitor.visit_mac_call(mac),
909 ExprKind::Paren(subexpression) => visitor.visit_expr(subexpression),
910 ExprKind::InlineAsm(asm) => visitor.visit_inline_asm(asm),
911 ExprKind::FormatArgs(f) => visitor.visit_format_args(f),
912 ExprKind::Yield(optional_expression) => {
913 walk_list!(visitor, visit_expr, optional_expression);
915 ExprKind::Try(subexpression) => visitor.visit_expr(subexpression),
916 ExprKind::TryBlock(body) => visitor.visit_block(body),
917 ExprKind::Lit(_) | ExprKind::IncludedBytes(..) | ExprKind::Err => {}
920 visitor.visit_expr_post(expression)
923 pub fn walk_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Param) {
924 walk_list!(visitor, visit_attribute, param.attrs.iter());
925 visitor.visit_pat(¶m.pat);
926 visitor.visit_ty(¶m.ty);
929 pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
930 visitor.visit_pat(&arm.pat);
931 walk_list!(visitor, visit_expr, &arm.guard);
932 visitor.visit_expr(&arm.body);
933 walk_list!(visitor, visit_attribute, &arm.attrs);
936 pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
937 if let VisibilityKind::Restricted { ref path, id, shorthand: _ } = vis.kind {
938 visitor.visit_path(path, id);
942 pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) {
944 AttrKind::Normal(normal) => walk_attr_args(visitor, &normal.item.args),
945 AttrKind::DocComment(..) => {}
949 pub fn walk_attr_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a AttrArgs) {
951 AttrArgs::Empty => {}
952 AttrArgs::Delimited(_) => {}
953 AttrArgs::Eq(_eq_span, AttrArgsEq::Ast(expr)) => visitor.visit_expr(expr),
954 AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => {
955 unreachable!("in literal form when walking mac args eq: {:?}", lit)