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