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