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.
17 use crate::token
::Token
;
18 use crate::tokenstream
::{TokenStream, TokenTree}
;
20 use rustc_span
::symbol
::{Ident, Symbol}
;
23 #[derive(Copy, Clone, PartialEq)]
29 #[derive(Copy, Clone, PartialEq)]
36 #[derive(Copy, Clone)]
38 /// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
39 Fn(FnCtxt
, Ident
, &'a FnSig
, &'a Visibility
, Option
<&'a Block
>),
41 /// E.g., `|x, y| body`.
42 Closure(&'a FnDecl
, &'a Expr
),
46 pub fn header(&self) -> Option
<&'a FnHeader
> {
48 FnKind
::Fn(_
, _
, sig
, _
, _
) => Some(&sig
.header
),
49 FnKind
::Closure(_
, _
) => None
,
53 pub fn decl(&self) -> &'a FnDecl
{
55 FnKind
::Fn(_
, _
, sig
, _
, _
) => &sig
.decl
,
56 FnKind
::Closure(decl
, _
) => decl
,
60 pub fn ctxt(&self) -> Option
<FnCtxt
> {
62 FnKind
::Fn(ctxt
, ..) => Some(*ctxt
),
63 FnKind
::Closure(..) => None
,
68 /// Each method of the `Visitor` trait is a hook to be potentially
69 /// overridden. Each method's default implementation recursively visits
70 /// the substructure of the input via the corresponding `walk` method;
71 /// e.g., the `visit_mod` method by default calls `visit::walk_mod`.
73 /// If you want to ensure that your code handles every variant
74 /// explicitly, you need to override each method. (And you also need
75 /// to monitor future changes to `Visitor` in case a new method with a
76 /// new default implementation gets introduced.)
77 pub trait Visitor
<'ast
>: Sized
{
78 fn visit_name(&mut self, _span
: Span
, _name
: Symbol
) {
81 fn visit_ident(&mut self, ident
: Ident
) {
82 walk_ident(self, ident
);
84 fn visit_mod(&mut self, m
: &'ast Mod
, _s
: Span
, _attrs
: &[Attribute
], _n
: NodeId
) {
87 fn visit_foreign_item(&mut self, i
: &'ast ForeignItem
) {
88 walk_foreign_item(self, i
)
90 fn visit_global_asm(&mut self, ga
: &'ast GlobalAsm
) {
91 walk_global_asm(self, ga
)
93 fn visit_item(&mut self, i
: &'ast Item
) {
96 fn visit_local(&mut self, l
: &'ast Local
) {
99 fn visit_block(&mut self, b
: &'ast Block
) {
102 fn visit_stmt(&mut self, s
: &'ast Stmt
) {
105 fn visit_param(&mut self, param
: &'ast Param
) {
106 walk_param(self, param
)
108 fn visit_arm(&mut self, a
: &'ast Arm
) {
111 fn visit_pat(&mut self, p
: &'ast Pat
) {
114 fn visit_anon_const(&mut self, c
: &'ast AnonConst
) {
115 walk_anon_const(self, c
)
117 fn visit_expr(&mut self, ex
: &'ast Expr
) {
120 fn visit_expr_post(&mut self, _ex
: &'ast Expr
) {}
121 fn visit_ty(&mut self, t
: &'ast Ty
) {
124 fn visit_generic_param(&mut self, param
: &'ast GenericParam
) {
125 walk_generic_param(self, param
)
127 fn visit_generics(&mut self, g
: &'ast Generics
) {
128 walk_generics(self, g
)
130 fn visit_where_predicate(&mut self, p
: &'ast WherePredicate
) {
131 walk_where_predicate(self, p
)
133 fn visit_fn(&mut self, fk
: FnKind
<'ast
>, s
: Span
, _
: NodeId
) {
136 fn visit_assoc_item(&mut self, i
: &'ast AssocItem
, ctxt
: AssocCtxt
) {
137 walk_assoc_item(self, i
, ctxt
)
139 fn visit_trait_ref(&mut self, t
: &'ast TraitRef
) {
140 walk_trait_ref(self, t
)
142 fn visit_param_bound(&mut self, bounds
: &'ast GenericBound
) {
143 walk_param_bound(self, bounds
)
145 fn visit_poly_trait_ref(&mut self, t
: &'ast PolyTraitRef
, m
: &'ast TraitBoundModifier
) {
146 walk_poly_trait_ref(self, t
, m
)
148 fn visit_variant_data(&mut self, s
: &'ast VariantData
) {
149 walk_struct_def(self, s
)
151 fn visit_struct_field(&mut self, s
: &'ast StructField
) {
152 walk_struct_field(self, s
)
156 enum_definition
: &'ast EnumDef
,
157 generics
: &'ast Generics
,
161 walk_enum_def(self, enum_definition
, generics
, item_id
)
163 fn visit_variant(&mut self, v
: &'ast Variant
) {
164 walk_variant(self, v
)
166 fn visit_label(&mut self, label
: &'ast Label
) {
167 walk_label(self, label
)
169 fn visit_lifetime(&mut self, lifetime
: &'ast Lifetime
) {
170 walk_lifetime(self, lifetime
)
172 fn visit_mac(&mut self, _mac
: &'ast MacCall
) {
173 panic
!("visit_mac disabled by default");
174 // N.B., see note about macros above.
175 // if you really want a visitor that
176 // works on macros, use this
177 // definition in your trait impl:
178 // visit::walk_mac(self, _mac)
180 fn visit_mac_def(&mut self, _mac
: &'ast MacroDef
, _id
: NodeId
) {
183 fn visit_path(&mut self, path
: &'ast Path
, _id
: NodeId
) {
184 walk_path(self, path
)
186 fn visit_use_tree(&mut self, use_tree
: &'ast UseTree
, id
: NodeId
, _nested
: bool
) {
187 walk_use_tree(self, use_tree
, id
)
189 fn visit_path_segment(&mut self, path_span
: Span
, path_segment
: &'ast PathSegment
) {
190 walk_path_segment(self, path_span
, path_segment
)
192 fn visit_generic_args(&mut self, path_span
: Span
, generic_args
: &'ast GenericArgs
) {
193 walk_generic_args(self, path_span
, generic_args
)
195 fn visit_generic_arg(&mut self, generic_arg
: &'ast GenericArg
) {
197 GenericArg
::Lifetime(lt
) => self.visit_lifetime(lt
),
198 GenericArg
::Type(ty
) => self.visit_ty(ty
),
199 GenericArg
::Const(ct
) => self.visit_anon_const(ct
),
202 fn visit_assoc_ty_constraint(&mut self, constraint
: &'ast AssocTyConstraint
) {
203 walk_assoc_ty_constraint(self, constraint
)
205 fn visit_attribute(&mut self, attr
: &'ast Attribute
) {
206 walk_attribute(self, attr
)
208 fn visit_tt(&mut self, tt
: TokenTree
) {
211 fn visit_tts(&mut self, tts
: TokenStream
) {
214 fn visit_token(&mut self, _t
: Token
) {}
215 // FIXME: add `visit_interpolated` and `walk_interpolated`
216 fn visit_vis(&mut self, vis
: &'ast Visibility
) {
219 fn visit_fn_ret_ty(&mut self, ret_ty
: &'ast FnRetTy
) {
220 walk_fn_ret_ty(self, ret_ty
)
222 fn visit_fn_header(&mut self, _header
: &'ast FnHeader
) {
225 fn visit_field(&mut self, f
: &'ast Field
) {
228 fn visit_field_pattern(&mut self, fp
: &'ast FieldPat
) {
229 walk_field_pattern(self, fp
)
234 macro_rules
! walk_list
{
235 ($visitor
: expr
, $method
: ident
, $list
: expr
) => {
237 $visitor
.$
method(elem
)
240 ($visitor
: expr
, $method
: ident
, $list
: expr
, $
($extra_args
: expr
),*) => {
242 $visitor
.$
method(elem
, $
($extra_args
,)*)
247 pub fn walk_ident
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, ident
: Ident
) {
248 visitor
.visit_name(ident
.span
, ident
.name
);
251 pub fn walk_crate
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, krate
: &'a Crate
) {
252 visitor
.visit_mod(&krate
.module
, krate
.span
, &krate
.attrs
, CRATE_NODE_ID
);
253 walk_list
!(visitor
, visit_attribute
, &krate
.attrs
);
256 pub fn walk_mod
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, module
: &'a Mod
) {
257 walk_list
!(visitor
, visit_item
, &module
.items
);
260 pub fn walk_local
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, local
: &'a Local
) {
261 for attr
in local
.attrs
.iter() {
262 visitor
.visit_attribute(attr
);
264 visitor
.visit_pat(&local
.pat
);
265 walk_list
!(visitor
, visit_ty
, &local
.ty
);
266 walk_list
!(visitor
, visit_expr
, &local
.init
);
269 pub fn walk_label
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, label
: &'a Label
) {
270 visitor
.visit_ident(label
.ident
);
273 pub fn walk_lifetime
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, lifetime
: &'a Lifetime
) {
274 visitor
.visit_ident(lifetime
.ident
);
277 pub fn walk_poly_trait_ref
<'a
, V
>(
279 trait_ref
: &'a PolyTraitRef
,
280 _
: &TraitBoundModifier
,
284 walk_list
!(visitor
, visit_generic_param
, &trait_ref
.bound_generic_params
);
285 visitor
.visit_trait_ref(&trait_ref
.trait_ref
);
288 pub fn walk_trait_ref
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, trait_ref
: &'a TraitRef
) {
289 visitor
.visit_path(&trait_ref
.path
, trait_ref
.ref_id
)
292 pub fn walk_item
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, item
: &'a Item
) {
293 visitor
.visit_vis(&item
.vis
);
294 visitor
.visit_ident(item
.ident
);
296 ItemKind
::ExternCrate(orig_name
) => {
297 if let Some(orig_name
) = orig_name
{
298 visitor
.visit_name(item
.span
, orig_name
);
301 ItemKind
::Use(ref use_tree
) => visitor
.visit_use_tree(use_tree
, item
.id
, false),
302 ItemKind
::Static(ref typ
, _
, ref expr
) | ItemKind
::Const(_
, ref typ
, ref expr
) => {
303 visitor
.visit_ty(typ
);
304 walk_list
!(visitor
, visit_expr
, expr
);
306 ItemKind
::Fn(_
, ref sig
, ref generics
, ref body
) => {
307 visitor
.visit_generics(generics
);
308 let kind
= FnKind
::Fn(FnCtxt
::Free
, item
.ident
, sig
, &item
.vis
, body
.as_deref());
309 visitor
.visit_fn(kind
, item
.span
, item
.id
)
311 ItemKind
::Mod(ref module
) => visitor
.visit_mod(module
, item
.span
, &item
.attrs
, item
.id
),
312 ItemKind
::ForeignMod(ref foreign_module
) => {
313 walk_list
!(visitor
, visit_foreign_item
, &foreign_module
.items
);
315 ItemKind
::GlobalAsm(ref ga
) => visitor
.visit_global_asm(ga
),
316 ItemKind
::TyAlias(_
, ref generics
, ref bounds
, ref ty
) => {
317 visitor
.visit_generics(generics
);
318 walk_list
!(visitor
, visit_param_bound
, bounds
);
319 walk_list
!(visitor
, visit_ty
, ty
);
321 ItemKind
::Enum(ref enum_definition
, ref generics
) => {
322 visitor
.visit_generics(generics
);
323 visitor
.visit_enum_def(enum_definition
, generics
, item
.id
, item
.span
)
335 visitor
.visit_generics(generics
);
336 walk_list
!(visitor
, visit_trait_ref
, of_trait
);
337 visitor
.visit_ty(self_ty
);
338 walk_list
!(visitor
, visit_assoc_item
, items
, AssocCtxt
::Impl
);
340 ItemKind
::Struct(ref struct_definition
, ref generics
)
341 | ItemKind
::Union(ref struct_definition
, ref generics
) => {
342 visitor
.visit_generics(generics
);
343 visitor
.visit_variant_data(struct_definition
);
345 ItemKind
::Trait(.., ref generics
, ref bounds
, ref items
) => {
346 visitor
.visit_generics(generics
);
347 walk_list
!(visitor
, visit_param_bound
, bounds
);
348 walk_list
!(visitor
, visit_assoc_item
, items
, AssocCtxt
::Trait
);
350 ItemKind
::TraitAlias(ref generics
, ref bounds
) => {
351 visitor
.visit_generics(generics
);
352 walk_list
!(visitor
, visit_param_bound
, bounds
);
354 ItemKind
::MacCall(ref mac
) => visitor
.visit_mac(mac
),
355 ItemKind
::MacroDef(ref ts
) => visitor
.visit_mac_def(ts
, item
.id
),
357 walk_list
!(visitor
, visit_attribute
, &item
.attrs
);
360 pub fn walk_enum_def
<'a
, V
: Visitor
<'a
>>(
362 enum_definition
: &'a EnumDef
,
366 walk_list
!(visitor
, visit_variant
, &enum_definition
.variants
);
369 pub fn walk_variant
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, variant
: &'a Variant
)
373 visitor
.visit_ident(variant
.ident
);
374 visitor
.visit_vis(&variant
.vis
);
375 visitor
.visit_variant_data(&variant
.data
);
376 walk_list
!(visitor
, visit_anon_const
, &variant
.disr_expr
);
377 walk_list
!(visitor
, visit_attribute
, &variant
.attrs
);
380 pub fn walk_field
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, f
: &'a Field
) {
381 visitor
.visit_expr(&f
.expr
);
382 visitor
.visit_ident(f
.ident
);
383 walk_list
!(visitor
, visit_attribute
, f
.attrs
.iter());
386 pub fn walk_field_pattern
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, fp
: &'a FieldPat
) {
387 visitor
.visit_ident(fp
.ident
);
388 visitor
.visit_pat(&fp
.pat
);
389 walk_list
!(visitor
, visit_attribute
, fp
.attrs
.iter());
392 pub fn walk_ty
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, typ
: &'a Ty
) {
394 TyKind
::Slice(ref ty
) | TyKind
::Paren(ref ty
) => visitor
.visit_ty(ty
),
395 TyKind
::Ptr(ref mutable_type
) => visitor
.visit_ty(&mutable_type
.ty
),
396 TyKind
::Rptr(ref opt_lifetime
, ref mutable_type
) => {
397 walk_list
!(visitor
, visit_lifetime
, opt_lifetime
);
398 visitor
.visit_ty(&mutable_type
.ty
)
400 TyKind
::Tup(ref tuple_element_types
) => {
401 walk_list
!(visitor
, visit_ty
, tuple_element_types
);
403 TyKind
::BareFn(ref function_declaration
) => {
404 walk_list
!(visitor
, visit_generic_param
, &function_declaration
.generic_params
);
405 walk_fn_decl(visitor
, &function_declaration
.decl
);
407 TyKind
::Path(ref maybe_qself
, ref path
) => {
408 if let Some(ref qself
) = *maybe_qself
{
409 visitor
.visit_ty(&qself
.ty
);
411 visitor
.visit_path(path
, typ
.id
);
413 TyKind
::Array(ref ty
, ref length
) => {
414 visitor
.visit_ty(ty
);
415 visitor
.visit_anon_const(length
)
417 TyKind
::TraitObject(ref bounds
, ..) | TyKind
::ImplTrait(_
, ref bounds
) => {
418 walk_list
!(visitor
, visit_param_bound
, bounds
);
420 TyKind
::Typeof(ref expression
) => visitor
.visit_anon_const(expression
),
421 TyKind
::Infer
| TyKind
::ImplicitSelf
| TyKind
::Err
=> {}
422 TyKind
::MacCall(ref mac
) => visitor
.visit_mac(mac
),
423 TyKind
::Never
| TyKind
::CVarArgs
=> {}
427 pub fn walk_path
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, path
: &'a Path
) {
428 for segment
in &path
.segments
{
429 visitor
.visit_path_segment(path
.span
, segment
);
433 pub fn walk_use_tree
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, use_tree
: &'a UseTree
, id
: NodeId
) {
434 visitor
.visit_path(&use_tree
.prefix
, id
);
435 match use_tree
.kind
{
436 UseTreeKind
::Simple(rename
, ..) => {
437 // The extra IDs are handled during HIR lowering.
438 if let Some(rename
) = rename
{
439 visitor
.visit_ident(rename
);
442 UseTreeKind
::Glob
=> {}
443 UseTreeKind
::Nested(ref use_trees
) => {
444 for &(ref nested_tree
, nested_id
) in use_trees
{
445 visitor
.visit_use_tree(nested_tree
, nested_id
, true);
451 pub fn walk_path_segment
<'a
, V
: Visitor
<'a
>>(
454 segment
: &'a PathSegment
,
456 visitor
.visit_ident(segment
.ident
);
457 if let Some(ref args
) = segment
.args
{
458 visitor
.visit_generic_args(path_span
, args
);
462 pub fn walk_generic_args
<'a
, V
>(visitor
: &mut V
, _path_span
: Span
, generic_args
: &'a GenericArgs
)
466 match *generic_args
{
467 GenericArgs
::AngleBracketed(ref data
) => {
468 for arg
in &data
.args
{
470 AngleBracketedArg
::Arg(a
) => visitor
.visit_generic_arg(a
),
471 AngleBracketedArg
::Constraint(c
) => visitor
.visit_assoc_ty_constraint(c
),
475 GenericArgs
::Parenthesized(ref data
) => {
476 walk_list
!(visitor
, visit_ty
, &data
.inputs
);
477 walk_fn_ret_ty(visitor
, &data
.output
);
482 pub fn walk_assoc_ty_constraint
<'a
, V
: Visitor
<'a
>>(
484 constraint
: &'a AssocTyConstraint
,
486 visitor
.visit_ident(constraint
.ident
);
487 match constraint
.kind
{
488 AssocTyConstraintKind
::Equality { ref ty }
=> {
489 visitor
.visit_ty(ty
);
491 AssocTyConstraintKind
::Bound { ref bounds }
=> {
492 walk_list
!(visitor
, visit_param_bound
, bounds
);
497 pub fn walk_pat
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, pattern
: &'a Pat
) {
499 PatKind
::TupleStruct(ref path
, ref elems
) => {
500 visitor
.visit_path(path
, pattern
.id
);
501 walk_list
!(visitor
, visit_pat
, elems
);
503 PatKind
::Path(ref opt_qself
, ref path
) => {
504 if let Some(ref qself
) = *opt_qself
{
505 visitor
.visit_ty(&qself
.ty
);
507 visitor
.visit_path(path
, pattern
.id
)
509 PatKind
::Struct(ref path
, ref fields
, _
) => {
510 visitor
.visit_path(path
, pattern
.id
);
511 walk_list
!(visitor
, visit_field_pattern
, fields
);
513 PatKind
::Box(ref subpattern
)
514 | PatKind
::Ref(ref subpattern
, _
)
515 | PatKind
::Paren(ref subpattern
) => visitor
.visit_pat(subpattern
),
516 PatKind
::Ident(_
, ident
, ref optional_subpattern
) => {
517 visitor
.visit_ident(ident
);
518 walk_list
!(visitor
, visit_pat
, optional_subpattern
);
520 PatKind
::Lit(ref expression
) => visitor
.visit_expr(expression
),
521 PatKind
::Range(ref lower_bound
, ref upper_bound
, _
) => {
522 walk_list
!(visitor
, visit_expr
, lower_bound
);
523 walk_list
!(visitor
, visit_expr
, upper_bound
);
525 PatKind
::Wild
| PatKind
::Rest
=> {}
526 PatKind
::Tuple(ref elems
) | PatKind
::Slice(ref elems
) | PatKind
::Or(ref elems
) => {
527 walk_list
!(visitor
, visit_pat
, elems
);
529 PatKind
::MacCall(ref mac
) => visitor
.visit_mac(mac
),
533 pub fn walk_foreign_item
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, item
: &'a ForeignItem
) {
534 let Item { id, span, ident, ref vis, ref attrs, ref kind, tokens: _ }
= *item
;
535 visitor
.visit_vis(vis
);
536 visitor
.visit_ident(ident
);
537 walk_list
!(visitor
, visit_attribute
, attrs
);
539 ForeignItemKind
::Static(ty
, _
, expr
) => {
540 visitor
.visit_ty(ty
);
541 walk_list
!(visitor
, visit_expr
, expr
);
543 ForeignItemKind
::Fn(_
, sig
, generics
, body
) => {
544 visitor
.visit_generics(generics
);
545 let kind
= FnKind
::Fn(FnCtxt
::Foreign
, ident
, sig
, vis
, body
.as_deref());
546 visitor
.visit_fn(kind
, span
, id
);
548 ForeignItemKind
::TyAlias(_
, generics
, bounds
, ty
) => {
549 visitor
.visit_generics(generics
);
550 walk_list
!(visitor
, visit_param_bound
, bounds
);
551 walk_list
!(visitor
, visit_ty
, ty
);
553 ForeignItemKind
::MacCall(mac
) => {
554 visitor
.visit_mac(mac
);
559 pub fn walk_global_asm
<'a
, V
: Visitor
<'a
>>(_
: &mut V
, _
: &'a GlobalAsm
) {
563 pub fn walk_param_bound
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, bound
: &'a GenericBound
) {
565 GenericBound
::Trait(ref typ
, ref modifier
) => visitor
.visit_poly_trait_ref(typ
, modifier
),
566 GenericBound
::Outlives(ref lifetime
) => visitor
.visit_lifetime(lifetime
),
570 pub fn walk_generic_param
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, param
: &'a GenericParam
) {
571 visitor
.visit_ident(param
.ident
);
572 walk_list
!(visitor
, visit_attribute
, param
.attrs
.iter());
573 walk_list
!(visitor
, visit_param_bound
, ¶m
.bounds
);
575 GenericParamKind
::Lifetime
=> (),
576 GenericParamKind
::Type { ref default }
=> walk_list
!(visitor
, visit_ty
, default),
577 GenericParamKind
::Const { ref ty, .. }
=> visitor
.visit_ty(ty
),
581 pub fn walk_generics
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, generics
: &'a Generics
) {
582 walk_list
!(visitor
, visit_generic_param
, &generics
.params
);
583 walk_list
!(visitor
, visit_where_predicate
, &generics
.where_clause
.predicates
);
586 pub fn walk_where_predicate
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, predicate
: &'a WherePredicate
) {
588 WherePredicate
::BoundPredicate(WhereBoundPredicate
{
591 ref bound_generic_params
,
594 visitor
.visit_ty(bounded_ty
);
595 walk_list
!(visitor
, visit_param_bound
, bounds
);
596 walk_list
!(visitor
, visit_generic_param
, bound_generic_params
);
598 WherePredicate
::RegionPredicate(WhereRegionPredicate
{
599 ref lifetime
, ref bounds
, ..
601 visitor
.visit_lifetime(lifetime
);
602 walk_list
!(visitor
, visit_param_bound
, bounds
);
604 WherePredicate
::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty, .. }
) => {
605 visitor
.visit_ty(lhs_ty
);
606 visitor
.visit_ty(rhs_ty
);
611 pub fn walk_fn_ret_ty
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, ret_ty
: &'a FnRetTy
) {
612 if let FnRetTy
::Ty(ref output_ty
) = *ret_ty
{
613 visitor
.visit_ty(output_ty
)
617 pub fn walk_fn_decl
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, function_declaration
: &'a FnDecl
) {
618 for param
in &function_declaration
.inputs
{
619 visitor
.visit_param(param
);
621 visitor
.visit_fn_ret_ty(&function_declaration
.output
);
624 pub fn walk_fn
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, kind
: FnKind
<'a
>, _span
: Span
) {
626 FnKind
::Fn(_
, _
, sig
, _
, body
) => {
627 visitor
.visit_fn_header(&sig
.header
);
628 walk_fn_decl(visitor
, &sig
.decl
);
629 walk_list
!(visitor
, visit_block
, body
);
631 FnKind
::Closure(decl
, body
) => {
632 walk_fn_decl(visitor
, decl
);
633 visitor
.visit_expr(body
);
638 pub fn walk_assoc_item
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, item
: &'a AssocItem
, ctxt
: AssocCtxt
) {
639 let Item { id, span, ident, ref vis, ref attrs, ref kind, tokens: _ }
= *item
;
640 visitor
.visit_vis(vis
);
641 visitor
.visit_ident(ident
);
642 walk_list
!(visitor
, visit_attribute
, attrs
);
644 AssocItemKind
::Const(_
, ty
, expr
) => {
645 visitor
.visit_ty(ty
);
646 walk_list
!(visitor
, visit_expr
, expr
);
648 AssocItemKind
::Fn(_
, sig
, generics
, body
) => {
649 visitor
.visit_generics(generics
);
650 let kind
= FnKind
::Fn(FnCtxt
::Assoc(ctxt
), ident
, sig
, vis
, body
.as_deref());
651 visitor
.visit_fn(kind
, span
, id
);
653 AssocItemKind
::TyAlias(_
, generics
, bounds
, ty
) => {
654 visitor
.visit_generics(generics
);
655 walk_list
!(visitor
, visit_param_bound
, bounds
);
656 walk_list
!(visitor
, visit_ty
, ty
);
658 AssocItemKind
::MacCall(mac
) => {
659 visitor
.visit_mac(mac
);
664 pub fn walk_struct_def
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, struct_definition
: &'a VariantData
) {
665 walk_list
!(visitor
, visit_struct_field
, struct_definition
.fields());
668 pub fn walk_struct_field
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, struct_field
: &'a StructField
) {
669 visitor
.visit_vis(&struct_field
.vis
);
670 if let Some(ident
) = struct_field
.ident
{
671 visitor
.visit_ident(ident
);
673 visitor
.visit_ty(&struct_field
.ty
);
674 walk_list
!(visitor
, visit_attribute
, &struct_field
.attrs
);
677 pub fn walk_block
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, block
: &'a Block
) {
678 walk_list
!(visitor
, visit_stmt
, &block
.stmts
);
681 pub fn walk_stmt
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, statement
: &'a Stmt
) {
682 match statement
.kind
{
683 StmtKind
::Local(ref local
) => visitor
.visit_local(local
),
684 StmtKind
::Item(ref item
) => visitor
.visit_item(item
),
685 StmtKind
::Expr(ref expr
) | StmtKind
::Semi(ref expr
) => visitor
.visit_expr(expr
),
686 StmtKind
::Empty
=> {}
687 StmtKind
::MacCall(ref mac
) => {
688 let (ref mac
, _
, ref attrs
) = **mac
;
689 visitor
.visit_mac(mac
);
690 for attr
in attrs
.iter() {
691 visitor
.visit_attribute(attr
);
697 pub fn walk_mac
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, mac
: &'a MacCall
) {
698 visitor
.visit_path(&mac
.path
, DUMMY_NODE_ID
);
701 pub fn walk_anon_const
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, constant
: &'a AnonConst
) {
702 visitor
.visit_expr(&constant
.value
);
705 pub fn walk_expr
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, expression
: &'a Expr
) {
706 walk_list
!(visitor
, visit_attribute
, expression
.attrs
.iter());
708 match expression
.kind
{
709 ExprKind
::Box(ref subexpression
) => visitor
.visit_expr(subexpression
),
710 ExprKind
::Array(ref subexpressions
) => {
711 walk_list
!(visitor
, visit_expr
, subexpressions
);
713 ExprKind
::Repeat(ref element
, ref count
) => {
714 visitor
.visit_expr(element
);
715 visitor
.visit_anon_const(count
)
717 ExprKind
::Struct(ref path
, ref fields
, ref optional_base
) => {
718 visitor
.visit_path(path
, expression
.id
);
719 walk_list
!(visitor
, visit_field
, fields
);
720 walk_list
!(visitor
, visit_expr
, optional_base
);
722 ExprKind
::Tup(ref subexpressions
) => {
723 walk_list
!(visitor
, visit_expr
, subexpressions
);
725 ExprKind
::Call(ref callee_expression
, ref arguments
) => {
726 visitor
.visit_expr(callee_expression
);
727 walk_list
!(visitor
, visit_expr
, arguments
);
729 ExprKind
::MethodCall(ref segment
, ref arguments
, _span
) => {
730 visitor
.visit_path_segment(expression
.span
, segment
);
731 walk_list
!(visitor
, visit_expr
, arguments
);
733 ExprKind
::Binary(_
, ref left_expression
, ref right_expression
) => {
734 visitor
.visit_expr(left_expression
);
735 visitor
.visit_expr(right_expression
)
737 ExprKind
::AddrOf(_
, _
, ref subexpression
) | ExprKind
::Unary(_
, ref subexpression
) => {
738 visitor
.visit_expr(subexpression
)
740 ExprKind
::Cast(ref subexpression
, ref typ
) | ExprKind
::Type(ref subexpression
, ref typ
) => {
741 visitor
.visit_expr(subexpression
);
742 visitor
.visit_ty(typ
)
744 ExprKind
::Let(ref pat
, ref scrutinee
) => {
745 visitor
.visit_pat(pat
);
746 visitor
.visit_expr(scrutinee
);
748 ExprKind
::If(ref head_expression
, ref if_block
, ref optional_else
) => {
749 visitor
.visit_expr(head_expression
);
750 visitor
.visit_block(if_block
);
751 walk_list
!(visitor
, visit_expr
, optional_else
);
753 ExprKind
::While(ref subexpression
, ref block
, ref opt_label
) => {
754 walk_list
!(visitor
, visit_label
, opt_label
);
755 visitor
.visit_expr(subexpression
);
756 visitor
.visit_block(block
);
758 ExprKind
::ForLoop(ref pattern
, ref subexpression
, ref block
, ref opt_label
) => {
759 walk_list
!(visitor
, visit_label
, opt_label
);
760 visitor
.visit_pat(pattern
);
761 visitor
.visit_expr(subexpression
);
762 visitor
.visit_block(block
);
764 ExprKind
::Loop(ref block
, ref opt_label
) => {
765 walk_list
!(visitor
, visit_label
, opt_label
);
766 visitor
.visit_block(block
);
768 ExprKind
::Match(ref subexpression
, ref arms
) => {
769 visitor
.visit_expr(subexpression
);
770 walk_list
!(visitor
, visit_arm
, arms
);
772 ExprKind
::Closure(_
, _
, _
, ref decl
, ref body
, _decl_span
) => {
773 visitor
.visit_fn(FnKind
::Closure(decl
, body
), expression
.span
, expression
.id
)
775 ExprKind
::Block(ref block
, ref opt_label
) => {
776 walk_list
!(visitor
, visit_label
, opt_label
);
777 visitor
.visit_block(block
);
779 ExprKind
::Async(_
, _
, ref body
) => {
780 visitor
.visit_block(body
);
782 ExprKind
::Await(ref expr
) => visitor
.visit_expr(expr
),
783 ExprKind
::Assign(ref lhs
, ref rhs
, _
) => {
784 visitor
.visit_expr(lhs
);
785 visitor
.visit_expr(rhs
);
787 ExprKind
::AssignOp(_
, ref left_expression
, ref right_expression
) => {
788 visitor
.visit_expr(left_expression
);
789 visitor
.visit_expr(right_expression
);
791 ExprKind
::Field(ref subexpression
, ident
) => {
792 visitor
.visit_expr(subexpression
);
793 visitor
.visit_ident(ident
);
795 ExprKind
::Index(ref main_expression
, ref index_expression
) => {
796 visitor
.visit_expr(main_expression
);
797 visitor
.visit_expr(index_expression
)
799 ExprKind
::Range(ref start
, ref end
, _
) => {
800 walk_list
!(visitor
, visit_expr
, start
);
801 walk_list
!(visitor
, visit_expr
, end
);
803 ExprKind
::Path(ref maybe_qself
, ref path
) => {
804 if let Some(ref qself
) = *maybe_qself
{
805 visitor
.visit_ty(&qself
.ty
);
807 visitor
.visit_path(path
, expression
.id
)
809 ExprKind
::Break(ref opt_label
, ref opt_expr
) => {
810 walk_list
!(visitor
, visit_label
, opt_label
);
811 walk_list
!(visitor
, visit_expr
, opt_expr
);
813 ExprKind
::Continue(ref opt_label
) => {
814 walk_list
!(visitor
, visit_label
, opt_label
);
816 ExprKind
::Ret(ref optional_expression
) => {
817 walk_list
!(visitor
, visit_expr
, optional_expression
);
819 ExprKind
::MacCall(ref mac
) => visitor
.visit_mac(mac
),
820 ExprKind
::Paren(ref subexpression
) => visitor
.visit_expr(subexpression
),
821 ExprKind
::InlineAsm(ref ia
) => {
822 for (op
, _
) in &ia
.operands
{
824 InlineAsmOperand
::In { expr, .. }
825 | InlineAsmOperand
::InOut { expr, .. }
826 | InlineAsmOperand
::Const { expr, .. }
827 | InlineAsmOperand
::Sym { expr, .. }
=> visitor
.visit_expr(expr
),
828 InlineAsmOperand
::Out { expr, .. }
=> {
829 if let Some(expr
) = expr
{
830 visitor
.visit_expr(expr
);
833 InlineAsmOperand
::SplitInOut { in_expr, out_expr, .. }
=> {
834 visitor
.visit_expr(in_expr
);
835 if let Some(out_expr
) = out_expr
{
836 visitor
.visit_expr(out_expr
);
842 ExprKind
::LlvmInlineAsm(ref ia
) => {
843 for &(_
, ref input
) in &ia
.inputs
{
844 visitor
.visit_expr(input
)
846 for output
in &ia
.outputs
{
847 visitor
.visit_expr(&output
.expr
)
850 ExprKind
::Yield(ref optional_expression
) => {
851 walk_list
!(visitor
, visit_expr
, optional_expression
);
853 ExprKind
::Try(ref subexpression
) => visitor
.visit_expr(subexpression
),
854 ExprKind
::TryBlock(ref body
) => visitor
.visit_block(body
),
855 ExprKind
::Lit(_
) | ExprKind
::Err
=> {}
858 visitor
.visit_expr_post(expression
)
861 pub fn walk_param
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, param
: &'a Param
) {
862 walk_list
!(visitor
, visit_attribute
, param
.attrs
.iter());
863 visitor
.visit_pat(¶m
.pat
);
864 visitor
.visit_ty(¶m
.ty
);
867 pub fn walk_arm
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, arm
: &'a Arm
) {
868 visitor
.visit_pat(&arm
.pat
);
869 walk_list
!(visitor
, visit_expr
, &arm
.guard
);
870 visitor
.visit_expr(&arm
.body
);
871 walk_list
!(visitor
, visit_attribute
, &arm
.attrs
);
874 pub fn walk_vis
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, vis
: &'a Visibility
) {
875 if let VisibilityKind
::Restricted { ref path, id }
= vis
.node
{
876 visitor
.visit_path(path
, id
);
880 pub fn walk_attribute
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, attr
: &'a Attribute
) {
882 AttrKind
::Normal(ref item
) => walk_mac_args(visitor
, &item
.args
),
883 AttrKind
::DocComment(_
) => {}
887 pub fn walk_mac_args
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, args
: &'a MacArgs
) {
890 MacArgs
::Delimited(_dspan
, _delim
, tokens
) => visitor
.visit_tts(tokens
.clone()),
891 MacArgs
::Eq(_eq_span
, tokens
) => visitor
.visit_tts(tokens
.clone()),
895 pub fn walk_tt
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, tt
: TokenTree
) {
897 TokenTree
::Token(token
) => visitor
.visit_token(token
),
898 TokenTree
::Delimited(_
, _
, tts
) => visitor
.visit_tts(tts
),
902 pub fn walk_tts
<'a
, V
: Visitor
<'a
>>(visitor
: &mut V
, tts
: TokenStream
) {
903 for tt
in tts
.trees() {
904 visitor
.visit_tt(tt
);