rustc_expand/
expand.rs

1use std::path::PathBuf;
2use std::rc::Rc;
3use std::sync::Arc;
4use std::{iter, mem};
5
6use rustc_ast::mut_visit::*;
7use rustc_ast::tokenstream::TokenStream;
8use rustc_ast::visit::{self, AssocCtxt, Visitor, VisitorResult, try_visit, walk_list};
9use rustc_ast::{
10    self as ast, AssocItemKind, AstNodeWrapper, AttrArgs, AttrStyle, AttrVec, DUMMY_NODE_ID,
11    ExprKind, ForeignItemKind, HasAttrs, HasNodeId, Inline, ItemKind, MacStmtStyle, MetaItemInner,
12    MetaItemKind, ModKind, NodeId, PatKind, StmtKind, TyKind, token,
13};
14use rustc_ast_pretty::pprust;
15use rustc_attr_parsing::{EvalConfigResult, ShouldEmit};
16use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
17use rustc_errors::PResult;
18use rustc_feature::Features;
19use rustc_parse::parser::{
20    AttemptLocalParseRecovery, CommaRecoveryMode, ForceCollect, Parser, RecoverColon, RecoverComma,
21    token_descr,
22};
23use rustc_parse::validate_attr;
24use rustc_session::lint::BuiltinLintDiag;
25use rustc_session::lint::builtin::{UNUSED_ATTRIBUTES, UNUSED_DOC_COMMENTS};
26use rustc_session::parse::feature_err;
27use rustc_session::{Limit, Session};
28use rustc_span::hygiene::SyntaxContext;
29use rustc_span::{ErrorGuaranteed, FileName, Ident, LocalExpnId, Span, Symbol, sym};
30use smallvec::SmallVec;
31
32use crate::base::*;
33use crate::config::{StripUnconfigured, attr_into_trace};
34use crate::errors::{
35    EmptyDelegationMac, GlobDelegationOutsideImpls, GlobDelegationTraitlessQpath, IncompleteParse,
36    RecursionLimitReached, RemoveExprNotSupported, RemoveNodeNotSupported, UnsupportedKeyValue,
37    WrongFragmentKind,
38};
39use crate::fluent_generated;
40use crate::mbe::diagnostics::annotate_err_with_kind;
41use crate::module::{
42    DirOwnership, ParsedExternalMod, mod_dir_path, mod_file_path_from_attr, parse_external_mod,
43};
44use crate::placeholders::{PlaceholderExpander, placeholder};
45use crate::stats::*;
46
47macro_rules! ast_fragments {
48    (
49        $($Kind:ident($AstTy:ty) {
50            $kind_name:expr;
51            $(one
52                fn $mut_visit_ast:ident;
53                fn $visit_ast:ident;
54                fn $ast_to_string:path;
55            )?
56            $(many
57                fn $flat_map_ast_elt:ident;
58                fn $visit_ast_elt:ident($($args:tt)*);
59                fn $ast_to_string_elt:path;
60            )?
61            fn $make_ast:ident;
62        })*
63    ) => {
64        /// A fragment of AST that can be produced by a single macro expansion.
65        /// Can also serve as an input and intermediate result for macro expansion operations.
66        pub enum AstFragment {
67            OptExpr(Option<Box<ast::Expr>>),
68            MethodReceiverExpr(Box<ast::Expr>),
69            $($Kind($AstTy),)*
70        }
71
72        /// "Discriminant" of an AST fragment.
73        #[derive(Copy, Clone, Debug, PartialEq, Eq)]
74        pub enum AstFragmentKind {
75            OptExpr,
76            MethodReceiverExpr,
77            $($Kind,)*
78        }
79
80        impl AstFragmentKind {
81            pub fn name(self) -> &'static str {
82                match self {
83                    AstFragmentKind::OptExpr => "expression",
84                    AstFragmentKind::MethodReceiverExpr => "expression",
85                    $(AstFragmentKind::$Kind => $kind_name,)*
86                }
87            }
88
89            fn make_from(self, result: Box<dyn MacResult + '_>) -> Option<AstFragment> {
90                match self {
91                    AstFragmentKind::OptExpr =>
92                        result.make_expr().map(Some).map(AstFragment::OptExpr),
93                    AstFragmentKind::MethodReceiverExpr =>
94                        result.make_expr().map(AstFragment::MethodReceiverExpr),
95                    $(AstFragmentKind::$Kind => result.$make_ast().map(AstFragment::$Kind),)*
96                }
97            }
98        }
99
100        impl AstFragment {
101            fn add_placeholders(&mut self, placeholders: &[NodeId]) {
102                if placeholders.is_empty() {
103                    return;
104                }
105                match self {
106                    $($(AstFragment::$Kind(ast) => ast.extend(placeholders.iter().flat_map(|id| {
107                        ${ignore($flat_map_ast_elt)}
108                        placeholder(AstFragmentKind::$Kind, *id, None).$make_ast()
109                    })),)?)*
110                    _ => panic!("unexpected AST fragment kind")
111                }
112            }
113
114            pub(crate) fn make_opt_expr(self) -> Option<Box<ast::Expr>> {
115                match self {
116                    AstFragment::OptExpr(expr) => expr,
117                    _ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
118                }
119            }
120
121            pub(crate) fn make_method_receiver_expr(self) -> Box<ast::Expr> {
122                match self {
123                    AstFragment::MethodReceiverExpr(expr) => expr,
124                    _ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
125                }
126            }
127
128            $(pub fn $make_ast(self) -> $AstTy {
129                match self {
130                    AstFragment::$Kind(ast) => ast,
131                    _ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
132                }
133            })*
134
135            fn make_ast<T: InvocationCollectorNode>(self) -> T::OutputTy {
136                T::fragment_to_output(self)
137            }
138
139            pub(crate) fn mut_visit_with(&mut self, vis: &mut impl MutVisitor) {
140                match self {
141                    AstFragment::OptExpr(opt_expr) => {
142                        if let Some(expr) = opt_expr.take() {
143                            *opt_expr = vis.filter_map_expr(expr)
144                        }
145                    }
146                    AstFragment::MethodReceiverExpr(expr) => vis.visit_method_receiver_expr(expr),
147                    $($(AstFragment::$Kind(ast) => vis.$mut_visit_ast(ast),)?)*
148                    $($(AstFragment::$Kind(ast) =>
149                        ast.flat_map_in_place(|ast| vis.$flat_map_ast_elt(ast, $($args)*)),)?)*
150                }
151            }
152
153            pub fn visit_with<'a, V: Visitor<'a>>(&'a self, visitor: &mut V) -> V::Result {
154                match self {
155                    AstFragment::OptExpr(Some(expr)) => try_visit!(visitor.visit_expr(expr)),
156                    AstFragment::OptExpr(None) => {}
157                    AstFragment::MethodReceiverExpr(expr) => try_visit!(visitor.visit_method_receiver_expr(expr)),
158                    $($(AstFragment::$Kind(ast) => try_visit!(visitor.$visit_ast(ast)),)?)*
159                    $($(AstFragment::$Kind(ast) => walk_list!(visitor, $visit_ast_elt, &ast[..], $($args)*),)?)*
160                }
161                V::Result::output()
162            }
163
164            pub(crate) fn to_string(&self) -> String {
165                match self {
166                    AstFragment::OptExpr(Some(expr)) => pprust::expr_to_string(expr),
167                    AstFragment::OptExpr(None) => unreachable!(),
168                    AstFragment::MethodReceiverExpr(expr) => pprust::expr_to_string(expr),
169                    $($(AstFragment::$Kind(ast) => $ast_to_string(ast),)?)*
170                    $($(
171                        AstFragment::$Kind(ast) => {
172                            // The closure unwraps a `P` if present, or does nothing otherwise.
173                            elems_to_string(&*ast, |ast| $ast_to_string_elt(&*ast))
174                        }
175                    )?)*
176                }
177            }
178        }
179
180        impl<'a> MacResult for crate::mbe::macro_rules::ParserAnyMacro<'a> {
181            $(fn $make_ast(self: Box<crate::mbe::macro_rules::ParserAnyMacro<'a>>)
182                           -> Option<$AstTy> {
183                Some(self.make(AstFragmentKind::$Kind).$make_ast())
184            })*
185        }
186    }
187}
188
189ast_fragments! {
190    Expr(Box<ast::Expr>) {
191        "expression";
192        one fn visit_expr; fn visit_expr; fn pprust::expr_to_string;
193        fn make_expr;
194    }
195    Pat(Box<ast::Pat>) {
196        "pattern";
197        one fn visit_pat; fn visit_pat; fn pprust::pat_to_string;
198        fn make_pat;
199    }
200    Ty(Box<ast::Ty>) {
201        "type";
202        one fn visit_ty; fn visit_ty; fn pprust::ty_to_string;
203        fn make_ty;
204    }
205    Stmts(SmallVec<[ast::Stmt; 1]>) {
206        "statement";
207        many fn flat_map_stmt; fn visit_stmt(); fn pprust::stmt_to_string;
208        fn make_stmts;
209    }
210    Items(SmallVec<[Box<ast::Item>; 1]>) {
211        "item";
212        many fn flat_map_item; fn visit_item(); fn pprust::item_to_string;
213        fn make_items;
214    }
215    TraitItems(SmallVec<[Box<ast::AssocItem>; 1]>) {
216        "trait item";
217        many fn flat_map_assoc_item; fn visit_assoc_item(AssocCtxt::Trait);
218            fn pprust::assoc_item_to_string;
219        fn make_trait_items;
220    }
221    ImplItems(SmallVec<[Box<ast::AssocItem>; 1]>) {
222        "impl item";
223        many fn flat_map_assoc_item; fn visit_assoc_item(AssocCtxt::Impl { of_trait: false });
224            fn pprust::assoc_item_to_string;
225        fn make_impl_items;
226    }
227    TraitImplItems(SmallVec<[Box<ast::AssocItem>; 1]>) {
228        "impl item";
229        many fn flat_map_assoc_item; fn visit_assoc_item(AssocCtxt::Impl { of_trait: true });
230            fn pprust::assoc_item_to_string;
231        fn make_trait_impl_items;
232    }
233    ForeignItems(SmallVec<[Box<ast::ForeignItem>; 1]>) {
234        "foreign item";
235        many fn flat_map_foreign_item; fn visit_foreign_item(); fn pprust::foreign_item_to_string;
236        fn make_foreign_items;
237    }
238    Arms(SmallVec<[ast::Arm; 1]>) {
239        "match arm";
240        many fn flat_map_arm; fn visit_arm(); fn unreachable_to_string;
241        fn make_arms;
242    }
243    ExprFields(SmallVec<[ast::ExprField; 1]>) {
244        "field expression";
245        many fn flat_map_expr_field; fn visit_expr_field(); fn unreachable_to_string;
246        fn make_expr_fields;
247    }
248    PatFields(SmallVec<[ast::PatField; 1]>) {
249        "field pattern";
250        many fn flat_map_pat_field; fn visit_pat_field(); fn unreachable_to_string;
251        fn make_pat_fields;
252    }
253    GenericParams(SmallVec<[ast::GenericParam; 1]>) {
254        "generic parameter";
255        many fn flat_map_generic_param; fn visit_generic_param(); fn unreachable_to_string;
256        fn make_generic_params;
257    }
258    Params(SmallVec<[ast::Param; 1]>) {
259        "function parameter";
260        many fn flat_map_param; fn visit_param(); fn unreachable_to_string;
261        fn make_params;
262    }
263    FieldDefs(SmallVec<[ast::FieldDef; 1]>) {
264        "field";
265        many fn flat_map_field_def; fn visit_field_def(); fn unreachable_to_string;
266        fn make_field_defs;
267    }
268    Variants(SmallVec<[ast::Variant; 1]>) {
269        "variant"; many fn flat_map_variant; fn visit_variant(); fn unreachable_to_string;
270        fn make_variants;
271    }
272    WherePredicates(SmallVec<[ast::WherePredicate; 1]>) {
273        "where predicate";
274        many fn flat_map_where_predicate; fn visit_where_predicate(); fn unreachable_to_string;
275        fn make_where_predicates;
276    }
277    Crate(ast::Crate) {
278        "crate";
279        one fn visit_crate; fn visit_crate; fn unreachable_to_string;
280        fn make_crate;
281    }
282}
283
284pub enum SupportsMacroExpansion {
285    No,
286    Yes { supports_inner_attrs: bool },
287}
288
289impl AstFragmentKind {
290    pub(crate) fn dummy(self, span: Span, guar: ErrorGuaranteed) -> AstFragment {
291        self.make_from(DummyResult::any(span, guar)).expect("couldn't create a dummy AST fragment")
292    }
293
294    pub fn supports_macro_expansion(self) -> SupportsMacroExpansion {
295        match self {
296            AstFragmentKind::OptExpr
297            | AstFragmentKind::Expr
298            | AstFragmentKind::MethodReceiverExpr
299            | AstFragmentKind::Stmts
300            | AstFragmentKind::Ty
301            | AstFragmentKind::Pat => SupportsMacroExpansion::Yes { supports_inner_attrs: false },
302            AstFragmentKind::Items
303            | AstFragmentKind::TraitItems
304            | AstFragmentKind::ImplItems
305            | AstFragmentKind::TraitImplItems
306            | AstFragmentKind::ForeignItems
307            | AstFragmentKind::Crate => SupportsMacroExpansion::Yes { supports_inner_attrs: true },
308            AstFragmentKind::Arms
309            | AstFragmentKind::ExprFields
310            | AstFragmentKind::PatFields
311            | AstFragmentKind::GenericParams
312            | AstFragmentKind::Params
313            | AstFragmentKind::FieldDefs
314            | AstFragmentKind::Variants
315            | AstFragmentKind::WherePredicates => SupportsMacroExpansion::No,
316        }
317    }
318
319    pub(crate) fn expect_from_annotatables(
320        self,
321        items: impl IntoIterator<Item = Annotatable>,
322    ) -> AstFragment {
323        let mut items = items.into_iter();
324        match self {
325            AstFragmentKind::Arms => {
326                AstFragment::Arms(items.map(Annotatable::expect_arm).collect())
327            }
328            AstFragmentKind::ExprFields => {
329                AstFragment::ExprFields(items.map(Annotatable::expect_expr_field).collect())
330            }
331            AstFragmentKind::PatFields => {
332                AstFragment::PatFields(items.map(Annotatable::expect_pat_field).collect())
333            }
334            AstFragmentKind::GenericParams => {
335                AstFragment::GenericParams(items.map(Annotatable::expect_generic_param).collect())
336            }
337            AstFragmentKind::Params => {
338                AstFragment::Params(items.map(Annotatable::expect_param).collect())
339            }
340            AstFragmentKind::FieldDefs => {
341                AstFragment::FieldDefs(items.map(Annotatable::expect_field_def).collect())
342            }
343            AstFragmentKind::Variants => {
344                AstFragment::Variants(items.map(Annotatable::expect_variant).collect())
345            }
346            AstFragmentKind::WherePredicates => AstFragment::WherePredicates(
347                items.map(Annotatable::expect_where_predicate).collect(),
348            ),
349            AstFragmentKind::Items => {
350                AstFragment::Items(items.map(Annotatable::expect_item).collect())
351            }
352            AstFragmentKind::ImplItems => {
353                AstFragment::ImplItems(items.map(Annotatable::expect_impl_item).collect())
354            }
355            AstFragmentKind::TraitImplItems => {
356                AstFragment::TraitImplItems(items.map(Annotatable::expect_impl_item).collect())
357            }
358            AstFragmentKind::TraitItems => {
359                AstFragment::TraitItems(items.map(Annotatable::expect_trait_item).collect())
360            }
361            AstFragmentKind::ForeignItems => {
362                AstFragment::ForeignItems(items.map(Annotatable::expect_foreign_item).collect())
363            }
364            AstFragmentKind::Stmts => {
365                AstFragment::Stmts(items.map(Annotatable::expect_stmt).collect())
366            }
367            AstFragmentKind::Expr => AstFragment::Expr(
368                items.next().expect("expected exactly one expression").expect_expr(),
369            ),
370            AstFragmentKind::MethodReceiverExpr => AstFragment::MethodReceiverExpr(
371                items.next().expect("expected exactly one expression").expect_expr(),
372            ),
373            AstFragmentKind::OptExpr => {
374                AstFragment::OptExpr(items.next().map(Annotatable::expect_expr))
375            }
376            AstFragmentKind::Crate => {
377                AstFragment::Crate(items.next().expect("expected exactly one crate").expect_crate())
378            }
379            AstFragmentKind::Pat | AstFragmentKind::Ty => {
380                panic!("patterns and types aren't annotatable")
381            }
382        }
383    }
384}
385
386pub struct Invocation {
387    pub kind: InvocationKind,
388    pub fragment_kind: AstFragmentKind,
389    pub expansion_data: ExpansionData,
390}
391
392pub enum InvocationKind {
393    Bang {
394        mac: Box<ast::MacCall>,
395        span: Span,
396    },
397    Attr {
398        attr: ast::Attribute,
399        /// Re-insertion position for inert attributes.
400        pos: usize,
401        item: Annotatable,
402        /// Required for resolving derive helper attributes.
403        derives: Vec<ast::Path>,
404    },
405    Derive {
406        path: ast::Path,
407        is_const: bool,
408        item: Annotatable,
409    },
410    GlobDelegation {
411        item: Box<ast::AssocItem>,
412        /// Whether this is a trait impl or an inherent impl
413        of_trait: bool,
414    },
415}
416
417impl InvocationKind {
418    fn placeholder_visibility(&self) -> Option<ast::Visibility> {
419        // HACK: For unnamed fields placeholders should have the same visibility as the actual
420        // fields because for tuple structs/variants resolve determines visibilities of their
421        // constructor using these field visibilities before attributes on them are expanded.
422        // The assumption is that the attribute expansion cannot change field visibilities,
423        // and it holds because only inert attributes are supported in this position.
424        match self {
425            InvocationKind::Attr { item: Annotatable::FieldDef(field), .. }
426            | InvocationKind::Derive { item: Annotatable::FieldDef(field), .. }
427                if field.ident.is_none() =>
428            {
429                Some(field.vis.clone())
430            }
431            _ => None,
432        }
433    }
434}
435
436impl Invocation {
437    pub fn span(&self) -> Span {
438        match &self.kind {
439            InvocationKind::Bang { span, .. } => *span,
440            InvocationKind::Attr { attr, .. } => attr.span,
441            InvocationKind::Derive { path, .. } => path.span,
442            InvocationKind::GlobDelegation { item, .. } => item.span,
443        }
444    }
445
446    fn span_mut(&mut self) -> &mut Span {
447        match &mut self.kind {
448            InvocationKind::Bang { span, .. } => span,
449            InvocationKind::Attr { attr, .. } => &mut attr.span,
450            InvocationKind::Derive { path, .. } => &mut path.span,
451            InvocationKind::GlobDelegation { item, .. } => &mut item.span,
452        }
453    }
454}
455
456pub struct MacroExpander<'a, 'b> {
457    pub cx: &'a mut ExtCtxt<'b>,
458    monotonic: bool, // cf. `cx.monotonic_expander()`
459}
460
461impl<'a, 'b> MacroExpander<'a, 'b> {
462    pub fn new(cx: &'a mut ExtCtxt<'b>, monotonic: bool) -> Self {
463        MacroExpander { cx, monotonic }
464    }
465
466    pub fn expand_crate(&mut self, krate: ast::Crate) -> ast::Crate {
467        let file_path = match self.cx.source_map().span_to_filename(krate.spans.inner_span) {
468            FileName::Real(name) => name
469                .into_local_path()
470                .expect("attempting to resolve a file path in an external file"),
471            other => PathBuf::from(other.prefer_local().to_string()),
472        };
473        let dir_path = file_path.parent().unwrap_or(&file_path).to_owned();
474        self.cx.root_path = dir_path.clone();
475        self.cx.current_expansion.module = Rc::new(ModuleData {
476            mod_path: vec![Ident::with_dummy_span(self.cx.ecfg.crate_name)],
477            file_path_stack: vec![file_path],
478            dir_path,
479        });
480        let krate = self.fully_expand_fragment(AstFragment::Crate(krate)).make_crate();
481        assert_eq!(krate.id, ast::CRATE_NODE_ID);
482        self.cx.trace_macros_diag();
483        krate
484    }
485
486    /// Recursively expand all macro invocations in this AST fragment.
487    pub fn fully_expand_fragment(&mut self, input_fragment: AstFragment) -> AstFragment {
488        let orig_expansion_data = self.cx.current_expansion.clone();
489        let orig_force_mode = self.cx.force_mode;
490
491        // Collect all macro invocations and replace them with placeholders.
492        let (mut fragment_with_placeholders, mut invocations) =
493            self.collect_invocations(input_fragment, &[]);
494
495        // Optimization: if we resolve all imports now,
496        // we'll be able to immediately resolve most of imported macros.
497        self.resolve_imports();
498
499        // Resolve paths in all invocations and produce output expanded fragments for them, but
500        // do not insert them into our input AST fragment yet, only store in `expanded_fragments`.
501        // The output fragments also go through expansion recursively until no invocations are left.
502        // Unresolved macros produce dummy outputs as a recovery measure.
503        invocations.reverse();
504        let mut expanded_fragments = Vec::new();
505        let mut undetermined_invocations = Vec::new();
506        let (mut progress, mut force) = (false, !self.monotonic);
507        loop {
508            let Some((invoc, ext)) = invocations.pop() else {
509                self.resolve_imports();
510                if undetermined_invocations.is_empty() {
511                    break;
512                }
513                invocations = mem::take(&mut undetermined_invocations);
514                force = !progress;
515                progress = false;
516                if force && self.monotonic {
517                    self.cx.dcx().span_delayed_bug(
518                        invocations.last().unwrap().0.span(),
519                        "expansion entered force mode without producing any errors",
520                    );
521                }
522                continue;
523            };
524
525            let ext = match ext {
526                Some(ext) => ext,
527                None => {
528                    let eager_expansion_root = if self.monotonic {
529                        invoc.expansion_data.id
530                    } else {
531                        orig_expansion_data.id
532                    };
533                    match self.cx.resolver.resolve_macro_invocation(
534                        &invoc,
535                        eager_expansion_root,
536                        force,
537                    ) {
538                        Ok(ext) => ext,
539                        Err(Indeterminate) => {
540                            // Cannot resolve, will retry this invocation later.
541                            undetermined_invocations.push((invoc, None));
542                            continue;
543                        }
544                    }
545                }
546            };
547
548            let ExpansionData { depth, id: expn_id, .. } = invoc.expansion_data;
549            let depth = depth - orig_expansion_data.depth;
550            self.cx.current_expansion = invoc.expansion_data.clone();
551            self.cx.force_mode = force;
552
553            let fragment_kind = invoc.fragment_kind;
554            match self.expand_invoc(invoc, &ext.kind) {
555                ExpandResult::Ready(fragment) => {
556                    let mut derive_invocations = Vec::new();
557                    let derive_placeholders = self
558                        .cx
559                        .resolver
560                        .take_derive_resolutions(expn_id)
561                        .map(|derives| {
562                            derive_invocations.reserve(derives.len());
563                            derives
564                                .into_iter()
565                                .map(|DeriveResolution { path, item, exts: _, is_const }| {
566                                    // FIXME: Consider using the derive resolutions (`_exts`)
567                                    // instead of enqueuing the derives to be resolved again later.
568                                    let expn_id = LocalExpnId::fresh_empty();
569                                    derive_invocations.push((
570                                        Invocation {
571                                            kind: InvocationKind::Derive { path, item, is_const },
572                                            fragment_kind,
573                                            expansion_data: ExpansionData {
574                                                id: expn_id,
575                                                ..self.cx.current_expansion.clone()
576                                            },
577                                        },
578                                        None,
579                                    ));
580                                    NodeId::placeholder_from_expn_id(expn_id)
581                                })
582                                .collect::<Vec<_>>()
583                        })
584                        .unwrap_or_default();
585
586                    let (expanded_fragment, collected_invocations) =
587                        self.collect_invocations(fragment, &derive_placeholders);
588                    // We choose to expand any derive invocations associated with this macro
589                    // invocation *before* any macro invocations collected from the output
590                    // fragment.
591                    derive_invocations.extend(collected_invocations);
592
593                    progress = true;
594                    if expanded_fragments.len() < depth {
595                        expanded_fragments.push(Vec::new());
596                    }
597                    expanded_fragments[depth - 1].push((expn_id, expanded_fragment));
598                    invocations.extend(derive_invocations.into_iter().rev());
599                }
600                ExpandResult::Retry(invoc) => {
601                    if force {
602                        self.cx.dcx().span_bug(
603                            invoc.span(),
604                            "expansion entered force mode but is still stuck",
605                        );
606                    } else {
607                        // Cannot expand, will retry this invocation later.
608                        undetermined_invocations.push((invoc, Some(ext)));
609                    }
610                }
611            }
612        }
613
614        self.cx.current_expansion = orig_expansion_data;
615        self.cx.force_mode = orig_force_mode;
616
617        // Finally incorporate all the expanded macros into the input AST fragment.
618        let mut placeholder_expander = PlaceholderExpander::default();
619        while let Some(expanded_fragments) = expanded_fragments.pop() {
620            for (expn_id, expanded_fragment) in expanded_fragments.into_iter().rev() {
621                placeholder_expander
622                    .add(NodeId::placeholder_from_expn_id(expn_id), expanded_fragment);
623            }
624        }
625        fragment_with_placeholders.mut_visit_with(&mut placeholder_expander);
626        fragment_with_placeholders
627    }
628
629    fn resolve_imports(&mut self) {
630        if self.monotonic {
631            self.cx.resolver.resolve_imports();
632        }
633    }
634
635    /// Collects all macro invocations reachable at this time in this AST fragment, and replace
636    /// them with "placeholders" - dummy macro invocations with specially crafted `NodeId`s.
637    /// Then call into resolver that builds a skeleton ("reduced graph") of the fragment and
638    /// prepares data for resolving paths of macro invocations.
639    fn collect_invocations(
640        &mut self,
641        mut fragment: AstFragment,
642        extra_placeholders: &[NodeId],
643    ) -> (AstFragment, Vec<(Invocation, Option<Arc<SyntaxExtension>>)>) {
644        // Resolve `$crate`s in the fragment for pretty-printing.
645        self.cx.resolver.resolve_dollar_crates();
646
647        let mut invocations = {
648            let mut collector = InvocationCollector {
649                // Non-derive macro invocations cannot see the results of cfg expansion - they
650                // will either be removed along with the item, or invoked before the cfg/cfg_attr
651                // attribute is expanded. Therefore, we don't need to configure the tokens
652                // Derive macros *can* see the results of cfg-expansion - they are handled
653                // specially in `fully_expand_fragment`
654                cx: self.cx,
655                invocations: Vec::new(),
656                monotonic: self.monotonic,
657            };
658            fragment.mut_visit_with(&mut collector);
659            fragment.add_placeholders(extra_placeholders);
660            collector.invocations
661        };
662
663        if self.monotonic {
664            self.cx
665                .resolver
666                .visit_ast_fragment_with_placeholders(self.cx.current_expansion.id, &fragment);
667
668            if self.cx.sess.opts.incremental.is_some() {
669                for (invoc, _) in invocations.iter_mut() {
670                    let expn_id = invoc.expansion_data.id;
671                    let parent_def = self.cx.resolver.invocation_parent(expn_id);
672                    let span = invoc.span_mut();
673                    *span = span.with_parent(Some(parent_def));
674                }
675            }
676        }
677
678        (fragment, invocations)
679    }
680
681    fn error_recursion_limit_reached(&mut self) -> ErrorGuaranteed {
682        let expn_data = self.cx.current_expansion.id.expn_data();
683        let suggested_limit = match self.cx.ecfg.recursion_limit {
684            Limit(0) => Limit(2),
685            limit => limit * 2,
686        };
687
688        let guar = self.cx.dcx().emit_err(RecursionLimitReached {
689            span: expn_data.call_site,
690            descr: expn_data.kind.descr(),
691            suggested_limit,
692            crate_name: self.cx.ecfg.crate_name,
693        });
694
695        self.cx.macro_error_and_trace_macros_diag();
696        guar
697    }
698
699    /// A macro's expansion does not fit in this fragment kind.
700    /// For example, a non-type macro in a type position.
701    fn error_wrong_fragment_kind(
702        &mut self,
703        kind: AstFragmentKind,
704        mac: &ast::MacCall,
705        span: Span,
706    ) -> ErrorGuaranteed {
707        let guar =
708            self.cx.dcx().emit_err(WrongFragmentKind { span, kind: kind.name(), name: &mac.path });
709        self.cx.macro_error_and_trace_macros_diag();
710        guar
711    }
712
713    fn expand_invoc(
714        &mut self,
715        invoc: Invocation,
716        ext: &SyntaxExtensionKind,
717    ) -> ExpandResult<AstFragment, Invocation> {
718        let recursion_limit = match self.cx.reduced_recursion_limit {
719            Some((limit, _)) => limit,
720            None => self.cx.ecfg.recursion_limit,
721        };
722
723        if !recursion_limit.value_within_limit(self.cx.current_expansion.depth) {
724            let guar = match self.cx.reduced_recursion_limit {
725                Some((_, guar)) => guar,
726                None => self.error_recursion_limit_reached(),
727            };
728
729            // Reduce the recursion limit by half each time it triggers.
730            self.cx.reduced_recursion_limit = Some((recursion_limit / 2, guar));
731
732            return ExpandResult::Ready(invoc.fragment_kind.dummy(invoc.span(), guar));
733        }
734
735        let macro_stats = self.cx.sess.opts.unstable_opts.macro_stats;
736
737        let (fragment_kind, span) = (invoc.fragment_kind, invoc.span());
738        ExpandResult::Ready(match invoc.kind {
739            InvocationKind::Bang { mac, span } => {
740                if let SyntaxExtensionKind::Bang(expander) = ext {
741                    match expander.expand(self.cx, span, mac.args.tokens.clone()) {
742                        Ok(tok_result) => {
743                            let fragment =
744                                self.parse_ast_fragment(tok_result, fragment_kind, &mac.path, span);
745                            if macro_stats {
746                                update_bang_macro_stats(
747                                    self.cx,
748                                    fragment_kind,
749                                    span,
750                                    mac,
751                                    &fragment,
752                                );
753                            }
754                            fragment
755                        }
756                        Err(guar) => return ExpandResult::Ready(fragment_kind.dummy(span, guar)),
757                    }
758                } else if let Some(expander) = ext.as_legacy_bang() {
759                    let tok_result = match expander.expand(self.cx, span, mac.args.tokens.clone()) {
760                        ExpandResult::Ready(tok_result) => tok_result,
761                        ExpandResult::Retry(_) => {
762                            // retry the original
763                            return ExpandResult::Retry(Invocation {
764                                kind: InvocationKind::Bang { mac, span },
765                                ..invoc
766                            });
767                        }
768                    };
769                    if let Some(fragment) = fragment_kind.make_from(tok_result) {
770                        if macro_stats {
771                            update_bang_macro_stats(self.cx, fragment_kind, span, mac, &fragment);
772                        }
773                        fragment
774                    } else {
775                        let guar = self.error_wrong_fragment_kind(fragment_kind, &mac, span);
776                        fragment_kind.dummy(span, guar)
777                    }
778                } else {
779                    unreachable!();
780                }
781            }
782            InvocationKind::Attr { attr, pos, mut item, derives } => {
783                if let Some(expander) = ext.as_attr() {
784                    self.gate_proc_macro_input(&item);
785                    self.gate_proc_macro_attr_item(span, &item);
786                    let tokens = match &item {
787                        // FIXME: Collect tokens and use them instead of generating
788                        // fake ones. These are unstable, so it needs to be
789                        // fixed prior to stabilization
790                        // Fake tokens when we are invoking an inner attribute, and
791                        // we are invoking it on an out-of-line module or crate.
792                        Annotatable::Crate(krate) => {
793                            rustc_parse::fake_token_stream_for_crate(&self.cx.sess.psess, krate)
794                        }
795                        Annotatable::Item(item_inner)
796                            if matches!(attr.style, AttrStyle::Inner)
797                                && matches!(
798                                    item_inner.kind,
799                                    ItemKind::Mod(
800                                        _,
801                                        _,
802                                        ModKind::Unloaded | ModKind::Loaded(_, Inline::No, _, _),
803                                    )
804                                ) =>
805                        {
806                            rustc_parse::fake_token_stream_for_item(&self.cx.sess.psess, item_inner)
807                        }
808                        _ => item.to_tokens(),
809                    };
810                    let attr_item = attr.get_normal_item();
811                    if let AttrArgs::Eq { .. } = attr_item.args {
812                        self.cx.dcx().emit_err(UnsupportedKeyValue { span });
813                    }
814                    let inner_tokens = attr_item.args.inner_tokens();
815                    match expander.expand(self.cx, span, inner_tokens, tokens) {
816                        Ok(tok_result) => {
817                            let fragment = self.parse_ast_fragment(
818                                tok_result,
819                                fragment_kind,
820                                &attr_item.path,
821                                span,
822                            );
823                            if macro_stats {
824                                update_attr_macro_stats(
825                                    self.cx,
826                                    fragment_kind,
827                                    span,
828                                    &attr_item.path,
829                                    &attr,
830                                    item,
831                                    &fragment,
832                                );
833                            }
834                            fragment
835                        }
836                        Err(guar) => return ExpandResult::Ready(fragment_kind.dummy(span, guar)),
837                    }
838                } else if let SyntaxExtensionKind::LegacyAttr(expander) = ext {
839                    match validate_attr::parse_meta(&self.cx.sess.psess, &attr) {
840                        Ok(meta) => {
841                            let item_clone = macro_stats.then(|| item.clone());
842                            let items = match expander.expand(self.cx, span, &meta, item, false) {
843                                ExpandResult::Ready(items) => items,
844                                ExpandResult::Retry(item) => {
845                                    // Reassemble the original invocation for retrying.
846                                    return ExpandResult::Retry(Invocation {
847                                        kind: InvocationKind::Attr { attr, pos, item, derives },
848                                        ..invoc
849                                    });
850                                }
851                            };
852                            if matches!(
853                                fragment_kind,
854                                AstFragmentKind::Expr | AstFragmentKind::MethodReceiverExpr
855                            ) && items.is_empty()
856                            {
857                                let guar = self.cx.dcx().emit_err(RemoveExprNotSupported { span });
858                                fragment_kind.dummy(span, guar)
859                            } else {
860                                let fragment = fragment_kind.expect_from_annotatables(items);
861                                if macro_stats {
862                                    update_attr_macro_stats(
863                                        self.cx,
864                                        fragment_kind,
865                                        span,
866                                        &meta.path,
867                                        &attr,
868                                        item_clone.unwrap(),
869                                        &fragment,
870                                    );
871                                }
872                                fragment
873                            }
874                        }
875                        Err(err) => {
876                            let _guar = err.emit();
877                            fragment_kind.expect_from_annotatables(iter::once(item))
878                        }
879                    }
880                } else if let SyntaxExtensionKind::NonMacroAttr = ext {
881                    // `-Zmacro-stats` ignores these because they don't do any real expansion.
882                    self.cx.expanded_inert_attrs.mark(&attr);
883                    item.visit_attrs(|attrs| attrs.insert(pos, attr));
884                    fragment_kind.expect_from_annotatables(iter::once(item))
885                } else {
886                    unreachable!();
887                }
888            }
889            InvocationKind::Derive { path, item, is_const } => match ext {
890                SyntaxExtensionKind::Derive(expander)
891                | SyntaxExtensionKind::LegacyDerive(expander) => {
892                    if let SyntaxExtensionKind::Derive(..) = ext {
893                        self.gate_proc_macro_input(&item);
894                    }
895                    // The `MetaItem` representing the trait to derive can't
896                    // have an unsafe around it (as of now).
897                    let meta = ast::MetaItem {
898                        unsafety: ast::Safety::Default,
899                        kind: MetaItemKind::Word,
900                        span,
901                        path,
902                    };
903                    let items = match expander.expand(self.cx, span, &meta, item, is_const) {
904                        ExpandResult::Ready(items) => items,
905                        ExpandResult::Retry(item) => {
906                            // Reassemble the original invocation for retrying.
907                            return ExpandResult::Retry(Invocation {
908                                kind: InvocationKind::Derive { path: meta.path, item, is_const },
909                                ..invoc
910                            });
911                        }
912                    };
913                    let fragment = fragment_kind.expect_from_annotatables(items);
914                    if macro_stats {
915                        update_derive_macro_stats(
916                            self.cx,
917                            fragment_kind,
918                            span,
919                            &meta.path,
920                            &fragment,
921                        );
922                    }
923                    fragment
924                }
925                _ => unreachable!(),
926            },
927            InvocationKind::GlobDelegation { item, of_trait } => {
928                let AssocItemKind::DelegationMac(deleg) = &item.kind else { unreachable!() };
929                let suffixes = match ext {
930                    SyntaxExtensionKind::GlobDelegation(expander) => match expander.expand(self.cx)
931                    {
932                        ExpandResult::Ready(suffixes) => suffixes,
933                        ExpandResult::Retry(()) => {
934                            // Reassemble the original invocation for retrying.
935                            return ExpandResult::Retry(Invocation {
936                                kind: InvocationKind::GlobDelegation { item, of_trait },
937                                ..invoc
938                            });
939                        }
940                    },
941                    SyntaxExtensionKind::LegacyBang(..) => {
942                        let msg = "expanded a dummy glob delegation";
943                        let guar = self.cx.dcx().span_delayed_bug(span, msg);
944                        return ExpandResult::Ready(fragment_kind.dummy(span, guar));
945                    }
946                    _ => unreachable!(),
947                };
948
949                type Node = AstNodeWrapper<Box<ast::AssocItem>, ImplItemTag>;
950                let single_delegations = build_single_delegations::<Node>(
951                    self.cx, deleg, &item, &suffixes, item.span, true,
952                );
953                // `-Zmacro-stats` ignores these because they don't seem important.
954                fragment_kind.expect_from_annotatables(single_delegations.map(|item| {
955                    Annotatable::AssocItem(Box::new(item), AssocCtxt::Impl { of_trait })
956                }))
957            }
958        })
959    }
960
961    #[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
962    fn gate_proc_macro_attr_item(&self, span: Span, item: &Annotatable) {
963        let kind = match item {
964            Annotatable::Item(_)
965            | Annotatable::AssocItem(..)
966            | Annotatable::ForeignItem(_)
967            | Annotatable::Crate(..) => return,
968            Annotatable::Stmt(stmt) => {
969                // Attributes are stable on item statements,
970                // but unstable on all other kinds of statements
971                if stmt.is_item() {
972                    return;
973                }
974                "statements"
975            }
976            Annotatable::Expr(_) => "expressions",
977            Annotatable::Arm(..)
978            | Annotatable::ExprField(..)
979            | Annotatable::PatField(..)
980            | Annotatable::GenericParam(..)
981            | Annotatable::Param(..)
982            | Annotatable::FieldDef(..)
983            | Annotatable::Variant(..)
984            | Annotatable::WherePredicate(..) => panic!("unexpected annotatable"),
985        };
986        if self.cx.ecfg.features.proc_macro_hygiene() {
987            return;
988        }
989        feature_err(
990            &self.cx.sess,
991            sym::proc_macro_hygiene,
992            span,
993            format!("custom attributes cannot be applied to {kind}"),
994        )
995        .emit();
996    }
997
998    fn gate_proc_macro_input(&self, annotatable: &Annotatable) {
999        struct GateProcMacroInput<'a> {
1000            sess: &'a Session,
1001        }
1002
1003        impl<'ast, 'a> Visitor<'ast> for GateProcMacroInput<'a> {
1004            fn visit_item(&mut self, item: &'ast ast::Item) {
1005                match &item.kind {
1006                    ItemKind::Mod(_, _, mod_kind)
1007                        if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _, _)) =>
1008                    {
1009                        feature_err(
1010                            self.sess,
1011                            sym::proc_macro_hygiene,
1012                            item.span,
1013                            fluent_generated::expand_non_inline_modules_in_proc_macro_input_are_unstable,
1014                        )
1015                        .emit();
1016                    }
1017                    _ => {}
1018                }
1019
1020                visit::walk_item(self, item);
1021            }
1022        }
1023
1024        if !self.cx.ecfg.features.proc_macro_hygiene() {
1025            annotatable.visit_with(&mut GateProcMacroInput { sess: &self.cx.sess });
1026        }
1027    }
1028
1029    fn parse_ast_fragment(
1030        &mut self,
1031        toks: TokenStream,
1032        kind: AstFragmentKind,
1033        path: &ast::Path,
1034        span: Span,
1035    ) -> AstFragment {
1036        let mut parser = self.cx.new_parser_from_tts(toks);
1037        match parse_ast_fragment(&mut parser, kind) {
1038            Ok(fragment) => {
1039                ensure_complete_parse(&parser, path, kind.name(), span);
1040                fragment
1041            }
1042            Err(mut err) => {
1043                if err.span.is_dummy() {
1044                    err.span(span);
1045                }
1046                annotate_err_with_kind(&mut err, kind, span);
1047                let guar = err.emit();
1048                self.cx.macro_error_and_trace_macros_diag();
1049                kind.dummy(span, guar)
1050            }
1051        }
1052    }
1053}
1054
1055pub fn parse_ast_fragment<'a>(
1056    this: &mut Parser<'a>,
1057    kind: AstFragmentKind,
1058) -> PResult<'a, AstFragment> {
1059    Ok(match kind {
1060        AstFragmentKind::Items => {
1061            let mut items = SmallVec::new();
1062            while let Some(item) = this.parse_item(ForceCollect::No)? {
1063                items.push(item);
1064            }
1065            AstFragment::Items(items)
1066        }
1067        AstFragmentKind::TraitItems => {
1068            let mut items = SmallVec::new();
1069            while let Some(item) = this.parse_trait_item(ForceCollect::No)? {
1070                items.extend(item);
1071            }
1072            AstFragment::TraitItems(items)
1073        }
1074        AstFragmentKind::ImplItems => {
1075            let mut items = SmallVec::new();
1076            while let Some(item) = this.parse_impl_item(ForceCollect::No)? {
1077                items.extend(item);
1078            }
1079            AstFragment::ImplItems(items)
1080        }
1081        AstFragmentKind::TraitImplItems => {
1082            let mut items = SmallVec::new();
1083            while let Some(item) = this.parse_impl_item(ForceCollect::No)? {
1084                items.extend(item);
1085            }
1086            AstFragment::TraitImplItems(items)
1087        }
1088        AstFragmentKind::ForeignItems => {
1089            let mut items = SmallVec::new();
1090            while let Some(item) = this.parse_foreign_item(ForceCollect::No)? {
1091                items.extend(item);
1092            }
1093            AstFragment::ForeignItems(items)
1094        }
1095        AstFragmentKind::Stmts => {
1096            let mut stmts = SmallVec::new();
1097            // Won't make progress on a `}`.
1098            while this.token != token::Eof && this.token != token::CloseBrace {
1099                if let Some(stmt) = this.parse_full_stmt(AttemptLocalParseRecovery::Yes)? {
1100                    stmts.push(stmt);
1101                }
1102            }
1103            AstFragment::Stmts(stmts)
1104        }
1105        AstFragmentKind::Expr => AstFragment::Expr(this.parse_expr()?),
1106        AstFragmentKind::MethodReceiverExpr => AstFragment::MethodReceiverExpr(this.parse_expr()?),
1107        AstFragmentKind::OptExpr => {
1108            if this.token != token::Eof {
1109                AstFragment::OptExpr(Some(this.parse_expr()?))
1110            } else {
1111                AstFragment::OptExpr(None)
1112            }
1113        }
1114        AstFragmentKind::Ty => AstFragment::Ty(this.parse_ty()?),
1115        AstFragmentKind::Pat => AstFragment::Pat(this.parse_pat_allow_top_guard(
1116            None,
1117            RecoverComma::No,
1118            RecoverColon::Yes,
1119            CommaRecoveryMode::LikelyTuple,
1120        )?),
1121        AstFragmentKind::Crate => AstFragment::Crate(this.parse_crate_mod()?),
1122        AstFragmentKind::Arms
1123        | AstFragmentKind::ExprFields
1124        | AstFragmentKind::PatFields
1125        | AstFragmentKind::GenericParams
1126        | AstFragmentKind::Params
1127        | AstFragmentKind::FieldDefs
1128        | AstFragmentKind::Variants
1129        | AstFragmentKind::WherePredicates => panic!("unexpected AST fragment kind"),
1130    })
1131}
1132
1133pub(crate) fn ensure_complete_parse<'a>(
1134    parser: &Parser<'a>,
1135    macro_path: &ast::Path,
1136    kind_name: &str,
1137    span: Span,
1138) {
1139    if parser.token != token::Eof {
1140        let descr = token_descr(&parser.token);
1141        // Avoid emitting backtrace info twice.
1142        let def_site_span = parser.token.span.with_ctxt(SyntaxContext::root());
1143
1144        let semi_span = parser.psess.source_map().next_point(span);
1145        let add_semicolon = match &parser.psess.source_map().span_to_snippet(semi_span) {
1146            Ok(snippet) if &snippet[..] != ";" && kind_name == "expression" => {
1147                Some(span.shrink_to_hi())
1148            }
1149            _ => None,
1150        };
1151
1152        let expands_to_match_arm = kind_name == "pattern" && parser.token == token::FatArrow;
1153
1154        parser.dcx().emit_err(IncompleteParse {
1155            span: def_site_span,
1156            descr,
1157            label_span: span,
1158            macro_path,
1159            kind_name,
1160            expands_to_match_arm,
1161            add_semicolon,
1162        });
1163    }
1164}
1165
1166/// Wraps a call to `walk_*` / `walk_flat_map_*`
1167/// for an AST node that supports attributes
1168/// (see the `Annotatable` enum)
1169/// This method assigns a `NodeId`, and sets that `NodeId`
1170/// as our current 'lint node id'. If a macro call is found
1171/// inside this AST node, we will use this AST node's `NodeId`
1172/// to emit lints associated with that macro (allowing
1173/// `#[allow]` / `#[deny]` to be applied close to
1174/// the macro invocation).
1175///
1176/// Do *not* call this for a macro AST node
1177/// (e.g. `ExprKind::MacCall`) - we cannot emit lints
1178/// at these AST nodes, since they are removed and
1179/// replaced with the result of macro expansion.
1180///
1181/// All other `NodeId`s are assigned by `visit_id`.
1182/// * `self` is the 'self' parameter for the current method,
1183/// * `id` is a mutable reference to the `NodeId` field
1184///    of the current AST node.
1185/// * `closure` is a closure that executes the
1186///   `walk_*` / `walk_flat_map_*` method
1187///   for the current AST node.
1188macro_rules! assign_id {
1189    ($self:ident, $id:expr, $closure:expr) => {{
1190        let old_id = $self.cx.current_expansion.lint_node_id;
1191        if $self.monotonic {
1192            debug_assert_eq!(*$id, ast::DUMMY_NODE_ID);
1193            let new_id = $self.cx.resolver.next_node_id();
1194            *$id = new_id;
1195            $self.cx.current_expansion.lint_node_id = new_id;
1196        }
1197        let ret = ($closure)();
1198        $self.cx.current_expansion.lint_node_id = old_id;
1199        ret
1200    }};
1201}
1202
1203enum AddSemicolon {
1204    Yes,
1205    No,
1206}
1207
1208/// A trait implemented for all `AstFragment` nodes and providing all pieces
1209/// of functionality used by `InvocationCollector`.
1210trait InvocationCollectorNode: HasAttrs + HasNodeId + Sized {
1211    type OutputTy = SmallVec<[Self; 1]>;
1212    type ItemKind = ItemKind;
1213    const KIND: AstFragmentKind;
1214    fn to_annotatable(self) -> Annotatable;
1215    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy;
1216    fn descr() -> &'static str {
1217        unreachable!()
1218    }
1219    fn walk_flat_map(self, _collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1220        unreachable!()
1221    }
1222    fn walk(&mut self, _collector: &mut InvocationCollector<'_, '_>) {
1223        unreachable!()
1224    }
1225    fn is_mac_call(&self) -> bool {
1226        false
1227    }
1228    fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1229        unreachable!()
1230    }
1231    fn delegation(&self) -> Option<(&ast::DelegationMac, &ast::Item<Self::ItemKind>)> {
1232        None
1233    }
1234    fn delegation_item_kind(_deleg: Box<ast::Delegation>) -> Self::ItemKind {
1235        unreachable!()
1236    }
1237    fn from_item(_item: ast::Item<Self::ItemKind>) -> Self {
1238        unreachable!()
1239    }
1240    fn flatten_outputs(_outputs: impl Iterator<Item = Self::OutputTy>) -> Self::OutputTy {
1241        unreachable!()
1242    }
1243    fn pre_flat_map_node_collect_attr(_cfg: &StripUnconfigured<'_>, _attr: &ast::Attribute) {}
1244    fn post_flat_map_node_collect_bang(_output: &mut Self::OutputTy, _add_semicolon: AddSemicolon) {
1245    }
1246    fn wrap_flat_map_node_walk_flat_map(
1247        node: Self,
1248        collector: &mut InvocationCollector<'_, '_>,
1249        walk_flat_map: impl FnOnce(Self, &mut InvocationCollector<'_, '_>) -> Self::OutputTy,
1250    ) -> Result<Self::OutputTy, Self> {
1251        Ok(walk_flat_map(node, collector))
1252    }
1253    fn expand_cfg_false(
1254        &mut self,
1255        collector: &mut InvocationCollector<'_, '_>,
1256        _pos: usize,
1257        span: Span,
1258    ) {
1259        collector.cx.dcx().emit_err(RemoveNodeNotSupported { span, descr: Self::descr() });
1260    }
1261
1262    /// All of the identifiers (items) declared by this node.
1263    /// This is an approximation and should only be used for diagnostics.
1264    fn declared_idents(&self) -> Vec<Ident> {
1265        vec![]
1266    }
1267}
1268
1269impl InvocationCollectorNode for Box<ast::Item> {
1270    const KIND: AstFragmentKind = AstFragmentKind::Items;
1271    fn to_annotatable(self) -> Annotatable {
1272        Annotatable::Item(self)
1273    }
1274    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1275        fragment.make_items()
1276    }
1277    fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1278        walk_flat_map_item(collector, self)
1279    }
1280    fn is_mac_call(&self) -> bool {
1281        matches!(self.kind, ItemKind::MacCall(..))
1282    }
1283    fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1284        match self.kind {
1285            ItemKind::MacCall(mac) => (mac, self.attrs, AddSemicolon::No),
1286            _ => unreachable!(),
1287        }
1288    }
1289    fn delegation(&self) -> Option<(&ast::DelegationMac, &ast::Item<Self::ItemKind>)> {
1290        match &self.kind {
1291            ItemKind::DelegationMac(deleg) => Some((deleg, self)),
1292            _ => None,
1293        }
1294    }
1295    fn delegation_item_kind(deleg: Box<ast::Delegation>) -> Self::ItemKind {
1296        ItemKind::Delegation(deleg)
1297    }
1298    fn from_item(item: ast::Item<Self::ItemKind>) -> Self {
1299        Box::new(item)
1300    }
1301    fn flatten_outputs(items: impl Iterator<Item = Self::OutputTy>) -> Self::OutputTy {
1302        items.flatten().collect()
1303    }
1304    fn wrap_flat_map_node_walk_flat_map(
1305        mut node: Self,
1306        collector: &mut InvocationCollector<'_, '_>,
1307        walk_flat_map: impl FnOnce(Self, &mut InvocationCollector<'_, '_>) -> Self::OutputTy,
1308    ) -> Result<Self::OutputTy, Self> {
1309        if !matches!(node.kind, ItemKind::Mod(..)) {
1310            return Ok(walk_flat_map(node, collector));
1311        }
1312
1313        // Work around borrow checker not seeing through `P`'s deref.
1314        let (span, mut attrs) = (node.span, mem::take(&mut node.attrs));
1315        let ItemKind::Mod(_, ident, ref mut mod_kind) = node.kind else { unreachable!() };
1316        let ecx = &mut collector.cx;
1317        let (file_path, dir_path, dir_ownership) = match mod_kind {
1318            ModKind::Loaded(_, inline, _, _) => {
1319                // Inline `mod foo { ... }`, but we still need to push directories.
1320                let (dir_path, dir_ownership) = mod_dir_path(
1321                    ecx.sess,
1322                    ident,
1323                    &attrs,
1324                    &ecx.current_expansion.module,
1325                    ecx.current_expansion.dir_ownership,
1326                    *inline,
1327                );
1328                // If the module was parsed from an external file, recover its path.
1329                // This lets `parse_external_mod` catch cycles if it's self-referential.
1330                let file_path = match inline {
1331                    Inline::Yes => None,
1332                    Inline::No => mod_file_path_from_attr(ecx.sess, &attrs, &dir_path),
1333                };
1334                node.attrs = attrs;
1335                (file_path, dir_path, dir_ownership)
1336            }
1337            ModKind::Unloaded => {
1338                // We have an outline `mod foo;` so we need to parse the file.
1339                let old_attrs_len = attrs.len();
1340                let ParsedExternalMod {
1341                    items,
1342                    spans,
1343                    file_path,
1344                    dir_path,
1345                    dir_ownership,
1346                    had_parse_error,
1347                } = parse_external_mod(
1348                    ecx.sess,
1349                    ident,
1350                    span,
1351                    &ecx.current_expansion.module,
1352                    ecx.current_expansion.dir_ownership,
1353                    &mut attrs,
1354                );
1355
1356                if let Some(lint_store) = ecx.lint_store {
1357                    lint_store.pre_expansion_lint(
1358                        ecx.sess,
1359                        ecx.ecfg.features,
1360                        ecx.resolver.registered_tools(),
1361                        ecx.current_expansion.lint_node_id,
1362                        &attrs,
1363                        &items,
1364                        ident.name,
1365                    );
1366                }
1367
1368                *mod_kind = ModKind::Loaded(items, Inline::No, spans, had_parse_error);
1369                node.attrs = attrs;
1370                if node.attrs.len() > old_attrs_len {
1371                    // If we loaded an out-of-line module and added some inner attributes,
1372                    // then we need to re-configure it and re-collect attributes for
1373                    // resolution and expansion.
1374                    return Err(node);
1375                }
1376                (Some(file_path), dir_path, dir_ownership)
1377            }
1378        };
1379
1380        // Set the module info before we flat map.
1381        let mut module = ecx.current_expansion.module.with_dir_path(dir_path);
1382        module.mod_path.push(ident);
1383        if let Some(file_path) = file_path {
1384            module.file_path_stack.push(file_path);
1385        }
1386
1387        let orig_module = mem::replace(&mut ecx.current_expansion.module, Rc::new(module));
1388        let orig_dir_ownership =
1389            mem::replace(&mut ecx.current_expansion.dir_ownership, dir_ownership);
1390
1391        let res = Ok(walk_flat_map(node, collector));
1392
1393        collector.cx.current_expansion.dir_ownership = orig_dir_ownership;
1394        collector.cx.current_expansion.module = orig_module;
1395        res
1396    }
1397
1398    fn declared_idents(&self) -> Vec<Ident> {
1399        if let ItemKind::Use(ut) = &self.kind {
1400            fn collect_use_tree_leaves(ut: &ast::UseTree, idents: &mut Vec<Ident>) {
1401                match &ut.kind {
1402                    ast::UseTreeKind::Glob => {}
1403                    ast::UseTreeKind::Simple(_) => idents.push(ut.ident()),
1404                    ast::UseTreeKind::Nested { items, .. } => {
1405                        for (ut, _) in items {
1406                            collect_use_tree_leaves(ut, idents);
1407                        }
1408                    }
1409                }
1410            }
1411            let mut idents = Vec::new();
1412            collect_use_tree_leaves(&ut, &mut idents);
1413            idents
1414        } else {
1415            self.kind.ident().into_iter().collect()
1416        }
1417    }
1418}
1419
1420struct TraitItemTag;
1421impl InvocationCollectorNode for AstNodeWrapper<Box<ast::AssocItem>, TraitItemTag> {
1422    type OutputTy = SmallVec<[Box<ast::AssocItem>; 1]>;
1423    type ItemKind = AssocItemKind;
1424    const KIND: AstFragmentKind = AstFragmentKind::TraitItems;
1425    fn to_annotatable(self) -> Annotatable {
1426        Annotatable::AssocItem(self.wrapped, AssocCtxt::Trait)
1427    }
1428    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1429        fragment.make_trait_items()
1430    }
1431    fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1432        walk_flat_map_assoc_item(collector, self.wrapped, AssocCtxt::Trait)
1433    }
1434    fn is_mac_call(&self) -> bool {
1435        matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
1436    }
1437    fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1438        let item = self.wrapped;
1439        match item.kind {
1440            AssocItemKind::MacCall(mac) => (mac, item.attrs, AddSemicolon::No),
1441            _ => unreachable!(),
1442        }
1443    }
1444    fn delegation(&self) -> Option<(&ast::DelegationMac, &ast::Item<Self::ItemKind>)> {
1445        match &self.wrapped.kind {
1446            AssocItemKind::DelegationMac(deleg) => Some((deleg, &self.wrapped)),
1447            _ => None,
1448        }
1449    }
1450    fn delegation_item_kind(deleg: Box<ast::Delegation>) -> Self::ItemKind {
1451        AssocItemKind::Delegation(deleg)
1452    }
1453    fn from_item(item: ast::Item<Self::ItemKind>) -> Self {
1454        AstNodeWrapper::new(Box::new(item), TraitItemTag)
1455    }
1456    fn flatten_outputs(items: impl Iterator<Item = Self::OutputTy>) -> Self::OutputTy {
1457        items.flatten().collect()
1458    }
1459}
1460
1461struct ImplItemTag;
1462impl InvocationCollectorNode for AstNodeWrapper<Box<ast::AssocItem>, ImplItemTag> {
1463    type OutputTy = SmallVec<[Box<ast::AssocItem>; 1]>;
1464    type ItemKind = AssocItemKind;
1465    const KIND: AstFragmentKind = AstFragmentKind::ImplItems;
1466    fn to_annotatable(self) -> Annotatable {
1467        Annotatable::AssocItem(self.wrapped, AssocCtxt::Impl { of_trait: false })
1468    }
1469    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1470        fragment.make_impl_items()
1471    }
1472    fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1473        walk_flat_map_assoc_item(collector, self.wrapped, AssocCtxt::Impl { of_trait: false })
1474    }
1475    fn is_mac_call(&self) -> bool {
1476        matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
1477    }
1478    fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1479        let item = self.wrapped;
1480        match item.kind {
1481            AssocItemKind::MacCall(mac) => (mac, item.attrs, AddSemicolon::No),
1482            _ => unreachable!(),
1483        }
1484    }
1485    fn delegation(&self) -> Option<(&ast::DelegationMac, &ast::Item<Self::ItemKind>)> {
1486        match &self.wrapped.kind {
1487            AssocItemKind::DelegationMac(deleg) => Some((deleg, &self.wrapped)),
1488            _ => None,
1489        }
1490    }
1491    fn delegation_item_kind(deleg: Box<ast::Delegation>) -> Self::ItemKind {
1492        AssocItemKind::Delegation(deleg)
1493    }
1494    fn from_item(item: ast::Item<Self::ItemKind>) -> Self {
1495        AstNodeWrapper::new(Box::new(item), ImplItemTag)
1496    }
1497    fn flatten_outputs(items: impl Iterator<Item = Self::OutputTy>) -> Self::OutputTy {
1498        items.flatten().collect()
1499    }
1500}
1501
1502struct TraitImplItemTag;
1503impl InvocationCollectorNode for AstNodeWrapper<Box<ast::AssocItem>, TraitImplItemTag> {
1504    type OutputTy = SmallVec<[Box<ast::AssocItem>; 1]>;
1505    type ItemKind = AssocItemKind;
1506    const KIND: AstFragmentKind = AstFragmentKind::TraitImplItems;
1507    fn to_annotatable(self) -> Annotatable {
1508        Annotatable::AssocItem(self.wrapped, AssocCtxt::Impl { of_trait: true })
1509    }
1510    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1511        fragment.make_trait_impl_items()
1512    }
1513    fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1514        walk_flat_map_assoc_item(collector, self.wrapped, AssocCtxt::Impl { of_trait: true })
1515    }
1516    fn is_mac_call(&self) -> bool {
1517        matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
1518    }
1519    fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1520        let item = self.wrapped;
1521        match item.kind {
1522            AssocItemKind::MacCall(mac) => (mac, item.attrs, AddSemicolon::No),
1523            _ => unreachable!(),
1524        }
1525    }
1526    fn delegation(&self) -> Option<(&ast::DelegationMac, &ast::Item<Self::ItemKind>)> {
1527        match &self.wrapped.kind {
1528            AssocItemKind::DelegationMac(deleg) => Some((deleg, &self.wrapped)),
1529            _ => None,
1530        }
1531    }
1532    fn delegation_item_kind(deleg: Box<ast::Delegation>) -> Self::ItemKind {
1533        AssocItemKind::Delegation(deleg)
1534    }
1535    fn from_item(item: ast::Item<Self::ItemKind>) -> Self {
1536        AstNodeWrapper::new(Box::new(item), TraitImplItemTag)
1537    }
1538    fn flatten_outputs(items: impl Iterator<Item = Self::OutputTy>) -> Self::OutputTy {
1539        items.flatten().collect()
1540    }
1541}
1542
1543impl InvocationCollectorNode for Box<ast::ForeignItem> {
1544    const KIND: AstFragmentKind = AstFragmentKind::ForeignItems;
1545    fn to_annotatable(self) -> Annotatable {
1546        Annotatable::ForeignItem(self)
1547    }
1548    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1549        fragment.make_foreign_items()
1550    }
1551    fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1552        walk_flat_map_foreign_item(collector, self)
1553    }
1554    fn is_mac_call(&self) -> bool {
1555        matches!(self.kind, ForeignItemKind::MacCall(..))
1556    }
1557    fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1558        match self.kind {
1559            ForeignItemKind::MacCall(mac) => (mac, self.attrs, AddSemicolon::No),
1560            _ => unreachable!(),
1561        }
1562    }
1563}
1564
1565impl InvocationCollectorNode for ast::Variant {
1566    const KIND: AstFragmentKind = AstFragmentKind::Variants;
1567    fn to_annotatable(self) -> Annotatable {
1568        Annotatable::Variant(self)
1569    }
1570    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1571        fragment.make_variants()
1572    }
1573    fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1574        walk_flat_map_variant(collector, self)
1575    }
1576}
1577
1578impl InvocationCollectorNode for ast::WherePredicate {
1579    const KIND: AstFragmentKind = AstFragmentKind::WherePredicates;
1580    fn to_annotatable(self) -> Annotatable {
1581        Annotatable::WherePredicate(self)
1582    }
1583    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1584        fragment.make_where_predicates()
1585    }
1586    fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1587        walk_flat_map_where_predicate(collector, self)
1588    }
1589}
1590
1591impl InvocationCollectorNode for ast::FieldDef {
1592    const KIND: AstFragmentKind = AstFragmentKind::FieldDefs;
1593    fn to_annotatable(self) -> Annotatable {
1594        Annotatable::FieldDef(self)
1595    }
1596    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1597        fragment.make_field_defs()
1598    }
1599    fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1600        walk_flat_map_field_def(collector, self)
1601    }
1602}
1603
1604impl InvocationCollectorNode for ast::PatField {
1605    const KIND: AstFragmentKind = AstFragmentKind::PatFields;
1606    fn to_annotatable(self) -> Annotatable {
1607        Annotatable::PatField(self)
1608    }
1609    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1610        fragment.make_pat_fields()
1611    }
1612    fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1613        walk_flat_map_pat_field(collector, self)
1614    }
1615}
1616
1617impl InvocationCollectorNode for ast::ExprField {
1618    const KIND: AstFragmentKind = AstFragmentKind::ExprFields;
1619    fn to_annotatable(self) -> Annotatable {
1620        Annotatable::ExprField(self)
1621    }
1622    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1623        fragment.make_expr_fields()
1624    }
1625    fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1626        walk_flat_map_expr_field(collector, self)
1627    }
1628}
1629
1630impl InvocationCollectorNode for ast::Param {
1631    const KIND: AstFragmentKind = AstFragmentKind::Params;
1632    fn to_annotatable(self) -> Annotatable {
1633        Annotatable::Param(self)
1634    }
1635    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1636        fragment.make_params()
1637    }
1638    fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1639        walk_flat_map_param(collector, self)
1640    }
1641}
1642
1643impl InvocationCollectorNode for ast::GenericParam {
1644    const KIND: AstFragmentKind = AstFragmentKind::GenericParams;
1645    fn to_annotatable(self) -> Annotatable {
1646        Annotatable::GenericParam(self)
1647    }
1648    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1649        fragment.make_generic_params()
1650    }
1651    fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1652        walk_flat_map_generic_param(collector, self)
1653    }
1654}
1655
1656impl InvocationCollectorNode for ast::Arm {
1657    const KIND: AstFragmentKind = AstFragmentKind::Arms;
1658    fn to_annotatable(self) -> Annotatable {
1659        Annotatable::Arm(self)
1660    }
1661    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1662        fragment.make_arms()
1663    }
1664    fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1665        walk_flat_map_arm(collector, self)
1666    }
1667}
1668
1669impl InvocationCollectorNode for ast::Stmt {
1670    const KIND: AstFragmentKind = AstFragmentKind::Stmts;
1671    fn to_annotatable(self) -> Annotatable {
1672        Annotatable::Stmt(Box::new(self))
1673    }
1674    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1675        fragment.make_stmts()
1676    }
1677    fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1678        walk_flat_map_stmt(collector, self)
1679    }
1680    fn is_mac_call(&self) -> bool {
1681        match &self.kind {
1682            StmtKind::MacCall(..) => true,
1683            StmtKind::Item(item) => matches!(item.kind, ItemKind::MacCall(..)),
1684            StmtKind::Semi(expr) => matches!(expr.kind, ExprKind::MacCall(..)),
1685            StmtKind::Expr(..) => unreachable!(),
1686            StmtKind::Let(..) | StmtKind::Empty => false,
1687        }
1688    }
1689    fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1690        // We pull macro invocations (both attributes and fn-like macro calls) out of their
1691        // `StmtKind`s and treat them as statement macro invocations, not as items or expressions.
1692        let (add_semicolon, mac, attrs) = match self.kind {
1693            StmtKind::MacCall(mac) => {
1694                let ast::MacCallStmt { mac, style, attrs, .. } = *mac;
1695                (style == MacStmtStyle::Semicolon, mac, attrs)
1696            }
1697            StmtKind::Item(item) => match *item {
1698                ast::Item { kind: ItemKind::MacCall(mac), attrs, .. } => {
1699                    (mac.args.need_semicolon(), mac, attrs)
1700                }
1701                _ => unreachable!(),
1702            },
1703            StmtKind::Semi(expr) => match *expr {
1704                ast::Expr { kind: ExprKind::MacCall(mac), attrs, .. } => {
1705                    (mac.args.need_semicolon(), mac, attrs)
1706                }
1707                _ => unreachable!(),
1708            },
1709            _ => unreachable!(),
1710        };
1711        (mac, attrs, if add_semicolon { AddSemicolon::Yes } else { AddSemicolon::No })
1712    }
1713    fn delegation(&self) -> Option<(&ast::DelegationMac, &ast::Item<Self::ItemKind>)> {
1714        match &self.kind {
1715            StmtKind::Item(item) => match &item.kind {
1716                ItemKind::DelegationMac(deleg) => Some((deleg, item)),
1717                _ => None,
1718            },
1719            _ => None,
1720        }
1721    }
1722    fn delegation_item_kind(deleg: Box<ast::Delegation>) -> Self::ItemKind {
1723        ItemKind::Delegation(deleg)
1724    }
1725    fn from_item(item: ast::Item<Self::ItemKind>) -> Self {
1726        ast::Stmt { id: ast::DUMMY_NODE_ID, span: item.span, kind: StmtKind::Item(Box::new(item)) }
1727    }
1728    fn flatten_outputs(items: impl Iterator<Item = Self::OutputTy>) -> Self::OutputTy {
1729        items.flatten().collect()
1730    }
1731    fn post_flat_map_node_collect_bang(stmts: &mut Self::OutputTy, add_semicolon: AddSemicolon) {
1732        // If this is a macro invocation with a semicolon, then apply that
1733        // semicolon to the final statement produced by expansion.
1734        if matches!(add_semicolon, AddSemicolon::Yes) {
1735            if let Some(stmt) = stmts.pop() {
1736                stmts.push(stmt.add_trailing_semicolon());
1737            }
1738        }
1739    }
1740}
1741
1742impl InvocationCollectorNode for ast::Crate {
1743    type OutputTy = ast::Crate;
1744    const KIND: AstFragmentKind = AstFragmentKind::Crate;
1745    fn to_annotatable(self) -> Annotatable {
1746        Annotatable::Crate(self)
1747    }
1748    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1749        fragment.make_crate()
1750    }
1751    fn walk(&mut self, collector: &mut InvocationCollector<'_, '_>) {
1752        walk_crate(collector, self)
1753    }
1754    fn expand_cfg_false(
1755        &mut self,
1756        collector: &mut InvocationCollector<'_, '_>,
1757        pos: usize,
1758        _span: Span,
1759    ) {
1760        // Attributes above `cfg(FALSE)` are left in place, because we may want to configure
1761        // some global crate properties even on fully unconfigured crates.
1762        self.attrs.truncate(pos);
1763        // Standard prelude imports are left in the crate for backward compatibility.
1764        self.items.truncate(collector.cx.num_standard_library_imports);
1765    }
1766}
1767
1768impl InvocationCollectorNode for ast::Ty {
1769    type OutputTy = Box<ast::Ty>;
1770    const KIND: AstFragmentKind = AstFragmentKind::Ty;
1771    fn to_annotatable(self) -> Annotatable {
1772        unreachable!()
1773    }
1774    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1775        fragment.make_ty()
1776    }
1777    fn walk(&mut self, collector: &mut InvocationCollector<'_, '_>) {
1778        // Save the pre-expanded name of this `ImplTrait`, so that later when defining
1779        // an APIT we use a name that doesn't have any placeholder fragments in it.
1780        if let ast::TyKind::ImplTrait(..) = self.kind {
1781            // HACK: pprust breaks strings with newlines when the type
1782            // gets too long. We don't want these to show up in compiler
1783            // output or built artifacts, so replace them here...
1784            // Perhaps we should instead format APITs more robustly.
1785            let name = Symbol::intern(&pprust::ty_to_string(self).replace('\n', " "));
1786            collector.cx.resolver.insert_impl_trait_name(self.id, name);
1787        }
1788        walk_ty(collector, self)
1789    }
1790    fn is_mac_call(&self) -> bool {
1791        matches!(self.kind, ast::TyKind::MacCall(..))
1792    }
1793    fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1794        match self.kind {
1795            TyKind::MacCall(mac) => (mac, AttrVec::new(), AddSemicolon::No),
1796            _ => unreachable!(),
1797        }
1798    }
1799}
1800
1801impl InvocationCollectorNode for ast::Pat {
1802    type OutputTy = Box<ast::Pat>;
1803    const KIND: AstFragmentKind = AstFragmentKind::Pat;
1804    fn to_annotatable(self) -> Annotatable {
1805        unreachable!()
1806    }
1807    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1808        fragment.make_pat()
1809    }
1810    fn walk(&mut self, collector: &mut InvocationCollector<'_, '_>) {
1811        walk_pat(collector, self)
1812    }
1813    fn is_mac_call(&self) -> bool {
1814        matches!(self.kind, PatKind::MacCall(..))
1815    }
1816    fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1817        match self.kind {
1818            PatKind::MacCall(mac) => (mac, AttrVec::new(), AddSemicolon::No),
1819            _ => unreachable!(),
1820        }
1821    }
1822}
1823
1824impl InvocationCollectorNode for ast::Expr {
1825    type OutputTy = Box<ast::Expr>;
1826    const KIND: AstFragmentKind = AstFragmentKind::Expr;
1827    fn to_annotatable(self) -> Annotatable {
1828        Annotatable::Expr(Box::new(self))
1829    }
1830    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1831        fragment.make_expr()
1832    }
1833    fn descr() -> &'static str {
1834        "an expression"
1835    }
1836    fn walk(&mut self, collector: &mut InvocationCollector<'_, '_>) {
1837        walk_expr(collector, self)
1838    }
1839    fn is_mac_call(&self) -> bool {
1840        matches!(self.kind, ExprKind::MacCall(..))
1841    }
1842    fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1843        match self.kind {
1844            ExprKind::MacCall(mac) => (mac, self.attrs, AddSemicolon::No),
1845            _ => unreachable!(),
1846        }
1847    }
1848}
1849
1850struct OptExprTag;
1851impl InvocationCollectorNode for AstNodeWrapper<Box<ast::Expr>, OptExprTag> {
1852    type OutputTy = Option<Box<ast::Expr>>;
1853    const KIND: AstFragmentKind = AstFragmentKind::OptExpr;
1854    fn to_annotatable(self) -> Annotatable {
1855        Annotatable::Expr(self.wrapped)
1856    }
1857    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1858        fragment.make_opt_expr()
1859    }
1860    fn walk_flat_map(mut self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1861        walk_expr(collector, &mut self.wrapped);
1862        Some(self.wrapped)
1863    }
1864    fn is_mac_call(&self) -> bool {
1865        matches!(self.wrapped.kind, ast::ExprKind::MacCall(..))
1866    }
1867    fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1868        let node = self.wrapped;
1869        match node.kind {
1870            ExprKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
1871            _ => unreachable!(),
1872        }
1873    }
1874    fn pre_flat_map_node_collect_attr(cfg: &StripUnconfigured<'_>, attr: &ast::Attribute) {
1875        cfg.maybe_emit_expr_attr_err(attr);
1876    }
1877}
1878
1879/// This struct is a hack to workaround unstable of `stmt_expr_attributes`.
1880/// It can be removed once that feature is stabilized.
1881struct MethodReceiverTag;
1882
1883impl InvocationCollectorNode for AstNodeWrapper<ast::Expr, MethodReceiverTag> {
1884    type OutputTy = AstNodeWrapper<Box<ast::Expr>, MethodReceiverTag>;
1885    const KIND: AstFragmentKind = AstFragmentKind::MethodReceiverExpr;
1886    fn descr() -> &'static str {
1887        "an expression"
1888    }
1889    fn to_annotatable(self) -> Annotatable {
1890        Annotatable::Expr(Box::new(self.wrapped))
1891    }
1892    fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1893        AstNodeWrapper::new(fragment.make_method_receiver_expr(), MethodReceiverTag)
1894    }
1895    fn walk(&mut self, collector: &mut InvocationCollector<'_, '_>) {
1896        walk_expr(collector, &mut self.wrapped)
1897    }
1898    fn is_mac_call(&self) -> bool {
1899        matches!(self.wrapped.kind, ast::ExprKind::MacCall(..))
1900    }
1901    fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1902        let node = self.wrapped;
1903        match node.kind {
1904            ExprKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
1905            _ => unreachable!(),
1906        }
1907    }
1908}
1909
1910fn build_single_delegations<'a, Node: InvocationCollectorNode>(
1911    ecx: &ExtCtxt<'_>,
1912    deleg: &'a ast::DelegationMac,
1913    item: &'a ast::Item<Node::ItemKind>,
1914    suffixes: &'a [(Ident, Option<Ident>)],
1915    item_span: Span,
1916    from_glob: bool,
1917) -> impl Iterator<Item = ast::Item<Node::ItemKind>> + 'a {
1918    if suffixes.is_empty() {
1919        // Report an error for now, to avoid keeping stem for resolution and
1920        // stability checks.
1921        let kind = String::from(if from_glob { "glob" } else { "list" });
1922        ecx.dcx().emit_err(EmptyDelegationMac { span: item.span, kind });
1923    }
1924
1925    suffixes.iter().map(move |&(ident, rename)| {
1926        let mut path = deleg.prefix.clone();
1927        path.segments.push(ast::PathSegment { ident, id: ast::DUMMY_NODE_ID, args: None });
1928
1929        ast::Item {
1930            attrs: item.attrs.clone(),
1931            id: ast::DUMMY_NODE_ID,
1932            span: if from_glob { item_span } else { ident.span },
1933            vis: item.vis.clone(),
1934            kind: Node::delegation_item_kind(Box::new(ast::Delegation {
1935                id: ast::DUMMY_NODE_ID,
1936                qself: deleg.qself.clone(),
1937                path,
1938                ident: rename.unwrap_or(ident),
1939                rename,
1940                body: deleg.body.clone(),
1941                from_glob,
1942            })),
1943            tokens: None,
1944        }
1945    })
1946}
1947
1948/// Required for `visit_node` obtained an owned `Node` from `&mut Node`.
1949trait DummyAstNode {
1950    fn dummy() -> Self;
1951}
1952
1953impl DummyAstNode for ast::Crate {
1954    fn dummy() -> Self {
1955        ast::Crate {
1956            attrs: Default::default(),
1957            items: Default::default(),
1958            spans: Default::default(),
1959            id: DUMMY_NODE_ID,
1960            is_placeholder: Default::default(),
1961        }
1962    }
1963}
1964
1965impl DummyAstNode for ast::Ty {
1966    fn dummy() -> Self {
1967        ast::Ty {
1968            id: DUMMY_NODE_ID,
1969            kind: TyKind::Dummy,
1970            span: Default::default(),
1971            tokens: Default::default(),
1972        }
1973    }
1974}
1975
1976impl DummyAstNode for ast::Pat {
1977    fn dummy() -> Self {
1978        ast::Pat {
1979            id: DUMMY_NODE_ID,
1980            kind: PatKind::Wild,
1981            span: Default::default(),
1982            tokens: Default::default(),
1983        }
1984    }
1985}
1986
1987impl DummyAstNode for ast::Expr {
1988    fn dummy() -> Self {
1989        ast::Expr::dummy()
1990    }
1991}
1992
1993impl DummyAstNode for AstNodeWrapper<ast::Expr, MethodReceiverTag> {
1994    fn dummy() -> Self {
1995        AstNodeWrapper::new(ast::Expr::dummy(), MethodReceiverTag)
1996    }
1997}
1998
1999struct InvocationCollector<'a, 'b> {
2000    cx: &'a mut ExtCtxt<'b>,
2001    invocations: Vec<(Invocation, Option<Arc<SyntaxExtension>>)>,
2002    monotonic: bool,
2003}
2004
2005impl<'a, 'b> InvocationCollector<'a, 'b> {
2006    fn cfg(&self) -> StripUnconfigured<'_> {
2007        StripUnconfigured {
2008            sess: self.cx.sess,
2009            features: Some(self.cx.ecfg.features),
2010            config_tokens: false,
2011            lint_node_id: self.cx.current_expansion.lint_node_id,
2012        }
2013    }
2014
2015    fn collect(&mut self, fragment_kind: AstFragmentKind, kind: InvocationKind) -> AstFragment {
2016        let expn_id = LocalExpnId::fresh_empty();
2017        if matches!(kind, InvocationKind::GlobDelegation { .. }) {
2018            // In resolver we need to know which invocation ids are delegations early,
2019            // before their `ExpnData` is filled.
2020            self.cx.resolver.register_glob_delegation(expn_id);
2021        }
2022        let vis = kind.placeholder_visibility();
2023        self.invocations.push((
2024            Invocation {
2025                kind,
2026                fragment_kind,
2027                expansion_data: ExpansionData {
2028                    id: expn_id,
2029                    depth: self.cx.current_expansion.depth + 1,
2030                    ..self.cx.current_expansion.clone()
2031                },
2032            },
2033            None,
2034        ));
2035        placeholder(fragment_kind, NodeId::placeholder_from_expn_id(expn_id), vis)
2036    }
2037
2038    fn collect_bang(&mut self, mac: Box<ast::MacCall>, kind: AstFragmentKind) -> AstFragment {
2039        // cache the macro call span so that it can be
2040        // easily adjusted for incremental compilation
2041        let span = mac.span();
2042        self.collect(kind, InvocationKind::Bang { mac, span })
2043    }
2044
2045    fn collect_attr(
2046        &mut self,
2047        (attr, pos, derives): (ast::Attribute, usize, Vec<ast::Path>),
2048        item: Annotatable,
2049        kind: AstFragmentKind,
2050    ) -> AstFragment {
2051        self.collect(kind, InvocationKind::Attr { attr, pos, item, derives })
2052    }
2053
2054    fn collect_glob_delegation(
2055        &mut self,
2056        item: Box<ast::AssocItem>,
2057        of_trait: bool,
2058        kind: AstFragmentKind,
2059    ) -> AstFragment {
2060        self.collect(kind, InvocationKind::GlobDelegation { item, of_trait })
2061    }
2062
2063    /// If `item` is an attribute invocation, remove the attribute and return it together with
2064    /// its position and derives following it. We have to collect the derives in order to resolve
2065    /// legacy derive helpers (helpers written before derives that introduce them).
2066    fn take_first_attr(
2067        &self,
2068        item: &mut impl HasAttrs,
2069    ) -> Option<(ast::Attribute, usize, Vec<ast::Path>)> {
2070        let mut attr = None;
2071
2072        let mut cfg_pos = None;
2073        let mut attr_pos = None;
2074        for (pos, attr) in item.attrs().iter().enumerate() {
2075            if !attr.is_doc_comment() && !self.cx.expanded_inert_attrs.is_marked(attr) {
2076                let name = attr.ident().map(|ident| ident.name);
2077                if name == Some(sym::cfg) || name == Some(sym::cfg_attr) {
2078                    cfg_pos = Some(pos); // a cfg attr found, no need to search anymore
2079                    break;
2080                } else if attr_pos.is_none()
2081                    && !name.is_some_and(rustc_feature::is_builtin_attr_name)
2082                {
2083                    attr_pos = Some(pos); // a non-cfg attr found, still may find a cfg attr
2084                }
2085            }
2086        }
2087
2088        item.visit_attrs(|attrs| {
2089            attr = Some(match (cfg_pos, attr_pos) {
2090                (Some(pos), _) => (attrs.remove(pos), pos, Vec::new()),
2091                (_, Some(pos)) => {
2092                    let attr = attrs.remove(pos);
2093                    let following_derives = attrs[pos..]
2094                        .iter()
2095                        .filter(|a| a.has_name(sym::derive))
2096                        .flat_map(|a| a.meta_item_list().unwrap_or_default())
2097                        .filter_map(|meta_item_inner| match meta_item_inner {
2098                            MetaItemInner::MetaItem(ast::MetaItem {
2099                                kind: MetaItemKind::Word,
2100                                path,
2101                                ..
2102                            }) => Some(path),
2103                            _ => None,
2104                        })
2105                        .collect();
2106
2107                    (attr, pos, following_derives)
2108                }
2109                _ => return,
2110            });
2111        });
2112
2113        attr
2114    }
2115
2116    // Detect use of feature-gated or invalid attributes on macro invocations
2117    // since they will not be detected after macro expansion.
2118    fn check_attributes(&self, attrs: &[ast::Attribute], call: &ast::MacCall) {
2119        let features = self.cx.ecfg.features;
2120        let mut attrs = attrs.iter().peekable();
2121        let mut span: Option<Span> = None;
2122        while let Some(attr) = attrs.next() {
2123            rustc_ast_passes::feature_gate::check_attribute(attr, self.cx.sess, features);
2124            validate_attr::check_attr(
2125                &self.cx.sess.psess,
2126                attr,
2127                self.cx.current_expansion.lint_node_id,
2128            );
2129
2130            let current_span = if let Some(sp) = span { sp.to(attr.span) } else { attr.span };
2131            span = Some(current_span);
2132
2133            if attrs.peek().is_some_and(|next_attr| next_attr.doc_str().is_some()) {
2134                continue;
2135            }
2136
2137            if attr.is_doc_comment() {
2138                self.cx.sess.psess.buffer_lint(
2139                    UNUSED_DOC_COMMENTS,
2140                    current_span,
2141                    self.cx.current_expansion.lint_node_id,
2142                    BuiltinLintDiag::UnusedDocComment(attr.span),
2143                );
2144            } else if rustc_attr_parsing::is_builtin_attr(attr) {
2145                let attr_name = attr.ident().unwrap().name;
2146                // `#[cfg]` and `#[cfg_attr]` are special - they are
2147                // eagerly evaluated.
2148                if attr_name != sym::cfg_trace && attr_name != sym::cfg_attr_trace {
2149                    self.cx.sess.psess.buffer_lint(
2150                        UNUSED_ATTRIBUTES,
2151                        attr.span,
2152                        self.cx.current_expansion.lint_node_id,
2153                        BuiltinLintDiag::UnusedBuiltinAttribute {
2154                            attr_name,
2155                            macro_name: pprust::path_to_string(&call.path),
2156                            invoc_span: call.path.span,
2157                            attr_span: attr.span,
2158                        },
2159                    );
2160                }
2161            }
2162        }
2163    }
2164
2165    fn expand_cfg_true(
2166        &mut self,
2167        node: &mut (impl HasAttrs + HasNodeId),
2168        attr: ast::Attribute,
2169        pos: usize,
2170    ) -> EvalConfigResult {
2171        let res = self.cfg().cfg_true(&attr, node.node_id(), ShouldEmit::ErrorsAndLints);
2172        if res.as_bool() {
2173            // A trace attribute left in AST in place of the original `cfg` attribute.
2174            // It can later be used by lints or other diagnostics.
2175            let trace_attr = attr_into_trace(attr, sym::cfg_trace);
2176            node.visit_attrs(|attrs| attrs.insert(pos, trace_attr));
2177        }
2178
2179        res
2180    }
2181
2182    fn expand_cfg_attr(&self, node: &mut impl HasAttrs, attr: &ast::Attribute, pos: usize) {
2183        node.visit_attrs(|attrs| {
2184            // Repeated `insert` calls is inefficient, but the number of
2185            // insertions is almost always 0 or 1 in practice.
2186            for cfg in self.cfg().expand_cfg_attr(attr, false).into_iter().rev() {
2187                attrs.insert(pos, cfg)
2188            }
2189        });
2190    }
2191
2192    fn flat_map_node<Node: InvocationCollectorNode<OutputTy: Default>>(
2193        &mut self,
2194        mut node: Node,
2195    ) -> Node::OutputTy {
2196        loop {
2197            return match self.take_first_attr(&mut node) {
2198                Some((attr, pos, derives)) => match attr.name() {
2199                    Some(sym::cfg) => {
2200                        let res = self.expand_cfg_true(&mut node, attr, pos);
2201                        match res {
2202                            EvalConfigResult::True => continue,
2203                            EvalConfigResult::False { reason, reason_span } => {
2204                                for ident in node.declared_idents() {
2205                                    self.cx.resolver.append_stripped_cfg_item(
2206                                        self.cx.current_expansion.lint_node_id,
2207                                        ident,
2208                                        reason.clone(),
2209                                        reason_span,
2210                                    )
2211                                }
2212                            }
2213                        }
2214
2215                        Default::default()
2216                    }
2217                    Some(sym::cfg_attr) => {
2218                        self.expand_cfg_attr(&mut node, &attr, pos);
2219                        continue;
2220                    }
2221                    _ => {
2222                        Node::pre_flat_map_node_collect_attr(&self.cfg(), &attr);
2223                        self.collect_attr((attr, pos, derives), node.to_annotatable(), Node::KIND)
2224                            .make_ast::<Node>()
2225                    }
2226                },
2227                None if node.is_mac_call() => {
2228                    let (mac, attrs, add_semicolon) = node.take_mac_call();
2229                    self.check_attributes(&attrs, &mac);
2230                    let mut res = self.collect_bang(mac, Node::KIND).make_ast::<Node>();
2231                    Node::post_flat_map_node_collect_bang(&mut res, add_semicolon);
2232                    res
2233                }
2234                None if let Some((deleg, item)) = node.delegation() => {
2235                    let Some(suffixes) = &deleg.suffixes else {
2236                        let traitless_qself =
2237                            matches!(&deleg.qself, Some(qself) if qself.position == 0);
2238                        let (item, of_trait) = match node.to_annotatable() {
2239                            Annotatable::AssocItem(item, AssocCtxt::Impl { of_trait }) => {
2240                                (item, of_trait)
2241                            }
2242                            ann @ (Annotatable::Item(_)
2243                            | Annotatable::AssocItem(..)
2244                            | Annotatable::Stmt(_)) => {
2245                                let span = ann.span();
2246                                self.cx.dcx().emit_err(GlobDelegationOutsideImpls { span });
2247                                return Default::default();
2248                            }
2249                            _ => unreachable!(),
2250                        };
2251                        if traitless_qself {
2252                            let span = item.span;
2253                            self.cx.dcx().emit_err(GlobDelegationTraitlessQpath { span });
2254                            return Default::default();
2255                        }
2256                        return self
2257                            .collect_glob_delegation(item, of_trait, Node::KIND)
2258                            .make_ast::<Node>();
2259                    };
2260
2261                    let single_delegations = build_single_delegations::<Node>(
2262                        self.cx, deleg, item, suffixes, item.span, false,
2263                    );
2264                    Node::flatten_outputs(single_delegations.map(|item| {
2265                        let mut item = Node::from_item(item);
2266                        assign_id!(self, item.node_id_mut(), || item.walk_flat_map(self))
2267                    }))
2268                }
2269                None => {
2270                    match Node::wrap_flat_map_node_walk_flat_map(node, self, |mut node, this| {
2271                        assign_id!(this, node.node_id_mut(), || node.walk_flat_map(this))
2272                    }) {
2273                        Ok(output) => output,
2274                        Err(returned_node) => {
2275                            node = returned_node;
2276                            continue;
2277                        }
2278                    }
2279                }
2280            };
2281        }
2282    }
2283
2284    fn visit_node<Node: InvocationCollectorNode<OutputTy: Into<Node>> + DummyAstNode>(
2285        &mut self,
2286        node: &mut Node,
2287    ) {
2288        loop {
2289            return match self.take_first_attr(node) {
2290                Some((attr, pos, derives)) => match attr.name() {
2291                    Some(sym::cfg) => {
2292                        let span = attr.span;
2293                        if self.expand_cfg_true(node, attr, pos).as_bool() {
2294                            continue;
2295                        }
2296
2297                        node.expand_cfg_false(self, pos, span);
2298                        continue;
2299                    }
2300                    Some(sym::cfg_attr) => {
2301                        self.expand_cfg_attr(node, &attr, pos);
2302                        continue;
2303                    }
2304                    _ => {
2305                        let n = mem::replace(node, Node::dummy());
2306                        *node = self
2307                            .collect_attr((attr, pos, derives), n.to_annotatable(), Node::KIND)
2308                            .make_ast::<Node>()
2309                            .into()
2310                    }
2311                },
2312                None if node.is_mac_call() => {
2313                    let n = mem::replace(node, Node::dummy());
2314                    let (mac, attrs, _) = n.take_mac_call();
2315                    self.check_attributes(&attrs, &mac);
2316
2317                    *node = self.collect_bang(mac, Node::KIND).make_ast::<Node>().into()
2318                }
2319                None if node.delegation().is_some() => unreachable!(),
2320                None => {
2321                    assign_id!(self, node.node_id_mut(), || node.walk(self))
2322                }
2323            };
2324        }
2325    }
2326}
2327
2328impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
2329    fn flat_map_item(&mut self, node: Box<ast::Item>) -> SmallVec<[Box<ast::Item>; 1]> {
2330        self.flat_map_node(node)
2331    }
2332
2333    fn flat_map_assoc_item(
2334        &mut self,
2335        node: Box<ast::AssocItem>,
2336        ctxt: AssocCtxt,
2337    ) -> SmallVec<[Box<ast::AssocItem>; 1]> {
2338        match ctxt {
2339            AssocCtxt::Trait => self.flat_map_node(AstNodeWrapper::new(node, TraitItemTag)),
2340            AssocCtxt::Impl { of_trait: false } => {
2341                self.flat_map_node(AstNodeWrapper::new(node, ImplItemTag))
2342            }
2343            AssocCtxt::Impl { of_trait: true } => {
2344                self.flat_map_node(AstNodeWrapper::new(node, TraitImplItemTag))
2345            }
2346        }
2347    }
2348
2349    fn flat_map_foreign_item(
2350        &mut self,
2351        node: Box<ast::ForeignItem>,
2352    ) -> SmallVec<[Box<ast::ForeignItem>; 1]> {
2353        self.flat_map_node(node)
2354    }
2355
2356    fn flat_map_variant(&mut self, node: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
2357        self.flat_map_node(node)
2358    }
2359
2360    fn flat_map_where_predicate(
2361        &mut self,
2362        node: ast::WherePredicate,
2363    ) -> SmallVec<[ast::WherePredicate; 1]> {
2364        self.flat_map_node(node)
2365    }
2366
2367    fn flat_map_field_def(&mut self, node: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
2368        self.flat_map_node(node)
2369    }
2370
2371    fn flat_map_pat_field(&mut self, node: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
2372        self.flat_map_node(node)
2373    }
2374
2375    fn flat_map_expr_field(&mut self, node: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
2376        self.flat_map_node(node)
2377    }
2378
2379    fn flat_map_param(&mut self, node: ast::Param) -> SmallVec<[ast::Param; 1]> {
2380        self.flat_map_node(node)
2381    }
2382
2383    fn flat_map_generic_param(
2384        &mut self,
2385        node: ast::GenericParam,
2386    ) -> SmallVec<[ast::GenericParam; 1]> {
2387        self.flat_map_node(node)
2388    }
2389
2390    fn flat_map_arm(&mut self, node: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
2391        self.flat_map_node(node)
2392    }
2393
2394    fn flat_map_stmt(&mut self, node: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
2395        // FIXME: invocations in semicolon-less expressions positions are expanded as expressions,
2396        // changing that requires some compatibility measures.
2397        if node.is_expr() {
2398            // The only way that we can end up with a `MacCall` expression statement,
2399            // (as opposed to a `StmtKind::MacCall`) is if we have a macro as the
2400            // trailing expression in a block (e.g. `fn foo() { my_macro!() }`).
2401            // Record this information, so that we can report a more specific
2402            // `SEMICOLON_IN_EXPRESSIONS_FROM_MACROS` lint if needed.
2403            // See #78991 for an investigation of treating macros in this position
2404            // as statements, rather than expressions, during parsing.
2405            return match &node.kind {
2406                StmtKind::Expr(expr)
2407                    if matches!(**expr, ast::Expr { kind: ExprKind::MacCall(..), .. }) =>
2408                {
2409                    self.cx.current_expansion.is_trailing_mac = true;
2410                    // Don't use `assign_id` for this statement - it may get removed
2411                    // entirely due to a `#[cfg]` on the contained expression
2412                    let res = walk_flat_map_stmt(self, node);
2413                    self.cx.current_expansion.is_trailing_mac = false;
2414                    res
2415                }
2416                _ => walk_flat_map_stmt(self, node),
2417            };
2418        }
2419
2420        self.flat_map_node(node)
2421    }
2422
2423    fn visit_crate(&mut self, node: &mut ast::Crate) {
2424        self.visit_node(node)
2425    }
2426
2427    fn visit_ty(&mut self, node: &mut ast::Ty) {
2428        self.visit_node(node)
2429    }
2430
2431    fn visit_pat(&mut self, node: &mut ast::Pat) {
2432        self.visit_node(node)
2433    }
2434
2435    fn visit_expr(&mut self, node: &mut ast::Expr) {
2436        // FIXME: Feature gating is performed inconsistently between `Expr` and `OptExpr`.
2437        if let Some(attr) = node.attrs.first() {
2438            self.cfg().maybe_emit_expr_attr_err(attr);
2439        }
2440        self.visit_node(node)
2441    }
2442
2443    fn visit_method_receiver_expr(&mut self, node: &mut ast::Expr) {
2444        self.visit_node(AstNodeWrapper::from_mut(node, MethodReceiverTag))
2445    }
2446
2447    fn filter_map_expr(&mut self, node: Box<ast::Expr>) -> Option<Box<ast::Expr>> {
2448        self.flat_map_node(AstNodeWrapper::new(node, OptExprTag))
2449    }
2450
2451    fn visit_block(&mut self, node: &mut ast::Block) {
2452        let orig_dir_ownership = mem::replace(
2453            &mut self.cx.current_expansion.dir_ownership,
2454            DirOwnership::UnownedViaBlock,
2455        );
2456        walk_block(self, node);
2457        self.cx.current_expansion.dir_ownership = orig_dir_ownership;
2458    }
2459
2460    fn visit_id(&mut self, id: &mut NodeId) {
2461        // We may have already assigned a `NodeId`
2462        // by calling `assign_id`
2463        if self.monotonic && *id == ast::DUMMY_NODE_ID {
2464            *id = self.cx.resolver.next_node_id();
2465        }
2466    }
2467}
2468
2469pub struct ExpansionConfig<'feat> {
2470    pub crate_name: Symbol,
2471    pub features: &'feat Features,
2472    pub recursion_limit: Limit,
2473    pub trace_mac: bool,
2474    /// If false, strip `#[test]` nodes
2475    pub should_test: bool,
2476    /// If true, use verbose debugging for `proc_macro::Span`
2477    pub span_debug: bool,
2478    /// If true, show backtraces for proc-macro panics
2479    pub proc_macro_backtrace: bool,
2480}
2481
2482impl ExpansionConfig<'_> {
2483    pub fn default(crate_name: Symbol, features: &Features) -> ExpansionConfig<'_> {
2484        ExpansionConfig {
2485            crate_name,
2486            features,
2487            recursion_limit: Limit::new(1024),
2488            trace_mac: false,
2489            should_test: false,
2490            span_debug: false,
2491            proc_macro_backtrace: false,
2492        }
2493    }
2494}