]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_expand/src/expand.rs
Auto merge of #107206 - cjgillot:no-h2l-map, r=WaffleLapkin
[rust.git] / compiler / rustc_expand / src / expand.rs
1 use crate::base::*;
2 use crate::config::StripUnconfigured;
3 use crate::errors::{
4     IncompleteParse, RecursionLimitReached, RemoveExprNotSupported, RemoveNodeNotSupported,
5     UnsupportedKeyValue, WrongFragmentKind,
6 };
7 use crate::hygiene::SyntaxContext;
8 use crate::mbe::diagnostics::annotate_err_with_kind;
9 use crate::module::{mod_dir_path, parse_external_mod, DirOwnership, ParsedExternalMod};
10 use crate::placeholders::{placeholder, PlaceholderExpander};
11
12 use rustc_ast as ast;
13 use rustc_ast::mut_visit::*;
14 use rustc_ast::ptr::P;
15 use rustc_ast::token::{self, Delimiter};
16 use rustc_ast::tokenstream::TokenStream;
17 use rustc_ast::visit::{self, AssocCtxt, Visitor};
18 use rustc_ast::{AssocItemKind, AstNodeWrapper, AttrArgs, AttrStyle, AttrVec, ExprKind};
19 use rustc_ast::{ForeignItemKind, HasAttrs, HasNodeId};
20 use rustc_ast::{Inline, ItemKind, MacStmtStyle, MetaItemKind, ModKind};
21 use rustc_ast::{NestedMetaItem, NodeId, PatKind, StmtKind, TyKind};
22 use rustc_ast_pretty::pprust;
23 use rustc_data_structures::map_in_place::MapInPlace;
24 use rustc_data_structures::sync::Lrc;
25 use rustc_errors::PResult;
26 use rustc_feature::Features;
27 use rustc_parse::parser::{
28     AttemptLocalParseRecovery, CommaRecoveryMode, ForceCollect, Parser, RecoverColon, RecoverComma,
29 };
30 use rustc_parse::validate_attr;
31 use rustc_session::lint::builtin::{UNUSED_ATTRIBUTES, UNUSED_DOC_COMMENTS};
32 use rustc_session::lint::BuiltinLintDiagnostics;
33 use rustc_session::parse::{feature_err, ParseSess};
34 use rustc_session::Limit;
35 use rustc_span::symbol::{sym, Ident};
36 use rustc_span::{FileName, LocalExpnId, Span};
37
38 use smallvec::SmallVec;
39 use std::ops::Deref;
40 use std::path::PathBuf;
41 use std::rc::Rc;
42 use std::{iter, mem};
43
44 macro_rules! ast_fragments {
45     (
46         $($Kind:ident($AstTy:ty) {
47             $kind_name:expr;
48             $(one fn $mut_visit_ast:ident; fn $visit_ast:ident;)?
49             $(many fn $flat_map_ast_elt:ident; fn $visit_ast_elt:ident($($args:tt)*);)?
50             fn $make_ast:ident;
51         })*
52     ) => {
53         /// A fragment of AST that can be produced by a single macro expansion.
54         /// Can also serve as an input and intermediate result for macro expansion operations.
55         pub enum AstFragment {
56             OptExpr(Option<P<ast::Expr>>),
57             MethodReceiverExpr(P<ast::Expr>),
58             $($Kind($AstTy),)*
59         }
60
61         /// "Discriminant" of an AST fragment.
62         #[derive(Copy, Clone, PartialEq, Eq)]
63         pub enum AstFragmentKind {
64             OptExpr,
65             MethodReceiverExpr,
66             $($Kind,)*
67         }
68
69         impl AstFragmentKind {
70             pub fn name(self) -> &'static str {
71                 match self {
72                     AstFragmentKind::OptExpr => "expression",
73                     AstFragmentKind::MethodReceiverExpr => "expression",
74                     $(AstFragmentKind::$Kind => $kind_name,)*
75                 }
76             }
77
78             fn make_from<'a>(self, result: Box<dyn MacResult + 'a>) -> Option<AstFragment> {
79                 match self {
80                     AstFragmentKind::OptExpr =>
81                         result.make_expr().map(Some).map(AstFragment::OptExpr),
82                     AstFragmentKind::MethodReceiverExpr =>
83                         result.make_expr().map(AstFragment::MethodReceiverExpr),
84                     $(AstFragmentKind::$Kind => result.$make_ast().map(AstFragment::$Kind),)*
85                 }
86             }
87         }
88
89         impl AstFragment {
90             pub fn add_placeholders(&mut self, placeholders: &[NodeId]) {
91                 if placeholders.is_empty() {
92                     return;
93                 }
94                 match self {
95                     $($(AstFragment::$Kind(ast) => ast.extend(placeholders.iter().flat_map(|id| {
96                         ${ignore(flat_map_ast_elt)}
97                         placeholder(AstFragmentKind::$Kind, *id, None).$make_ast()
98                     })),)?)*
99                     _ => panic!("unexpected AST fragment kind")
100                 }
101             }
102
103             pub fn make_opt_expr(self) -> Option<P<ast::Expr>> {
104                 match self {
105                     AstFragment::OptExpr(expr) => expr,
106                     _ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
107                 }
108             }
109
110             pub fn make_method_receiver_expr(self) -> P<ast::Expr> {
111                 match self {
112                     AstFragment::MethodReceiverExpr(expr) => expr,
113                     _ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
114                 }
115             }
116
117             $(pub fn $make_ast(self) -> $AstTy {
118                 match self {
119                     AstFragment::$Kind(ast) => ast,
120                     _ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
121                 }
122             })*
123
124             fn make_ast<T: InvocationCollectorNode>(self) -> T::OutputTy {
125                 T::fragment_to_output(self)
126             }
127
128             pub fn mut_visit_with<F: MutVisitor>(&mut self, vis: &mut F) {
129                 match self {
130                     AstFragment::OptExpr(opt_expr) => {
131                         visit_clobber(opt_expr, |opt_expr| {
132                             if let Some(expr) = opt_expr {
133                                 vis.filter_map_expr(expr)
134                             } else {
135                                 None
136                             }
137                         });
138                     }
139                     AstFragment::MethodReceiverExpr(expr) => vis.visit_method_receiver_expr(expr),
140                     $($(AstFragment::$Kind(ast) => vis.$mut_visit_ast(ast),)?)*
141                     $($(AstFragment::$Kind(ast) =>
142                         ast.flat_map_in_place(|ast| vis.$flat_map_ast_elt(ast)),)?)*
143                 }
144             }
145
146             pub fn visit_with<'a, V: Visitor<'a>>(&'a self, visitor: &mut V) {
147                 match self {
148                     AstFragment::OptExpr(Some(expr)) => visitor.visit_expr(expr),
149                     AstFragment::OptExpr(None) => {}
150                     AstFragment::MethodReceiverExpr(expr) => visitor.visit_method_receiver_expr(expr),
151                     $($(AstFragment::$Kind(ast) => visitor.$visit_ast(ast),)?)*
152                     $($(AstFragment::$Kind(ast) => for ast_elt in &ast[..] {
153                         visitor.$visit_ast_elt(ast_elt, $($args)*);
154                     })?)*
155                 }
156             }
157         }
158
159         impl<'a> MacResult for crate::mbe::macro_rules::ParserAnyMacro<'a> {
160             $(fn $make_ast(self: Box<crate::mbe::macro_rules::ParserAnyMacro<'a>>)
161                            -> Option<$AstTy> {
162                 Some(self.make(AstFragmentKind::$Kind).$make_ast())
163             })*
164         }
165     }
166 }
167
168 ast_fragments! {
169     Expr(P<ast::Expr>) { "expression"; one fn visit_expr; fn visit_expr; fn make_expr; }
170     Pat(P<ast::Pat>) { "pattern"; one fn visit_pat; fn visit_pat; fn make_pat; }
171     Ty(P<ast::Ty>) { "type"; one fn visit_ty; fn visit_ty; fn make_ty; }
172     Stmts(SmallVec<[ast::Stmt; 1]>) {
173         "statement"; many fn flat_map_stmt; fn visit_stmt(); fn make_stmts;
174     }
175     Items(SmallVec<[P<ast::Item>; 1]>) {
176         "item"; many fn flat_map_item; fn visit_item(); fn make_items;
177     }
178     TraitItems(SmallVec<[P<ast::AssocItem>; 1]>) {
179         "trait item";
180         many fn flat_map_trait_item;
181         fn visit_assoc_item(AssocCtxt::Trait);
182         fn make_trait_items;
183     }
184     ImplItems(SmallVec<[P<ast::AssocItem>; 1]>) {
185         "impl item";
186         many fn flat_map_impl_item;
187         fn visit_assoc_item(AssocCtxt::Impl);
188         fn make_impl_items;
189     }
190     ForeignItems(SmallVec<[P<ast::ForeignItem>; 1]>) {
191         "foreign item";
192         many fn flat_map_foreign_item;
193         fn visit_foreign_item();
194         fn make_foreign_items;
195     }
196     Arms(SmallVec<[ast::Arm; 1]>) {
197         "match arm"; many fn flat_map_arm; fn visit_arm(); fn make_arms;
198     }
199     ExprFields(SmallVec<[ast::ExprField; 1]>) {
200         "field expression"; many fn flat_map_expr_field; fn visit_expr_field(); fn make_expr_fields;
201     }
202     PatFields(SmallVec<[ast::PatField; 1]>) {
203         "field pattern";
204         many fn flat_map_pat_field;
205         fn visit_pat_field();
206         fn make_pat_fields;
207     }
208     GenericParams(SmallVec<[ast::GenericParam; 1]>) {
209         "generic parameter";
210         many fn flat_map_generic_param;
211         fn visit_generic_param();
212         fn make_generic_params;
213     }
214     Params(SmallVec<[ast::Param; 1]>) {
215         "function parameter"; many fn flat_map_param; fn visit_param(); fn make_params;
216     }
217     FieldDefs(SmallVec<[ast::FieldDef; 1]>) {
218         "field";
219         many fn flat_map_field_def;
220         fn visit_field_def();
221         fn make_field_defs;
222     }
223     Variants(SmallVec<[ast::Variant; 1]>) {
224         "variant"; many fn flat_map_variant; fn visit_variant(); fn make_variants;
225     }
226     Crate(ast::Crate) { "crate"; one fn visit_crate; fn visit_crate; fn make_crate; }
227 }
228
229 pub enum SupportsMacroExpansion {
230     No,
231     Yes { supports_inner_attrs: bool },
232 }
233
234 impl AstFragmentKind {
235     pub(crate) fn dummy(self, span: Span) -> AstFragment {
236         self.make_from(DummyResult::any(span)).expect("couldn't create a dummy AST fragment")
237     }
238
239     pub fn supports_macro_expansion(self) -> SupportsMacroExpansion {
240         match self {
241             AstFragmentKind::OptExpr
242             | AstFragmentKind::Expr
243             | AstFragmentKind::MethodReceiverExpr
244             | AstFragmentKind::Stmts
245             | AstFragmentKind::Ty
246             | AstFragmentKind::Pat => SupportsMacroExpansion::Yes { supports_inner_attrs: false },
247             AstFragmentKind::Items
248             | AstFragmentKind::TraitItems
249             | AstFragmentKind::ImplItems
250             | AstFragmentKind::ForeignItems
251             | AstFragmentKind::Crate => SupportsMacroExpansion::Yes { supports_inner_attrs: true },
252             AstFragmentKind::Arms
253             | AstFragmentKind::ExprFields
254             | AstFragmentKind::PatFields
255             | AstFragmentKind::GenericParams
256             | AstFragmentKind::Params
257             | AstFragmentKind::FieldDefs
258             | AstFragmentKind::Variants => SupportsMacroExpansion::No,
259         }
260     }
261
262     fn expect_from_annotatables<I: IntoIterator<Item = Annotatable>>(
263         self,
264         items: I,
265     ) -> AstFragment {
266         let mut items = items.into_iter();
267         match self {
268             AstFragmentKind::Arms => {
269                 AstFragment::Arms(items.map(Annotatable::expect_arm).collect())
270             }
271             AstFragmentKind::ExprFields => {
272                 AstFragment::ExprFields(items.map(Annotatable::expect_expr_field).collect())
273             }
274             AstFragmentKind::PatFields => {
275                 AstFragment::PatFields(items.map(Annotatable::expect_pat_field).collect())
276             }
277             AstFragmentKind::GenericParams => {
278                 AstFragment::GenericParams(items.map(Annotatable::expect_generic_param).collect())
279             }
280             AstFragmentKind::Params => {
281                 AstFragment::Params(items.map(Annotatable::expect_param).collect())
282             }
283             AstFragmentKind::FieldDefs => {
284                 AstFragment::FieldDefs(items.map(Annotatable::expect_field_def).collect())
285             }
286             AstFragmentKind::Variants => {
287                 AstFragment::Variants(items.map(Annotatable::expect_variant).collect())
288             }
289             AstFragmentKind::Items => {
290                 AstFragment::Items(items.map(Annotatable::expect_item).collect())
291             }
292             AstFragmentKind::ImplItems => {
293                 AstFragment::ImplItems(items.map(Annotatable::expect_impl_item).collect())
294             }
295             AstFragmentKind::TraitItems => {
296                 AstFragment::TraitItems(items.map(Annotatable::expect_trait_item).collect())
297             }
298             AstFragmentKind::ForeignItems => {
299                 AstFragment::ForeignItems(items.map(Annotatable::expect_foreign_item).collect())
300             }
301             AstFragmentKind::Stmts => {
302                 AstFragment::Stmts(items.map(Annotatable::expect_stmt).collect())
303             }
304             AstFragmentKind::Expr => AstFragment::Expr(
305                 items.next().expect("expected exactly one expression").expect_expr(),
306             ),
307             AstFragmentKind::MethodReceiverExpr => AstFragment::MethodReceiverExpr(
308                 items.next().expect("expected exactly one expression").expect_expr(),
309             ),
310             AstFragmentKind::OptExpr => {
311                 AstFragment::OptExpr(items.next().map(Annotatable::expect_expr))
312             }
313             AstFragmentKind::Crate => {
314                 AstFragment::Crate(items.next().expect("expected exactly one crate").expect_crate())
315             }
316             AstFragmentKind::Pat | AstFragmentKind::Ty => {
317                 panic!("patterns and types aren't annotatable")
318             }
319         }
320     }
321 }
322
323 pub struct Invocation {
324     pub kind: InvocationKind,
325     pub fragment_kind: AstFragmentKind,
326     pub expansion_data: ExpansionData,
327 }
328
329 pub enum InvocationKind {
330     Bang {
331         mac: P<ast::MacCall>,
332         span: Span,
333     },
334     Attr {
335         attr: ast::Attribute,
336         // Re-insertion position for inert attributes.
337         pos: usize,
338         item: Annotatable,
339         // Required for resolving derive helper attributes.
340         derives: Vec<ast::Path>,
341     },
342     Derive {
343         path: ast::Path,
344         is_const: bool,
345         item: Annotatable,
346     },
347 }
348
349 impl InvocationKind {
350     fn placeholder_visibility(&self) -> Option<ast::Visibility> {
351         // HACK: For unnamed fields placeholders should have the same visibility as the actual
352         // fields because for tuple structs/variants resolve determines visibilities of their
353         // constructor using these field visibilities before attributes on them are expanded.
354         // The assumption is that the attribute expansion cannot change field visibilities,
355         // and it holds because only inert attributes are supported in this position.
356         match self {
357             InvocationKind::Attr { item: Annotatable::FieldDef(field), .. }
358             | InvocationKind::Derive { item: Annotatable::FieldDef(field), .. }
359                 if field.ident.is_none() =>
360             {
361                 Some(field.vis.clone())
362             }
363             _ => None,
364         }
365     }
366 }
367
368 impl Invocation {
369     pub fn span(&self) -> Span {
370         match &self.kind {
371             InvocationKind::Bang { span, .. } => *span,
372             InvocationKind::Attr { attr, .. } => attr.span,
373             InvocationKind::Derive { path, .. } => path.span,
374         }
375     }
376 }
377
378 pub struct MacroExpander<'a, 'b> {
379     pub cx: &'a mut ExtCtxt<'b>,
380     monotonic: bool, // cf. `cx.monotonic_expander()`
381 }
382
383 impl<'a, 'b> MacroExpander<'a, 'b> {
384     pub fn new(cx: &'a mut ExtCtxt<'b>, monotonic: bool) -> Self {
385         MacroExpander { cx, monotonic }
386     }
387
388     pub fn expand_crate(&mut self, krate: ast::Crate) -> ast::Crate {
389         let file_path = match self.cx.source_map().span_to_filename(krate.spans.inner_span) {
390             FileName::Real(name) => name
391                 .into_local_path()
392                 .expect("attempting to resolve a file path in an external file"),
393             other => PathBuf::from(other.prefer_local().to_string()),
394         };
395         let dir_path = file_path.parent().unwrap_or(&file_path).to_owned();
396         self.cx.root_path = dir_path.clone();
397         self.cx.current_expansion.module = Rc::new(ModuleData {
398             mod_path: vec![Ident::from_str(&self.cx.ecfg.crate_name)],
399             file_path_stack: vec![file_path],
400             dir_path,
401         });
402         let krate = self.fully_expand_fragment(AstFragment::Crate(krate)).make_crate();
403         assert_eq!(krate.id, ast::CRATE_NODE_ID);
404         self.cx.trace_macros_diag();
405         krate
406     }
407
408     /// Recursively expand all macro invocations in this AST fragment.
409     pub fn fully_expand_fragment(&mut self, input_fragment: AstFragment) -> AstFragment {
410         let orig_expansion_data = self.cx.current_expansion.clone();
411         let orig_force_mode = self.cx.force_mode;
412
413         // Collect all macro invocations and replace them with placeholders.
414         let (mut fragment_with_placeholders, mut invocations) =
415             self.collect_invocations(input_fragment, &[]);
416
417         // Optimization: if we resolve all imports now,
418         // we'll be able to immediately resolve most of imported macros.
419         self.resolve_imports();
420
421         // Resolve paths in all invocations and produce output expanded fragments for them, but
422         // do not insert them into our input AST fragment yet, only store in `expanded_fragments`.
423         // The output fragments also go through expansion recursively until no invocations are left.
424         // Unresolved macros produce dummy outputs as a recovery measure.
425         invocations.reverse();
426         let mut expanded_fragments = Vec::new();
427         let mut undetermined_invocations = Vec::new();
428         let (mut progress, mut force) = (false, !self.monotonic);
429         loop {
430             let Some((invoc, ext)) = invocations.pop() else {
431                 self.resolve_imports();
432                 if undetermined_invocations.is_empty() {
433                     break;
434                 }
435                 invocations = mem::take(&mut undetermined_invocations);
436                 force = !mem::replace(&mut progress, false);
437                 if force && self.monotonic {
438                     self.cx.sess.delay_span_bug(
439                         invocations.last().unwrap().0.span(),
440                         "expansion entered force mode without producing any errors",
441                     );
442                 }
443                 continue;
444             };
445
446             let ext = match ext {
447                 Some(ext) => ext,
448                 None => {
449                     let eager_expansion_root = if self.monotonic {
450                         invoc.expansion_data.id
451                     } else {
452                         orig_expansion_data.id
453                     };
454                     match self.cx.resolver.resolve_macro_invocation(
455                         &invoc,
456                         eager_expansion_root,
457                         force,
458                     ) {
459                         Ok(ext) => ext,
460                         Err(Indeterminate) => {
461                             // Cannot resolve, will retry this invocation later.
462                             undetermined_invocations.push((invoc, None));
463                             continue;
464                         }
465                     }
466                 }
467             };
468
469             let ExpansionData { depth, id: expn_id, .. } = invoc.expansion_data;
470             let depth = depth - orig_expansion_data.depth;
471             self.cx.current_expansion = invoc.expansion_data.clone();
472             self.cx.force_mode = force;
473
474             let fragment_kind = invoc.fragment_kind;
475             let (expanded_fragment, new_invocations) = match self.expand_invoc(invoc, &ext.kind) {
476                 ExpandResult::Ready(fragment) => {
477                     let mut derive_invocations = Vec::new();
478                     let derive_placeholders = self
479                         .cx
480                         .resolver
481                         .take_derive_resolutions(expn_id)
482                         .map(|derives| {
483                             derive_invocations.reserve(derives.len());
484                             derives
485                                 .into_iter()
486                                 .map(|(path, item, _exts, is_const)| {
487                                     // FIXME: Consider using the derive resolutions (`_exts`)
488                                     // instead of enqueuing the derives to be resolved again later.
489                                     let expn_id = LocalExpnId::fresh_empty();
490                                     derive_invocations.push((
491                                         Invocation {
492                                             kind: InvocationKind::Derive { path, item, is_const },
493                                             fragment_kind,
494                                             expansion_data: ExpansionData {
495                                                 id: expn_id,
496                                                 ..self.cx.current_expansion.clone()
497                                             },
498                                         },
499                                         None,
500                                     ));
501                                     NodeId::placeholder_from_expn_id(expn_id)
502                                 })
503                                 .collect::<Vec<_>>()
504                         })
505                         .unwrap_or_default();
506
507                     let (fragment, collected_invocations) =
508                         self.collect_invocations(fragment, &derive_placeholders);
509                     // We choose to expand any derive invocations associated with this macro invocation
510                     // *before* any macro invocations collected from the output fragment
511                     derive_invocations.extend(collected_invocations);
512                     (fragment, derive_invocations)
513                 }
514                 ExpandResult::Retry(invoc) => {
515                     if force {
516                         self.cx.span_bug(
517                             invoc.span(),
518                             "expansion entered force mode but is still stuck",
519                         );
520                     } else {
521                         // Cannot expand, will retry this invocation later.
522                         undetermined_invocations.push((invoc, Some(ext)));
523                         continue;
524                     }
525                 }
526             };
527
528             progress = true;
529             if expanded_fragments.len() < depth {
530                 expanded_fragments.push(Vec::new());
531             }
532             expanded_fragments[depth - 1].push((expn_id, expanded_fragment));
533             invocations.extend(new_invocations.into_iter().rev());
534         }
535
536         self.cx.current_expansion = orig_expansion_data;
537         self.cx.force_mode = orig_force_mode;
538
539         // Finally incorporate all the expanded macros into the input AST fragment.
540         let mut placeholder_expander = PlaceholderExpander::default();
541         while let Some(expanded_fragments) = expanded_fragments.pop() {
542             for (expn_id, expanded_fragment) in expanded_fragments.into_iter().rev() {
543                 placeholder_expander
544                     .add(NodeId::placeholder_from_expn_id(expn_id), expanded_fragment);
545             }
546         }
547         fragment_with_placeholders.mut_visit_with(&mut placeholder_expander);
548         fragment_with_placeholders
549     }
550
551     fn resolve_imports(&mut self) {
552         if self.monotonic {
553             self.cx.resolver.resolve_imports();
554         }
555     }
556
557     /// Collects all macro invocations reachable at this time in this AST fragment, and replace
558     /// them with "placeholders" - dummy macro invocations with specially crafted `NodeId`s.
559     /// Then call into resolver that builds a skeleton ("reduced graph") of the fragment and
560     /// prepares data for resolving paths of macro invocations.
561     fn collect_invocations(
562         &mut self,
563         mut fragment: AstFragment,
564         extra_placeholders: &[NodeId],
565     ) -> (AstFragment, Vec<(Invocation, Option<Lrc<SyntaxExtension>>)>) {
566         // Resolve `$crate`s in the fragment for pretty-printing.
567         self.cx.resolver.resolve_dollar_crates();
568
569         let mut invocations = {
570             let mut collector = InvocationCollector {
571                 // Non-derive macro invocations cannot see the results of cfg expansion - they
572                 // will either be removed along with the item, or invoked before the cfg/cfg_attr
573                 // attribute is expanded. Therefore, we don't need to configure the tokens
574                 // Derive macros *can* see the results of cfg-expansion - they are handled
575                 // specially in `fully_expand_fragment`
576                 cx: self.cx,
577                 invocations: Vec::new(),
578                 monotonic: self.monotonic,
579             };
580             fragment.mut_visit_with(&mut collector);
581             fragment.add_placeholders(extra_placeholders);
582             collector.invocations
583         };
584
585         if self.monotonic {
586             self.cx
587                 .resolver
588                 .visit_ast_fragment_with_placeholders(self.cx.current_expansion.id, &fragment);
589
590             if self.cx.sess.opts.incremental_relative_spans() {
591                 for (invoc, _) in invocations.iter_mut() {
592                     let expn_id = invoc.expansion_data.id;
593                     let parent_def = self.cx.resolver.invocation_parent(expn_id);
594                     let span = match &mut invoc.kind {
595                         InvocationKind::Bang { span, .. } => span,
596                         InvocationKind::Attr { attr, .. } => &mut attr.span,
597                         InvocationKind::Derive { path, .. } => &mut path.span,
598                     };
599                     *span = span.with_parent(Some(parent_def));
600                 }
601             }
602         }
603
604         (fragment, invocations)
605     }
606
607     fn error_recursion_limit_reached(&mut self) {
608         let expn_data = self.cx.current_expansion.id.expn_data();
609         let suggested_limit = match self.cx.ecfg.recursion_limit {
610             Limit(0) => Limit(2),
611             limit => limit * 2,
612         };
613
614         self.cx.emit_err(RecursionLimitReached {
615             span: expn_data.call_site,
616             descr: expn_data.kind.descr(),
617             suggested_limit,
618             crate_name: &self.cx.ecfg.crate_name,
619         });
620
621         self.cx.trace_macros_diag();
622     }
623
624     /// A macro's expansion does not fit in this fragment kind.
625     /// For example, a non-type macro in a type position.
626     fn error_wrong_fragment_kind(&mut self, kind: AstFragmentKind, mac: &ast::MacCall, span: Span) {
627         self.cx.emit_err(WrongFragmentKind { span, kind: kind.name(), name: &mac.path });
628
629         self.cx.trace_macros_diag();
630     }
631
632     fn expand_invoc(
633         &mut self,
634         invoc: Invocation,
635         ext: &SyntaxExtensionKind,
636     ) -> ExpandResult<AstFragment, Invocation> {
637         let recursion_limit =
638             self.cx.reduced_recursion_limit.unwrap_or(self.cx.ecfg.recursion_limit);
639         if !recursion_limit.value_within_limit(self.cx.current_expansion.depth) {
640             if self.cx.reduced_recursion_limit.is_none() {
641                 self.error_recursion_limit_reached();
642             }
643
644             // Reduce the recursion limit by half each time it triggers.
645             self.cx.reduced_recursion_limit = Some(recursion_limit / 2);
646
647             return ExpandResult::Ready(invoc.fragment_kind.dummy(invoc.span()));
648         }
649
650         let (fragment_kind, span) = (invoc.fragment_kind, invoc.span());
651         ExpandResult::Ready(match invoc.kind {
652             InvocationKind::Bang { mac, .. } => match ext {
653                 SyntaxExtensionKind::Bang(expander) => {
654                     let Ok(tok_result) = expander.expand(self.cx, span, mac.args.tokens.clone()) else {
655                         return ExpandResult::Ready(fragment_kind.dummy(span));
656                     };
657                     self.parse_ast_fragment(tok_result, fragment_kind, &mac.path, span)
658                 }
659                 SyntaxExtensionKind::LegacyBang(expander) => {
660                     let prev = self.cx.current_expansion.prior_type_ascription;
661                     self.cx.current_expansion.prior_type_ascription = mac.prior_type_ascription;
662                     let tok_result = expander.expand(self.cx, span, mac.args.tokens.clone());
663                     let result = if let Some(result) = fragment_kind.make_from(tok_result) {
664                         result
665                     } else {
666                         self.error_wrong_fragment_kind(fragment_kind, &mac, span);
667                         fragment_kind.dummy(span)
668                     };
669                     self.cx.current_expansion.prior_type_ascription = prev;
670                     result
671                 }
672                 _ => unreachable!(),
673             },
674             InvocationKind::Attr { attr, pos, mut item, derives } => match ext {
675                 SyntaxExtensionKind::Attr(expander) => {
676                     self.gate_proc_macro_input(&item);
677                     self.gate_proc_macro_attr_item(span, &item);
678                     let tokens = match &item {
679                         // FIXME: Collect tokens and use them instead of generating
680                         // fake ones. These are unstable, so it needs to be
681                         // fixed prior to stabilization
682                         // Fake tokens when we are invoking an inner attribute, and
683                         // we are invoking it on an out-of-line module or crate.
684                         Annotatable::Crate(krate) => rustc_parse::fake_token_stream_for_crate(
685                             &self.cx.sess.parse_sess,
686                             krate,
687                         ),
688                         Annotatable::Item(item_inner)
689                             if matches!(attr.style, AttrStyle::Inner)
690                                 && matches!(
691                                     item_inner.kind,
692                                     ItemKind::Mod(
693                                         _,
694                                         ModKind::Unloaded | ModKind::Loaded(_, Inline::No, _),
695                                     )
696                                 ) =>
697                         {
698                             rustc_parse::fake_token_stream_for_item(
699                                 &self.cx.sess.parse_sess,
700                                 item_inner,
701                             )
702                         }
703                         _ => item.to_tokens(),
704                     };
705                     let attr_item = attr.unwrap_normal_item();
706                     if let AttrArgs::Eq(..) = attr_item.args {
707                         self.cx.emit_err(UnsupportedKeyValue { span });
708                     }
709                     let inner_tokens = attr_item.args.inner_tokens();
710                     let Ok(tok_result) = expander.expand(self.cx, span, inner_tokens, tokens) else {
711                         return ExpandResult::Ready(fragment_kind.dummy(span));
712                     };
713                     self.parse_ast_fragment(tok_result, fragment_kind, &attr_item.path, span)
714                 }
715                 SyntaxExtensionKind::LegacyAttr(expander) => {
716                     match validate_attr::parse_meta(&self.cx.sess.parse_sess, &attr) {
717                         Ok(meta) => {
718                             let items = match expander.expand(self.cx, span, &meta, item, false) {
719                                 ExpandResult::Ready(items) => items,
720                                 ExpandResult::Retry(item) => {
721                                     // Reassemble the original invocation for retrying.
722                                     return ExpandResult::Retry(Invocation {
723                                         kind: InvocationKind::Attr { attr, pos, item, derives },
724                                         ..invoc
725                                     });
726                                 }
727                             };
728                             if fragment_kind == AstFragmentKind::Expr && items.is_empty() {
729                                 self.cx.emit_err(RemoveExprNotSupported { span });
730                                 fragment_kind.dummy(span)
731                             } else {
732                                 fragment_kind.expect_from_annotatables(items)
733                             }
734                         }
735                         Err(mut err) => {
736                             err.emit();
737                             fragment_kind.dummy(span)
738                         }
739                     }
740                 }
741                 SyntaxExtensionKind::NonMacroAttr => {
742                     self.cx.expanded_inert_attrs.mark(&attr);
743                     item.visit_attrs(|attrs| attrs.insert(pos, attr));
744                     fragment_kind.expect_from_annotatables(iter::once(item))
745                 }
746                 _ => unreachable!(),
747             },
748             InvocationKind::Derive { path, item, is_const } => match ext {
749                 SyntaxExtensionKind::Derive(expander)
750                 | SyntaxExtensionKind::LegacyDerive(expander) => {
751                     if let SyntaxExtensionKind::Derive(..) = ext {
752                         self.gate_proc_macro_input(&item);
753                     }
754                     let meta = ast::MetaItem { kind: MetaItemKind::Word, span, path };
755                     let items = match expander.expand(self.cx, span, &meta, item, is_const) {
756                         ExpandResult::Ready(items) => items,
757                         ExpandResult::Retry(item) => {
758                             // Reassemble the original invocation for retrying.
759                             return ExpandResult::Retry(Invocation {
760                                 kind: InvocationKind::Derive { path: meta.path, item, is_const },
761                                 ..invoc
762                             });
763                         }
764                     };
765                     fragment_kind.expect_from_annotatables(items)
766                 }
767                 _ => unreachable!(),
768             },
769         })
770     }
771
772     fn gate_proc_macro_attr_item(&self, span: Span, item: &Annotatable) {
773         let kind = match item {
774             Annotatable::Item(_)
775             | Annotatable::TraitItem(_)
776             | Annotatable::ImplItem(_)
777             | Annotatable::ForeignItem(_)
778             | Annotatable::Crate(..) => return,
779             Annotatable::Stmt(stmt) => {
780                 // Attributes are stable on item statements,
781                 // but unstable on all other kinds of statements
782                 if stmt.is_item() {
783                     return;
784                 }
785                 "statements"
786             }
787             Annotatable::Expr(_) => "expressions",
788             Annotatable::Arm(..)
789             | Annotatable::ExprField(..)
790             | Annotatable::PatField(..)
791             | Annotatable::GenericParam(..)
792             | Annotatable::Param(..)
793             | Annotatable::FieldDef(..)
794             | Annotatable::Variant(..) => panic!("unexpected annotatable"),
795         };
796         if self.cx.ecfg.proc_macro_hygiene() {
797             return;
798         }
799         feature_err(
800             &self.cx.sess.parse_sess,
801             sym::proc_macro_hygiene,
802             span,
803             &format!("custom attributes cannot be applied to {}", kind),
804         )
805         .emit();
806     }
807
808     fn gate_proc_macro_input(&self, annotatable: &Annotatable) {
809         struct GateProcMacroInput<'a> {
810             parse_sess: &'a ParseSess,
811         }
812
813         impl<'ast, 'a> Visitor<'ast> for GateProcMacroInput<'a> {
814             fn visit_item(&mut self, item: &'ast ast::Item) {
815                 match &item.kind {
816                     ItemKind::Mod(_, mod_kind)
817                         if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _)) =>
818                     {
819                         feature_err(
820                             self.parse_sess,
821                             sym::proc_macro_hygiene,
822                             item.span,
823                             "non-inline modules in proc macro input are unstable",
824                         )
825                         .emit();
826                     }
827                     _ => {}
828                 }
829
830                 visit::walk_item(self, item);
831             }
832         }
833
834         if !self.cx.ecfg.proc_macro_hygiene() {
835             annotatable
836                 .visit_with(&mut GateProcMacroInput { parse_sess: &self.cx.sess.parse_sess });
837         }
838     }
839
840     fn parse_ast_fragment(
841         &mut self,
842         toks: TokenStream,
843         kind: AstFragmentKind,
844         path: &ast::Path,
845         span: Span,
846     ) -> AstFragment {
847         let mut parser = self.cx.new_parser_from_tts(toks);
848         match parse_ast_fragment(&mut parser, kind) {
849             Ok(fragment) => {
850                 ensure_complete_parse(&mut parser, path, kind.name(), span);
851                 fragment
852             }
853             Err(mut err) => {
854                 if err.span.is_dummy() {
855                     err.set_span(span);
856                 }
857                 annotate_err_with_kind(&mut err, kind, span);
858                 err.emit();
859                 self.cx.trace_macros_diag();
860                 kind.dummy(span)
861             }
862         }
863     }
864 }
865
866 pub fn parse_ast_fragment<'a>(
867     this: &mut Parser<'a>,
868     kind: AstFragmentKind,
869 ) -> PResult<'a, AstFragment> {
870     Ok(match kind {
871         AstFragmentKind::Items => {
872             let mut items = SmallVec::new();
873             while let Some(item) = this.parse_item(ForceCollect::No)? {
874                 items.push(item);
875             }
876             AstFragment::Items(items)
877         }
878         AstFragmentKind::TraitItems => {
879             let mut items = SmallVec::new();
880             while let Some(item) = this.parse_trait_item(ForceCollect::No)? {
881                 items.extend(item);
882             }
883             AstFragment::TraitItems(items)
884         }
885         AstFragmentKind::ImplItems => {
886             let mut items = SmallVec::new();
887             while let Some(item) = this.parse_impl_item(ForceCollect::No)? {
888                 items.extend(item);
889             }
890             AstFragment::ImplItems(items)
891         }
892         AstFragmentKind::ForeignItems => {
893             let mut items = SmallVec::new();
894             while let Some(item) = this.parse_foreign_item(ForceCollect::No)? {
895                 items.extend(item);
896             }
897             AstFragment::ForeignItems(items)
898         }
899         AstFragmentKind::Stmts => {
900             let mut stmts = SmallVec::new();
901             // Won't make progress on a `}`.
902             while this.token != token::Eof && this.token != token::CloseDelim(Delimiter::Brace) {
903                 if let Some(stmt) = this.parse_full_stmt(AttemptLocalParseRecovery::Yes)? {
904                     stmts.push(stmt);
905                 }
906             }
907             AstFragment::Stmts(stmts)
908         }
909         AstFragmentKind::Expr => AstFragment::Expr(this.parse_expr()?),
910         AstFragmentKind::MethodReceiverExpr => AstFragment::MethodReceiverExpr(this.parse_expr()?),
911         AstFragmentKind::OptExpr => {
912             if this.token != token::Eof {
913                 AstFragment::OptExpr(Some(this.parse_expr()?))
914             } else {
915                 AstFragment::OptExpr(None)
916             }
917         }
918         AstFragmentKind::Ty => AstFragment::Ty(this.parse_ty()?),
919         AstFragmentKind::Pat => AstFragment::Pat(this.parse_pat_allow_top_alt(
920             None,
921             RecoverComma::No,
922             RecoverColon::Yes,
923             CommaRecoveryMode::LikelyTuple,
924         )?),
925         AstFragmentKind::Crate => AstFragment::Crate(this.parse_crate_mod()?),
926         AstFragmentKind::Arms
927         | AstFragmentKind::ExprFields
928         | AstFragmentKind::PatFields
929         | AstFragmentKind::GenericParams
930         | AstFragmentKind::Params
931         | AstFragmentKind::FieldDefs
932         | AstFragmentKind::Variants => panic!("unexpected AST fragment kind"),
933     })
934 }
935
936 pub fn ensure_complete_parse<'a>(
937     parser: &mut Parser<'a>,
938     macro_path: &ast::Path,
939     kind_name: &str,
940     span: Span,
941 ) {
942     if parser.token != token::Eof {
943         let token = pprust::token_to_string(&parser.token);
944         // Avoid emitting backtrace info twice.
945         let def_site_span = parser.token.span.with_ctxt(SyntaxContext::root());
946
947         let semi_span = parser.sess.source_map().next_point(span);
948         let add_semicolon = match &parser.sess.source_map().span_to_snippet(semi_span) {
949             Ok(snippet) if &snippet[..] != ";" && kind_name == "expression" => {
950                 Some(span.shrink_to_hi())
951             }
952             _ => None,
953         };
954
955         parser.sess.emit_err(IncompleteParse {
956             span: def_site_span,
957             token,
958             label_span: span,
959             macro_path,
960             kind_name,
961             add_semicolon,
962         });
963     }
964 }
965
966 /// Wraps a call to `noop_visit_*` / `noop_flat_map_*`
967 /// for an AST node that supports attributes
968 /// (see the `Annotatable` enum)
969 /// This method assigns a `NodeId`, and sets that `NodeId`
970 /// as our current 'lint node id'. If a macro call is found
971 /// inside this AST node, we will use this AST node's `NodeId`
972 /// to emit lints associated with that macro (allowing
973 /// `#[allow]` / `#[deny]` to be applied close to
974 /// the macro invocation).
975 ///
976 /// Do *not* call this for a macro AST node
977 /// (e.g. `ExprKind::MacCall`) - we cannot emit lints
978 /// at these AST nodes, since they are removed and
979 /// replaced with the result of macro expansion.
980 ///
981 /// All other `NodeId`s are assigned by `visit_id`.
982 /// * `self` is the 'self' parameter for the current method,
983 /// * `id` is a mutable reference to the `NodeId` field
984 ///    of the current AST node.
985 /// * `closure` is a closure that executes the
986 ///   `noop_visit_*` / `noop_flat_map_*` method
987 ///   for the current AST node.
988 macro_rules! assign_id {
989     ($self:ident, $id:expr, $closure:expr) => {{
990         let old_id = $self.cx.current_expansion.lint_node_id;
991         if $self.monotonic {
992             debug_assert_eq!(*$id, ast::DUMMY_NODE_ID);
993             let new_id = $self.cx.resolver.next_node_id();
994             *$id = new_id;
995             $self.cx.current_expansion.lint_node_id = new_id;
996         }
997         let ret = ($closure)();
998         $self.cx.current_expansion.lint_node_id = old_id;
999         ret
1000     }};
1001 }
1002
1003 enum AddSemicolon {
1004     Yes,
1005     No,
1006 }
1007
1008 /// A trait implemented for all `AstFragment` nodes and providing all pieces
1009 /// of functionality used by `InvocationCollector`.
1010 trait InvocationCollectorNode: HasAttrs + HasNodeId + Sized {
1011     type OutputTy = SmallVec<[Self; 1]>;
1012     type AttrsTy: Deref<Target = [ast::Attribute]> = ast::AttrVec;
1013     const KIND: AstFragmentKind;
1014     fn to_annotatable(self) -> Annotatable;
1015     fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy;
1016     fn descr() -> &'static str {
1017         unreachable!()
1018     }
1019     fn noop_flat_map<V: MutVisitor>(self, _visitor: &mut V) -> Self::OutputTy {
1020         unreachable!()
1021     }
1022     fn noop_visit<V: MutVisitor>(&mut self, _visitor: &mut V) {
1023         unreachable!()
1024     }
1025     fn is_mac_call(&self) -> bool {
1026         false
1027     }
1028     fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1029         unreachable!()
1030     }
1031     fn pre_flat_map_node_collect_attr(_cfg: &StripUnconfigured<'_>, _attr: &ast::Attribute) {}
1032     fn post_flat_map_node_collect_bang(_output: &mut Self::OutputTy, _add_semicolon: AddSemicolon) {
1033     }
1034     fn wrap_flat_map_node_noop_flat_map(
1035         node: Self,
1036         collector: &mut InvocationCollector<'_, '_>,
1037         noop_flat_map: impl FnOnce(Self, &mut InvocationCollector<'_, '_>) -> Self::OutputTy,
1038     ) -> Result<Self::OutputTy, Self> {
1039         Ok(noop_flat_map(node, collector))
1040     }
1041 }
1042
1043 impl InvocationCollectorNode for P<ast::Item> {
1044     const KIND: AstFragmentKind = AstFragmentKind::Items;
1045     fn to_annotatable(self) -> Annotatable {
1046         Annotatable::Item(self)
1047     }
1048     fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1049         fragment.make_items()
1050     }
1051     fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1052         noop_flat_map_item(self, visitor)
1053     }
1054     fn is_mac_call(&self) -> bool {
1055         matches!(self.kind, ItemKind::MacCall(..))
1056     }
1057     fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1058         let node = self.into_inner();
1059         match node.kind {
1060             ItemKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
1061             _ => unreachable!(),
1062         }
1063     }
1064     fn wrap_flat_map_node_noop_flat_map(
1065         mut node: Self,
1066         collector: &mut InvocationCollector<'_, '_>,
1067         noop_flat_map: impl FnOnce(Self, &mut InvocationCollector<'_, '_>) -> Self::OutputTy,
1068     ) -> Result<Self::OutputTy, Self> {
1069         if !matches!(node.kind, ItemKind::Mod(..)) {
1070             return Ok(noop_flat_map(node, collector));
1071         }
1072
1073         // Work around borrow checker not seeing through `P`'s deref.
1074         let (ident, span, mut attrs) = (node.ident, node.span, mem::take(&mut node.attrs));
1075         let ItemKind::Mod(_, mod_kind) = &mut node.kind else {
1076             unreachable!()
1077         };
1078
1079         let ecx = &mut collector.cx;
1080         let (file_path, dir_path, dir_ownership) = match mod_kind {
1081             ModKind::Loaded(_, inline, _) => {
1082                 // Inline `mod foo { ... }`, but we still need to push directories.
1083                 let (dir_path, dir_ownership) = mod_dir_path(
1084                     &ecx.sess,
1085                     ident,
1086                     &attrs,
1087                     &ecx.current_expansion.module,
1088                     ecx.current_expansion.dir_ownership,
1089                     *inline,
1090                 );
1091                 node.attrs = attrs;
1092                 (None, dir_path, dir_ownership)
1093             }
1094             ModKind::Unloaded => {
1095                 // We have an outline `mod foo;` so we need to parse the file.
1096                 let old_attrs_len = attrs.len();
1097                 let ParsedExternalMod { items, spans, file_path, dir_path, dir_ownership } =
1098                     parse_external_mod(
1099                         &ecx.sess,
1100                         ident,
1101                         span,
1102                         &ecx.current_expansion.module,
1103                         ecx.current_expansion.dir_ownership,
1104                         &mut attrs,
1105                     );
1106
1107                 if let Some(lint_store) = ecx.lint_store {
1108                     lint_store.pre_expansion_lint(
1109                         ecx.sess,
1110                         ecx.resolver.registered_tools(),
1111                         ecx.current_expansion.lint_node_id,
1112                         &attrs,
1113                         &items,
1114                         ident.name,
1115                     );
1116                 }
1117
1118                 *mod_kind = ModKind::Loaded(items, Inline::No, spans);
1119                 node.attrs = attrs;
1120                 if node.attrs.len() > old_attrs_len {
1121                     // If we loaded an out-of-line module and added some inner attributes,
1122                     // then we need to re-configure it and re-collect attributes for
1123                     // resolution and expansion.
1124                     return Err(node);
1125                 }
1126                 (Some(file_path), dir_path, dir_ownership)
1127             }
1128         };
1129
1130         // Set the module info before we flat map.
1131         let mut module = ecx.current_expansion.module.with_dir_path(dir_path);
1132         module.mod_path.push(ident);
1133         if let Some(file_path) = file_path {
1134             module.file_path_stack.push(file_path);
1135         }
1136
1137         let orig_module = mem::replace(&mut ecx.current_expansion.module, Rc::new(module));
1138         let orig_dir_ownership =
1139             mem::replace(&mut ecx.current_expansion.dir_ownership, dir_ownership);
1140
1141         let res = Ok(noop_flat_map(node, collector));
1142
1143         collector.cx.current_expansion.dir_ownership = orig_dir_ownership;
1144         collector.cx.current_expansion.module = orig_module;
1145         res
1146     }
1147 }
1148
1149 struct TraitItemTag;
1150 impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, TraitItemTag> {
1151     type OutputTy = SmallVec<[P<ast::AssocItem>; 1]>;
1152     const KIND: AstFragmentKind = AstFragmentKind::TraitItems;
1153     fn to_annotatable(self) -> Annotatable {
1154         Annotatable::TraitItem(self.wrapped)
1155     }
1156     fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1157         fragment.make_trait_items()
1158     }
1159     fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1160         noop_flat_map_assoc_item(self.wrapped, visitor)
1161     }
1162     fn is_mac_call(&self) -> bool {
1163         matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
1164     }
1165     fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1166         let item = self.wrapped.into_inner();
1167         match item.kind {
1168             AssocItemKind::MacCall(mac) => (mac, item.attrs, AddSemicolon::No),
1169             _ => unreachable!(),
1170         }
1171     }
1172 }
1173
1174 struct ImplItemTag;
1175 impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, ImplItemTag> {
1176     type OutputTy = SmallVec<[P<ast::AssocItem>; 1]>;
1177     const KIND: AstFragmentKind = AstFragmentKind::ImplItems;
1178     fn to_annotatable(self) -> Annotatable {
1179         Annotatable::ImplItem(self.wrapped)
1180     }
1181     fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1182         fragment.make_impl_items()
1183     }
1184     fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1185         noop_flat_map_assoc_item(self.wrapped, visitor)
1186     }
1187     fn is_mac_call(&self) -> bool {
1188         matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
1189     }
1190     fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1191         let item = self.wrapped.into_inner();
1192         match item.kind {
1193             AssocItemKind::MacCall(mac) => (mac, item.attrs, AddSemicolon::No),
1194             _ => unreachable!(),
1195         }
1196     }
1197 }
1198
1199 impl InvocationCollectorNode for P<ast::ForeignItem> {
1200     const KIND: AstFragmentKind = AstFragmentKind::ForeignItems;
1201     fn to_annotatable(self) -> Annotatable {
1202         Annotatable::ForeignItem(self)
1203     }
1204     fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1205         fragment.make_foreign_items()
1206     }
1207     fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1208         noop_flat_map_foreign_item(self, visitor)
1209     }
1210     fn is_mac_call(&self) -> bool {
1211         matches!(self.kind, ForeignItemKind::MacCall(..))
1212     }
1213     fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1214         let node = self.into_inner();
1215         match node.kind {
1216             ForeignItemKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
1217             _ => unreachable!(),
1218         }
1219     }
1220 }
1221
1222 impl InvocationCollectorNode for ast::Variant {
1223     const KIND: AstFragmentKind = AstFragmentKind::Variants;
1224     fn to_annotatable(self) -> Annotatable {
1225         Annotatable::Variant(self)
1226     }
1227     fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1228         fragment.make_variants()
1229     }
1230     fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1231         noop_flat_map_variant(self, visitor)
1232     }
1233 }
1234
1235 impl InvocationCollectorNode for ast::FieldDef {
1236     const KIND: AstFragmentKind = AstFragmentKind::FieldDefs;
1237     fn to_annotatable(self) -> Annotatable {
1238         Annotatable::FieldDef(self)
1239     }
1240     fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1241         fragment.make_field_defs()
1242     }
1243     fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1244         noop_flat_map_field_def(self, visitor)
1245     }
1246 }
1247
1248 impl InvocationCollectorNode for ast::PatField {
1249     const KIND: AstFragmentKind = AstFragmentKind::PatFields;
1250     fn to_annotatable(self) -> Annotatable {
1251         Annotatable::PatField(self)
1252     }
1253     fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1254         fragment.make_pat_fields()
1255     }
1256     fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1257         noop_flat_map_pat_field(self, visitor)
1258     }
1259 }
1260
1261 impl InvocationCollectorNode for ast::ExprField {
1262     const KIND: AstFragmentKind = AstFragmentKind::ExprFields;
1263     fn to_annotatable(self) -> Annotatable {
1264         Annotatable::ExprField(self)
1265     }
1266     fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1267         fragment.make_expr_fields()
1268     }
1269     fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1270         noop_flat_map_expr_field(self, visitor)
1271     }
1272 }
1273
1274 impl InvocationCollectorNode for ast::Param {
1275     const KIND: AstFragmentKind = AstFragmentKind::Params;
1276     fn to_annotatable(self) -> Annotatable {
1277         Annotatable::Param(self)
1278     }
1279     fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1280         fragment.make_params()
1281     }
1282     fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1283         noop_flat_map_param(self, visitor)
1284     }
1285 }
1286
1287 impl InvocationCollectorNode for ast::GenericParam {
1288     const KIND: AstFragmentKind = AstFragmentKind::GenericParams;
1289     fn to_annotatable(self) -> Annotatable {
1290         Annotatable::GenericParam(self)
1291     }
1292     fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1293         fragment.make_generic_params()
1294     }
1295     fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1296         noop_flat_map_generic_param(self, visitor)
1297     }
1298 }
1299
1300 impl InvocationCollectorNode for ast::Arm {
1301     const KIND: AstFragmentKind = AstFragmentKind::Arms;
1302     fn to_annotatable(self) -> Annotatable {
1303         Annotatable::Arm(self)
1304     }
1305     fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1306         fragment.make_arms()
1307     }
1308     fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1309         noop_flat_map_arm(self, visitor)
1310     }
1311 }
1312
1313 impl InvocationCollectorNode for ast::Stmt {
1314     type AttrsTy = ast::AttrVec;
1315     const KIND: AstFragmentKind = AstFragmentKind::Stmts;
1316     fn to_annotatable(self) -> Annotatable {
1317         Annotatable::Stmt(P(self))
1318     }
1319     fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1320         fragment.make_stmts()
1321     }
1322     fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1323         noop_flat_map_stmt(self, visitor)
1324     }
1325     fn is_mac_call(&self) -> bool {
1326         match &self.kind {
1327             StmtKind::MacCall(..) => true,
1328             StmtKind::Item(item) => matches!(item.kind, ItemKind::MacCall(..)),
1329             StmtKind::Semi(expr) => matches!(expr.kind, ExprKind::MacCall(..)),
1330             StmtKind::Expr(..) => unreachable!(),
1331             StmtKind::Local(..) | StmtKind::Empty => false,
1332         }
1333     }
1334     fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1335         // We pull macro invocations (both attributes and fn-like macro calls) out of their
1336         // `StmtKind`s and treat them as statement macro invocations, not as items or expressions.
1337         let (add_semicolon, mac, attrs) = match self.kind {
1338             StmtKind::MacCall(mac) => {
1339                 let ast::MacCallStmt { mac, style, attrs, .. } = mac.into_inner();
1340                 (style == MacStmtStyle::Semicolon, mac, attrs)
1341             }
1342             StmtKind::Item(item) => match item.into_inner() {
1343                 ast::Item { kind: ItemKind::MacCall(mac), attrs, .. } => {
1344                     (mac.args.need_semicolon(), mac, attrs)
1345                 }
1346                 _ => unreachable!(),
1347             },
1348             StmtKind::Semi(expr) => match expr.into_inner() {
1349                 ast::Expr { kind: ExprKind::MacCall(mac), attrs, .. } => {
1350                     (mac.args.need_semicolon(), mac, attrs)
1351                 }
1352                 _ => unreachable!(),
1353             },
1354             _ => unreachable!(),
1355         };
1356         (mac, attrs, if add_semicolon { AddSemicolon::Yes } else { AddSemicolon::No })
1357     }
1358     fn post_flat_map_node_collect_bang(stmts: &mut Self::OutputTy, add_semicolon: AddSemicolon) {
1359         // If this is a macro invocation with a semicolon, then apply that
1360         // semicolon to the final statement produced by expansion.
1361         if matches!(add_semicolon, AddSemicolon::Yes) {
1362             if let Some(stmt) = stmts.pop() {
1363                 stmts.push(stmt.add_trailing_semicolon());
1364             }
1365         }
1366     }
1367 }
1368
1369 impl InvocationCollectorNode for ast::Crate {
1370     type OutputTy = ast::Crate;
1371     const KIND: AstFragmentKind = AstFragmentKind::Crate;
1372     fn to_annotatable(self) -> Annotatable {
1373         Annotatable::Crate(self)
1374     }
1375     fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1376         fragment.make_crate()
1377     }
1378     fn noop_visit<V: MutVisitor>(&mut self, visitor: &mut V) {
1379         noop_visit_crate(self, visitor)
1380     }
1381 }
1382
1383 impl InvocationCollectorNode for P<ast::Ty> {
1384     type OutputTy = P<ast::Ty>;
1385     const KIND: AstFragmentKind = AstFragmentKind::Ty;
1386     fn to_annotatable(self) -> Annotatable {
1387         unreachable!()
1388     }
1389     fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1390         fragment.make_ty()
1391     }
1392     fn noop_visit<V: MutVisitor>(&mut self, visitor: &mut V) {
1393         noop_visit_ty(self, visitor)
1394     }
1395     fn is_mac_call(&self) -> bool {
1396         matches!(self.kind, ast::TyKind::MacCall(..))
1397     }
1398     fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1399         let node = self.into_inner();
1400         match node.kind {
1401             TyKind::MacCall(mac) => (mac, AttrVec::new(), AddSemicolon::No),
1402             _ => unreachable!(),
1403         }
1404     }
1405 }
1406
1407 impl InvocationCollectorNode for P<ast::Pat> {
1408     type OutputTy = P<ast::Pat>;
1409     const KIND: AstFragmentKind = AstFragmentKind::Pat;
1410     fn to_annotatable(self) -> Annotatable {
1411         unreachable!()
1412     }
1413     fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1414         fragment.make_pat()
1415     }
1416     fn noop_visit<V: MutVisitor>(&mut self, visitor: &mut V) {
1417         noop_visit_pat(self, visitor)
1418     }
1419     fn is_mac_call(&self) -> bool {
1420         matches!(self.kind, PatKind::MacCall(..))
1421     }
1422     fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1423         let node = self.into_inner();
1424         match node.kind {
1425             PatKind::MacCall(mac) => (mac, AttrVec::new(), AddSemicolon::No),
1426             _ => unreachable!(),
1427         }
1428     }
1429 }
1430
1431 impl InvocationCollectorNode for P<ast::Expr> {
1432     type OutputTy = P<ast::Expr>;
1433     type AttrsTy = ast::AttrVec;
1434     const KIND: AstFragmentKind = AstFragmentKind::Expr;
1435     fn to_annotatable(self) -> Annotatable {
1436         Annotatable::Expr(self)
1437     }
1438     fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1439         fragment.make_expr()
1440     }
1441     fn descr() -> &'static str {
1442         "an expression"
1443     }
1444     fn noop_visit<V: MutVisitor>(&mut self, visitor: &mut V) {
1445         noop_visit_expr(self, visitor)
1446     }
1447     fn is_mac_call(&self) -> bool {
1448         matches!(self.kind, ExprKind::MacCall(..))
1449     }
1450     fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1451         let node = self.into_inner();
1452         match node.kind {
1453             ExprKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
1454             _ => unreachable!(),
1455         }
1456     }
1457 }
1458
1459 struct OptExprTag;
1460 impl InvocationCollectorNode for AstNodeWrapper<P<ast::Expr>, OptExprTag> {
1461     type OutputTy = Option<P<ast::Expr>>;
1462     type AttrsTy = ast::AttrVec;
1463     const KIND: AstFragmentKind = AstFragmentKind::OptExpr;
1464     fn to_annotatable(self) -> Annotatable {
1465         Annotatable::Expr(self.wrapped)
1466     }
1467     fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1468         fragment.make_opt_expr()
1469     }
1470     fn noop_flat_map<V: MutVisitor>(mut self, visitor: &mut V) -> Self::OutputTy {
1471         noop_visit_expr(&mut self.wrapped, visitor);
1472         Some(self.wrapped)
1473     }
1474     fn is_mac_call(&self) -> bool {
1475         matches!(self.wrapped.kind, ast::ExprKind::MacCall(..))
1476     }
1477     fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1478         let node = self.wrapped.into_inner();
1479         match node.kind {
1480             ExprKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
1481             _ => unreachable!(),
1482         }
1483     }
1484     fn pre_flat_map_node_collect_attr(cfg: &StripUnconfigured<'_>, attr: &ast::Attribute) {
1485         cfg.maybe_emit_expr_attr_err(&attr);
1486     }
1487 }
1488
1489 /// This struct is a hack to workaround unstable of `stmt_expr_attributes`.
1490 /// It can be removed once that feature is stabilized.
1491 struct MethodReceiverTag;
1492 impl DummyAstNode for MethodReceiverTag {
1493     fn dummy() -> MethodReceiverTag {
1494         MethodReceiverTag
1495     }
1496 }
1497 impl InvocationCollectorNode for AstNodeWrapper<P<ast::Expr>, MethodReceiverTag> {
1498     type OutputTy = Self;
1499     type AttrsTy = ast::AttrVec;
1500     const KIND: AstFragmentKind = AstFragmentKind::MethodReceiverExpr;
1501     fn descr() -> &'static str {
1502         "an expression"
1503     }
1504     fn to_annotatable(self) -> Annotatable {
1505         Annotatable::Expr(self.wrapped)
1506     }
1507     fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1508         AstNodeWrapper::new(fragment.make_method_receiver_expr(), MethodReceiverTag)
1509     }
1510     fn noop_visit<V: MutVisitor>(&mut self, visitor: &mut V) {
1511         noop_visit_expr(&mut self.wrapped, visitor)
1512     }
1513     fn is_mac_call(&self) -> bool {
1514         matches!(self.wrapped.kind, ast::ExprKind::MacCall(..))
1515     }
1516     fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1517         let node = self.wrapped.into_inner();
1518         match node.kind {
1519             ExprKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
1520             _ => unreachable!(),
1521         }
1522     }
1523 }
1524
1525 struct InvocationCollector<'a, 'b> {
1526     cx: &'a mut ExtCtxt<'b>,
1527     invocations: Vec<(Invocation, Option<Lrc<SyntaxExtension>>)>,
1528     monotonic: bool,
1529 }
1530
1531 impl<'a, 'b> InvocationCollector<'a, 'b> {
1532     fn cfg(&self) -> StripUnconfigured<'_> {
1533         StripUnconfigured {
1534             sess: &self.cx.sess,
1535             features: self.cx.ecfg.features,
1536             config_tokens: false,
1537             lint_node_id: self.cx.current_expansion.lint_node_id,
1538         }
1539     }
1540
1541     fn collect(&mut self, fragment_kind: AstFragmentKind, kind: InvocationKind) -> AstFragment {
1542         let expn_id = LocalExpnId::fresh_empty();
1543         let vis = kind.placeholder_visibility();
1544         self.invocations.push((
1545             Invocation {
1546                 kind,
1547                 fragment_kind,
1548                 expansion_data: ExpansionData {
1549                     id: expn_id,
1550                     depth: self.cx.current_expansion.depth + 1,
1551                     ..self.cx.current_expansion.clone()
1552                 },
1553             },
1554             None,
1555         ));
1556         placeholder(fragment_kind, NodeId::placeholder_from_expn_id(expn_id), vis)
1557     }
1558
1559     fn collect_bang(&mut self, mac: P<ast::MacCall>, kind: AstFragmentKind) -> AstFragment {
1560         // cache the macro call span so that it can be
1561         // easily adjusted for incremental compilation
1562         let span = mac.span();
1563         self.collect(kind, InvocationKind::Bang { mac, span })
1564     }
1565
1566     fn collect_attr(
1567         &mut self,
1568         (attr, pos, derives): (ast::Attribute, usize, Vec<ast::Path>),
1569         item: Annotatable,
1570         kind: AstFragmentKind,
1571     ) -> AstFragment {
1572         self.collect(kind, InvocationKind::Attr { attr, pos, item, derives })
1573     }
1574
1575     /// If `item` is an attribute invocation, remove the attribute and return it together with
1576     /// its position and derives following it. We have to collect the derives in order to resolve
1577     /// legacy derive helpers (helpers written before derives that introduce them).
1578     fn take_first_attr(
1579         &self,
1580         item: &mut impl HasAttrs,
1581     ) -> Option<(ast::Attribute, usize, Vec<ast::Path>)> {
1582         let mut attr = None;
1583
1584         let mut cfg_pos = None;
1585         let mut attr_pos = None;
1586         for (pos, attr) in item.attrs().iter().enumerate() {
1587             if !attr.is_doc_comment() && !self.cx.expanded_inert_attrs.is_marked(attr) {
1588                 let name = attr.ident().map(|ident| ident.name);
1589                 if name == Some(sym::cfg) || name == Some(sym::cfg_attr) {
1590                     cfg_pos = Some(pos); // a cfg attr found, no need to search anymore
1591                     break;
1592                 } else if attr_pos.is_none()
1593                     && !name.map_or(false, rustc_feature::is_builtin_attr_name)
1594                 {
1595                     attr_pos = Some(pos); // a non-cfg attr found, still may find a cfg attr
1596                 }
1597             }
1598         }
1599
1600         item.visit_attrs(|attrs| {
1601             attr = Some(match (cfg_pos, attr_pos) {
1602                 (Some(pos), _) => (attrs.remove(pos), pos, Vec::new()),
1603                 (_, Some(pos)) => {
1604                     let attr = attrs.remove(pos);
1605                     let following_derives = attrs[pos..]
1606                         .iter()
1607                         .filter(|a| a.has_name(sym::derive))
1608                         .flat_map(|a| a.meta_item_list().unwrap_or_default())
1609                         .filter_map(|nested_meta| match nested_meta {
1610                             NestedMetaItem::MetaItem(ast::MetaItem {
1611                                 kind: MetaItemKind::Word,
1612                                 path,
1613                                 ..
1614                             }) => Some(path),
1615                             _ => None,
1616                         })
1617                         .collect();
1618
1619                     (attr, pos, following_derives)
1620                 }
1621                 _ => return,
1622             });
1623         });
1624
1625         attr
1626     }
1627
1628     // Detect use of feature-gated or invalid attributes on macro invocations
1629     // since they will not be detected after macro expansion.
1630     fn check_attributes(&self, attrs: &[ast::Attribute], call: &ast::MacCall) {
1631         let features = self.cx.ecfg.features.unwrap();
1632         let mut attrs = attrs.iter().peekable();
1633         let mut span: Option<Span> = None;
1634         while let Some(attr) = attrs.next() {
1635             rustc_ast_passes::feature_gate::check_attribute(attr, self.cx.sess, features);
1636             validate_attr::check_attr(&self.cx.sess.parse_sess, attr);
1637
1638             let current_span = if let Some(sp) = span { sp.to(attr.span) } else { attr.span };
1639             span = Some(current_span);
1640
1641             if attrs.peek().map_or(false, |next_attr| next_attr.doc_str().is_some()) {
1642                 continue;
1643             }
1644
1645             if attr.is_doc_comment() {
1646                 self.cx.sess.parse_sess.buffer_lint_with_diagnostic(
1647                     &UNUSED_DOC_COMMENTS,
1648                     current_span,
1649                     self.cx.current_expansion.lint_node_id,
1650                     "unused doc comment",
1651                     BuiltinLintDiagnostics::UnusedDocComment(attr.span),
1652                 );
1653             } else if rustc_attr::is_builtin_attr(attr) {
1654                 let attr_name = attr.ident().unwrap().name;
1655                 // `#[cfg]` and `#[cfg_attr]` are special - they are
1656                 // eagerly evaluated.
1657                 if attr_name != sym::cfg && attr_name != sym::cfg_attr {
1658                     self.cx.sess.parse_sess.buffer_lint_with_diagnostic(
1659                         &UNUSED_ATTRIBUTES,
1660                         attr.span,
1661                         self.cx.current_expansion.lint_node_id,
1662                         &format!("unused attribute `{}`", attr_name),
1663                         BuiltinLintDiagnostics::UnusedBuiltinAttribute {
1664                             attr_name,
1665                             macro_name: pprust::path_to_string(&call.path),
1666                             invoc_span: call.path.span,
1667                         },
1668                     );
1669                 }
1670             }
1671         }
1672     }
1673
1674     fn expand_cfg_true(
1675         &mut self,
1676         node: &mut impl HasAttrs,
1677         attr: ast::Attribute,
1678         pos: usize,
1679     ) -> bool {
1680         let res = self.cfg().cfg_true(&attr);
1681         if res {
1682             // FIXME: `cfg(TRUE)` attributes do not currently remove themselves during expansion,
1683             // and some tools like rustdoc and clippy rely on that. Find a way to remove them
1684             // while keeping the tools working.
1685             self.cx.expanded_inert_attrs.mark(&attr);
1686             node.visit_attrs(|attrs| attrs.insert(pos, attr));
1687         }
1688         res
1689     }
1690
1691     fn expand_cfg_attr(&self, node: &mut impl HasAttrs, attr: ast::Attribute, pos: usize) {
1692         node.visit_attrs(|attrs| {
1693             // Repeated `insert` calls is inefficient, but the number of
1694             // insertions is almost always 0 or 1 in practice.
1695             for cfg in self.cfg().expand_cfg_attr(attr, false).into_iter().rev() {
1696                 attrs.insert(pos, cfg)
1697             }
1698         });
1699     }
1700
1701     fn flat_map_node<Node: InvocationCollectorNode<OutputTy: Default>>(
1702         &mut self,
1703         mut node: Node,
1704     ) -> Node::OutputTy {
1705         loop {
1706             return match self.take_first_attr(&mut node) {
1707                 Some((attr, pos, derives)) => match attr.name_or_empty() {
1708                     sym::cfg => {
1709                         if self.expand_cfg_true(&mut node, attr, pos) {
1710                             continue;
1711                         }
1712                         Default::default()
1713                     }
1714                     sym::cfg_attr => {
1715                         self.expand_cfg_attr(&mut node, attr, pos);
1716                         continue;
1717                     }
1718                     _ => {
1719                         Node::pre_flat_map_node_collect_attr(&self.cfg(), &attr);
1720                         self.collect_attr((attr, pos, derives), node.to_annotatable(), Node::KIND)
1721                             .make_ast::<Node>()
1722                     }
1723                 },
1724                 None if node.is_mac_call() => {
1725                     let (mac, attrs, add_semicolon) = node.take_mac_call();
1726                     self.check_attributes(&attrs, &mac);
1727                     let mut res = self.collect_bang(mac, Node::KIND).make_ast::<Node>();
1728                     Node::post_flat_map_node_collect_bang(&mut res, add_semicolon);
1729                     res
1730                 }
1731                 None => {
1732                     match Node::wrap_flat_map_node_noop_flat_map(node, self, |mut node, this| {
1733                         assign_id!(this, node.node_id_mut(), || node.noop_flat_map(this))
1734                     }) {
1735                         Ok(output) => output,
1736                         Err(returned_node) => {
1737                             node = returned_node;
1738                             continue;
1739                         }
1740                     }
1741                 }
1742             };
1743         }
1744     }
1745
1746     fn visit_node<Node: InvocationCollectorNode<OutputTy = Node> + DummyAstNode>(
1747         &mut self,
1748         node: &mut Node,
1749     ) {
1750         loop {
1751             return match self.take_first_attr(node) {
1752                 Some((attr, pos, derives)) => match attr.name_or_empty() {
1753                     sym::cfg => {
1754                         let span = attr.span;
1755                         if self.expand_cfg_true(node, attr, pos) {
1756                             continue;
1757                         }
1758
1759                         self.cx.emit_err(RemoveNodeNotSupported { span, descr: Node::descr() });
1760                         continue;
1761                     }
1762                     sym::cfg_attr => {
1763                         self.expand_cfg_attr(node, attr, pos);
1764                         continue;
1765                     }
1766                     _ => visit_clobber(node, |node| {
1767                         self.collect_attr((attr, pos, derives), node.to_annotatable(), Node::KIND)
1768                             .make_ast::<Node>()
1769                     }),
1770                 },
1771                 None if node.is_mac_call() => {
1772                     visit_clobber(node, |node| {
1773                         // Do not clobber unless it's actually a macro (uncommon case).
1774                         let (mac, attrs, _) = node.take_mac_call();
1775                         self.check_attributes(&attrs, &mac);
1776                         self.collect_bang(mac, Node::KIND).make_ast::<Node>()
1777                     })
1778                 }
1779                 None => {
1780                     assign_id!(self, node.node_id_mut(), || node.noop_visit(self))
1781                 }
1782             };
1783         }
1784     }
1785 }
1786
1787 impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
1788     fn flat_map_item(&mut self, node: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
1789         self.flat_map_node(node)
1790     }
1791
1792     fn flat_map_trait_item(&mut self, node: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
1793         self.flat_map_node(AstNodeWrapper::new(node, TraitItemTag))
1794     }
1795
1796     fn flat_map_impl_item(&mut self, node: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
1797         self.flat_map_node(AstNodeWrapper::new(node, ImplItemTag))
1798     }
1799
1800     fn flat_map_foreign_item(
1801         &mut self,
1802         node: P<ast::ForeignItem>,
1803     ) -> SmallVec<[P<ast::ForeignItem>; 1]> {
1804         self.flat_map_node(node)
1805     }
1806
1807     fn flat_map_variant(&mut self, node: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
1808         self.flat_map_node(node)
1809     }
1810
1811     fn flat_map_field_def(&mut self, node: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
1812         self.flat_map_node(node)
1813     }
1814
1815     fn flat_map_pat_field(&mut self, node: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
1816         self.flat_map_node(node)
1817     }
1818
1819     fn flat_map_expr_field(&mut self, node: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
1820         self.flat_map_node(node)
1821     }
1822
1823     fn flat_map_param(&mut self, node: ast::Param) -> SmallVec<[ast::Param; 1]> {
1824         self.flat_map_node(node)
1825     }
1826
1827     fn flat_map_generic_param(
1828         &mut self,
1829         node: ast::GenericParam,
1830     ) -> SmallVec<[ast::GenericParam; 1]> {
1831         self.flat_map_node(node)
1832     }
1833
1834     fn flat_map_arm(&mut self, node: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
1835         self.flat_map_node(node)
1836     }
1837
1838     fn flat_map_stmt(&mut self, node: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
1839         // FIXME: invocations in semicolon-less expressions positions are expanded as expressions,
1840         // changing that requires some compatibility measures.
1841         if node.is_expr() {
1842             // The only way that we can end up with a `MacCall` expression statement,
1843             // (as opposed to a `StmtKind::MacCall`) is if we have a macro as the
1844             // trailing expression in a block (e.g. `fn foo() { my_macro!() }`).
1845             // Record this information, so that we can report a more specific
1846             // `SEMICOLON_IN_EXPRESSIONS_FROM_MACROS` lint if needed.
1847             // See #78991 for an investigation of treating macros in this position
1848             // as statements, rather than expressions, during parsing.
1849             return match &node.kind {
1850                 StmtKind::Expr(expr)
1851                     if matches!(**expr, ast::Expr { kind: ExprKind::MacCall(..), .. }) =>
1852                 {
1853                     self.cx.current_expansion.is_trailing_mac = true;
1854                     // Don't use `assign_id` for this statement - it may get removed
1855                     // entirely due to a `#[cfg]` on the contained expression
1856                     let res = noop_flat_map_stmt(node, self);
1857                     self.cx.current_expansion.is_trailing_mac = false;
1858                     res
1859                 }
1860                 _ => noop_flat_map_stmt(node, self),
1861             };
1862         }
1863
1864         self.flat_map_node(node)
1865     }
1866
1867     fn visit_crate(&mut self, node: &mut ast::Crate) {
1868         self.visit_node(node)
1869     }
1870
1871     fn visit_ty(&mut self, node: &mut P<ast::Ty>) {
1872         self.visit_node(node)
1873     }
1874
1875     fn visit_pat(&mut self, node: &mut P<ast::Pat>) {
1876         self.visit_node(node)
1877     }
1878
1879     fn visit_expr(&mut self, node: &mut P<ast::Expr>) {
1880         // FIXME: Feature gating is performed inconsistently between `Expr` and `OptExpr`.
1881         if let Some(attr) = node.attrs.first() {
1882             self.cfg().maybe_emit_expr_attr_err(attr);
1883         }
1884         self.visit_node(node)
1885     }
1886
1887     fn visit_method_receiver_expr(&mut self, node: &mut P<ast::Expr>) {
1888         visit_clobber(node, |node| {
1889             let mut wrapper = AstNodeWrapper::new(node, MethodReceiverTag);
1890             self.visit_node(&mut wrapper);
1891             wrapper.wrapped
1892         })
1893     }
1894
1895     fn filter_map_expr(&mut self, node: P<ast::Expr>) -> Option<P<ast::Expr>> {
1896         self.flat_map_node(AstNodeWrapper::new(node, OptExprTag))
1897     }
1898
1899     fn visit_block(&mut self, node: &mut P<ast::Block>) {
1900         let orig_dir_ownership = mem::replace(
1901             &mut self.cx.current_expansion.dir_ownership,
1902             DirOwnership::UnownedViaBlock,
1903         );
1904         noop_visit_block(node, self);
1905         self.cx.current_expansion.dir_ownership = orig_dir_ownership;
1906     }
1907
1908     fn visit_id(&mut self, id: &mut NodeId) {
1909         // We may have already assigned a `NodeId`
1910         // by calling `assign_id`
1911         if self.monotonic && *id == ast::DUMMY_NODE_ID {
1912             *id = self.cx.resolver.next_node_id();
1913         }
1914     }
1915 }
1916
1917 pub struct ExpansionConfig<'feat> {
1918     pub crate_name: String,
1919     pub features: Option<&'feat Features>,
1920     pub recursion_limit: Limit,
1921     pub trace_mac: bool,
1922     /// If false, strip `#[test]` nodes
1923     pub should_test: bool,
1924     /// If true, use verbose debugging for `proc_macro::Span`
1925     pub span_debug: bool,
1926     /// If true, show backtraces for proc-macro panics
1927     pub proc_macro_backtrace: bool,
1928 }
1929
1930 impl<'feat> ExpansionConfig<'feat> {
1931     pub fn default(crate_name: String) -> ExpansionConfig<'static> {
1932         ExpansionConfig {
1933             crate_name,
1934             features: None,
1935             recursion_limit: Limit::new(1024),
1936             trace_mac: false,
1937             should_test: false,
1938             span_debug: false,
1939             proc_macro_backtrace: false,
1940         }
1941     }
1942
1943     fn proc_macro_hygiene(&self) -> bool {
1944         self.features.map_or(false, |features| features.proc_macro_hygiene)
1945     }
1946 }