rustc_expand/
expand.rs

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