1 //! A `MutVisitor` represents an AST modification; it accepts an AST piece and
2 //! mutates it in place. So, for instance, macro expansion is a `MutVisitor`
3 //! that walks over an AST and modifies it.
5 //! Note: using a `MutVisitor` (other than the `MacroExpander` `MutVisitor`) on
6 //! an AST before macro expansion is probably a bad idea. For instance,
7 //! a `MutVisitor` renaming item names in a module will miss all of those
8 //! that are created by the expansion of a macro.
12 use crate::token::{self, Token};
13 use crate::tokenstream::*;
15 use rustc_data_structures::map_in_place::MapInPlace;
16 use rustc_data_structures::sync::Lrc;
17 use rustc_data_structures::thin_vec::ThinVec;
18 use rustc_span::source_map::Spanned;
19 use rustc_span::symbol::Ident;
22 use smallvec::{smallvec, Array, SmallVec};
23 use std::ops::DerefMut;
24 use std::{panic, ptr};
26 pub trait ExpectOne<A: Array> {
27 fn expect_one(self, err: &'static str) -> A::Item;
30 impl<A: Array> ExpectOne<A> for SmallVec<A> {
31 fn expect_one(self, err: &'static str) -> A::Item {
32 assert!(self.len() == 1, "{}", err);
33 self.into_iter().next().unwrap()
37 pub trait MutVisitor: Sized {
38 /// Mutable token visiting only exists for the `macro_rules` token marker and should not be
39 /// used otherwise. Token visitor would be entirely separate from the regular visitor if
40 /// the marker didn't have to visit AST fragments in nonterminal tokens.
41 const VISIT_TOKENS: bool = false;
43 // Methods in this trait have one of three forms:
45 // fn visit_t(&mut self, t: &mut T); // common
46 // fn flat_map_t(&mut self, t: T) -> SmallVec<[T; 1]>; // rare
47 // fn filter_map_t(&mut self, t: T) -> Option<T>; // rarest
49 // Any additions to this trait should happen in form of a call to a public
50 // `noop_*` function that only calls out to the visitor again, not other
51 // `noop_*` functions. This is a necessary API workaround to the problem of
52 // not being able to call out to the super default method in an overridden
55 // When writing these methods, it is better to use destructuring like this:
57 // fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
62 // than to use field access like this:
64 // fn visit_abc(&mut self, abc: &mut ABC) {
65 // visit_a(&mut abc.a);
66 // visit_b(&mut abc.b);
70 // As well as being more concise, the former is explicit about which fields
71 // are skipped. Furthermore, if a new field is added, the destructuring
72 // version will cause a compile error, which is good. In comparison, the
73 // field access version will continue working and it would be easy to
74 // forget to add handling for it.
76 fn visit_crate(&mut self, c: &mut Crate) {
77 noop_visit_crate(c, self)
80 fn visit_meta_list_item(&mut self, list_item: &mut NestedMetaItem) {
81 noop_visit_meta_list_item(list_item, self);
84 fn visit_meta_item(&mut self, meta_item: &mut MetaItem) {
85 noop_visit_meta_item(meta_item, self);
88 fn visit_use_tree(&mut self, use_tree: &mut UseTree) {
89 noop_visit_use_tree(use_tree, self);
92 fn flat_map_foreign_item(&mut self, ni: P<ForeignItem>) -> SmallVec<[P<ForeignItem>; 1]> {
93 noop_flat_map_foreign_item(ni, self)
96 fn flat_map_item(&mut self, i: P<Item>) -> SmallVec<[P<Item>; 1]> {
97 noop_flat_map_item(i, self)
100 fn visit_fn_header(&mut self, header: &mut FnHeader) {
101 noop_visit_fn_header(header, self);
104 fn flat_map_field_def(&mut self, fd: FieldDef) -> SmallVec<[FieldDef; 1]> {
105 noop_flat_map_field_def(fd, self)
108 fn visit_item_kind(&mut self, i: &mut ItemKind) {
109 noop_visit_item_kind(i, self);
112 fn flat_map_trait_item(&mut self, i: P<AssocItem>) -> SmallVec<[P<AssocItem>; 1]> {
113 noop_flat_map_assoc_item(i, self)
116 fn flat_map_impl_item(&mut self, i: P<AssocItem>) -> SmallVec<[P<AssocItem>; 1]> {
117 noop_flat_map_assoc_item(i, self)
120 fn visit_fn_decl(&mut self, d: &mut P<FnDecl>) {
121 noop_visit_fn_decl(d, self);
124 fn visit_asyncness(&mut self, a: &mut Async) {
125 noop_visit_asyncness(a, self);
128 fn visit_block(&mut self, b: &mut P<Block>) {
129 noop_visit_block(b, self);
132 fn flat_map_stmt(&mut self, s: Stmt) -> SmallVec<[Stmt; 1]> {
133 noop_flat_map_stmt(s, self)
136 fn flat_map_arm(&mut self, arm: Arm) -> SmallVec<[Arm; 1]> {
137 noop_flat_map_arm(arm, self)
140 fn visit_pat(&mut self, p: &mut P<Pat>) {
141 noop_visit_pat(p, self);
144 fn visit_anon_const(&mut self, c: &mut AnonConst) {
145 noop_visit_anon_const(c, self);
148 fn visit_expr(&mut self, e: &mut P<Expr>) {
149 noop_visit_expr(e, self);
152 fn filter_map_expr(&mut self, e: P<Expr>) -> Option<P<Expr>> {
153 noop_filter_map_expr(e, self)
156 fn visit_generic_arg(&mut self, arg: &mut GenericArg) {
157 noop_visit_generic_arg(arg, self);
160 fn visit_ty(&mut self, t: &mut P<Ty>) {
161 noop_visit_ty(t, self);
164 fn visit_lifetime(&mut self, l: &mut Lifetime) {
165 noop_visit_lifetime(l, self);
168 fn visit_constraint(&mut self, t: &mut AssocConstraint) {
169 noop_visit_constraint(t, self);
172 fn visit_foreign_mod(&mut self, nm: &mut ForeignMod) {
173 noop_visit_foreign_mod(nm, self);
176 fn flat_map_variant(&mut self, v: Variant) -> SmallVec<[Variant; 1]> {
177 noop_flat_map_variant(v, self)
180 fn visit_ident(&mut self, i: &mut Ident) {
181 noop_visit_ident(i, self);
184 fn visit_path(&mut self, p: &mut Path) {
185 noop_visit_path(p, self);
188 fn visit_qself(&mut self, qs: &mut Option<QSelf>) {
189 noop_visit_qself(qs, self);
192 fn visit_generic_args(&mut self, p: &mut GenericArgs) {
193 noop_visit_generic_args(p, self);
196 fn visit_angle_bracketed_parameter_data(&mut self, p: &mut AngleBracketedArgs) {
197 noop_visit_angle_bracketed_parameter_data(p, self);
200 fn visit_parenthesized_parameter_data(&mut self, p: &mut ParenthesizedArgs) {
201 noop_visit_parenthesized_parameter_data(p, self);
204 fn visit_local(&mut self, l: &mut P<Local>) {
205 noop_visit_local(l, self);
208 fn visit_mac_call(&mut self, mac: &mut MacCall) {
209 noop_visit_mac(mac, self);
212 fn visit_macro_def(&mut self, def: &mut MacroDef) {
213 noop_visit_macro_def(def, self);
216 fn visit_label(&mut self, label: &mut Label) {
217 noop_visit_label(label, self);
220 fn visit_attribute(&mut self, at: &mut Attribute) {
221 noop_visit_attribute(at, self);
224 fn flat_map_param(&mut self, param: Param) -> SmallVec<[Param; 1]> {
225 noop_flat_map_param(param, self)
228 fn visit_generics(&mut self, generics: &mut Generics) {
229 noop_visit_generics(generics, self);
232 fn visit_trait_ref(&mut self, tr: &mut TraitRef) {
233 noop_visit_trait_ref(tr, self);
236 fn visit_poly_trait_ref(&mut self, p: &mut PolyTraitRef) {
237 noop_visit_poly_trait_ref(p, self);
240 fn visit_variant_data(&mut self, vdata: &mut VariantData) {
241 noop_visit_variant_data(vdata, self);
244 fn flat_map_generic_param(&mut self, param: GenericParam) -> SmallVec<[GenericParam; 1]> {
245 noop_flat_map_generic_param(param, self)
248 fn visit_param_bound(&mut self, tpb: &mut GenericBound) {
249 noop_visit_param_bound(tpb, self);
252 fn visit_mt(&mut self, mt: &mut MutTy) {
253 noop_visit_mt(mt, self);
256 fn flat_map_expr_field(&mut self, f: ExprField) -> SmallVec<[ExprField; 1]> {
257 noop_flat_map_expr_field(f, self)
260 fn visit_where_clause(&mut self, where_clause: &mut WhereClause) {
261 noop_visit_where_clause(where_clause, self);
264 fn visit_where_predicate(&mut self, where_predicate: &mut WherePredicate) {
265 noop_visit_where_predicate(where_predicate, self);
268 fn visit_vis(&mut self, vis: &mut Visibility) {
269 noop_visit_vis(vis, self);
272 fn visit_id(&mut self, _id: &mut NodeId) {
276 fn visit_span(&mut self, _sp: &mut Span) {
280 fn flat_map_pat_field(&mut self, fp: PatField) -> SmallVec<[PatField; 1]> {
281 noop_flat_map_pat_field(fp, self)
285 /// Use a map-style function (`FnOnce(T) -> T`) to overwrite a `&mut T`. Useful
286 /// when using a `flat_map_*` or `filter_map_*` method within a `visit_`
289 // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
290 pub fn visit_clobber<T: DummyAstNode>(t: &mut T, f: impl FnOnce(T) -> T) {
292 // Safe because `t` is used in a read-only fashion by `read()` before
293 // being overwritten by `write()`.
294 let old_t = ptr::read(t);
296 panic::catch_unwind(panic::AssertUnwindSafe(|| f(old_t))).unwrap_or_else(|err| {
297 // Set `t` to some valid but possible meaningless value,
298 // and pass the fatal error further.
299 ptr::write(t, T::dummy());
300 panic::resume_unwind(err);
302 ptr::write(t, new_t);
306 // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
308 pub fn visit_vec<T, F>(elems: &mut Vec<T>, mut visit_elem: F)
317 // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
319 pub fn visit_opt<T, F>(opt: &mut Option<T>, mut visit_elem: F)
323 if let Some(elem) = opt {
328 // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
329 pub fn visit_attrs<T: MutVisitor>(attrs: &mut Vec<Attribute>, vis: &mut T) {
330 visit_vec(attrs, |attr| vis.visit_attribute(attr));
333 // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
334 pub fn visit_thin_attrs<T: MutVisitor>(attrs: &mut AttrVec, vis: &mut T) {
335 for attr in attrs.iter_mut() {
336 vis.visit_attribute(attr);
340 // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
341 pub fn visit_exprs<T: MutVisitor>(exprs: &mut Vec<P<Expr>>, vis: &mut T) {
342 exprs.flat_map_in_place(|expr| vis.filter_map_expr(expr))
345 // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
346 pub fn visit_bounds<T: MutVisitor>(bounds: &mut GenericBounds, vis: &mut T) {
347 visit_vec(bounds, |bound| vis.visit_param_bound(bound));
350 // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
351 pub fn visit_fn_sig<T: MutVisitor>(FnSig { header, decl, span }: &mut FnSig, vis: &mut T) {
352 vis.visit_fn_header(header);
353 vis.visit_fn_decl(decl);
354 vis.visit_span(span);
357 // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
358 pub fn visit_mac_args<T: MutVisitor>(args: &mut MacArgs, vis: &mut T) {
361 MacArgs::Delimited(dspan, _delim, tokens) => {
362 visit_delim_span(dspan, vis);
363 visit_tts(tokens, vis);
365 MacArgs::Eq(eq_span, token) => {
366 vis.visit_span(eq_span);
368 visit_token(token, vis);
370 // The value in `#[key = VALUE]` must be visited as an expression for backward
371 // compatibility, so that macros can be expanded in that position.
372 match &mut token.kind {
373 token::Interpolated(nt) => match Lrc::make_mut(nt) {
374 token::NtExpr(expr) => vis.visit_expr(expr),
375 t => panic!("unexpected token in key-value attribute: {:?}", t),
377 t => panic!("unexpected token in key-value attribute: {:?}", t),
384 pub fn visit_delim_span<T: MutVisitor>(dspan: &mut DelimSpan, vis: &mut T) {
385 vis.visit_span(&mut dspan.open);
386 vis.visit_span(&mut dspan.close);
389 pub fn noop_flat_map_pat_field<T: MutVisitor>(
392 ) -> SmallVec<[PatField; 1]> {
393 let PatField { attrs, id, ident, is_placeholder: _, is_shorthand: _, pat, span } = &mut fp;
395 vis.visit_ident(ident);
397 vis.visit_span(span);
398 visit_thin_attrs(attrs, vis);
402 pub fn noop_visit_use_tree<T: MutVisitor>(use_tree: &mut UseTree, vis: &mut T) {
403 let UseTree { prefix, kind, span } = use_tree;
404 vis.visit_path(prefix);
406 UseTreeKind::Simple(rename, id1, id2) => {
407 visit_opt(rename, |rename| vis.visit_ident(rename));
411 UseTreeKind::Nested(items) => {
412 for (tree, id) in items {
413 vis.visit_use_tree(tree);
417 UseTreeKind::Glob => {}
419 vis.visit_span(span);
422 pub fn noop_flat_map_arm<T: MutVisitor>(mut arm: Arm, vis: &mut T) -> SmallVec<[Arm; 1]> {
423 let Arm { attrs, pat, guard, body, span, id, is_placeholder: _ } = &mut arm;
424 visit_thin_attrs(attrs, vis);
427 visit_opt(guard, |guard| vis.visit_expr(guard));
428 vis.visit_expr(body);
429 vis.visit_span(span);
433 pub fn noop_visit_constraint<T: MutVisitor>(
434 AssocConstraint { id, ident, gen_args, kind, span }: &mut AssocConstraint,
438 vis.visit_ident(ident);
439 if let Some(ref mut gen_args) = gen_args {
440 vis.visit_generic_args(gen_args);
443 AssocConstraintKind::Equality { ref mut term } => match term {
444 Term::Ty(ty) => vis.visit_ty(ty),
445 Term::Const(c) => vis.visit_anon_const(c),
447 AssocConstraintKind::Bound { ref mut bounds } => visit_bounds(bounds, vis),
449 vis.visit_span(span);
452 pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
453 let Ty { id, kind, span, tokens } = ty.deref_mut();
456 TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err | TyKind::Never | TyKind::CVarArgs => {}
457 TyKind::Slice(ty) => vis.visit_ty(ty),
458 TyKind::Ptr(mt) => vis.visit_mt(mt),
459 TyKind::Rptr(lt, mt) => {
460 visit_opt(lt, |lt| noop_visit_lifetime(lt, vis));
463 TyKind::BareFn(bft) => {
464 let BareFnTy { unsafety, ext: _, generic_params, decl } = bft.deref_mut();
465 visit_unsafety(unsafety, vis);
466 generic_params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
467 vis.visit_fn_decl(decl);
469 TyKind::Tup(tys) => visit_vec(tys, |ty| vis.visit_ty(ty)),
470 TyKind::Paren(ty) => vis.visit_ty(ty),
471 TyKind::Path(qself, path) => {
472 vis.visit_qself(qself);
473 vis.visit_path(path);
475 TyKind::Array(ty, length) => {
477 vis.visit_anon_const(length);
479 TyKind::Typeof(expr) => vis.visit_anon_const(expr),
480 TyKind::TraitObject(bounds, _syntax) => {
481 visit_vec(bounds, |bound| vis.visit_param_bound(bound))
483 TyKind::ImplTrait(id, bounds) => {
485 visit_vec(bounds, |bound| vis.visit_param_bound(bound));
487 TyKind::MacCall(mac) => vis.visit_mac_call(mac),
489 vis.visit_span(span);
490 visit_lazy_tts(tokens, vis);
493 pub fn noop_visit_foreign_mod<T: MutVisitor>(foreign_mod: &mut ForeignMod, vis: &mut T) {
494 let ForeignMod { unsafety, abi: _, items } = foreign_mod;
495 visit_unsafety(unsafety, vis);
496 items.flat_map_in_place(|item| vis.flat_map_foreign_item(item));
499 pub fn noop_flat_map_variant<T: MutVisitor>(
500 mut variant: Variant,
502 ) -> SmallVec<[Variant; 1]> {
503 let Variant { ident, vis, attrs, id, data, disr_expr, span, is_placeholder: _ } = &mut variant;
504 visitor.visit_ident(ident);
505 visitor.visit_vis(vis);
506 visit_thin_attrs(attrs, visitor);
507 visitor.visit_id(id);
508 visitor.visit_variant_data(data);
509 visit_opt(disr_expr, |disr_expr| visitor.visit_anon_const(disr_expr));
510 visitor.visit_span(span);
514 pub fn noop_visit_ident<T: MutVisitor>(Ident { name: _, span }: &mut Ident, vis: &mut T) {
515 vis.visit_span(span);
518 pub fn noop_visit_path<T: MutVisitor>(Path { segments, span, tokens }: &mut Path, vis: &mut T) {
519 vis.visit_span(span);
520 for PathSegment { ident, id, args } in segments {
521 vis.visit_ident(ident);
523 visit_opt(args, |args| vis.visit_generic_args(args));
525 visit_lazy_tts(tokens, vis);
528 pub fn noop_visit_qself<T: MutVisitor>(qself: &mut Option<QSelf>, vis: &mut T) {
529 visit_opt(qself, |QSelf { ty, path_span, position: _ }| {
531 vis.visit_span(path_span);
535 pub fn noop_visit_generic_args<T: MutVisitor>(generic_args: &mut GenericArgs, vis: &mut T) {
537 GenericArgs::AngleBracketed(data) => vis.visit_angle_bracketed_parameter_data(data),
538 GenericArgs::Parenthesized(data) => vis.visit_parenthesized_parameter_data(data),
542 pub fn noop_visit_generic_arg<T: MutVisitor>(arg: &mut GenericArg, vis: &mut T) {
544 GenericArg::Lifetime(lt) => vis.visit_lifetime(lt),
545 GenericArg::Type(ty) => vis.visit_ty(ty),
546 GenericArg::Const(ct) => vis.visit_anon_const(ct),
550 pub fn noop_visit_angle_bracketed_parameter_data<T: MutVisitor>(
551 data: &mut AngleBracketedArgs,
554 let AngleBracketedArgs { args, span } = data;
555 visit_vec(args, |arg| match arg {
556 AngleBracketedArg::Arg(arg) => vis.visit_generic_arg(arg),
557 AngleBracketedArg::Constraint(constraint) => vis.visit_constraint(constraint),
559 vis.visit_span(span);
562 pub fn noop_visit_parenthesized_parameter_data<T: MutVisitor>(
563 args: &mut ParenthesizedArgs,
566 let ParenthesizedArgs { inputs, output, span, .. } = args;
567 visit_vec(inputs, |input| vis.visit_ty(input));
568 noop_visit_fn_ret_ty(output, vis);
569 vis.visit_span(span);
572 pub fn noop_visit_local<T: MutVisitor>(local: &mut P<Local>, vis: &mut T) {
573 let Local { id, pat, ty, kind, span, attrs, tokens } = local.deref_mut();
576 visit_opt(ty, |ty| vis.visit_ty(ty));
578 LocalKind::Decl => {}
579 LocalKind::Init(init) => {
580 vis.visit_expr(init);
582 LocalKind::InitElse(init, els) => {
583 vis.visit_expr(init);
584 vis.visit_block(els);
587 vis.visit_span(span);
588 visit_thin_attrs(attrs, vis);
589 visit_lazy_tts(tokens, vis);
592 pub fn noop_visit_attribute<T: MutVisitor>(attr: &mut Attribute, vis: &mut T) {
593 let Attribute { kind, id: _, style: _, span } = attr;
595 AttrKind::Normal(AttrItem { path, args, tokens }, attr_tokens) => {
596 vis.visit_path(path);
597 visit_mac_args(args, vis);
598 visit_lazy_tts(tokens, vis);
599 visit_lazy_tts(attr_tokens, vis);
601 AttrKind::DocComment(..) => {}
603 vis.visit_span(span);
606 pub fn noop_visit_mac<T: MutVisitor>(mac: &mut MacCall, vis: &mut T) {
607 let MacCall { path, args, prior_type_ascription: _ } = mac;
608 vis.visit_path(path);
609 visit_mac_args(args, vis);
612 pub fn noop_visit_macro_def<T: MutVisitor>(macro_def: &mut MacroDef, vis: &mut T) {
613 let MacroDef { body, macro_rules: _ } = macro_def;
614 visit_mac_args(body, vis);
617 pub fn noop_visit_meta_list_item<T: MutVisitor>(li: &mut NestedMetaItem, vis: &mut T) {
619 NestedMetaItem::MetaItem(mi) => vis.visit_meta_item(mi),
620 NestedMetaItem::Literal(_lit) => {}
624 pub fn noop_visit_meta_item<T: MutVisitor>(mi: &mut MetaItem, vis: &mut T) {
625 let MetaItem { path: _, kind, span } = mi;
627 MetaItemKind::Word => {}
628 MetaItemKind::List(mis) => visit_vec(mis, |mi| vis.visit_meta_list_item(mi)),
629 MetaItemKind::NameValue(_s) => {}
631 vis.visit_span(span);
634 pub fn noop_flat_map_param<T: MutVisitor>(mut param: Param, vis: &mut T) -> SmallVec<[Param; 1]> {
635 let Param { attrs, id, pat, span, ty, is_placeholder: _ } = &mut param;
637 visit_thin_attrs(attrs, vis);
639 vis.visit_span(span);
644 // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
645 pub fn visit_attr_annotated_tt<T: MutVisitor>(tt: &mut AttrAnnotatedTokenTree, vis: &mut T) {
647 AttrAnnotatedTokenTree::Token(token) => {
648 visit_token(token, vis);
650 AttrAnnotatedTokenTree::Delimited(DelimSpan { open, close }, _delim, tts) => {
651 vis.visit_span(open);
652 vis.visit_span(close);
653 visit_attr_annotated_tts(tts, vis);
655 AttrAnnotatedTokenTree::Attributes(data) => {
656 for attr in &mut *data.attrs {
657 match &mut attr.kind {
658 AttrKind::Normal(_, attr_tokens) => {
659 visit_lazy_tts(attr_tokens, vis);
661 AttrKind::DocComment(..) => {
662 vis.visit_span(&mut attr.span);
666 visit_lazy_tts_opt_mut(Some(&mut data.tokens), vis);
671 // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
672 pub fn visit_tt<T: MutVisitor>(tt: &mut TokenTree, vis: &mut T) {
674 TokenTree::Token(token) => {
675 visit_token(token, vis);
677 TokenTree::Delimited(DelimSpan { open, close }, _delim, tts) => {
678 vis.visit_span(open);
679 vis.visit_span(close);
685 // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
686 pub fn visit_tts<T: MutVisitor>(TokenStream(tts): &mut TokenStream, vis: &mut T) {
687 if T::VISIT_TOKENS && !tts.is_empty() {
688 let tts = Lrc::make_mut(tts);
689 visit_vec(tts, |(tree, _is_joint)| visit_tt(tree, vis));
693 pub fn visit_attr_annotated_tts<T: MutVisitor>(
694 AttrAnnotatedTokenStream(tts): &mut AttrAnnotatedTokenStream,
697 if T::VISIT_TOKENS && !tts.is_empty() {
698 let tts = Lrc::make_mut(tts);
699 visit_vec(tts, |(tree, _is_joint)| visit_attr_annotated_tt(tree, vis));
703 pub fn visit_lazy_tts_opt_mut<T: MutVisitor>(lazy_tts: Option<&mut LazyTokenStream>, vis: &mut T) {
705 if let Some(lazy_tts) = lazy_tts {
706 let mut tts = lazy_tts.create_token_stream();
707 visit_attr_annotated_tts(&mut tts, vis);
708 *lazy_tts = LazyTokenStream::new(tts);
713 pub fn visit_lazy_tts<T: MutVisitor>(lazy_tts: &mut Option<LazyTokenStream>, vis: &mut T) {
714 visit_lazy_tts_opt_mut(lazy_tts.as_mut(), vis);
717 // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
718 // Applies ident visitor if it's an ident; applies other visits to interpolated nodes.
719 // In practice the ident part is not actually used by specific visitors right now,
720 // but there's a test below checking that it works.
721 pub fn visit_token<T: MutVisitor>(t: &mut Token, vis: &mut T) {
722 let Token { kind, span } = t;
724 token::Ident(name, _) | token::Lifetime(name) => {
725 let mut ident = Ident::new(*name, *span);
726 vis.visit_ident(&mut ident);
729 return; // Avoid visiting the span for the second time.
731 token::Interpolated(nt) => {
732 let mut nt = Lrc::make_mut(nt);
733 visit_interpolated(&mut nt, vis);
737 vis.visit_span(span);
740 // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
741 /// Applies the visitor to elements of interpolated nodes.
743 // N.B., this can occur only when applying a visitor to partially expanded
744 // code, where parsed pieces have gotten implanted ito *other* macro
745 // invocations. This is relevant for macro hygiene, but possibly not elsewhere.
747 // One problem here occurs because the types for flat_map_item, flat_map_stmt,
748 // etc., allow the visitor to return *multiple* items; this is a problem for the
749 // nodes here, because they insist on having exactly one piece. One solution
750 // would be to mangle the MutVisitor trait to include one-to-many and
751 // one-to-one versions of these entry points, but that would probably confuse a
752 // lot of people and help very few. Instead, I'm just going to put in dynamic
753 // checks. I think the performance impact of this will be pretty much
754 // nonexistent. The danger is that someone will apply a `MutVisitor` to a
755 // partially expanded node, and will be confused by the fact that their
756 // `flat_map_item` or `flat_map_stmt` isn't getting called on `NtItem` or `NtStmt`
757 // nodes. Hopefully they'll wind up reading this comment, and doing something
760 // BTW, design choice: I considered just changing the type of, e.g., `NtItem` to
761 // contain multiple items, but decided against it when I looked at
762 // `parse_item_or_view_item` and tried to figure out what I would do with
763 // multiple items there....
764 pub fn visit_interpolated<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T) {
766 token::NtItem(item) => visit_clobber(item, |item| {
767 // This is probably okay, because the only visitors likely to
768 // peek inside interpolated nodes will be renamings/markings,
769 // which map single items to single items.
770 vis.flat_map_item(item).expect_one("expected visitor to produce exactly one item")
772 token::NtBlock(block) => vis.visit_block(block),
773 token::NtStmt(stmt) => visit_clobber(stmt, |stmt| {
774 // See reasoning above.
775 vis.flat_map_stmt(stmt).expect_one("expected visitor to produce exactly one item")
777 token::NtPat(pat) => vis.visit_pat(pat),
778 token::NtExpr(expr) => vis.visit_expr(expr),
779 token::NtTy(ty) => vis.visit_ty(ty),
780 token::NtIdent(ident, _is_raw) => vis.visit_ident(ident),
781 token::NtLifetime(ident) => vis.visit_ident(ident),
782 token::NtLiteral(expr) => vis.visit_expr(expr),
783 token::NtMeta(item) => {
784 let AttrItem { path, args, tokens } = item.deref_mut();
785 vis.visit_path(path);
786 visit_mac_args(args, vis);
787 visit_lazy_tts(tokens, vis);
789 token::NtPath(path) => vis.visit_path(path),
790 token::NtTT(tt) => visit_tt(tt, vis),
791 token::NtVis(visib) => vis.visit_vis(visib),
795 // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
796 pub fn visit_defaultness<T: MutVisitor>(defaultness: &mut Defaultness, vis: &mut T) {
798 Defaultness::Default(span) => vis.visit_span(span),
799 Defaultness::Final => {}
803 // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
804 pub fn visit_unsafety<T: MutVisitor>(unsafety: &mut Unsafe, vis: &mut T) {
806 Unsafe::Yes(span) => vis.visit_span(span),
811 // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
812 pub fn visit_polarity<T: MutVisitor>(polarity: &mut ImplPolarity, vis: &mut T) {
814 ImplPolarity::Positive => {}
815 ImplPolarity::Negative(span) => vis.visit_span(span),
819 // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
820 pub fn visit_constness<T: MutVisitor>(constness: &mut Const, vis: &mut T) {
822 Const::Yes(span) => vis.visit_span(span),
827 pub fn noop_visit_asyncness<T: MutVisitor>(asyncness: &mut Async, vis: &mut T) {
829 Async::Yes { span: _, closure_id, return_impl_trait_id } => {
830 vis.visit_id(closure_id);
831 vis.visit_id(return_impl_trait_id);
837 pub fn noop_visit_fn_decl<T: MutVisitor>(decl: &mut P<FnDecl>, vis: &mut T) {
838 let FnDecl { inputs, output } = decl.deref_mut();
839 inputs.flat_map_in_place(|param| vis.flat_map_param(param));
840 noop_visit_fn_ret_ty(output, vis);
843 pub fn noop_visit_fn_ret_ty<T: MutVisitor>(fn_ret_ty: &mut FnRetTy, vis: &mut T) {
845 FnRetTy::Default(span) => vis.visit_span(span),
846 FnRetTy::Ty(ty) => vis.visit_ty(ty),
850 pub fn noop_visit_param_bound<T: MutVisitor>(pb: &mut GenericBound, vis: &mut T) {
852 GenericBound::Trait(ty, _modifier) => vis.visit_poly_trait_ref(ty),
853 GenericBound::Outlives(lifetime) => noop_visit_lifetime(lifetime, vis),
857 pub fn noop_flat_map_generic_param<T: MutVisitor>(
858 mut param: GenericParam,
860 ) -> SmallVec<[GenericParam; 1]> {
861 let GenericParam { id, ident, attrs, bounds, kind, is_placeholder: _ } = &mut param;
863 vis.visit_ident(ident);
864 visit_thin_attrs(attrs, vis);
865 visit_vec(bounds, |bound| noop_visit_param_bound(bound, vis));
867 GenericParamKind::Lifetime => {}
868 GenericParamKind::Type { default } => {
869 visit_opt(default, |default| vis.visit_ty(default));
871 GenericParamKind::Const { ty, kw_span: _, default } => {
873 visit_opt(default, |default| vis.visit_anon_const(default));
879 pub fn noop_visit_label<T: MutVisitor>(Label { ident }: &mut Label, vis: &mut T) {
880 vis.visit_ident(ident);
883 fn noop_visit_lifetime<T: MutVisitor>(Lifetime { id, ident }: &mut Lifetime, vis: &mut T) {
885 vis.visit_ident(ident);
888 pub fn noop_visit_generics<T: MutVisitor>(generics: &mut Generics, vis: &mut T) {
889 let Generics { params, where_clause, span } = generics;
890 params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
891 vis.visit_where_clause(where_clause);
892 vis.visit_span(span);
895 pub fn noop_visit_where_clause<T: MutVisitor>(wc: &mut WhereClause, vis: &mut T) {
896 let WhereClause { has_where_token: _, predicates, span } = wc;
897 visit_vec(predicates, |predicate| vis.visit_where_predicate(predicate));
898 vis.visit_span(span);
901 pub fn noop_visit_where_predicate<T: MutVisitor>(pred: &mut WherePredicate, vis: &mut T) {
903 WherePredicate::BoundPredicate(bp) => {
904 let WhereBoundPredicate { span, bound_generic_params, bounded_ty, bounds } = bp;
905 vis.visit_span(span);
906 bound_generic_params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
907 vis.visit_ty(bounded_ty);
908 visit_vec(bounds, |bound| vis.visit_param_bound(bound));
910 WherePredicate::RegionPredicate(rp) => {
911 let WhereRegionPredicate { span, lifetime, bounds } = rp;
912 vis.visit_span(span);
913 noop_visit_lifetime(lifetime, vis);
914 visit_vec(bounds, |bound| noop_visit_param_bound(bound, vis));
916 WherePredicate::EqPredicate(ep) => {
917 let WhereEqPredicate { id, span, lhs_ty, rhs_ty } = ep;
919 vis.visit_span(span);
920 vis.visit_ty(lhs_ty);
921 vis.visit_ty(rhs_ty);
926 pub fn noop_visit_variant_data<T: MutVisitor>(vdata: &mut VariantData, vis: &mut T) {
928 VariantData::Struct(fields, ..) => {
929 fields.flat_map_in_place(|field| vis.flat_map_field_def(field));
931 VariantData::Tuple(fields, id) => {
932 fields.flat_map_in_place(|field| vis.flat_map_field_def(field));
935 VariantData::Unit(id) => vis.visit_id(id),
939 pub fn noop_visit_trait_ref<T: MutVisitor>(TraitRef { path, ref_id }: &mut TraitRef, vis: &mut T) {
940 vis.visit_path(path);
941 vis.visit_id(ref_id);
944 pub fn noop_visit_poly_trait_ref<T: MutVisitor>(p: &mut PolyTraitRef, vis: &mut T) {
945 let PolyTraitRef { bound_generic_params, trait_ref, span } = p;
946 bound_generic_params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
947 vis.visit_trait_ref(trait_ref);
948 vis.visit_span(span);
951 pub fn noop_flat_map_field_def<T: MutVisitor>(
954 ) -> SmallVec<[FieldDef; 1]> {
955 let FieldDef { span, ident, vis, id, ty, attrs, is_placeholder: _ } = &mut fd;
956 visitor.visit_span(span);
957 visit_opt(ident, |ident| visitor.visit_ident(ident));
958 visitor.visit_vis(vis);
959 visitor.visit_id(id);
960 visitor.visit_ty(ty);
961 visit_thin_attrs(attrs, visitor);
965 pub fn noop_flat_map_expr_field<T: MutVisitor>(
968 ) -> SmallVec<[ExprField; 1]> {
969 let ExprField { ident, expr, span, is_shorthand: _, attrs, id, is_placeholder: _ } = &mut f;
970 vis.visit_ident(ident);
971 vis.visit_expr(expr);
973 vis.visit_span(span);
974 visit_thin_attrs(attrs, vis);
978 pub fn noop_visit_mt<T: MutVisitor>(MutTy { ty, mutbl: _ }: &mut MutTy, vis: &mut T) {
982 pub fn noop_visit_block<T: MutVisitor>(block: &mut P<Block>, vis: &mut T) {
983 let Block { id, stmts, rules: _, span, tokens, could_be_bare_literal: _ } = block.deref_mut();
985 stmts.flat_map_in_place(|stmt| vis.flat_map_stmt(stmt));
986 vis.visit_span(span);
987 visit_lazy_tts(tokens, vis);
990 pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
992 ItemKind::ExternCrate(_orig_name) => {}
993 ItemKind::Use(use_tree) => vis.visit_use_tree(use_tree),
994 ItemKind::Static(ty, _, expr) => {
996 visit_opt(expr, |expr| vis.visit_expr(expr));
998 ItemKind::Const(defaultness, ty, expr) => {
999 visit_defaultness(defaultness, vis);
1001 visit_opt(expr, |expr| vis.visit_expr(expr));
1003 ItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
1004 visit_defaultness(defaultness, vis);
1005 visit_fn_sig(sig, vis);
1006 vis.visit_generics(generics);
1007 visit_opt(body, |body| vis.visit_block(body));
1009 ItemKind::Mod(unsafety, mod_kind) => {
1010 visit_unsafety(unsafety, vis);
1012 ModKind::Loaded(items, _inline, inner_span) => {
1013 vis.visit_span(inner_span);
1014 items.flat_map_in_place(|item| vis.flat_map_item(item));
1016 ModKind::Unloaded => {}
1019 ItemKind::ForeignMod(nm) => vis.visit_foreign_mod(nm),
1020 ItemKind::GlobalAsm(asm) => noop_visit_inline_asm(asm, vis),
1021 ItemKind::TyAlias(box TyAlias {
1022 defaultness, generics, where_clauses, bounds, ty, ..
1024 visit_defaultness(defaultness, vis);
1025 vis.visit_generics(generics);
1026 vis.visit_span(&mut where_clauses.0.1);
1027 vis.visit_span(&mut where_clauses.1.1);
1028 visit_bounds(bounds, vis);
1029 visit_opt(ty, |ty| vis.visit_ty(ty));
1031 ItemKind::Enum(EnumDef { variants }, generics) => {
1032 variants.flat_map_in_place(|variant| vis.flat_map_variant(variant));
1033 vis.visit_generics(generics);
1035 ItemKind::Struct(variant_data, generics) | ItemKind::Union(variant_data, generics) => {
1036 vis.visit_variant_data(variant_data);
1037 vis.visit_generics(generics);
1039 ItemKind::Impl(box Impl {
1049 visit_defaultness(defaultness, vis);
1050 visit_unsafety(unsafety, vis);
1051 vis.visit_generics(generics);
1052 visit_constness(constness, vis);
1053 visit_polarity(polarity, vis);
1054 visit_opt(of_trait, |trait_ref| vis.visit_trait_ref(trait_ref));
1055 vis.visit_ty(self_ty);
1056 items.flat_map_in_place(|item| vis.flat_map_impl_item(item));
1058 ItemKind::Trait(box Trait { unsafety, is_auto: _, generics, bounds, items }) => {
1059 visit_unsafety(unsafety, vis);
1060 vis.visit_generics(generics);
1061 visit_bounds(bounds, vis);
1062 items.flat_map_in_place(|item| vis.flat_map_trait_item(item));
1064 ItemKind::TraitAlias(generics, bounds) => {
1065 vis.visit_generics(generics);
1066 visit_bounds(bounds, vis);
1068 ItemKind::MacCall(m) => vis.visit_mac_call(m),
1069 ItemKind::MacroDef(def) => vis.visit_macro_def(def),
1073 pub fn noop_flat_map_assoc_item<T: MutVisitor>(
1074 mut item: P<AssocItem>,
1076 ) -> SmallVec<[P<AssocItem>; 1]> {
1077 let Item { id, ident, vis, attrs, kind, span, tokens } = item.deref_mut();
1078 visitor.visit_id(id);
1079 visitor.visit_ident(ident);
1080 visitor.visit_vis(vis);
1081 visit_attrs(attrs, visitor);
1083 AssocItemKind::Const(defaultness, ty, expr) => {
1084 visit_defaultness(defaultness, visitor);
1085 visitor.visit_ty(ty);
1086 visit_opt(expr, |expr| visitor.visit_expr(expr));
1088 AssocItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
1089 visit_defaultness(defaultness, visitor);
1090 visitor.visit_generics(generics);
1091 visit_fn_sig(sig, visitor);
1092 visit_opt(body, |body| visitor.visit_block(body));
1094 AssocItemKind::TyAlias(box TyAlias {
1102 visit_defaultness(defaultness, visitor);
1103 visitor.visit_generics(generics);
1104 visitor.visit_span(&mut where_clauses.0.1);
1105 visitor.visit_span(&mut where_clauses.1.1);
1106 visit_bounds(bounds, visitor);
1107 visit_opt(ty, |ty| visitor.visit_ty(ty));
1109 AssocItemKind::MacCall(mac) => visitor.visit_mac_call(mac),
1111 visitor.visit_span(span);
1112 visit_lazy_tts(tokens, visitor);
1116 pub fn noop_visit_fn_header<T: MutVisitor>(header: &mut FnHeader, vis: &mut T) {
1117 let FnHeader { unsafety, asyncness, constness, ext: _ } = header;
1118 visit_constness(constness, vis);
1119 vis.visit_asyncness(asyncness);
1120 visit_unsafety(unsafety, vis);
1123 pub fn noop_visit_crate<T: MutVisitor>(krate: &mut Crate, vis: &mut T) {
1124 let Crate { attrs, items, span, id, is_placeholder: _ } = krate;
1126 visit_attrs(attrs, vis);
1127 items.flat_map_in_place(|item| vis.flat_map_item(item));
1128 vis.visit_span(span);
1131 // Mutates one item into possibly many items.
1132 pub fn noop_flat_map_item<T: MutVisitor>(
1135 ) -> SmallVec<[P<Item>; 1]> {
1136 let Item { ident, attrs, id, kind, vis, span, tokens } = item.deref_mut();
1137 visitor.visit_ident(ident);
1138 visit_attrs(attrs, visitor);
1139 visitor.visit_id(id);
1140 visitor.visit_item_kind(kind);
1141 visitor.visit_vis(vis);
1142 visitor.visit_span(span);
1143 visit_lazy_tts(tokens, visitor);
1148 pub fn noop_flat_map_foreign_item<T: MutVisitor>(
1149 mut item: P<ForeignItem>,
1151 ) -> SmallVec<[P<ForeignItem>; 1]> {
1152 let Item { ident, attrs, id, kind, vis, span, tokens } = item.deref_mut();
1153 visitor.visit_id(id);
1154 visitor.visit_ident(ident);
1155 visitor.visit_vis(vis);
1156 visit_attrs(attrs, visitor);
1158 ForeignItemKind::Static(ty, _, expr) => {
1159 visitor.visit_ty(ty);
1160 visit_opt(expr, |expr| visitor.visit_expr(expr));
1162 ForeignItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
1163 visit_defaultness(defaultness, visitor);
1164 visitor.visit_generics(generics);
1165 visit_fn_sig(sig, visitor);
1166 visit_opt(body, |body| visitor.visit_block(body));
1168 ForeignItemKind::TyAlias(box TyAlias {
1176 visit_defaultness(defaultness, visitor);
1177 visitor.visit_generics(generics);
1178 visitor.visit_span(&mut where_clauses.0.1);
1179 visitor.visit_span(&mut where_clauses.1.1);
1180 visit_bounds(bounds, visitor);
1181 visit_opt(ty, |ty| visitor.visit_ty(ty));
1183 ForeignItemKind::MacCall(mac) => visitor.visit_mac_call(mac),
1185 visitor.visit_span(span);
1186 visit_lazy_tts(tokens, visitor);
1190 pub fn noop_visit_pat<T: MutVisitor>(pat: &mut P<Pat>, vis: &mut T) {
1191 let Pat { id, kind, span, tokens } = pat.deref_mut();
1194 PatKind::Wild | PatKind::Rest => {}
1195 PatKind::Ident(_binding_mode, ident, sub) => {
1196 vis.visit_ident(ident);
1197 visit_opt(sub, |sub| vis.visit_pat(sub));
1199 PatKind::Lit(e) => vis.visit_expr(e),
1200 PatKind::TupleStruct(qself, path, elems) => {
1201 vis.visit_qself(qself);
1202 vis.visit_path(path);
1203 visit_vec(elems, |elem| vis.visit_pat(elem));
1205 PatKind::Path(qself, path) => {
1206 vis.visit_qself(qself);
1207 vis.visit_path(path);
1209 PatKind::Struct(qself, path, fields, _etc) => {
1210 vis.visit_qself(qself);
1211 vis.visit_path(path);
1212 fields.flat_map_in_place(|field| vis.flat_map_pat_field(field));
1214 PatKind::Box(inner) => vis.visit_pat(inner),
1215 PatKind::Ref(inner, _mutbl) => vis.visit_pat(inner),
1216 PatKind::Range(e1, e2, Spanned { span: _, node: _ }) => {
1217 visit_opt(e1, |e| vis.visit_expr(e));
1218 visit_opt(e2, |e| vis.visit_expr(e));
1219 vis.visit_span(span);
1221 PatKind::Tuple(elems) | PatKind::Slice(elems) | PatKind::Or(elems) => {
1222 visit_vec(elems, |elem| vis.visit_pat(elem))
1224 PatKind::Paren(inner) => vis.visit_pat(inner),
1225 PatKind::MacCall(mac) => vis.visit_mac_call(mac),
1227 vis.visit_span(span);
1228 visit_lazy_tts(tokens, vis);
1231 pub fn noop_visit_anon_const<T: MutVisitor>(AnonConst { id, value }: &mut AnonConst, vis: &mut T) {
1233 vis.visit_expr(value);
1236 fn noop_visit_inline_asm<T: MutVisitor>(asm: &mut InlineAsm, vis: &mut T) {
1237 for (op, _) in &mut asm.operands {
1239 InlineAsmOperand::In { expr, .. }
1240 | InlineAsmOperand::Out { expr: Some(expr), .. }
1241 | InlineAsmOperand::InOut { expr, .. }
1242 | InlineAsmOperand::Sym { expr, .. } => vis.visit_expr(expr),
1243 InlineAsmOperand::Out { expr: None, .. } => {}
1244 InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
1245 vis.visit_expr(in_expr);
1246 if let Some(out_expr) = out_expr {
1247 vis.visit_expr(out_expr);
1250 InlineAsmOperand::Const { anon_const, .. } => vis.visit_anon_const(anon_const),
1255 pub fn noop_visit_expr<T: MutVisitor>(
1256 Expr { kind, id, span, attrs, tokens }: &mut Expr,
1260 ExprKind::Box(expr) => vis.visit_expr(expr),
1261 ExprKind::Array(exprs) => visit_exprs(exprs, vis),
1262 ExprKind::ConstBlock(anon_const) => {
1263 vis.visit_anon_const(anon_const);
1265 ExprKind::Repeat(expr, count) => {
1266 vis.visit_expr(expr);
1267 vis.visit_anon_const(count);
1269 ExprKind::Tup(exprs) => visit_exprs(exprs, vis),
1270 ExprKind::Call(f, args) => {
1272 visit_exprs(args, vis);
1274 ExprKind::MethodCall(PathSegment { ident, id, args }, exprs, span) => {
1275 vis.visit_ident(ident);
1277 visit_opt(args, |args| vis.visit_generic_args(args));
1278 visit_exprs(exprs, vis);
1279 vis.visit_span(span);
1281 ExprKind::Binary(_binop, lhs, rhs) => {
1282 vis.visit_expr(lhs);
1283 vis.visit_expr(rhs);
1285 ExprKind::Unary(_unop, ohs) => vis.visit_expr(ohs),
1286 ExprKind::Cast(expr, ty) => {
1287 vis.visit_expr(expr);
1290 ExprKind::Type(expr, ty) => {
1291 vis.visit_expr(expr);
1294 ExprKind::AddrOf(_, _, ohs) => vis.visit_expr(ohs),
1295 ExprKind::Let(pat, scrutinee, _) => {
1297 vis.visit_expr(scrutinee);
1299 ExprKind::If(cond, tr, fl) => {
1300 vis.visit_expr(cond);
1301 vis.visit_block(tr);
1302 visit_opt(fl, |fl| vis.visit_expr(fl));
1304 ExprKind::While(cond, body, label) => {
1305 vis.visit_expr(cond);
1306 vis.visit_block(body);
1307 visit_opt(label, |label| vis.visit_label(label));
1309 ExprKind::ForLoop(pat, iter, body, label) => {
1311 vis.visit_expr(iter);
1312 vis.visit_block(body);
1313 visit_opt(label, |label| vis.visit_label(label));
1315 ExprKind::Loop(body, label) => {
1316 vis.visit_block(body);
1317 visit_opt(label, |label| vis.visit_label(label));
1319 ExprKind::Match(expr, arms) => {
1320 vis.visit_expr(expr);
1321 arms.flat_map_in_place(|arm| vis.flat_map_arm(arm));
1323 ExprKind::Closure(_capture_by, asyncness, _movability, decl, body, span) => {
1324 vis.visit_asyncness(asyncness);
1325 vis.visit_fn_decl(decl);
1326 vis.visit_expr(body);
1327 vis.visit_span(span);
1329 ExprKind::Block(blk, label) => {
1330 vis.visit_block(blk);
1331 visit_opt(label, |label| vis.visit_label(label));
1333 ExprKind::Async(_capture_by, node_id, body) => {
1334 vis.visit_id(node_id);
1335 vis.visit_block(body);
1337 ExprKind::Await(expr) => vis.visit_expr(expr),
1338 ExprKind::Assign(el, er, _) => {
1342 ExprKind::AssignOp(_op, el, er) => {
1346 ExprKind::Field(el, ident) => {
1348 vis.visit_ident(ident);
1350 ExprKind::Index(el, er) => {
1354 ExprKind::Range(e1, e2, _lim) => {
1355 visit_opt(e1, |e1| vis.visit_expr(e1));
1356 visit_opt(e2, |e2| vis.visit_expr(e2));
1358 ExprKind::Underscore => {}
1359 ExprKind::Path(qself, path) => {
1360 vis.visit_qself(qself);
1361 vis.visit_path(path);
1363 ExprKind::Break(label, expr) => {
1364 visit_opt(label, |label| vis.visit_label(label));
1365 visit_opt(expr, |expr| vis.visit_expr(expr));
1367 ExprKind::Continue(label) => {
1368 visit_opt(label, |label| vis.visit_label(label));
1370 ExprKind::Ret(expr) => {
1371 visit_opt(expr, |expr| vis.visit_expr(expr));
1373 ExprKind::InlineAsm(asm) => noop_visit_inline_asm(asm, vis),
1374 ExprKind::MacCall(mac) => vis.visit_mac_call(mac),
1375 ExprKind::Struct(se) => {
1376 let StructExpr { qself, path, fields, rest } = se.deref_mut();
1377 vis.visit_qself(qself);
1378 vis.visit_path(path);
1379 fields.flat_map_in_place(|field| vis.flat_map_expr_field(field));
1381 StructRest::Base(expr) => vis.visit_expr(expr),
1382 StructRest::Rest(_span) => {}
1383 StructRest::None => {}
1386 ExprKind::Paren(expr) => {
1387 vis.visit_expr(expr);
1389 ExprKind::Yield(expr) => {
1390 visit_opt(expr, |expr| vis.visit_expr(expr));
1392 ExprKind::Try(expr) => vis.visit_expr(expr),
1393 ExprKind::TryBlock(body) => vis.visit_block(body),
1394 ExprKind::Lit(_) | ExprKind::Err => {}
1397 vis.visit_span(span);
1398 visit_thin_attrs(attrs, vis);
1399 visit_lazy_tts(tokens, vis);
1402 pub fn noop_filter_map_expr<T: MutVisitor>(mut e: P<Expr>, vis: &mut T) -> Option<P<Expr>> {
1404 vis.visit_expr(&mut e);
1409 pub fn noop_flat_map_stmt<T: MutVisitor>(
1410 Stmt { kind, mut span, mut id }: Stmt,
1412 ) -> SmallVec<[Stmt; 1]> {
1413 vis.visit_id(&mut id);
1414 vis.visit_span(&mut span);
1415 let stmts: SmallVec<_> = noop_flat_map_stmt_kind(kind, vis)
1417 .map(|kind| Stmt { id, kind, span })
1419 if stmts.len() > 1 {
1421 "cloning statement `NodeId`s is prohibited by default, \
1422 the visitor should implement custom statement visiting"
1428 pub fn noop_flat_map_stmt_kind<T: MutVisitor>(
1431 ) -> SmallVec<[StmtKind; 1]> {
1433 StmtKind::Local(mut local) => smallvec![StmtKind::Local({
1434 vis.visit_local(&mut local);
1437 StmtKind::Item(item) => vis.flat_map_item(item).into_iter().map(StmtKind::Item).collect(),
1438 StmtKind::Expr(expr) => vis.filter_map_expr(expr).into_iter().map(StmtKind::Expr).collect(),
1439 StmtKind::Semi(expr) => vis.filter_map_expr(expr).into_iter().map(StmtKind::Semi).collect(),
1440 StmtKind::Empty => smallvec![StmtKind::Empty],
1441 StmtKind::MacCall(mut mac) => {
1442 let MacCallStmt { mac: mac_, style: _, attrs, tokens } = mac.deref_mut();
1443 vis.visit_mac_call(mac_);
1444 visit_thin_attrs(attrs, vis);
1445 visit_lazy_tts(tokens, vis);
1446 smallvec![StmtKind::MacCall(mac)]
1451 pub fn noop_visit_vis<T: MutVisitor>(visibility: &mut Visibility, vis: &mut T) {
1452 match &mut visibility.kind {
1453 VisibilityKind::Public | VisibilityKind::Crate(_) | VisibilityKind::Inherited => {}
1454 VisibilityKind::Restricted { path, id } => {
1455 vis.visit_path(path);
1459 vis.visit_span(&mut visibility.span);
1462 /// Some value for the AST node that is valid but possibly meaningless.
1463 pub trait DummyAstNode {
1467 impl<T> DummyAstNode for Option<T> {
1468 fn dummy() -> Self {
1473 impl<T: DummyAstNode + 'static> DummyAstNode for P<T> {
1474 fn dummy() -> Self {
1475 P(DummyAstNode::dummy())
1479 impl<T> DummyAstNode for ThinVec<T> {
1480 fn dummy() -> Self {
1485 impl DummyAstNode for Item {
1486 fn dummy() -> Self {
1488 attrs: Default::default(),
1490 span: Default::default(),
1492 kind: VisibilityKind::Public,
1493 span: Default::default(),
1494 tokens: Default::default(),
1496 ident: Ident::empty(),
1497 kind: ItemKind::ExternCrate(None),
1498 tokens: Default::default(),
1503 impl DummyAstNode for Expr {
1504 fn dummy() -> Self {
1507 kind: ExprKind::Err,
1508 span: Default::default(),
1509 attrs: Default::default(),
1510 tokens: Default::default(),
1515 impl DummyAstNode for Ty {
1516 fn dummy() -> Self {
1520 span: Default::default(),
1521 tokens: Default::default(),
1526 impl DummyAstNode for Pat {
1527 fn dummy() -> Self {
1530 kind: PatKind::Wild,
1531 span: Default::default(),
1532 tokens: Default::default(),
1537 impl DummyAstNode for Stmt {
1538 fn dummy() -> Self {
1539 Stmt { id: DUMMY_NODE_ID, kind: StmtKind::Empty, span: Default::default() }
1543 impl DummyAstNode for Block {
1544 fn dummy() -> Self {
1546 stmts: Default::default(),
1548 rules: BlockCheckMode::Default,
1549 span: Default::default(),
1550 tokens: Default::default(),
1551 could_be_bare_literal: Default::default(),
1556 impl DummyAstNode for Crate {
1557 fn dummy() -> Self {
1559 attrs: Default::default(),
1560 items: Default::default(),
1561 span: Default::default(),
1563 is_placeholder: Default::default(),