rustc_expand/
expand.rs

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