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