1//! AST walker. Each overridden visit method has full control over what2//! 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 prevent4//! deeper traversal by doing nothing.5//!6//! Note: it is an important invariant that the default visitor walks the body7//! of a function in "execution order" (more concretely, reverse post-order8//! with respect to the CFG implied by the AST), meaning that if AST node A may9//! execute before AST node B, then A is visited first. The borrow checker in10//! particular relies on this property.11//!12//! Note: walking an AST before macro expansion is probably a bad idea. For13//! instance, a walker looking for item names in a module will miss all of14//! those that are created by the expansion of a macro.1516pub use rustc_ast_ir::visit::VisitorResult;17pub use rustc_ast_ir::{try_visit, visit_opt, walk_list, walk_visitable_list};18use rustc_span::{Ident, Span, Spanned, Symbol};19use thin_vec::ThinVec;2021use crate::ast::*;22use crate::tokenstream::DelimSpan;2324#[derive(Copy, Clone, Debug, PartialEq)]25pub enum AssocCtxt {26 Trait,27 Impl { of_trait: bool },28}2930#[derive(Copy, Clone, Debug, PartialEq)]31pub enum FnCtxt {32 Free,33 Foreign,34 Assoc(AssocCtxt),35}3637#[derive(Copy, Clone, Debug)]38pub enum BoundKind {39 /// Trait bounds in generics bounds and type/trait alias.40 /// E.g., `<T: Bound>`, `type A: Bound`, or `where T: Bound`.41 Bound,4243 /// Trait bounds in `impl` type.44 /// E.g., `type Foo = impl Bound1 + Bound2 + Bound3`.45 Impl,4647 /// Trait bounds in trait object type.48 /// E.g., `dyn Bound1 + Bound2 + Bound3`.49 TraitObject,5051 /// Super traits of a trait.52 /// E.g., `trait A: B`53 SuperTraits,54}55impl BoundKind {56 pub fn descr(self) -> &'static str {57 match self {58 BoundKind::Bound => "bounds",59 BoundKind::Impl => "`impl Trait`",60 BoundKind::TraitObject => "`dyn` trait object bounds",61 BoundKind::SuperTraits => "supertrait bounds",62 }63 }64}6566#[derive(Copy, Clone, Debug)]67pub enum LifetimeCtxt {68 /// Appears in a reference type.69 Ref,70 /// Appears as a bound on a type or another lifetime.71 Bound,72 /// Appears as a generic argument.73 GenericArg,74}7576pub(crate) trait Visitable<'a, V: Visitor<'a>> {77 type Extra: Copy;7879 #[must_use]80 fn visit(&'a self, visitor: &mut V, extra: Self::Extra) -> V::Result;81}8283impl<'a, V: Visitor<'a>, T: ?Sized> Visitable<'a, V> for Box<T>84where85 T: Visitable<'a, V>,86{87 type Extra = T::Extra;88 fn visit(&'a self, visitor: &mut V, extra: Self::Extra) -> V::Result {89 (**self).visit(visitor, extra)90 }91}9293impl<'a, V: Visitor<'a>, T> Visitable<'a, V> for Option<T>94where95 T: Visitable<'a, V>,96{97 type Extra = T::Extra;98 fn visit(&'a self, visitor: &mut V, extra: Self::Extra) -> V::Result {99 if let Some(this) = self {100 try_visit!(this.visit(visitor, extra));101 }102 V::Result::output()103 }104}105106impl<'a, V: Visitor<'a>, T> Visitable<'a, V> for Spanned<T>107where108 T: Visitable<'a, V>,109{110 type Extra = T::Extra;111 fn visit(&'a self, visitor: &mut V, extra: Self::Extra) -> V::Result {112 let Spanned { span: _, node } = self;113 node.visit(visitor, extra)114 }115}116117impl<'a, V: Visitor<'a>, T> Visitable<'a, V> for [T]118where119 T: Visitable<'a, V>,120{121 type Extra = T::Extra;122 fn visit(&'a self, visitor: &mut V, extra: Self::Extra) -> V::Result {123 for item in self {124 try_visit!(item.visit(visitor, extra));125 }126 V::Result::output()127 }128}129130impl<'a, V: Visitor<'a>, T> Visitable<'a, V> for Vec<T>131where132 T: Visitable<'a, V>,133{134 type Extra = T::Extra;135 fn visit(&'a self, visitor: &mut V, extra: Self::Extra) -> V::Result {136 for item in self {137 try_visit!(item.visit(visitor, extra));138 }139 V::Result::output()140 }141}142143impl<'a, V: Visitor<'a>, T> Visitable<'a, V> for (T,)144where145 T: Visitable<'a, V>,146{147 type Extra = T::Extra;148 fn visit(&'a self, visitor: &mut V, extra: Self::Extra) -> V::Result {149 self.0.visit(visitor, extra)150 }151}152153impl<'a, V: Visitor<'a>, T1, T2> Visitable<'a, V> for (T1, T2)154where155 T1: Visitable<'a, V, Extra = ()>,156 T2: Visitable<'a, V, Extra = ()>,157{158 type Extra = ();159 fn visit(&'a self, visitor: &mut V, extra: Self::Extra) -> V::Result {160 try_visit!(self.0.visit(visitor, extra));161 try_visit!(self.1.visit(visitor, extra));162 V::Result::output()163 }164}165166impl<'a, V: Visitor<'a>, T1, T2, T3> Visitable<'a, V> for (T1, T2, T3)167where168 T1: Visitable<'a, V, Extra = ()>,169 T2: Visitable<'a, V, Extra = ()>,170 T3: Visitable<'a, V, Extra = ()>,171{172 type Extra = ();173 fn visit(&'a self, visitor: &mut V, extra: Self::Extra) -> V::Result {174 try_visit!(self.0.visit(visitor, extra));175 try_visit!(self.1.visit(visitor, extra));176 try_visit!(self.2.visit(visitor, extra));177 V::Result::output()178 }179}180181impl<'a, V: Visitor<'a>, T1, T2, T3, T4> Visitable<'a, V> for (T1, T2, T3, T4)182where183 T1: Visitable<'a, V, Extra = ()>,184 T2: Visitable<'a, V, Extra = ()>,185 T3: Visitable<'a, V, Extra = ()>,186 T4: Visitable<'a, V, Extra = ()>,187{188 type Extra = ();189 fn visit(&'a self, visitor: &mut V, extra: Self::Extra) -> V::Result {190 try_visit!(self.0.visit(visitor, extra));191 try_visit!(self.1.visit(visitor, extra));192 try_visit!(self.2.visit(visitor, extra));193 try_visit!(self.3.visit(visitor, extra));194 V::Result::output()195 }196}197198pub(crate) trait Walkable<'a, V: Visitor<'a>> {199 #[must_use]200 fn walk_ref(&'a self, visitor: &mut V) -> V::Result;201}202203macro_rules! visit_visitable {204 ($visitor:expr, $($expr:expr),* $(,)?) => {{205 $(try_visit!(Visitable::visit($expr, $visitor, ()));)*206 }};207}208209macro_rules! visit_visitable_with {210 ($visitor:expr, $expr:expr, $extra:expr $(,)?) => {211 try_visit!(Visitable::visit($expr, $visitor, $extra))212 };213}214215macro_rules! walk_walkable {216 ($visitor:expr, $expr:expr, ) => {217 Walkable::walk_ref($expr, $visitor)218 };219}220221macro_rules! impl_visitable {222 (|&$lt:lifetime $self:ident: $self_ty:ty,223 $vis:ident: &mut $vis_ty:ident,224 $extra:ident: $extra_ty:ty| $block:block) => {225 #[allow(unused_parens, non_local_definitions)]226 impl<$lt, $vis_ty: Visitor<$lt>> Visitable<$lt, $vis_ty> for $self_ty {227 type Extra = $extra_ty;228 fn visit(&$lt $self, $vis: &mut $vis_ty, $extra: Self::Extra) -> V::Result {229 $block230 }231 }232 };233}234235macro_rules! impl_walkable {236 ($(<$K:ident: $Kb:ident>)? |&$lt:lifetime $self:ident: $self_ty:ty,237 $vis:ident: &mut $vis_ty:ident| $block:block) => {238 #[allow(unused_parens, non_local_definitions)]239 impl<$($K: $Kb,)? $lt, $vis_ty: Visitor<$lt>> Walkable<$lt, $vis_ty> for $self_ty {240 fn walk_ref(&$lt $self, $vis: &mut $vis_ty) -> V::Result {241 $block242 }243 }244 };245}246247macro_rules! impl_visitable_noop {248 (<$lt:lifetime> $($ty:ty,)*) => {249 $(250 impl_visitable!(|&$lt self: $ty, _vis: &mut V, _extra: ()| {251 V::Result::output()252 });253 )*254 };255}256257macro_rules! impl_visitable_list {258 (<$lt:lifetime> $($ty:ty,)*) => {259 $(impl<$lt, V: Visitor<$lt>, T> Visitable<$lt, V> for $ty260 where261 &$lt $ty: IntoIterator<Item = &$lt T>,262 T: $lt + Visitable<$lt, V>,263 {264 type Extra = <T as Visitable<$lt, V>>::Extra;265266 #[inline]267 fn visit(&$lt self, visitor: &mut V, extra: Self::Extra) -> V::Result {268 for i in self {269 try_visit!(i.visit(visitor, extra));270 }271 V::Result::output()272 }273 })*274 };275}276277macro_rules! impl_visitable_direct {278 (<$lt:lifetime> $($ty:ty,)*) => {279 $(impl_visitable!(280 |&$lt self: $ty, visitor: &mut V, _extra: ()| {281 Walkable::walk_ref(self, visitor)282 }283 );)*284 };285}286287macro_rules! impl_visitable_calling_walkable {288 (<$lt:lifetime>289 $( fn $method:ident($ty:ty $(, $extra_name:ident: $extra_ty:ty)?); )*290 ) => {291 $(fn $method(&mut self, node: &$lt $ty $(, $extra_name:$extra_ty)?) -> Self::Result {292 impl_visitable!(|&$lt self: $ty, visitor: &mut V, extra: ($($extra_ty)?)| {293 let ($($extra_name)?) = extra;294 visitor.$method(self $(, $extra_name)?)295 });296 walk_walkable!(self, node, )297 })*298 };299}300301macro_rules! define_named_walk {302 ($Visitor:ident<$lt:lifetime>303 $( pub fn $method:ident($ty:ty); )*304 ) => {305 $(pub fn $method<$lt, V: $Visitor<$lt>>(visitor: &mut V, node: &$lt $ty) -> V::Result {306 walk_walkable!(visitor, node,)307 })*308 };309}310311#[macro_export]312macro_rules! common_visitor_and_walkers {313 ($(($mut: ident))? $Visitor:ident$(<$lt:lifetime>)?) => {314 $(${ignore($lt)}315 #[derive(Copy, Clone)]316 )?317 #[derive(Debug)]318 pub enum FnKind<'a> {319 /// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.320 Fn(FnCtxt, &'a $($mut)? Visibility, &'a $($mut)? Fn),321322 /// E.g., `|x, y| body`.323 Closure(&'a $($mut)? ClosureBinder, &'a $($mut)? Option<CoroutineKind>, &'a $($mut)? Box<FnDecl>, &'a $($mut)? Box<Expr>),324 }325326 impl<'a> FnKind<'a> {327 pub fn header(&'a $($mut)? self) -> Option<&'a $($mut)? FnHeader> {328 match *self {329 FnKind::Fn(_, _, Fn { sig, .. }) => Some(&$($mut)? sig.header),330 FnKind::Closure(..) => None,331 }332 }333334 pub fn ident(&'a $($mut)? self) -> Option<&'a $($mut)? Ident> {335 match self {336 FnKind::Fn(_, _, Fn { ident, .. }) => Some(ident),337 _ => None,338 }339 }340341 pub fn decl(&'a $($mut)? self) -> &'a $($mut)? FnDecl {342 match self {343 FnKind::Fn(_, _, Fn { sig, .. }) => &$($mut)? sig.decl,344 FnKind::Closure(_, _, decl, _) => decl,345 }346 }347348 pub fn ctxt(&self) -> Option<FnCtxt> {349 match self {350 FnKind::Fn(ctxt, ..) => Some(*ctxt),351 FnKind::Closure(..) => None,352 }353 }354 }355356 // This macro generates `impl Visitable` and `impl MutVisitable` that do nothing.357 impl_visitable_noop!(<$($lt)? $($mut)?>358 AttrId,359 bool,360 rustc_span::ByteSymbol,361 char,362 crate::token::CommentKind,363 crate::token::Delimiter,364 crate::token::Lit,365 crate::token::LitKind,366 crate::tokenstream::LazyAttrTokenStream,367 crate::tokenstream::TokenStream,368 EarlyParsedAttribute,369 Movability,370 Mutability,371 Pinnedness,372 Result<(), rustc_span::ErrorGuaranteed>,373 rustc_data_structures::fx::FxHashMap<Symbol, usize>,374 rustc_span::ErrorGuaranteed,375 std::borrow::Cow<'_, str>,376 Symbol,377 u8,378 usize,379 );380 // `Span` is only a no-op for the non-mutable visitor.381 $(impl_visitable_noop!(<$lt> Span,);)?382383 // This macro generates `impl Visitable` and `impl MutVisitable` that simply iterate over384 // their contents. We do not use a generic impl for `ThinVec` because we want to allow385 // custom visits for the `MutVisitor`.386 impl_visitable_list!(<$($lt)? $($mut)?>387 ThinVec<AngleBracketedArg>,388 ThinVec<Attribute>,389 ThinVec<(Ident, Option<Ident>)>,390 ThinVec<(NodeId, Path)>,391 ThinVec<PathSegment>,392 ThinVec<PreciseCapturingArg>,393 ThinVec<Pat>,394 ThinVec<Box<Ty>>,395 ThinVec<TyPat>,396 ThinVec<EiiImpl>,397 );398399 // This macro generates `impl Visitable` and `impl MutVisitable` that forward to `Walkable`400 // or `MutWalkable`. By default, all types that do not have a custom visit method in the401 // visitor should appear here.402 impl_visitable_direct!(<$($lt)? $($mut)?>403 AngleBracketedArg,404 AngleBracketedArgs,405 AsmMacro,406 AssignOpKind,407 AssocItemConstraintKind,408 AttrArgs,409 AttrItem,410 AttrKind,411 AttrStyle,412 FnPtrTy,413 BindingMode,414 GenBlockKind,415 RangeLimits,416 UnsafeBinderCastKind,417 BinOpKind,418 BlockCheckMode,419 MgcaDisambiguation,420 BorrowKind,421 BoundAsyncness,422 BoundConstness,423 BoundPolarity,424 ByRef,425 Closure,426 Const,427 ConstBlockItem,428 ConstItem,429 ConstItemRhsKind,430 Defaultness,431 Delegation,432 DelegationMac,433 DelegationSuffixes,434 DelimArgs,435 DelimSpan,436 EnumDef,437 Extern,438 ForLoopKind,439 FormatArgPosition,440 FormatArgsPiece,441 FormatArgument,442 FormatArgumentKind,443 FormatArguments,444 FormatPlaceholder,445 GenericParamKind,446 Guard,447 Impl,448 ImplPolarity,449 Inline,450 InlineAsmOperand,451 InlineAsmRegOrRegClass,452 InlineAsmTemplatePiece,453 IsAuto,454 LocalKind,455 MacCallStmt,456 MacStmtStyle,457 MatchKind,458 MethodCall,459 ModKind,460 ModSpans,461 MutTy,462 NormalAttr,463 AttrItemKind,464 Parens,465 ParenthesizedArgs,466 PatFieldsRest,467 PatKind,468 RangeEnd,469 RangeSyntax,470 Recovered,471 RestrictionKind,472 Safety,473 StaticItem,474 StrLit,475 StrStyle,476 StructExpr,477 StructRest,478 Term,479 Trait,480 TraitBoundModifiers,481 TraitObjectSyntax,482 TyAlias,483 TyKind,484 TyPatKind,485 UnOp,486 UnsafeBinderTy,487 UnsafeSource,488 UseTreeKind,489 VisibilityKind,490 WhereBoundPredicate,491 WhereClause,492 WhereEqPredicate,493 WhereRegionPredicate,494 YieldKind,495 EiiDecl,496 EiiImpl,497 );498499 /// Each method of this trait is a hook to be potentially500 /// overridden. Each method's default implementation recursively visits501 /// the substructure of the input via the corresponding `walk` method;502 #[doc = concat!(" e.g., the `visit_item` method by default calls `visit"$(, "_", stringify!($mut))?, "::walk_item`.")]503 ///504 /// If you want to ensure that your code handles every variant505 /// explicitly, you need to override each method. (And you also need506 /// to monitor future changes to this trait in case a new method with a507 /// new default implementation gets introduced.)508 ///509 /// Every `walk_*` method uses deconstruction to access fields of structs and510 /// enums. This will result in a compile error if a field is added, which makes511 /// it more likely the appropriate visit call will be added for it.512 pub trait $Visitor<$($lt)?> : Sized $(${ignore($mut)} + MutVisitorResult<Result = ()>)? {513 $(514 ${ignore($lt)}515 /// The result type of the `visit_*` methods. Can be either `()`,516 /// or `ControlFlow<T>`.517 type Result: VisitorResult = ();518 )?519520 // Methods in this trait have one of three forms, with the last two forms521 // only occurring on `MutVisitor`:522 //523 // fn visit_t(&mut self, t: &mut T); // common524 // fn flat_map_t(&mut self, t: T) -> SmallVec<[T; 1]>; // rare525 // fn filter_map_t(&mut self, t: T) -> Option<T>; // rarest526 //527 // When writing these methods, it is better to use destructuring like this:528 //529 // fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {530 // visit_a(a);531 // visit_b(b);532 // }533 //534 // than to use field access like this:535 //536 // fn visit_abc(&mut self, abc: &mut ABC) {537 // visit_a(&mut abc.a);538 // visit_b(&mut abc.b);539 // // ignore abc.c540 // }541 //542 // As well as being more concise, the former is explicit about which fields543 // are skipped. Furthermore, if a new field is added, the destructuring544 // version will cause a compile error, which is good. In comparison, the545 // field access version will continue working and it would be easy to546 // forget to add handling for it.547 fn visit_ident(&mut self, Ident { name: _, span }: &$($lt)? $($mut)? Ident) -> Self::Result {548 impl_visitable!(|&$($lt)? $($mut)? self: Ident, visitor: &mut V, _extra: ()| {549 visitor.visit_ident(self)550 });551 visit_span(self, span)552 }553554 // This macro defines a custom visit method for each listed type.555 // It implements `impl Visitable` and `impl MutVisitable` to call those methods on the556 // visitor.557 impl_visitable_calling_walkable!(<$($lt)? $($mut)?>558 fn visit_anon_const(AnonConst);559 fn visit_arm(Arm);560 //fn visit_assoc_item(AssocItem, _ctxt: AssocCtxt);561 fn visit_assoc_item_constraint(AssocItemConstraint);562 fn visit_attribute(Attribute);563 fn visit_block(Block);564 //fn visit_nested_use_tree((UseTree, NodeId));565 fn visit_capture_by(CaptureBy);566 fn visit_closure_binder(ClosureBinder);567 fn visit_contract(FnContract);568 fn visit_coroutine_kind(CoroutineKind);569 fn visit_crate(Crate);570 fn visit_expr(Expr);571 fn visit_expr_field(ExprField);572 fn visit_field_def(FieldDef);573 fn visit_fn_decl(FnDecl);574 fn visit_fn_header(FnHeader);575 fn visit_fn_ret_ty(FnRetTy);576 //fn visit_foreign_item(ForeignItem);577 fn visit_foreign_mod(ForeignMod);578 fn visit_format_args(FormatArgs);579 fn visit_generic_arg(GenericArg);580 fn visit_generic_args(GenericArgs);581 fn visit_generic_param(GenericParam);582 fn visit_generics(Generics);583 fn visit_inline_asm(InlineAsm);584 fn visit_inline_asm_sym(InlineAsmSym);585 fn visit_impl_restriction(ImplRestriction);586 //fn visit_item(Item);587 fn visit_label(Label);588 fn visit_lifetime(Lifetime, _ctxt: LifetimeCtxt);589 fn visit_local(Local);590 fn visit_mac_call(MacCall);591 fn visit_macro_def(MacroDef);592 fn visit_mut_restriction(MutRestriction);593 fn visit_param_bound(GenericBound, _ctxt: BoundKind);594 fn visit_param(Param);595 fn visit_pat_field(PatField);596 fn visit_path(Path);597 fn visit_path_segment(PathSegment);598 fn visit_pat(Pat);599 fn visit_poly_trait_ref(PolyTraitRef);600 fn visit_precise_capturing_arg(PreciseCapturingArg);601 fn visit_qself(QSelf);602 fn visit_trait_ref(TraitRef);603 fn visit_ty_pat(TyPat);604 fn visit_ty(Ty);605 fn visit_use_tree(UseTree);606 fn visit_variant_data(VariantData);607 fn visit_variant(Variant);608 fn visit_vis(Visibility);609 fn visit_where_predicate_kind(WherePredicateKind);610 fn visit_where_predicate(WherePredicate);611 );612613 // We want `Visitor` to take the `NodeId` by value.614 fn visit_id(&mut self, _id: $(&$mut)? NodeId) -> Self::Result {615 $(impl_visitable!(616 |&$lt self: NodeId, visitor: &mut V, _extra: ()| {617 visitor.visit_id(*self)618 }619 );)?620 $(impl_visitable!(621 |&$mut self: NodeId, visitor: &mut V, _extra: ()| {622 visitor.visit_id(self)623 }624 );)?625 Self::Result::output()626 }627628 /// This method is a hack to workaround unstable of `stmt_expr_attributes`.629 /// It can be removed once that feature is stabilized.630 fn visit_method_receiver_expr(&mut self, ex: &$($lt)? $($mut)? Expr) -> Self::Result {631 self.visit_expr(ex)632 }633634 fn visit_item(&mut self, item: &$($lt)? $($mut)? Item) -> Self::Result {635 impl_visitable!(|&$($lt)? $($mut)? self: Item, vis: &mut V, _extra: ()| {636 vis.visit_item(self)637 });638 walk_item(self, item)639 }640641 fn visit_foreign_item(&mut self, item: &$($lt)? $($mut)? ForeignItem) -> Self::Result {642 impl_visitable!(|&$($lt)? $($mut)? self: ForeignItem, vis: &mut V, _extra: ()| {643 vis.visit_foreign_item(self)644 });645 walk_item(self, item)646 }647648 fn visit_assoc_item(&mut self, item: &$($lt)? $($mut)? AssocItem, ctxt: AssocCtxt) -> Self::Result {649 impl_visitable!(|&$($lt)? $($mut)? self: AssocItem, vis: &mut V, ctxt: AssocCtxt| {650 vis.visit_assoc_item(self, ctxt)651 });652 walk_assoc_item(self, item, ctxt)653 }654655 // for `MutVisitor`: `Span` and `NodeId` are mutated at the caller site.656 fn visit_fn(657 &mut self,658 fk: FnKind<$($lt)? $(${ignore($mut)} '_)?>,659 _: &AttrVec,660 _: Span,661 _: NodeId,662 ) -> Self::Result {663 walk_fn(self, fk)664 }665666 // (non-mut) `Visitor`-only methods667 $(668 fn visit_stmt(&mut self, s: &$lt Stmt) -> Self::Result {669 walk_stmt(self, s)670 }671672 fn visit_nested_use_tree(&mut self, use_tree: &$lt UseTree, id: NodeId) -> Self::Result {673 try_visit!(self.visit_id(id));674 self.visit_use_tree(use_tree)675 }676 )?677678 // `MutVisitor`-only methods679 $(680 // Span visiting is no longer used, but we keep it for now,681 // in case it's needed for something like #127241.682 #[inline]683 fn visit_span(&mut self, _sp: &$mut Span) {684 impl_visitable!(|&mut self: Span, visitor: &mut V, _extra: ()| {685 visitor.visit_span(self)686 });687 // Do nothing.688 }689690 fn flat_map_foreign_item(&mut self, ni: Box<ForeignItem>) -> SmallVec<[Box<ForeignItem>; 1]> {691 walk_flat_map_foreign_item(self, ni)692 }693694 fn flat_map_item(&mut self, i: Box<Item>) -> SmallVec<[Box<Item>; 1]> {695 walk_flat_map_item(self, i)696 }697698 fn flat_map_field_def(&mut self, fd: FieldDef) -> SmallVec<[FieldDef; 1]> {699 walk_flat_map_field_def(self, fd)700 }701702 fn flat_map_assoc_item(703 &mut self,704 i: Box<AssocItem>,705 ctxt: AssocCtxt,706 ) -> SmallVec<[Box<AssocItem>; 1]> {707 walk_flat_map_assoc_item(self, i, ctxt)708 }709710 fn flat_map_stmt(&mut self, s: Stmt) -> SmallVec<[Stmt; 1]> {711 walk_flat_map_stmt(self, s)712 }713714 fn flat_map_arm(&mut self, arm: Arm) -> SmallVec<[Arm; 1]> {715 walk_flat_map_arm(self, arm)716 }717718 fn filter_map_expr(&mut self, e: Box<Expr>) -> Option<Box<Expr>> {719 walk_filter_map_expr(self, e)720 }721722 fn flat_map_variant(&mut self, v: Variant) -> SmallVec<[Variant; 1]> {723 walk_flat_map_variant(self, v)724 }725726 fn flat_map_param(&mut self, param: Param) -> SmallVec<[Param; 1]> {727 walk_flat_map_param(self, param)728 }729730 fn flat_map_generic_param(&mut self, param: GenericParam) -> SmallVec<[GenericParam; 1]> {731 walk_flat_map_generic_param(self, param)732 }733734 fn flat_map_expr_field(&mut self, f: ExprField) -> SmallVec<[ExprField; 1]> {735 walk_flat_map_expr_field(self, f)736 }737738 fn flat_map_where_predicate(739 &mut self,740 where_predicate: WherePredicate,741 ) -> SmallVec<[WherePredicate; 1]> {742 walk_flat_map_where_predicate(self, where_predicate)743 }744745 fn flat_map_pat_field(&mut self, fp: PatField) -> SmallVec<[PatField; 1]> {746 walk_flat_map_pat_field(self, fp)747 }748 )?749 }750751 pub trait WalkItemKind {752 type Ctxt;753 fn walk<$($lt,)? V: $Visitor$(<$lt>)?>(754 &$($lt)? $($mut)? self,755 attrs: &AttrVec,756 span: Span,757 id: NodeId,758 visibility: &$($lt)? $($mut)? Visibility,759 ctxt: Self::Ctxt,760 vis: &mut V,761 ) -> V::Result;762 }763764 // This is only used by the MutVisitor. We include this symmetry here to make writing other765 // functions easier.766 $(${ignore($lt)}767 #[expect(unused, rustc::disallowed_pass_by_ref)]768 #[inline]769 )?770 fn visit_span<$($lt,)? V: $Visitor$(<$lt>)?>(vis: &mut V, span: &$($lt)? $($mut)? Span) -> V::Result {771 $(${ignore($mut)} vis.visit_span(span))?;772 V::Result::output()773 }774775 $(impl_visitable!(|&$lt self: ThinVec<(UseTree, NodeId)>, vis: &mut V, _extra: ()| {776 for (nested_tree, nested_id) in self {777 try_visit!(vis.visit_nested_use_tree(nested_tree, *nested_id));778 }779 V::Result::output()780 });)?781 $(impl_visitable_list!(<$mut> ThinVec<(UseTree, NodeId)>,);)?782783 fn walk_item_inner<$($lt,)? K: WalkItemKind, V: $Visitor$(<$lt>)?>(784 visitor: &mut V,785 item: &$($mut)? $($lt)? Item<K>,786 ctxt: K::Ctxt,787 ) -> V::Result {788 let Item { attrs, id, kind, vis, span, tokens: _ } = item;789 visit_visitable!($($mut)? visitor, id, attrs, vis);790 try_visit!(kind.walk(attrs, *span, *id, vis, ctxt, visitor));791 visit_visitable!($($mut)? visitor, span);792 V::Result::output()793 }794795 // Do not implement `Walkable`/`MutWalkable` for *Item to avoid confusion.796 pub fn walk_item<$($lt,)? K: WalkItemKind<Ctxt = ()>, V: $Visitor$(<$lt>)?>(797 visitor: &mut V,798 item: &$($mut)? $($lt)? Item<K>,799 ) -> V::Result {800 walk_item_inner(visitor, item, ())801 }802803 // Do not implement `Walkable`/`MutWalkable` for *Item to avoid confusion.804 pub fn walk_assoc_item<$($lt,)? K: WalkItemKind<Ctxt = AssocCtxt>, V: $Visitor$(<$lt>)?>(805 visitor: &mut V,806 item: &$($mut)? $($lt)? Item<K>,807 ctxt: AssocCtxt,808 ) -> V::Result {809 walk_item_inner(visitor, item, ctxt)810 }811812 impl WalkItemKind for ItemKind {813 type Ctxt = ();814 fn walk<$($lt,)? V: $Visitor$(<$lt>)?>(815 &$($lt)? $($mut)? self,816 attrs: &AttrVec,817 span: Span,818 id: NodeId,819 visibility: &$($lt)? $($mut)? Visibility,820 _ctxt: Self::Ctxt,821 vis: &mut V,822 ) -> V::Result {823 match self {824 ItemKind::Fn(func) => {825 let kind = FnKind::Fn(FnCtxt::Free, visibility, &$($mut)? *func);826 try_visit!(vis.visit_fn(kind, attrs, span, id));827 }828 ItemKind::ExternCrate(orig_name, ident) =>829 visit_visitable!($($mut)? vis, orig_name, ident),830 ItemKind::Use(use_tree) =>831 visit_visitable!($($mut)? vis, use_tree),832 ItemKind::Static(item) =>833 visit_visitable!($($mut)? vis, item),834 ItemKind::ConstBlock(item) =>835 visit_visitable!($($mut)? vis, item),836 ItemKind::Const(item) =>837 visit_visitable!($($mut)? vis, item),838 ItemKind::Mod(safety, ident, mod_kind) =>839 visit_visitable!($($mut)? vis, safety, ident, mod_kind),840 ItemKind::ForeignMod(nm) =>841 visit_visitable!($($mut)? vis, nm),842 ItemKind::GlobalAsm(asm) =>843 visit_visitable!($($mut)? vis, asm),844 ItemKind::TyAlias(ty_alias) =>845 visit_visitable!($($mut)? vis, ty_alias),846 ItemKind::Enum(ident, generics, enum_definition) =>847 visit_visitable!($($mut)? vis, ident, generics, enum_definition),848 ItemKind::Struct(ident, generics, variant_data)849 | ItemKind::Union(ident, generics, variant_data) =>850 visit_visitable!($($mut)? vis, ident, generics, variant_data),851 ItemKind::Impl(impl_) =>852 visit_visitable!($($mut)? vis, impl_),853 ItemKind::Trait(trait_) =>854 visit_visitable!($($mut)? vis, trait_),855 ItemKind::TraitAlias(TraitAlias { constness, ident, generics, bounds}) => {856 visit_visitable!($($mut)? vis, constness, ident, generics);857 visit_visitable_with!($($mut)? vis, bounds, BoundKind::Bound)858 }859 ItemKind::MacCall(m) =>860 visit_visitable!($($mut)? vis, m),861 ItemKind::MacroDef(ident, def) =>862 visit_visitable!($($mut)? vis, ident, def),863 ItemKind::Delegation(delegation) =>864 visit_visitable!($($mut)? vis, delegation),865 ItemKind::DelegationMac(dm) =>866 visit_visitable!($($mut)? vis, dm),867 }868 V::Result::output()869 }870 }871872 impl WalkItemKind for AssocItemKind {873 type Ctxt = AssocCtxt;874 fn walk<$($lt,)? V: $Visitor$(<$lt>)?>(875 &$($lt)? $($mut)? self,876 attrs: &AttrVec,877 span: Span,878 id: NodeId,879 visibility: &$($lt)? $($mut)? Visibility,880 ctxt: Self::Ctxt,881 vis: &mut V,882 ) -> V::Result {883 match self {884 AssocItemKind::Const(item) =>885 visit_visitable!($($mut)? vis, item),886 AssocItemKind::Fn(func) => {887 let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), visibility, &$($mut)? *func);888 try_visit!(vis.visit_fn(kind, attrs, span, id))889 }890 AssocItemKind::Type(alias) =>891 visit_visitable!($($mut)? vis, alias),892 AssocItemKind::MacCall(mac) =>893 visit_visitable!($($mut)? vis, mac),894 AssocItemKind::Delegation(delegation) =>895 visit_visitable!($($mut)? vis, delegation),896 AssocItemKind::DelegationMac(dm) =>897 visit_visitable!($($mut)? vis, dm),898 }899 V::Result::output()900 }901 }902903 impl WalkItemKind for ForeignItemKind {904 type Ctxt = ();905 fn walk<$($lt,)? V: $Visitor$(<$lt>)?>(906 &$($lt)? $($mut)? self,907 attrs: &AttrVec,908 span: Span,909 id: NodeId,910 visibility: &$($lt)? $($mut)? Visibility,911 _ctxt: Self::Ctxt,912 vis: &mut V,913 ) -> V::Result {914 match self {915 ForeignItemKind::Static(item) =>916 visit_visitable!($($mut)? vis, item),917 ForeignItemKind::Fn(func) => {918 let kind = FnKind::Fn(FnCtxt::Foreign, visibility, &$($mut)?*func);919 try_visit!(vis.visit_fn(kind, attrs, span, id))920 }921 ForeignItemKind::TyAlias(alias) =>922 visit_visitable!($($mut)? vis, alias),923 ForeignItemKind::MacCall(mac) =>924 visit_visitable!($($mut)? vis, mac),925 }926 V::Result::output()927 }928 }929930 pub fn walk_fn<$($lt,)? V: $Visitor$(<$lt>)?>(vis: &mut V, kind: FnKind<$($lt)? $(${ignore($mut)} '_)?>) -> V::Result {931 match kind {932 FnKind::Fn(933 _ctxt,934 // Visibility is visited as a part of the item.935 _vis,936 Fn { defaultness, ident, sig, generics, contract, body, define_opaque, eii_impls },937 ) => {938 let FnSig { header, decl, span } = sig;939 visit_visitable!($($mut)? vis,940 defaultness, ident, header, generics, decl,941 contract, body, span, define_opaque, eii_impls942 );943 }944 FnKind::Closure(binder, coroutine_kind, decl, body) =>945 visit_visitable!($($mut)? vis, binder, coroutine_kind, decl, body),946 }947 V::Result::output()948 }949950 impl_walkable!(|&$($mut)? $($lt)? self: Impl, vis: &mut V| {951 let Impl { generics, of_trait, self_ty, items, constness: _ } = self;952 try_visit!(vis.visit_generics(generics));953 if let Some(of_trait) = of_trait {954 let TraitImplHeader { defaultness, safety, polarity, trait_ref } = of_trait;955 visit_visitable!($($mut)? vis, defaultness, safety, polarity, trait_ref);956 }957 try_visit!(vis.visit_ty(self_ty));958 visit_visitable_with!($($mut)? vis, items, AssocCtxt::Impl { of_trait: of_trait.is_some() });959 V::Result::output()960 });961962 // Special case to call `visit_method_receiver_expr`.963 impl_walkable!(|&$($mut)? $($lt)? self: MethodCall, vis: &mut V| {964 let MethodCall { seg, receiver, args, span } = self;965 try_visit!(vis.visit_method_receiver_expr(receiver));966 visit_visitable!($($mut)? vis, seg, args, span);967 V::Result::output()968 });969970 impl_walkable!(|&$($mut)? $($lt)? self: Expr, vis: &mut V| {971 let Expr { id, kind, span, attrs, tokens: _ } = self;972 visit_visitable!($($mut)? vis, id, attrs);973 match kind {974 ExprKind::Array(exprs) =>975 visit_visitable!($($mut)? vis, exprs),976 ExprKind::ConstBlock(anon_const) =>977 visit_visitable!($($mut)? vis, anon_const),978 ExprKind::Repeat(element, count) =>979 visit_visitable!($($mut)? vis, element, count),980 ExprKind::Struct(se) =>981 visit_visitable!($($mut)? vis, se),982 ExprKind::Tup(exprs) =>983 visit_visitable!($($mut)? vis, exprs),984 ExprKind::Call(callee_expression, arguments) =>985 visit_visitable!($($mut)? vis, callee_expression, arguments),986 ExprKind::MethodCall(mc) =>987 visit_visitable!($($mut)? vis, mc),988 ExprKind::Binary(op, lhs, rhs) =>989 visit_visitable!($($mut)? vis, op, lhs, rhs),990 ExprKind::AddrOf(kind, mutbl, subexpression) =>991 visit_visitable!($($mut)? vis, kind, mutbl, subexpression),992 ExprKind::Unary(op, subexpression) =>993 visit_visitable!($($mut)? vis, op, subexpression),994 ExprKind::Cast(subexpression, typ) | ExprKind::Type(subexpression, typ) =>995 visit_visitable!($($mut)? vis, subexpression, typ),996 ExprKind::Let(pat, expr, span, _recovered) =>997 visit_visitable!($($mut)? vis, pat, expr, span),998 ExprKind::If(head_expression, if_block, optional_else) =>999 visit_visitable!($($mut)? vis, head_expression, if_block, optional_else),1000 ExprKind::While(subexpression, block, opt_label) =>1001 visit_visitable!($($mut)? vis, subexpression, block, opt_label),1002 ExprKind::ForLoop { pat, iter, body, label, kind } =>1003 visit_visitable!($($mut)? vis, pat, iter, body, label, kind),1004 ExprKind::Loop(block, opt_label, span) =>1005 visit_visitable!($($mut)? vis, block, opt_label, span),1006 ExprKind::Match(subexpression, arms, kind) =>1007 visit_visitable!($($mut)? vis, subexpression, arms, kind),1008 ExprKind::Closure(Closure {1009 binder,1010 capture_clause,1011 coroutine_kind,1012 constness,1013 movability,1014 fn_decl,1015 body,1016 fn_decl_span,1017 fn_arg_span,1018 }) => {1019 visit_visitable!($($mut)? vis, constness, movability, capture_clause);1020 let kind = FnKind::Closure(binder, coroutine_kind, fn_decl, body);1021 try_visit!(vis.visit_fn(kind, attrs, *span, *id));1022 visit_visitable!($($mut)? vis, fn_decl_span, fn_arg_span);1023 }1024 ExprKind::Block(block, opt_label) =>1025 visit_visitable!($($mut)? vis, block, opt_label),1026 ExprKind::Gen(capt, body, kind, decl_span) =>1027 visit_visitable!($($mut)? vis, capt, body, kind, decl_span),1028 ExprKind::Await(expr, span)1029 | ExprKind::Move(expr, span)1030 | ExprKind::Use(expr, span) =>1031 visit_visitable!($($mut)? vis, expr, span),1032 ExprKind::Assign(lhs, rhs, span) =>1033 visit_visitable!($($mut)? vis, lhs, rhs, span),1034 ExprKind::AssignOp(op, lhs, rhs) =>1035 visit_visitable!($($mut)? vis, op, lhs, rhs),1036 ExprKind::Field(subexpression, ident) =>1037 visit_visitable!($($mut)? vis, subexpression, ident),1038 ExprKind::Index(main_expression, index_expression, span) =>1039 visit_visitable!($($mut)? vis, main_expression, index_expression, span),1040 ExprKind::Range(start, end, limit) =>1041 visit_visitable!($($mut)? vis, start, end, limit),1042 ExprKind::Underscore => {}1043 ExprKind::Path(maybe_qself, path) =>1044 visit_visitable!($($mut)? vis, maybe_qself, path),1045 ExprKind::Break(opt_label, opt_expr) =>1046 visit_visitable!($($mut)? vis, opt_label, opt_expr),1047 ExprKind::Continue(opt_label) =>1048 visit_visitable!($($mut)? vis, opt_label),1049 ExprKind::Ret(optional_expression) | ExprKind::Yeet(optional_expression) =>1050 visit_visitable!($($mut)? vis, optional_expression),1051 ExprKind::Become(expr) =>1052 visit_visitable!($($mut)? vis, expr),1053 ExprKind::MacCall(mac) =>1054 visit_visitable!($($mut)? vis, mac),1055 ExprKind::Paren(subexpression) =>1056 visit_visitable!($($mut)? vis, subexpression),1057 ExprKind::InlineAsm(asm) =>1058 visit_visitable!($($mut)? vis, asm),1059 ExprKind::FormatArgs(f) =>1060 visit_visitable!($($mut)? vis, f),1061 ExprKind::OffsetOf(container, fields) =>1062 visit_visitable!($($mut)? vis, container, fields),1063 ExprKind::Yield(kind) =>1064 visit_visitable!($($mut)? vis, kind),1065 ExprKind::Try(subexpression) =>1066 visit_visitable!($($mut)? vis, subexpression),1067 ExprKind::TryBlock(body, optional_type) =>1068 visit_visitable!($($mut)? vis, body, optional_type),1069 ExprKind::Lit(token) =>1070 visit_visitable!($($mut)? vis, token),1071 ExprKind::IncludedBytes(bytes) =>1072 visit_visitable!($($mut)? vis, bytes),1073 ExprKind::UnsafeBinderCast(kind, expr, ty) =>1074 visit_visitable!($($mut)? vis, kind, expr, ty),1075 ExprKind::Err(_guar) => {}1076 ExprKind::Dummy => {}1077 }10781079 visit_span(vis, span)1080 });10811082 define_named_walk!($(($mut))? $Visitor$(<$lt>)?1083 pub fn walk_anon_const(AnonConst);1084 pub fn walk_arm(Arm);1085 //pub fn walk_assoc_item(AssocItem, _ctxt: AssocCtxt);1086 pub fn walk_assoc_item_constraint(AssocItemConstraint);1087 pub fn walk_attribute(Attribute);1088 pub fn walk_block(Block);1089 //pub fn walk_nested_use_tree((UseTree, NodeId));1090 pub fn walk_capture_by(CaptureBy);1091 pub fn walk_closure_binder(ClosureBinder);1092 pub fn walk_contract(FnContract);1093 pub fn walk_coroutine_kind(CoroutineKind);1094 pub fn walk_crate(Crate);1095 pub fn walk_expr(Expr);1096 pub fn walk_expr_field(ExprField);1097 pub fn walk_field_def(FieldDef);1098 pub fn walk_fn_decl(FnDecl);1099 pub fn walk_fn_header(FnHeader);1100 pub fn walk_fn_ret_ty(FnRetTy);1101 //pub fn walk_foreign_item(ForeignItem);1102 pub fn walk_foreign_mod(ForeignMod);1103 pub fn walk_format_args(FormatArgs);1104 pub fn walk_generic_arg(GenericArg);1105 pub fn walk_generic_args(GenericArgs);1106 pub fn walk_generic_param(GenericParam);1107 pub fn walk_generics(Generics);1108 pub fn walk_inline_asm(InlineAsm);1109 pub fn walk_inline_asm_sym(InlineAsmSym);1110 pub fn walk_impl_restriction(ImplRestriction);1111 //pub fn walk_item(Item);1112 pub fn walk_label(Label);1113 pub fn walk_lifetime(Lifetime);1114 pub fn walk_local(Local);1115 pub fn walk_mac(MacCall);1116 pub fn walk_macro_def(MacroDef);1117 pub fn walk_mut_restriction(MutRestriction);1118 pub fn walk_param_bound(GenericBound);1119 pub fn walk_param(Param);1120 pub fn walk_pat_field(PatField);1121 pub fn walk_path(Path);1122 pub fn walk_path_segment(PathSegment);1123 pub fn walk_pat(Pat);1124 pub fn walk_poly_trait_ref(PolyTraitRef);1125 pub fn walk_precise_capturing_arg(PreciseCapturingArg);1126 pub fn walk_qself(QSelf);1127 pub fn walk_trait_ref(TraitRef);1128 pub fn walk_ty_pat(TyPat);1129 pub fn walk_ty(Ty);1130 pub fn walk_use_tree(UseTree);1131 pub fn walk_variant_data(VariantData);1132 pub fn walk_variant(Variant);1133 pub fn walk_vis(Visibility);1134 pub fn walk_where_predicate_kind(WherePredicateKind);1135 pub fn walk_where_predicate(WherePredicate);1136 );1137 };1138}11391140common_visitor_and_walkers!(Visitor<'a>);11411142macro_rules! generate_list_visit_fns {1143 ($($name:ident, $Ty:ty, $visit_fn:ident$(, $param:ident: $ParamTy:ty)*;)+) => {1144 $(1145 #[allow(unused_parens)]1146 impl<'a, V: Visitor<'a>> Visitable<'a, V> for ThinVec<$Ty> {1147 type Extra = ($($ParamTy),*);11481149 #[inline]1150 fn visit(1151 &'a self,1152 visitor: &mut V,1153 ($($param),*): Self::Extra,1154 ) -> V::Result {1155 $name(visitor, self $(, $param)*)1156 }1157 }11581159 fn $name<'a, V: Visitor<'a>>(1160 vis: &mut V,1161 values: &'a ThinVec<$Ty>,1162 $(1163 $param: $ParamTy,1164 )*1165 ) -> V::Result {1166 walk_list!(vis, $visit_fn, values$(,$param)*);1167 V::Result::output()1168 }1169 )+1170 }1171}11721173generate_list_visit_fns! {1174 visit_items, Box<Item>, visit_item;1175 visit_foreign_items, Box<ForeignItem>, visit_foreign_item;1176 visit_generic_params, GenericParam, visit_generic_param;1177 visit_stmts, Stmt, visit_stmt;1178 visit_exprs, Box<Expr>, visit_expr;1179 visit_expr_fields, ExprField, visit_expr_field;1180 visit_pat_fields, PatField, visit_pat_field;1181 visit_variants, Variant, visit_variant;1182 visit_assoc_items, Box<AssocItem>, visit_assoc_item, ctxt: AssocCtxt;1183 visit_where_predicates, WherePredicate, visit_where_predicate;1184 visit_params, Param, visit_param;1185 visit_field_defs, FieldDef, visit_field_def;1186 visit_arms, Arm, visit_arm;1187}11881189pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) -> V::Result {1190 let Stmt { id, kind, span: _ } = statement;1191 try_visit!(visitor.visit_id(*id));1192 match kind {1193 StmtKind::Let(local) => try_visit!(visitor.visit_local(local)),1194 StmtKind::Item(item) => try_visit!(visitor.visit_item(item)),1195 StmtKind::Expr(expr) | StmtKind::Semi(expr) => try_visit!(visitor.visit_expr(expr)),1196 StmtKind::Empty => {}1197 StmtKind::MacCall(mac) => {1198 let MacCallStmt { mac, attrs, style: _, tokens: _ } = &**mac;1199 walk_list!(visitor, visit_attribute, attrs);1200 try_visit!(visitor.visit_mac_call(mac));1201 }1202 }1203 V::Result::output()1204}