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_inline_asm_sym(&mut self, sym: &'ast InlineAsmSym) {
246 walk_inline_asm_sym(self, sym)
251 macro_rules! walk_list {
252 ($visitor: expr, $method: ident, $list: expr $(, $($extra_args: expr),* )?) => {
254 #[allow(for_loops_over_fallibles)]
256 $visitor.$method(elem $(, $($extra_args,)* )?)
262 pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) {
263 walk_list!(visitor, visit_item, &krate.items);
264 walk_list!(visitor, visit_attribute, &krate.attrs);
267 pub fn walk_local<'a, V: Visitor<'a>>(visitor: &mut V, local: &'a Local) {
268 for attr in local.attrs.iter() {
269 visitor.visit_attribute(attr);
271 visitor.visit_pat(&local.pat);
272 walk_list!(visitor, visit_ty, &local.ty);
273 if let Some((init, els)) = local.kind.init_else_opt() {
274 visitor.visit_expr(init);
275 walk_list!(visitor, visit_block, els);
279 pub fn walk_label<'a, V: Visitor<'a>>(visitor: &mut V, label: &'a Label) {
280 visitor.visit_ident(label.ident);
283 pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime) {
284 visitor.visit_ident(lifetime.ident);
287 pub fn walk_poly_trait_ref<'a, V>(visitor: &mut V, trait_ref: &'a PolyTraitRef)
291 walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
292 visitor.visit_trait_ref(&trait_ref.trait_ref);
295 pub fn walk_trait_ref<'a, V: Visitor<'a>>(visitor: &mut V, trait_ref: &'a TraitRef) {
296 visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
299 pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
300 visitor.visit_vis(&item.vis);
301 visitor.visit_ident(item.ident);
303 ItemKind::ExternCrate(_) => {}
304 ItemKind::Use(ref use_tree) => visitor.visit_use_tree(use_tree, item.id, false),
305 ItemKind::Static(ref typ, _, ref expr) | ItemKind::Const(_, ref typ, ref expr) => {
306 visitor.visit_ty(typ);
307 walk_list!(visitor, visit_expr, expr);
309 ItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
311 FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, generics, body.as_deref());
312 visitor.visit_fn(kind, item.span, item.id)
314 ItemKind::Mod(_unsafety, ref mod_kind) => match mod_kind {
315 ModKind::Loaded(items, _inline, _inner_span) => {
316 walk_list!(visitor, visit_item, items)
318 ModKind::Unloaded => {}
320 ItemKind::ForeignMod(ref foreign_module) => {
321 walk_list!(visitor, visit_foreign_item, &foreign_module.items);
323 ItemKind::GlobalAsm(ref asm) => visitor.visit_inline_asm(asm),
324 ItemKind::TyAlias(box TyAlias { ref generics, ref bounds, ref ty, .. }) => {
325 visitor.visit_generics(generics);
326 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
327 walk_list!(visitor, visit_ty, ty);
329 ItemKind::Enum(ref enum_definition, ref generics) => {
330 visitor.visit_generics(generics);
331 visitor.visit_enum_def(enum_definition)
333 ItemKind::Impl(box Impl {
343 visitor.visit_generics(generics);
344 walk_list!(visitor, visit_trait_ref, of_trait);
345 visitor.visit_ty(self_ty);
346 walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Impl);
348 ItemKind::Struct(ref struct_definition, ref generics)
349 | ItemKind::Union(ref struct_definition, ref generics) => {
350 visitor.visit_generics(generics);
351 visitor.visit_variant_data(struct_definition);
353 ItemKind::Trait(box Trait {
360 visitor.visit_generics(generics);
361 walk_list!(visitor, visit_param_bound, bounds, BoundKind::SuperTraits);
362 walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);
364 ItemKind::TraitAlias(ref generics, ref bounds) => {
365 visitor.visit_generics(generics);
366 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
368 ItemKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
369 ItemKind::MacroDef(ref ts) => visitor.visit_mac_def(ts, item.id),
371 walk_list!(visitor, visit_attribute, &item.attrs);
374 pub fn walk_enum_def<'a, V: Visitor<'a>>(visitor: &mut V, enum_definition: &'a EnumDef) {
375 walk_list!(visitor, visit_variant, &enum_definition.variants);
378 pub fn walk_variant<'a, V: Visitor<'a>>(visitor: &mut V, variant: &'a Variant)
382 visitor.visit_ident(variant.ident);
383 visitor.visit_vis(&variant.vis);
384 visitor.visit_variant_data(&variant.data);
385 walk_list!(visitor, visit_anon_const, &variant.disr_expr);
386 walk_list!(visitor, visit_attribute, &variant.attrs);
389 pub fn walk_expr_field<'a, V: Visitor<'a>>(visitor: &mut V, f: &'a ExprField) {
390 visitor.visit_expr(&f.expr);
391 visitor.visit_ident(f.ident);
392 walk_list!(visitor, visit_attribute, f.attrs.iter());
395 pub fn walk_pat_field<'a, V: Visitor<'a>>(visitor: &mut V, fp: &'a PatField) {
396 visitor.visit_ident(fp.ident);
397 visitor.visit_pat(&fp.pat);
398 walk_list!(visitor, visit_attribute, fp.attrs.iter());
401 pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
403 TyKind::Slice(ref ty) | TyKind::Paren(ref ty) => visitor.visit_ty(ty),
404 TyKind::Ptr(ref mutable_type) => visitor.visit_ty(&mutable_type.ty),
405 TyKind::Rptr(ref opt_lifetime, ref mutable_type) => {
406 walk_list!(visitor, visit_lifetime, opt_lifetime, LifetimeCtxt::Rptr);
407 visitor.visit_ty(&mutable_type.ty)
409 TyKind::Tup(ref tuple_element_types) => {
410 walk_list!(visitor, visit_ty, tuple_element_types);
412 TyKind::BareFn(ref function_declaration) => {
413 walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
414 walk_fn_decl(visitor, &function_declaration.decl);
416 TyKind::Path(ref maybe_qself, ref path) => {
417 if let Some(ref qself) = *maybe_qself {
418 visitor.visit_ty(&qself.ty);
420 visitor.visit_path(path, typ.id);
422 TyKind::Array(ref ty, ref length) => {
423 visitor.visit_ty(ty);
424 visitor.visit_anon_const(length)
426 TyKind::TraitObject(ref bounds, ..) => {
427 walk_list!(visitor, visit_param_bound, bounds, BoundKind::TraitObject);
429 TyKind::ImplTrait(_, ref bounds) => {
430 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Impl);
432 TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
433 TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
434 TyKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
435 TyKind::Never | TyKind::CVarArgs => {}
439 pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) {
440 for segment in &path.segments {
441 visitor.visit_path_segment(segment);
445 pub fn walk_use_tree<'a, V: Visitor<'a>>(visitor: &mut V, use_tree: &'a UseTree, id: NodeId) {
446 visitor.visit_path(&use_tree.prefix, id);
447 match use_tree.kind {
448 UseTreeKind::Simple(rename, ..) => {
449 // The extra IDs are handled during HIR lowering.
450 if let Some(rename) = rename {
451 visitor.visit_ident(rename);
454 UseTreeKind::Glob => {}
455 UseTreeKind::Nested(ref use_trees) => {
456 for &(ref nested_tree, nested_id) in use_trees {
457 visitor.visit_use_tree(nested_tree, nested_id, true);
463 pub fn walk_path_segment<'a, V: Visitor<'a>>(visitor: &mut V, segment: &'a PathSegment) {
464 visitor.visit_ident(segment.ident);
465 if let Some(ref args) = segment.args {
466 visitor.visit_generic_args(args);
470 pub fn walk_generic_args<'a, V>(visitor: &mut V, generic_args: &'a GenericArgs)
474 match *generic_args {
475 GenericArgs::AngleBracketed(ref data) => {
476 for arg in &data.args {
478 AngleBracketedArg::Arg(a) => visitor.visit_generic_arg(a),
479 AngleBracketedArg::Constraint(c) => visitor.visit_assoc_constraint(c),
483 GenericArgs::Parenthesized(ref data) => {
484 walk_list!(visitor, visit_ty, &data.inputs);
485 walk_fn_ret_ty(visitor, &data.output);
490 pub fn walk_generic_arg<'a, V>(visitor: &mut V, generic_arg: &'a GenericArg)
495 GenericArg::Lifetime(lt) => visitor.visit_lifetime(lt, LifetimeCtxt::GenericArg),
496 GenericArg::Type(ty) => visitor.visit_ty(ty),
497 GenericArg::Const(ct) => visitor.visit_anon_const(ct),
501 pub fn walk_assoc_constraint<'a, V: Visitor<'a>>(visitor: &mut V, constraint: &'a AssocConstraint) {
502 visitor.visit_ident(constraint.ident);
503 if let Some(ref gen_args) = constraint.gen_args {
504 visitor.visit_generic_args(gen_args);
506 match constraint.kind {
507 AssocConstraintKind::Equality { ref term } => match term {
508 Term::Ty(ty) => visitor.visit_ty(ty),
509 Term::Const(c) => visitor.visit_anon_const(c),
511 AssocConstraintKind::Bound { ref bounds } => {
512 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
517 pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) {
519 PatKind::TupleStruct(ref opt_qself, ref path, ref elems) => {
520 if let Some(ref qself) = *opt_qself {
521 visitor.visit_ty(&qself.ty);
523 visitor.visit_path(path, pattern.id);
524 walk_list!(visitor, visit_pat, elems);
526 PatKind::Path(ref opt_qself, ref path) => {
527 if let Some(ref qself) = *opt_qself {
528 visitor.visit_ty(&qself.ty);
530 visitor.visit_path(path, pattern.id)
532 PatKind::Struct(ref opt_qself, ref path, ref fields, _) => {
533 if let Some(ref qself) = *opt_qself {
534 visitor.visit_ty(&qself.ty);
536 visitor.visit_path(path, pattern.id);
537 walk_list!(visitor, visit_pat_field, fields);
539 PatKind::Box(ref subpattern)
540 | PatKind::Ref(ref subpattern, _)
541 | PatKind::Paren(ref subpattern) => visitor.visit_pat(subpattern),
542 PatKind::Ident(_, ident, ref optional_subpattern) => {
543 visitor.visit_ident(ident);
544 walk_list!(visitor, visit_pat, optional_subpattern);
546 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
547 PatKind::Range(ref lower_bound, ref upper_bound, _) => {
548 walk_list!(visitor, visit_expr, lower_bound);
549 walk_list!(visitor, visit_expr, upper_bound);
551 PatKind::Wild | PatKind::Rest => {}
552 PatKind::Tuple(ref elems) | PatKind::Slice(ref elems) | PatKind::Or(ref elems) => {
553 walk_list!(visitor, visit_pat, elems);
555 PatKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
559 pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignItem) {
560 let Item { id, span, ident, ref vis, ref attrs, ref kind, tokens: _ } = *item;
561 visitor.visit_vis(vis);
562 visitor.visit_ident(ident);
563 walk_list!(visitor, visit_attribute, attrs);
565 ForeignItemKind::Static(ty, _, expr) => {
566 visitor.visit_ty(ty);
567 walk_list!(visitor, visit_expr, expr);
569 ForeignItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
570 let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, generics, body.as_deref());
571 visitor.visit_fn(kind, span, id);
573 ForeignItemKind::TyAlias(box TyAlias { generics, bounds, ty, .. }) => {
574 visitor.visit_generics(generics);
575 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
576 walk_list!(visitor, visit_ty, ty);
578 ForeignItemKind::MacCall(mac) => {
579 visitor.visit_mac_call(mac);
584 pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericBound) {
586 GenericBound::Trait(ref typ, ref _modifier) => visitor.visit_poly_trait_ref(typ),
587 GenericBound::Outlives(ref lifetime) => {
588 visitor.visit_lifetime(lifetime, LifetimeCtxt::Bound)
593 pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a GenericParam) {
594 visitor.visit_ident(param.ident);
595 walk_list!(visitor, visit_attribute, param.attrs.iter());
596 walk_list!(visitor, visit_param_bound, ¶m.bounds, BoundKind::Bound);
598 GenericParamKind::Lifetime => (),
599 GenericParamKind::Type { ref default } => walk_list!(visitor, visit_ty, default),
600 GenericParamKind::Const { ref ty, ref default, .. } => {
601 visitor.visit_ty(ty);
602 if let Some(default) = default {
603 visitor.visit_anon_const(default);
609 pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) {
610 walk_list!(visitor, visit_generic_param, &generics.params);
611 walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
614 pub fn walk_closure_binder<'a, V: Visitor<'a>>(visitor: &mut V, binder: &'a ClosureBinder) {
616 ClosureBinder::NotPresent => {}
617 ClosureBinder::For { generic_params, span: _ } => {
618 walk_list!(visitor, visit_generic_param, generic_params)
623 pub fn walk_where_predicate<'a, V: Visitor<'a>>(visitor: &mut V, predicate: &'a WherePredicate) {
625 WherePredicate::BoundPredicate(WhereBoundPredicate {
628 ref bound_generic_params,
631 visitor.visit_ty(bounded_ty);
632 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
633 walk_list!(visitor, visit_generic_param, bound_generic_params);
635 WherePredicate::RegionPredicate(WhereRegionPredicate {
636 ref lifetime, ref bounds, ..
638 visitor.visit_lifetime(lifetime, LifetimeCtxt::Bound);
639 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
641 WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty, .. }) => {
642 visitor.visit_ty(lhs_ty);
643 visitor.visit_ty(rhs_ty);
648 pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FnRetTy) {
649 if let FnRetTy::Ty(ref output_ty) = *ret_ty {
650 visitor.visit_ty(output_ty)
654 pub fn walk_fn_decl<'a, V: Visitor<'a>>(visitor: &mut V, function_declaration: &'a FnDecl) {
655 for param in &function_declaration.inputs {
656 visitor.visit_param(param);
658 visitor.visit_fn_ret_ty(&function_declaration.output);
661 pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>) {
663 FnKind::Fn(_, _, sig, _, generics, body) => {
664 visitor.visit_generics(generics);
665 visitor.visit_fn_header(&sig.header);
666 walk_fn_decl(visitor, &sig.decl);
667 walk_list!(visitor, visit_block, body);
669 FnKind::Closure(binder, decl, body) => {
670 visitor.visit_closure_binder(binder);
671 walk_fn_decl(visitor, decl);
672 visitor.visit_expr(body);
677 pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem, ctxt: AssocCtxt) {
678 let Item { id, span, ident, ref vis, ref attrs, ref kind, tokens: _ } = *item;
679 visitor.visit_vis(vis);
680 visitor.visit_ident(ident);
681 walk_list!(visitor, visit_attribute, attrs);
683 AssocItemKind::Const(_, ty, expr) => {
684 visitor.visit_ty(ty);
685 walk_list!(visitor, visit_expr, expr);
687 AssocItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
688 let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, generics, body.as_deref());
689 visitor.visit_fn(kind, span, id);
691 AssocItemKind::Type(box TyAlias { generics, bounds, ty, .. }) => {
692 visitor.visit_generics(generics);
693 walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
694 walk_list!(visitor, visit_ty, ty);
696 AssocItemKind::MacCall(mac) => {
697 visitor.visit_mac_call(mac);
702 pub fn walk_struct_def<'a, V: Visitor<'a>>(visitor: &mut V, struct_definition: &'a VariantData) {
703 walk_list!(visitor, visit_field_def, struct_definition.fields());
706 pub fn walk_field_def<'a, V: Visitor<'a>>(visitor: &mut V, field: &'a FieldDef) {
707 visitor.visit_vis(&field.vis);
708 if let Some(ident) = field.ident {
709 visitor.visit_ident(ident);
711 visitor.visit_ty(&field.ty);
712 walk_list!(visitor, visit_attribute, &field.attrs);
715 pub fn walk_block<'a, V: Visitor<'a>>(visitor: &mut V, block: &'a Block) {
716 walk_list!(visitor, visit_stmt, &block.stmts);
719 pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) {
720 match statement.kind {
721 StmtKind::Local(ref local) => visitor.visit_local(local),
722 StmtKind::Item(ref item) => visitor.visit_item(item),
723 StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => visitor.visit_expr(expr),
724 StmtKind::Empty => {}
725 StmtKind::MacCall(ref mac) => {
726 let MacCallStmt { ref mac, style: _, ref attrs, tokens: _ } = **mac;
727 visitor.visit_mac_call(mac);
728 for attr in attrs.iter() {
729 visitor.visit_attribute(attr);
735 pub fn walk_mac<'a, V: Visitor<'a>>(visitor: &mut V, mac: &'a MacCall) {
736 visitor.visit_path(&mac.path, DUMMY_NODE_ID);
739 pub fn walk_anon_const<'a, V: Visitor<'a>>(visitor: &mut V, constant: &'a AnonConst) {
740 visitor.visit_expr(&constant.value);
743 pub fn walk_inline_asm<'a, V: Visitor<'a>>(visitor: &mut V, asm: &'a InlineAsm) {
744 for (op, _) in &asm.operands {
746 InlineAsmOperand::In { expr, .. }
747 | InlineAsmOperand::Out { expr: Some(expr), .. }
748 | InlineAsmOperand::InOut { expr, .. } => visitor.visit_expr(expr),
749 InlineAsmOperand::Out { expr: None, .. } => {}
750 InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
751 visitor.visit_expr(in_expr);
752 if let Some(out_expr) = out_expr {
753 visitor.visit_expr(out_expr);
756 InlineAsmOperand::Const { anon_const, .. } => visitor.visit_anon_const(anon_const),
757 InlineAsmOperand::Sym { sym } => visitor.visit_inline_asm_sym(sym),
762 pub fn walk_inline_asm_sym<'a, V: Visitor<'a>>(visitor: &mut V, sym: &'a InlineAsmSym) {
763 if let Some(ref qself) = sym.qself {
764 visitor.visit_ty(&qself.ty);
766 visitor.visit_path(&sym.path, sym.id);
769 pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
770 walk_list!(visitor, visit_attribute, expression.attrs.iter());
772 match expression.kind {
773 ExprKind::Box(ref subexpression) => visitor.visit_expr(subexpression),
774 ExprKind::Array(ref subexpressions) => {
775 walk_list!(visitor, visit_expr, subexpressions);
777 ExprKind::ConstBlock(ref anon_const) => visitor.visit_anon_const(anon_const),
778 ExprKind::Repeat(ref element, ref count) => {
779 visitor.visit_expr(element);
780 visitor.visit_anon_const(count)
782 ExprKind::Struct(ref se) => {
783 if let Some(ref qself) = se.qself {
784 visitor.visit_ty(&qself.ty);
786 visitor.visit_path(&se.path, expression.id);
787 walk_list!(visitor, visit_expr_field, &se.fields);
789 StructRest::Base(expr) => visitor.visit_expr(expr),
790 StructRest::Rest(_span) => {}
791 StructRest::None => {}
794 ExprKind::Tup(ref subexpressions) => {
795 walk_list!(visitor, visit_expr, subexpressions);
797 ExprKind::Call(ref callee_expression, ref arguments) => {
798 visitor.visit_expr(callee_expression);
799 walk_list!(visitor, visit_expr, arguments);
801 ExprKind::MethodCall(box MethodCall { ref seg, ref receiver, ref args, span: _ }) => {
802 visitor.visit_path_segment(seg);
803 visitor.visit_expr(receiver);
804 walk_list!(visitor, visit_expr, args);
806 ExprKind::Binary(_, ref left_expression, ref right_expression) => {
807 visitor.visit_expr(left_expression);
808 visitor.visit_expr(right_expression)
810 ExprKind::AddrOf(_, _, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
811 visitor.visit_expr(subexpression)
813 ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
814 visitor.visit_expr(subexpression);
815 visitor.visit_ty(typ)
817 ExprKind::Let(ref pat, ref expr, _) => {
818 visitor.visit_pat(pat);
819 visitor.visit_expr(expr);
821 ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
822 visitor.visit_expr(head_expression);
823 visitor.visit_block(if_block);
824 walk_list!(visitor, visit_expr, optional_else);
826 ExprKind::While(ref subexpression, ref block, ref opt_label) => {
827 walk_list!(visitor, visit_label, opt_label);
828 visitor.visit_expr(subexpression);
829 visitor.visit_block(block);
831 ExprKind::ForLoop(ref pattern, ref subexpression, ref block, ref opt_label) => {
832 walk_list!(visitor, visit_label, opt_label);
833 visitor.visit_pat(pattern);
834 visitor.visit_expr(subexpression);
835 visitor.visit_block(block);
837 ExprKind::Loop(ref block, ref opt_label) => {
838 walk_list!(visitor, visit_label, opt_label);
839 visitor.visit_block(block);
841 ExprKind::Match(ref subexpression, ref arms) => {
842 visitor.visit_expr(subexpression);
843 walk_list!(visitor, visit_arm, arms);
845 ExprKind::Closure(box Closure {
854 visitor.visit_fn(FnKind::Closure(binder, fn_decl, body), expression.span, expression.id)
856 ExprKind::Block(ref block, ref opt_label) => {
857 walk_list!(visitor, visit_label, opt_label);
858 visitor.visit_block(block);
860 ExprKind::Async(_, _, ref body) => {
861 visitor.visit_block(body);
863 ExprKind::Await(ref expr) => visitor.visit_expr(expr),
864 ExprKind::Assign(ref lhs, ref rhs, _) => {
865 visitor.visit_expr(lhs);
866 visitor.visit_expr(rhs);
868 ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
869 visitor.visit_expr(left_expression);
870 visitor.visit_expr(right_expression);
872 ExprKind::Field(ref subexpression, ident) => {
873 visitor.visit_expr(subexpression);
874 visitor.visit_ident(ident);
876 ExprKind::Index(ref main_expression, ref index_expression) => {
877 visitor.visit_expr(main_expression);
878 visitor.visit_expr(index_expression)
880 ExprKind::Range(ref start, ref end, _) => {
881 walk_list!(visitor, visit_expr, start);
882 walk_list!(visitor, visit_expr, end);
884 ExprKind::Underscore => {}
885 ExprKind::Path(ref maybe_qself, ref path) => {
886 if let Some(ref qself) = *maybe_qself {
887 visitor.visit_ty(&qself.ty);
889 visitor.visit_path(path, expression.id)
891 ExprKind::Break(ref opt_label, ref opt_expr) => {
892 walk_list!(visitor, visit_label, opt_label);
893 walk_list!(visitor, visit_expr, opt_expr);
895 ExprKind::Continue(ref opt_label) => {
896 walk_list!(visitor, visit_label, opt_label);
898 ExprKind::Ret(ref optional_expression) => {
899 walk_list!(visitor, visit_expr, optional_expression);
901 ExprKind::Yeet(ref optional_expression) => {
902 walk_list!(visitor, visit_expr, optional_expression);
904 ExprKind::MacCall(ref mac) => visitor.visit_mac_call(mac),
905 ExprKind::Paren(ref subexpression) => visitor.visit_expr(subexpression),
906 ExprKind::InlineAsm(ref asm) => visitor.visit_inline_asm(asm),
907 ExprKind::Yield(ref optional_expression) => {
908 walk_list!(visitor, visit_expr, optional_expression);
910 ExprKind::Try(ref subexpression) => visitor.visit_expr(subexpression),
911 ExprKind::TryBlock(ref body) => visitor.visit_block(body),
912 ExprKind::Lit(_) | ExprKind::IncludedBytes(..) | ExprKind::Err => {}
915 visitor.visit_expr_post(expression)
918 pub fn walk_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Param) {
919 walk_list!(visitor, visit_attribute, param.attrs.iter());
920 visitor.visit_pat(¶m.pat);
921 visitor.visit_ty(¶m.ty);
924 pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
925 visitor.visit_pat(&arm.pat);
926 walk_list!(visitor, visit_expr, &arm.guard);
927 visitor.visit_expr(&arm.body);
928 walk_list!(visitor, visit_attribute, &arm.attrs);
931 pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
932 if let VisibilityKind::Restricted { ref path, id, shorthand: _ } = vis.kind {
933 visitor.visit_path(path, id);
937 pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) {
939 AttrKind::Normal(ref normal) => walk_mac_args(visitor, &normal.item.args),
940 AttrKind::DocComment(..) => {}
944 pub fn walk_mac_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a MacArgs) {
947 MacArgs::Delimited(_dspan, _delim, _tokens) => {}
948 MacArgs::Eq(_eq_span, MacArgsEq::Ast(expr)) => visitor.visit_expr(expr),
949 MacArgs::Eq(_, MacArgsEq::Hir(lit)) => {
950 unreachable!("in literal form when walking mac args eq: {:?}", lit)