rustc_expand/
expand.rs

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