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_parse::parser::{
23 AttemptLocalParseRecovery, CommaRecoveryMode, ForceCollect, Parser, RecoverColon, RecoverComma,
24 token_descr,
25};
26use rustc_session::lint::BuiltinLintDiag;
27use rustc_session::lint::builtin::{UNUSED_ATTRIBUTES, UNUSED_DOC_COMMENTS};
28use rustc_session::parse::feature_err;
29use rustc_session::{Limit, Session};
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 if let AttrArgs::Eq { .. } = attr_item.args {
816 self.cx.dcx().emit_err(UnsupportedKeyValue { span });
817 }
818 let inner_tokens = attr_item.args.inner_tokens();
819 match expander.expand(self.cx, span, inner_tokens, tokens) {
820 Ok(tok_result) => {
821 let fragment = self.parse_ast_fragment(
822 tok_result,
823 fragment_kind,
824 &attr_item.path,
825 span,
826 );
827 if macro_stats {
828 update_attr_macro_stats(
829 self.cx,
830 fragment_kind,
831 span,
832 &attr_item.path,
833 &attr,
834 item,
835 &fragment,
836 );
837 }
838 fragment
839 }
840 Err(guar) => return ExpandResult::Ready(fragment_kind.dummy(span, guar)),
841 }
842 } else if let SyntaxExtensionKind::LegacyAttr(expander) = ext {
843 match validate_attr::parse_meta(&self.cx.sess.psess, &attr) {
844 Ok(meta) => {
845 let item_clone = macro_stats.then(|| item.clone());
846 let items = match expander.expand(self.cx, span, &meta, item, false) {
847 ExpandResult::Ready(items) => items,
848 ExpandResult::Retry(item) => {
849 return ExpandResult::Retry(Invocation {
851 kind: InvocationKind::Attr { attr, pos, item, derives },
852 ..invoc
853 });
854 }
855 };
856 if matches!(
857 fragment_kind,
858 AstFragmentKind::Expr | AstFragmentKind::MethodReceiverExpr
859 ) && items.is_empty()
860 {
861 let guar = self.cx.dcx().emit_err(RemoveExprNotSupported { span });
862 fragment_kind.dummy(span, guar)
863 } else {
864 let fragment = fragment_kind.expect_from_annotatables(items);
865 if macro_stats {
866 update_attr_macro_stats(
867 self.cx,
868 fragment_kind,
869 span,
870 &meta.path,
871 &attr,
872 item_clone.unwrap(),
873 &fragment,
874 );
875 }
876 fragment
877 }
878 }
879 Err(err) => {
880 let _guar = err.emit();
881 fragment_kind.expect_from_annotatables(iter::once(item))
882 }
883 }
884 } else if let SyntaxExtensionKind::NonMacroAttr = ext {
885 self.cx.expanded_inert_attrs.mark(&attr);
887 item.visit_attrs(|attrs| attrs.insert(pos, attr));
888 fragment_kind.expect_from_annotatables(iter::once(item))
889 } else {
890 unreachable!();
891 }
892 }
893 InvocationKind::Derive { path, item, is_const } => match ext {
894 SyntaxExtensionKind::Derive(expander)
895 | SyntaxExtensionKind::LegacyDerive(expander) => {
896 if let SyntaxExtensionKind::Derive(..) = ext {
897 self.gate_proc_macro_input(&item);
898 }
899 let meta = ast::MetaItem {
902 unsafety: ast::Safety::Default,
903 kind: MetaItemKind::Word,
904 span,
905 path,
906 };
907 let items = match expander.expand(self.cx, span, &meta, item, is_const) {
908 ExpandResult::Ready(items) => items,
909 ExpandResult::Retry(item) => {
910 return ExpandResult::Retry(Invocation {
912 kind: InvocationKind::Derive { path: meta.path, item, is_const },
913 ..invoc
914 });
915 }
916 };
917 let fragment = fragment_kind.expect_from_annotatables(items);
918 if macro_stats {
919 update_derive_macro_stats(
920 self.cx,
921 fragment_kind,
922 span,
923 &meta.path,
924 &fragment,
925 );
926 }
927 fragment
928 }
929 SyntaxExtensionKind::MacroRules(expander)
930 if expander.kinds().contains(MacroKinds::DERIVE) =>
931 {
932 if is_const {
933 let guar = self
934 .cx
935 .dcx()
936 .span_err(span, "macro `derive` does not support const derives");
937 return ExpandResult::Ready(fragment_kind.dummy(span, guar));
938 }
939 let body = item.to_tokens();
940 match expander.expand_derive(self.cx, span, &body) {
941 Ok(tok_result) => {
942 let fragment =
943 self.parse_ast_fragment(tok_result, fragment_kind, &path, span);
944 if macro_stats {
945 update_derive_macro_stats(
946 self.cx,
947 fragment_kind,
948 span,
949 &path,
950 &fragment,
951 );
952 }
953 fragment
954 }
955 Err(guar) => return ExpandResult::Ready(fragment_kind.dummy(span, guar)),
956 }
957 }
958 _ => unreachable!(),
959 },
960 InvocationKind::GlobDelegation { item, of_trait } => {
961 let AssocItemKind::DelegationMac(deleg) = &item.kind else { unreachable!() };
962 let suffixes = match ext {
963 SyntaxExtensionKind::GlobDelegation(expander) => match expander.expand(self.cx)
964 {
965 ExpandResult::Ready(suffixes) => suffixes,
966 ExpandResult::Retry(()) => {
967 return ExpandResult::Retry(Invocation {
969 kind: InvocationKind::GlobDelegation { item, of_trait },
970 ..invoc
971 });
972 }
973 },
974 SyntaxExtensionKind::LegacyBang(..) => {
975 let msg = "expanded a dummy glob delegation";
976 let guar = self.cx.dcx().span_delayed_bug(span, msg);
977 return ExpandResult::Ready(fragment_kind.dummy(span, guar));
978 }
979 _ => unreachable!(),
980 };
981
982 type Node = AstNodeWrapper<Box<ast::AssocItem>, ImplItemTag>;
983 let single_delegations = build_single_delegations::<Node>(
984 self.cx, deleg, &item, &suffixes, item.span, true,
985 );
986 fragment_kind.expect_from_annotatables(single_delegations.map(|item| {
988 Annotatable::AssocItem(Box::new(item), AssocCtxt::Impl { of_trait })
989 }))
990 }
991 })
992 }
993
994 #[allow(rustc::untranslatable_diagnostic)] fn gate_proc_macro_attr_item(&self, span: Span, item: &Annotatable) {
996 let kind = match item {
997 Annotatable::Item(_)
998 | Annotatable::AssocItem(..)
999 | Annotatable::ForeignItem(_)
1000 | Annotatable::Crate(..) => return,
1001 Annotatable::Stmt(stmt) => {
1002 if stmt.is_item() {
1005 return;
1006 }
1007 "statements"
1008 }
1009 Annotatable::Expr(_) => "expressions",
1010 Annotatable::Arm(..)
1011 | Annotatable::ExprField(..)
1012 | Annotatable::PatField(..)
1013 | Annotatable::GenericParam(..)
1014 | Annotatable::Param(..)
1015 | Annotatable::FieldDef(..)
1016 | Annotatable::Variant(..)
1017 | Annotatable::WherePredicate(..) => panic!("unexpected annotatable"),
1018 };
1019 if self.cx.ecfg.features.proc_macro_hygiene() {
1020 return;
1021 }
1022 feature_err(
1023 &self.cx.sess,
1024 sym::proc_macro_hygiene,
1025 span,
1026 format!("custom attributes cannot be applied to {kind}"),
1027 )
1028 .emit();
1029 }
1030
1031 fn gate_proc_macro_input(&self, annotatable: &Annotatable) {
1032 struct GateProcMacroInput<'a> {
1033 sess: &'a Session,
1034 }
1035
1036 impl<'ast, 'a> Visitor<'ast> for GateProcMacroInput<'a> {
1037 fn visit_item(&mut self, item: &'ast ast::Item) {
1038 match &item.kind {
1039 ItemKind::Mod(_, _, mod_kind)
1040 if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _)) =>
1041 {
1042 feature_err(
1043 self.sess,
1044 sym::proc_macro_hygiene,
1045 item.span,
1046 fluent_generated::expand_non_inline_modules_in_proc_macro_input_are_unstable,
1047 )
1048 .emit();
1049 }
1050 _ => {}
1051 }
1052
1053 visit::walk_item(self, item);
1054 }
1055 }
1056
1057 if !self.cx.ecfg.features.proc_macro_hygiene() {
1058 annotatable.visit_with(&mut GateProcMacroInput { sess: &self.cx.sess });
1059 }
1060 }
1061
1062 fn parse_ast_fragment(
1063 &mut self,
1064 toks: TokenStream,
1065 kind: AstFragmentKind,
1066 path: &ast::Path,
1067 span: Span,
1068 ) -> AstFragment {
1069 let mut parser = self.cx.new_parser_from_tts(toks);
1070 match parse_ast_fragment(&mut parser, kind) {
1071 Ok(fragment) => {
1072 ensure_complete_parse(&parser, path, kind.name(), span);
1073 fragment
1074 }
1075 Err(mut err) => {
1076 if err.span.is_dummy() {
1077 err.span(span);
1078 }
1079 annotate_err_with_kind(&mut err, kind, span);
1080 let guar = err.emit();
1081 self.cx.macro_error_and_trace_macros_diag();
1082 kind.dummy(span, guar)
1083 }
1084 }
1085 }
1086}
1087
1088pub fn parse_ast_fragment<'a>(
1089 this: &mut Parser<'a>,
1090 kind: AstFragmentKind,
1091) -> PResult<'a, AstFragment> {
1092 Ok(match kind {
1093 AstFragmentKind::Items => {
1094 let mut items = SmallVec::new();
1095 while let Some(item) = this.parse_item(ForceCollect::No)? {
1096 items.push(item);
1097 }
1098 AstFragment::Items(items)
1099 }
1100 AstFragmentKind::TraitItems => {
1101 let mut items = SmallVec::new();
1102 while let Some(item) = this.parse_trait_item(ForceCollect::No)? {
1103 items.extend(item);
1104 }
1105 AstFragment::TraitItems(items)
1106 }
1107 AstFragmentKind::ImplItems => {
1108 let mut items = SmallVec::new();
1109 while let Some(item) = this.parse_impl_item(ForceCollect::No)? {
1110 items.extend(item);
1111 }
1112 AstFragment::ImplItems(items)
1113 }
1114 AstFragmentKind::TraitImplItems => {
1115 let mut items = SmallVec::new();
1116 while let Some(item) = this.parse_impl_item(ForceCollect::No)? {
1117 items.extend(item);
1118 }
1119 AstFragment::TraitImplItems(items)
1120 }
1121 AstFragmentKind::ForeignItems => {
1122 let mut items = SmallVec::new();
1123 while let Some(item) = this.parse_foreign_item(ForceCollect::No)? {
1124 items.extend(item);
1125 }
1126 AstFragment::ForeignItems(items)
1127 }
1128 AstFragmentKind::Stmts => {
1129 let mut stmts = SmallVec::new();
1130 while this.token != token::Eof && this.token != token::CloseBrace {
1132 if let Some(stmt) = this.parse_full_stmt(AttemptLocalParseRecovery::Yes)? {
1133 stmts.push(stmt);
1134 }
1135 }
1136 AstFragment::Stmts(stmts)
1137 }
1138 AstFragmentKind::Expr => AstFragment::Expr(this.parse_expr()?),
1139 AstFragmentKind::MethodReceiverExpr => AstFragment::MethodReceiverExpr(this.parse_expr()?),
1140 AstFragmentKind::OptExpr => {
1141 if this.token != token::Eof {
1142 AstFragment::OptExpr(Some(this.parse_expr()?))
1143 } else {
1144 AstFragment::OptExpr(None)
1145 }
1146 }
1147 AstFragmentKind::Ty => AstFragment::Ty(this.parse_ty()?),
1148 AstFragmentKind::Pat => AstFragment::Pat(this.parse_pat_allow_top_guard(
1149 None,
1150 RecoverComma::No,
1151 RecoverColon::Yes,
1152 CommaRecoveryMode::LikelyTuple,
1153 )?),
1154 AstFragmentKind::Crate => AstFragment::Crate(this.parse_crate_mod()?),
1155 AstFragmentKind::Arms
1156 | AstFragmentKind::ExprFields
1157 | AstFragmentKind::PatFields
1158 | AstFragmentKind::GenericParams
1159 | AstFragmentKind::Params
1160 | AstFragmentKind::FieldDefs
1161 | AstFragmentKind::Variants
1162 | AstFragmentKind::WherePredicates => panic!("unexpected AST fragment kind"),
1163 })
1164}
1165
1166pub(crate) fn ensure_complete_parse<'a>(
1167 parser: &Parser<'a>,
1168 macro_path: &ast::Path,
1169 kind_name: &str,
1170 span: Span,
1171) {
1172 if parser.token != token::Eof {
1173 let descr = token_descr(&parser.token);
1174 let def_site_span = parser.token.span.with_ctxt(SyntaxContext::root());
1176
1177 let semi_span = parser.psess.source_map().next_point(span);
1178 let add_semicolon = match &parser.psess.source_map().span_to_snippet(semi_span) {
1179 Ok(snippet) if &snippet[..] != ";" && kind_name == "expression" => {
1180 Some(span.shrink_to_hi())
1181 }
1182 _ => None,
1183 };
1184
1185 let expands_to_match_arm = kind_name == "pattern" && parser.token == token::FatArrow;
1186
1187 parser.dcx().emit_err(IncompleteParse {
1188 span: def_site_span,
1189 descr,
1190 label_span: span,
1191 macro_path,
1192 kind_name,
1193 expands_to_match_arm,
1194 add_semicolon,
1195 });
1196 }
1197}
1198
1199macro_rules! assign_id {
1222 ($self:ident, $id:expr, $closure:expr) => {{
1223 let old_id = $self.cx.current_expansion.lint_node_id;
1224 if $self.monotonic {
1225 debug_assert_eq!(*$id, ast::DUMMY_NODE_ID);
1226 let new_id = $self.cx.resolver.next_node_id();
1227 *$id = new_id;
1228 $self.cx.current_expansion.lint_node_id = new_id;
1229 }
1230 let ret = ($closure)();
1231 $self.cx.current_expansion.lint_node_id = old_id;
1232 ret
1233 }};
1234}
1235
1236enum AddSemicolon {
1237 Yes,
1238 No,
1239}
1240
1241trait InvocationCollectorNode: HasAttrs + HasNodeId + Sized {
1244 type OutputTy = SmallVec<[Self; 1]>;
1245 type ItemKind = ItemKind;
1246 const KIND: AstFragmentKind;
1247 fn to_annotatable(self) -> Annotatable;
1248 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy;
1249 fn descr() -> &'static str {
1250 unreachable!()
1251 }
1252 fn walk_flat_map(self, _collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1253 unreachable!()
1254 }
1255 fn walk(&mut self, _collector: &mut InvocationCollector<'_, '_>) {
1256 unreachable!()
1257 }
1258 fn is_mac_call(&self) -> bool {
1259 false
1260 }
1261 fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1262 unreachable!()
1263 }
1264 fn delegation(&self) -> Option<(&ast::DelegationMac, &ast::Item<Self::ItemKind>)> {
1265 None
1266 }
1267 fn delegation_item_kind(_deleg: Box<ast::Delegation>) -> Self::ItemKind {
1268 unreachable!()
1269 }
1270 fn from_item(_item: ast::Item<Self::ItemKind>) -> Self {
1271 unreachable!()
1272 }
1273 fn flatten_outputs(_outputs: impl Iterator<Item = Self::OutputTy>) -> Self::OutputTy {
1274 unreachable!()
1275 }
1276 fn pre_flat_map_node_collect_attr(_cfg: &StripUnconfigured<'_>, _attr: &ast::Attribute) {}
1277 fn post_flat_map_node_collect_bang(_output: &mut Self::OutputTy, _add_semicolon: AddSemicolon) {
1278 }
1279 fn wrap_flat_map_node_walk_flat_map(
1280 node: Self,
1281 collector: &mut InvocationCollector<'_, '_>,
1282 walk_flat_map: impl FnOnce(Self, &mut InvocationCollector<'_, '_>) -> Self::OutputTy,
1283 ) -> Result<Self::OutputTy, Self> {
1284 Ok(walk_flat_map(node, collector))
1285 }
1286 fn expand_cfg_false(
1287 &mut self,
1288 collector: &mut InvocationCollector<'_, '_>,
1289 _pos: usize,
1290 span: Span,
1291 ) {
1292 collector.cx.dcx().emit_err(RemoveNodeNotSupported { span, descr: Self::descr() });
1293 }
1294
1295 fn declared_idents(&self) -> Vec<Ident> {
1298 vec![]
1299 }
1300}
1301
1302impl InvocationCollectorNode for Box<ast::Item> {
1303 const KIND: AstFragmentKind = AstFragmentKind::Items;
1304 fn to_annotatable(self) -> Annotatable {
1305 Annotatable::Item(self)
1306 }
1307 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1308 fragment.make_items()
1309 }
1310 fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1311 walk_flat_map_item(collector, self)
1312 }
1313 fn is_mac_call(&self) -> bool {
1314 matches!(self.kind, ItemKind::MacCall(..))
1315 }
1316 fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1317 match self.kind {
1318 ItemKind::MacCall(mac) => (mac, self.attrs, AddSemicolon::No),
1319 _ => unreachable!(),
1320 }
1321 }
1322 fn delegation(&self) -> Option<(&ast::DelegationMac, &ast::Item<Self::ItemKind>)> {
1323 match &self.kind {
1324 ItemKind::DelegationMac(deleg) => Some((deleg, self)),
1325 _ => None,
1326 }
1327 }
1328 fn delegation_item_kind(deleg: Box<ast::Delegation>) -> Self::ItemKind {
1329 ItemKind::Delegation(deleg)
1330 }
1331 fn from_item(item: ast::Item<Self::ItemKind>) -> Self {
1332 Box::new(item)
1333 }
1334 fn flatten_outputs(items: impl Iterator<Item = Self::OutputTy>) -> Self::OutputTy {
1335 items.flatten().collect()
1336 }
1337 fn wrap_flat_map_node_walk_flat_map(
1338 mut node: Self,
1339 collector: &mut InvocationCollector<'_, '_>,
1340 walk_flat_map: impl FnOnce(Self, &mut InvocationCollector<'_, '_>) -> Self::OutputTy,
1341 ) -> Result<Self::OutputTy, Self> {
1342 if !matches!(node.kind, ItemKind::Mod(..)) {
1343 return Ok(walk_flat_map(node, collector));
1344 }
1345
1346 let (span, mut attrs) = (node.span, mem::take(&mut node.attrs));
1348 let ItemKind::Mod(_, ident, ref mut mod_kind) = node.kind else { unreachable!() };
1349 let ecx = &mut collector.cx;
1350 let (file_path, dir_path, dir_ownership) = match mod_kind {
1351 ModKind::Loaded(_, inline, _) => {
1352 let (dir_path, dir_ownership) = mod_dir_path(
1354 ecx.sess,
1355 ident,
1356 &attrs,
1357 &ecx.current_expansion.module,
1358 ecx.current_expansion.dir_ownership,
1359 *inline,
1360 );
1361 let file_path = match inline {
1364 Inline::Yes => None,
1365 Inline::No { .. } => mod_file_path_from_attr(ecx.sess, &attrs, &dir_path),
1366 };
1367 node.attrs = attrs;
1368 (file_path, dir_path, dir_ownership)
1369 }
1370 ModKind::Unloaded => {
1371 let old_attrs_len = attrs.len();
1373 let ParsedExternalMod {
1374 items,
1375 spans,
1376 file_path,
1377 dir_path,
1378 dir_ownership,
1379 had_parse_error,
1380 } = parse_external_mod(
1381 ecx.sess,
1382 ident,
1383 span,
1384 &ecx.current_expansion.module,
1385 ecx.current_expansion.dir_ownership,
1386 &mut attrs,
1387 );
1388
1389 if let Some(lint_store) = ecx.lint_store {
1390 lint_store.pre_expansion_lint(
1391 ecx.sess,
1392 ecx.ecfg.features,
1393 ecx.resolver.registered_tools(),
1394 ecx.current_expansion.lint_node_id,
1395 &attrs,
1396 &items,
1397 ident.name,
1398 );
1399 }
1400
1401 *mod_kind = ModKind::Loaded(items, Inline::No { had_parse_error }, spans);
1402 node.attrs = attrs;
1403 if node.attrs.len() > old_attrs_len {
1404 return Err(node);
1408 }
1409 (Some(file_path), dir_path, dir_ownership)
1410 }
1411 };
1412
1413 let mut module = ecx.current_expansion.module.with_dir_path(dir_path);
1415 module.mod_path.push(ident);
1416 if let Some(file_path) = file_path {
1417 module.file_path_stack.push(file_path);
1418 }
1419
1420 let orig_module = mem::replace(&mut ecx.current_expansion.module, Rc::new(module));
1421 let orig_dir_ownership =
1422 mem::replace(&mut ecx.current_expansion.dir_ownership, dir_ownership);
1423
1424 let res = Ok(walk_flat_map(node, collector));
1425
1426 collector.cx.current_expansion.dir_ownership = orig_dir_ownership;
1427 collector.cx.current_expansion.module = orig_module;
1428 res
1429 }
1430
1431 fn declared_idents(&self) -> Vec<Ident> {
1432 if let ItemKind::Use(ut) = &self.kind {
1433 fn collect_use_tree_leaves(ut: &ast::UseTree, idents: &mut Vec<Ident>) {
1434 match &ut.kind {
1435 ast::UseTreeKind::Glob => {}
1436 ast::UseTreeKind::Simple(_) => idents.push(ut.ident()),
1437 ast::UseTreeKind::Nested { items, .. } => {
1438 for (ut, _) in items {
1439 collect_use_tree_leaves(ut, idents);
1440 }
1441 }
1442 }
1443 }
1444 let mut idents = Vec::new();
1445 collect_use_tree_leaves(&ut, &mut idents);
1446 idents
1447 } else {
1448 self.kind.ident().into_iter().collect()
1449 }
1450 }
1451}
1452
1453struct TraitItemTag;
1454impl InvocationCollectorNode for AstNodeWrapper<Box<ast::AssocItem>, TraitItemTag> {
1455 type OutputTy = SmallVec<[Box<ast::AssocItem>; 1]>;
1456 type ItemKind = AssocItemKind;
1457 const KIND: AstFragmentKind = AstFragmentKind::TraitItems;
1458 fn to_annotatable(self) -> Annotatable {
1459 Annotatable::AssocItem(self.wrapped, AssocCtxt::Trait)
1460 }
1461 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1462 fragment.make_trait_items()
1463 }
1464 fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1465 walk_flat_map_assoc_item(collector, self.wrapped, AssocCtxt::Trait)
1466 }
1467 fn is_mac_call(&self) -> bool {
1468 matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
1469 }
1470 fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1471 let item = self.wrapped;
1472 match item.kind {
1473 AssocItemKind::MacCall(mac) => (mac, item.attrs, AddSemicolon::No),
1474 _ => unreachable!(),
1475 }
1476 }
1477 fn delegation(&self) -> Option<(&ast::DelegationMac, &ast::Item<Self::ItemKind>)> {
1478 match &self.wrapped.kind {
1479 AssocItemKind::DelegationMac(deleg) => Some((deleg, &self.wrapped)),
1480 _ => None,
1481 }
1482 }
1483 fn delegation_item_kind(deleg: Box<ast::Delegation>) -> Self::ItemKind {
1484 AssocItemKind::Delegation(deleg)
1485 }
1486 fn from_item(item: ast::Item<Self::ItemKind>) -> Self {
1487 AstNodeWrapper::new(Box::new(item), TraitItemTag)
1488 }
1489 fn flatten_outputs(items: impl Iterator<Item = Self::OutputTy>) -> Self::OutputTy {
1490 items.flatten().collect()
1491 }
1492}
1493
1494struct ImplItemTag;
1495impl InvocationCollectorNode for AstNodeWrapper<Box<ast::AssocItem>, ImplItemTag> {
1496 type OutputTy = SmallVec<[Box<ast::AssocItem>; 1]>;
1497 type ItemKind = AssocItemKind;
1498 const KIND: AstFragmentKind = AstFragmentKind::ImplItems;
1499 fn to_annotatable(self) -> Annotatable {
1500 Annotatable::AssocItem(self.wrapped, AssocCtxt::Impl { of_trait: false })
1501 }
1502 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1503 fragment.make_impl_items()
1504 }
1505 fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1506 walk_flat_map_assoc_item(collector, self.wrapped, AssocCtxt::Impl { of_trait: false })
1507 }
1508 fn is_mac_call(&self) -> bool {
1509 matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
1510 }
1511 fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1512 let item = self.wrapped;
1513 match item.kind {
1514 AssocItemKind::MacCall(mac) => (mac, item.attrs, AddSemicolon::No),
1515 _ => unreachable!(),
1516 }
1517 }
1518 fn delegation(&self) -> Option<(&ast::DelegationMac, &ast::Item<Self::ItemKind>)> {
1519 match &self.wrapped.kind {
1520 AssocItemKind::DelegationMac(deleg) => Some((deleg, &self.wrapped)),
1521 _ => None,
1522 }
1523 }
1524 fn delegation_item_kind(deleg: Box<ast::Delegation>) -> Self::ItemKind {
1525 AssocItemKind::Delegation(deleg)
1526 }
1527 fn from_item(item: ast::Item<Self::ItemKind>) -> Self {
1528 AstNodeWrapper::new(Box::new(item), ImplItemTag)
1529 }
1530 fn flatten_outputs(items: impl Iterator<Item = Self::OutputTy>) -> Self::OutputTy {
1531 items.flatten().collect()
1532 }
1533}
1534
1535struct TraitImplItemTag;
1536impl InvocationCollectorNode for AstNodeWrapper<Box<ast::AssocItem>, TraitImplItemTag> {
1537 type OutputTy = SmallVec<[Box<ast::AssocItem>; 1]>;
1538 type ItemKind = AssocItemKind;
1539 const KIND: AstFragmentKind = AstFragmentKind::TraitImplItems;
1540 fn to_annotatable(self) -> Annotatable {
1541 Annotatable::AssocItem(self.wrapped, AssocCtxt::Impl { of_trait: true })
1542 }
1543 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1544 fragment.make_trait_impl_items()
1545 }
1546 fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1547 walk_flat_map_assoc_item(collector, self.wrapped, AssocCtxt::Impl { of_trait: true })
1548 }
1549 fn is_mac_call(&self) -> bool {
1550 matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
1551 }
1552 fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1553 let item = self.wrapped;
1554 match item.kind {
1555 AssocItemKind::MacCall(mac) => (mac, item.attrs, AddSemicolon::No),
1556 _ => unreachable!(),
1557 }
1558 }
1559 fn delegation(&self) -> Option<(&ast::DelegationMac, &ast::Item<Self::ItemKind>)> {
1560 match &self.wrapped.kind {
1561 AssocItemKind::DelegationMac(deleg) => Some((deleg, &self.wrapped)),
1562 _ => None,
1563 }
1564 }
1565 fn delegation_item_kind(deleg: Box<ast::Delegation>) -> Self::ItemKind {
1566 AssocItemKind::Delegation(deleg)
1567 }
1568 fn from_item(item: ast::Item<Self::ItemKind>) -> Self {
1569 AstNodeWrapper::new(Box::new(item), TraitImplItemTag)
1570 }
1571 fn flatten_outputs(items: impl Iterator<Item = Self::OutputTy>) -> Self::OutputTy {
1572 items.flatten().collect()
1573 }
1574}
1575
1576impl InvocationCollectorNode for Box<ast::ForeignItem> {
1577 const KIND: AstFragmentKind = AstFragmentKind::ForeignItems;
1578 fn to_annotatable(self) -> Annotatable {
1579 Annotatable::ForeignItem(self)
1580 }
1581 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1582 fragment.make_foreign_items()
1583 }
1584 fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1585 walk_flat_map_foreign_item(collector, self)
1586 }
1587 fn is_mac_call(&self) -> bool {
1588 matches!(self.kind, ForeignItemKind::MacCall(..))
1589 }
1590 fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1591 match self.kind {
1592 ForeignItemKind::MacCall(mac) => (mac, self.attrs, AddSemicolon::No),
1593 _ => unreachable!(),
1594 }
1595 }
1596}
1597
1598impl InvocationCollectorNode for ast::Variant {
1599 const KIND: AstFragmentKind = AstFragmentKind::Variants;
1600 fn to_annotatable(self) -> Annotatable {
1601 Annotatable::Variant(self)
1602 }
1603 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1604 fragment.make_variants()
1605 }
1606 fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1607 walk_flat_map_variant(collector, self)
1608 }
1609}
1610
1611impl InvocationCollectorNode for ast::WherePredicate {
1612 const KIND: AstFragmentKind = AstFragmentKind::WherePredicates;
1613 fn to_annotatable(self) -> Annotatable {
1614 Annotatable::WherePredicate(self)
1615 }
1616 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1617 fragment.make_where_predicates()
1618 }
1619 fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1620 walk_flat_map_where_predicate(collector, self)
1621 }
1622}
1623
1624impl InvocationCollectorNode for ast::FieldDef {
1625 const KIND: AstFragmentKind = AstFragmentKind::FieldDefs;
1626 fn to_annotatable(self) -> Annotatable {
1627 Annotatable::FieldDef(self)
1628 }
1629 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1630 fragment.make_field_defs()
1631 }
1632 fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1633 walk_flat_map_field_def(collector, self)
1634 }
1635}
1636
1637impl InvocationCollectorNode for ast::PatField {
1638 const KIND: AstFragmentKind = AstFragmentKind::PatFields;
1639 fn to_annotatable(self) -> Annotatable {
1640 Annotatable::PatField(self)
1641 }
1642 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1643 fragment.make_pat_fields()
1644 }
1645 fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1646 walk_flat_map_pat_field(collector, self)
1647 }
1648}
1649
1650impl InvocationCollectorNode for ast::ExprField {
1651 const KIND: AstFragmentKind = AstFragmentKind::ExprFields;
1652 fn to_annotatable(self) -> Annotatable {
1653 Annotatable::ExprField(self)
1654 }
1655 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1656 fragment.make_expr_fields()
1657 }
1658 fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1659 walk_flat_map_expr_field(collector, self)
1660 }
1661}
1662
1663impl InvocationCollectorNode for ast::Param {
1664 const KIND: AstFragmentKind = AstFragmentKind::Params;
1665 fn to_annotatable(self) -> Annotatable {
1666 Annotatable::Param(self)
1667 }
1668 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1669 fragment.make_params()
1670 }
1671 fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1672 walk_flat_map_param(collector, self)
1673 }
1674}
1675
1676impl InvocationCollectorNode for ast::GenericParam {
1677 const KIND: AstFragmentKind = AstFragmentKind::GenericParams;
1678 fn to_annotatable(self) -> Annotatable {
1679 Annotatable::GenericParam(self)
1680 }
1681 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1682 fragment.make_generic_params()
1683 }
1684 fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1685 walk_flat_map_generic_param(collector, self)
1686 }
1687}
1688
1689impl InvocationCollectorNode for ast::Arm {
1690 const KIND: AstFragmentKind = AstFragmentKind::Arms;
1691 fn to_annotatable(self) -> Annotatable {
1692 Annotatable::Arm(self)
1693 }
1694 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1695 fragment.make_arms()
1696 }
1697 fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1698 walk_flat_map_arm(collector, self)
1699 }
1700}
1701
1702impl InvocationCollectorNode for ast::Stmt {
1703 const KIND: AstFragmentKind = AstFragmentKind::Stmts;
1704 fn to_annotatable(self) -> Annotatable {
1705 Annotatable::Stmt(Box::new(self))
1706 }
1707 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1708 fragment.make_stmts()
1709 }
1710 fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1711 walk_flat_map_stmt(collector, self)
1712 }
1713 fn is_mac_call(&self) -> bool {
1714 match &self.kind {
1715 StmtKind::MacCall(..) => true,
1716 StmtKind::Item(item) => matches!(item.kind, ItemKind::MacCall(..)),
1717 StmtKind::Semi(expr) => matches!(expr.kind, ExprKind::MacCall(..)),
1718 StmtKind::Expr(..) => unreachable!(),
1719 StmtKind::Let(..) | StmtKind::Empty => false,
1720 }
1721 }
1722 fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1723 let (add_semicolon, mac, attrs) = match self.kind {
1726 StmtKind::MacCall(mac) => {
1727 let ast::MacCallStmt { mac, style, attrs, .. } = *mac;
1728 (style == MacStmtStyle::Semicolon, mac, attrs)
1729 }
1730 StmtKind::Item(item) => match *item {
1731 ast::Item { kind: ItemKind::MacCall(mac), attrs, .. } => {
1732 (mac.args.need_semicolon(), mac, attrs)
1733 }
1734 _ => unreachable!(),
1735 },
1736 StmtKind::Semi(expr) => match *expr {
1737 ast::Expr { kind: ExprKind::MacCall(mac), attrs, .. } => {
1738 (mac.args.need_semicolon(), mac, attrs)
1739 }
1740 _ => unreachable!(),
1741 },
1742 _ => unreachable!(),
1743 };
1744 (mac, attrs, if add_semicolon { AddSemicolon::Yes } else { AddSemicolon::No })
1745 }
1746 fn delegation(&self) -> Option<(&ast::DelegationMac, &ast::Item<Self::ItemKind>)> {
1747 match &self.kind {
1748 StmtKind::Item(item) => match &item.kind {
1749 ItemKind::DelegationMac(deleg) => Some((deleg, item)),
1750 _ => None,
1751 },
1752 _ => None,
1753 }
1754 }
1755 fn delegation_item_kind(deleg: Box<ast::Delegation>) -> Self::ItemKind {
1756 ItemKind::Delegation(deleg)
1757 }
1758 fn from_item(item: ast::Item<Self::ItemKind>) -> Self {
1759 ast::Stmt { id: ast::DUMMY_NODE_ID, span: item.span, kind: StmtKind::Item(Box::new(item)) }
1760 }
1761 fn flatten_outputs(items: impl Iterator<Item = Self::OutputTy>) -> Self::OutputTy {
1762 items.flatten().collect()
1763 }
1764 fn post_flat_map_node_collect_bang(stmts: &mut Self::OutputTy, add_semicolon: AddSemicolon) {
1765 if matches!(add_semicolon, AddSemicolon::Yes) {
1768 if let Some(stmt) = stmts.pop() {
1769 stmts.push(stmt.add_trailing_semicolon());
1770 }
1771 }
1772 }
1773}
1774
1775impl InvocationCollectorNode for ast::Crate {
1776 type OutputTy = ast::Crate;
1777 const KIND: AstFragmentKind = AstFragmentKind::Crate;
1778 fn to_annotatable(self) -> Annotatable {
1779 Annotatable::Crate(self)
1780 }
1781 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1782 fragment.make_crate()
1783 }
1784 fn walk(&mut self, collector: &mut InvocationCollector<'_, '_>) {
1785 walk_crate(collector, self)
1786 }
1787 fn expand_cfg_false(
1788 &mut self,
1789 collector: &mut InvocationCollector<'_, '_>,
1790 pos: usize,
1791 _span: Span,
1792 ) {
1793 self.attrs.truncate(pos);
1796 self.items.truncate(collector.cx.num_standard_library_imports);
1798 }
1799}
1800
1801impl InvocationCollectorNode for ast::Ty {
1802 type OutputTy = Box<ast::Ty>;
1803 const KIND: AstFragmentKind = AstFragmentKind::Ty;
1804 fn to_annotatable(self) -> Annotatable {
1805 unreachable!()
1806 }
1807 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1808 fragment.make_ty()
1809 }
1810 fn walk(&mut self, collector: &mut InvocationCollector<'_, '_>) {
1811 if let ast::TyKind::ImplTrait(..) = self.kind {
1814 let name = Symbol::intern(&pprust::ty_to_string(self).replace('\n', " "));
1819 collector.cx.resolver.insert_impl_trait_name(self.id, name);
1820 }
1821 walk_ty(collector, self)
1822 }
1823 fn is_mac_call(&self) -> bool {
1824 matches!(self.kind, ast::TyKind::MacCall(..))
1825 }
1826 fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1827 match self.kind {
1828 TyKind::MacCall(mac) => (mac, AttrVec::new(), AddSemicolon::No),
1829 _ => unreachable!(),
1830 }
1831 }
1832}
1833
1834impl InvocationCollectorNode for ast::Pat {
1835 type OutputTy = Box<ast::Pat>;
1836 const KIND: AstFragmentKind = AstFragmentKind::Pat;
1837 fn to_annotatable(self) -> Annotatable {
1838 unreachable!()
1839 }
1840 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1841 fragment.make_pat()
1842 }
1843 fn walk(&mut self, collector: &mut InvocationCollector<'_, '_>) {
1844 walk_pat(collector, self)
1845 }
1846 fn is_mac_call(&self) -> bool {
1847 matches!(self.kind, PatKind::MacCall(..))
1848 }
1849 fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1850 match self.kind {
1851 PatKind::MacCall(mac) => (mac, AttrVec::new(), AddSemicolon::No),
1852 _ => unreachable!(),
1853 }
1854 }
1855}
1856
1857impl InvocationCollectorNode for ast::Expr {
1858 type OutputTy = Box<ast::Expr>;
1859 const KIND: AstFragmentKind = AstFragmentKind::Expr;
1860 fn to_annotatable(self) -> Annotatable {
1861 Annotatable::Expr(Box::new(self))
1862 }
1863 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1864 fragment.make_expr()
1865 }
1866 fn descr() -> &'static str {
1867 "an expression"
1868 }
1869 fn walk(&mut self, collector: &mut InvocationCollector<'_, '_>) {
1870 walk_expr(collector, self)
1871 }
1872 fn is_mac_call(&self) -> bool {
1873 matches!(self.kind, ExprKind::MacCall(..))
1874 }
1875 fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1876 match self.kind {
1877 ExprKind::MacCall(mac) => (mac, self.attrs, AddSemicolon::No),
1878 _ => unreachable!(),
1879 }
1880 }
1881}
1882
1883struct OptExprTag;
1884impl InvocationCollectorNode for AstNodeWrapper<Box<ast::Expr>, OptExprTag> {
1885 type OutputTy = Option<Box<ast::Expr>>;
1886 const KIND: AstFragmentKind = AstFragmentKind::OptExpr;
1887 fn to_annotatable(self) -> Annotatable {
1888 Annotatable::Expr(self.wrapped)
1889 }
1890 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1891 fragment.make_opt_expr()
1892 }
1893 fn walk_flat_map(mut self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
1894 walk_expr(collector, &mut self.wrapped);
1895 Some(self.wrapped)
1896 }
1897 fn is_mac_call(&self) -> bool {
1898 matches!(self.wrapped.kind, ast::ExprKind::MacCall(..))
1899 }
1900 fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1901 let node = self.wrapped;
1902 match node.kind {
1903 ExprKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
1904 _ => unreachable!(),
1905 }
1906 }
1907 fn pre_flat_map_node_collect_attr(cfg: &StripUnconfigured<'_>, attr: &ast::Attribute) {
1908 cfg.maybe_emit_expr_attr_err(attr);
1909 }
1910}
1911
1912struct MethodReceiverTag;
1915
1916impl InvocationCollectorNode for AstNodeWrapper<ast::Expr, MethodReceiverTag> {
1917 type OutputTy = AstNodeWrapper<Box<ast::Expr>, MethodReceiverTag>;
1918 const KIND: AstFragmentKind = AstFragmentKind::MethodReceiverExpr;
1919 fn descr() -> &'static str {
1920 "an expression"
1921 }
1922 fn to_annotatable(self) -> Annotatable {
1923 Annotatable::Expr(Box::new(self.wrapped))
1924 }
1925 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1926 AstNodeWrapper::new(fragment.make_method_receiver_expr(), MethodReceiverTag)
1927 }
1928 fn walk(&mut self, collector: &mut InvocationCollector<'_, '_>) {
1929 walk_expr(collector, &mut self.wrapped)
1930 }
1931 fn is_mac_call(&self) -> bool {
1932 matches!(self.wrapped.kind, ast::ExprKind::MacCall(..))
1933 }
1934 fn take_mac_call(self) -> (Box<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1935 let node = self.wrapped;
1936 match node.kind {
1937 ExprKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
1938 _ => unreachable!(),
1939 }
1940 }
1941}
1942
1943fn build_single_delegations<'a, Node: InvocationCollectorNode>(
1944 ecx: &ExtCtxt<'_>,
1945 deleg: &'a ast::DelegationMac,
1946 item: &'a ast::Item<Node::ItemKind>,
1947 suffixes: &'a [(Ident, Option<Ident>)],
1948 item_span: Span,
1949 from_glob: bool,
1950) -> impl Iterator<Item = ast::Item<Node::ItemKind>> + 'a {
1951 if suffixes.is_empty() {
1952 let kind = String::from(if from_glob { "glob" } else { "list" });
1955 ecx.dcx().emit_err(EmptyDelegationMac { span: item.span, kind });
1956 }
1957
1958 suffixes.iter().map(move |&(ident, rename)| {
1959 let mut path = deleg.prefix.clone();
1960 path.segments.push(ast::PathSegment { ident, id: ast::DUMMY_NODE_ID, args: None });
1961
1962 ast::Item {
1963 attrs: item.attrs.clone(),
1964 id: ast::DUMMY_NODE_ID,
1965 span: if from_glob { item_span } else { ident.span },
1966 vis: item.vis.clone(),
1967 kind: Node::delegation_item_kind(Box::new(ast::Delegation {
1968 id: ast::DUMMY_NODE_ID,
1969 qself: deleg.qself.clone(),
1970 path,
1971 ident: rename.unwrap_or(ident),
1972 rename,
1973 body: deleg.body.clone(),
1974 from_glob,
1975 })),
1976 tokens: None,
1977 }
1978 })
1979}
1980
1981trait DummyAstNode {
1983 fn dummy() -> Self;
1984}
1985
1986impl DummyAstNode for ast::Crate {
1987 fn dummy() -> Self {
1988 ast::Crate {
1989 attrs: Default::default(),
1990 items: Default::default(),
1991 spans: Default::default(),
1992 id: DUMMY_NODE_ID,
1993 is_placeholder: Default::default(),
1994 }
1995 }
1996}
1997
1998impl DummyAstNode for ast::Ty {
1999 fn dummy() -> Self {
2000 ast::Ty {
2001 id: DUMMY_NODE_ID,
2002 kind: TyKind::Dummy,
2003 span: Default::default(),
2004 tokens: Default::default(),
2005 }
2006 }
2007}
2008
2009impl DummyAstNode for ast::Pat {
2010 fn dummy() -> Self {
2011 ast::Pat {
2012 id: DUMMY_NODE_ID,
2013 kind: PatKind::Wild,
2014 span: Default::default(),
2015 tokens: Default::default(),
2016 }
2017 }
2018}
2019
2020impl DummyAstNode for ast::Expr {
2021 fn dummy() -> Self {
2022 ast::Expr::dummy()
2023 }
2024}
2025
2026impl DummyAstNode for AstNodeWrapper<ast::Expr, MethodReceiverTag> {
2027 fn dummy() -> Self {
2028 AstNodeWrapper::new(ast::Expr::dummy(), MethodReceiverTag)
2029 }
2030}
2031
2032struct InvocationCollector<'a, 'b> {
2033 cx: &'a mut ExtCtxt<'b>,
2034 invocations: Vec<(Invocation, Option<Arc<SyntaxExtension>>)>,
2035 monotonic: bool,
2036}
2037
2038impl<'a, 'b> InvocationCollector<'a, 'b> {
2039 fn cfg(&self) -> StripUnconfigured<'_> {
2040 StripUnconfigured {
2041 sess: self.cx.sess,
2042 features: Some(self.cx.ecfg.features),
2043 config_tokens: false,
2044 lint_node_id: self.cx.current_expansion.lint_node_id,
2045 }
2046 }
2047
2048 fn collect(&mut self, fragment_kind: AstFragmentKind, kind: InvocationKind) -> AstFragment {
2049 let expn_id = LocalExpnId::fresh_empty();
2050 if matches!(kind, InvocationKind::GlobDelegation { .. }) {
2051 self.cx.resolver.register_glob_delegation(expn_id);
2054 }
2055 let vis = kind.placeholder_visibility();
2056 self.invocations.push((
2057 Invocation {
2058 kind,
2059 fragment_kind,
2060 expansion_data: ExpansionData {
2061 id: expn_id,
2062 depth: self.cx.current_expansion.depth + 1,
2063 ..self.cx.current_expansion.clone()
2064 },
2065 },
2066 None,
2067 ));
2068 placeholder(fragment_kind, NodeId::placeholder_from_expn_id(expn_id), vis)
2069 }
2070
2071 fn collect_bang(&mut self, mac: Box<ast::MacCall>, kind: AstFragmentKind) -> AstFragment {
2072 let span = mac.span();
2075 self.collect(kind, InvocationKind::Bang { mac, span })
2076 }
2077
2078 fn collect_attr(
2079 &mut self,
2080 (attr, pos, derives): (ast::Attribute, usize, Vec<ast::Path>),
2081 item: Annotatable,
2082 kind: AstFragmentKind,
2083 ) -> AstFragment {
2084 self.collect(kind, InvocationKind::Attr { attr, pos, item, derives })
2085 }
2086
2087 fn collect_glob_delegation(
2088 &mut self,
2089 item: Box<ast::AssocItem>,
2090 of_trait: bool,
2091 kind: AstFragmentKind,
2092 ) -> AstFragment {
2093 self.collect(kind, InvocationKind::GlobDelegation { item, of_trait })
2094 }
2095
2096 fn take_first_attr(
2100 &self,
2101 item: &mut impl HasAttrs,
2102 ) -> Option<(ast::Attribute, usize, Vec<ast::Path>)> {
2103 let mut attr = None;
2104
2105 let mut cfg_pos = None;
2106 let mut attr_pos = None;
2107 for (pos, attr) in item.attrs().iter().enumerate() {
2108 if !attr.is_doc_comment() && !self.cx.expanded_inert_attrs.is_marked(attr) {
2109 let name = attr.ident().map(|ident| ident.name);
2110 if name == Some(sym::cfg) || name == Some(sym::cfg_attr) {
2111 cfg_pos = Some(pos); break;
2113 } else if attr_pos.is_none()
2114 && !name.is_some_and(rustc_feature::is_builtin_attr_name)
2115 {
2116 attr_pos = Some(pos); }
2118 }
2119 }
2120
2121 item.visit_attrs(|attrs| {
2122 attr = Some(match (cfg_pos, attr_pos) {
2123 (Some(pos), _) => (attrs.remove(pos), pos, Vec::new()),
2124 (_, Some(pos)) => {
2125 let attr = attrs.remove(pos);
2126 let following_derives = attrs[pos..]
2127 .iter()
2128 .filter(|a| a.has_name(sym::derive))
2129 .flat_map(|a| a.meta_item_list().unwrap_or_default())
2130 .filter_map(|meta_item_inner| match meta_item_inner {
2131 MetaItemInner::MetaItem(ast::MetaItem {
2132 kind: MetaItemKind::Word,
2133 path,
2134 ..
2135 }) => Some(path),
2136 _ => None,
2137 })
2138 .collect();
2139
2140 (attr, pos, following_derives)
2141 }
2142 _ => return,
2143 });
2144 });
2145
2146 attr
2147 }
2148
2149 fn check_attributes(&self, attrs: &[ast::Attribute], call: &ast::MacCall) {
2152 let features = self.cx.ecfg.features;
2153 let mut attrs = attrs.iter().peekable();
2154 let mut span: Option<Span> = None;
2155 while let Some(attr) = attrs.next() {
2156 rustc_ast_passes::feature_gate::check_attribute(attr, self.cx.sess, features);
2157 validate_attr::check_attr(
2158 &self.cx.sess.psess,
2159 attr,
2160 self.cx.current_expansion.lint_node_id,
2161 );
2162 AttributeParser::parse_limited_all(
2163 self.cx.sess,
2164 slice::from_ref(attr),
2165 None,
2166 Target::MacroCall,
2167 call.span(),
2168 self.cx.current_expansion.lint_node_id,
2169 Some(self.cx.ecfg.features),
2170 ShouldEmit::ErrorsAndLints,
2171 );
2172
2173 let current_span = if let Some(sp) = span { sp.to(attr.span) } else { attr.span };
2174 span = Some(current_span);
2175
2176 if attrs.peek().is_some_and(|next_attr| next_attr.doc_str().is_some()) {
2177 continue;
2178 }
2179
2180 if attr.is_doc_comment() {
2181 self.cx.sess.psess.buffer_lint(
2182 UNUSED_DOC_COMMENTS,
2183 current_span,
2184 self.cx.current_expansion.lint_node_id,
2185 BuiltinLintDiag::UnusedDocComment(attr.span),
2186 );
2187 } else if rustc_attr_parsing::is_builtin_attr(attr)
2188 && !AttributeParser::<Early>::is_parsed_attribute(&attr.path())
2189 {
2190 let attr_name = attr.ident().unwrap().name;
2191 if attr_name != sym::cfg_trace && attr_name != sym::cfg_attr_trace {
2194 self.cx.sess.psess.buffer_lint(
2195 UNUSED_ATTRIBUTES,
2196 attr.span,
2197 self.cx.current_expansion.lint_node_id,
2198 BuiltinLintDiag::UnusedBuiltinAttribute {
2199 attr_name,
2200 macro_name: pprust::path_to_string(&call.path),
2201 invoc_span: call.path.span,
2202 attr_span: attr.span,
2203 },
2204 );
2205 }
2206 }
2207 }
2208 }
2209
2210 fn expand_cfg_true(
2211 &mut self,
2212 node: &mut (impl HasAttrs + HasNodeId),
2213 attr: ast::Attribute,
2214 pos: usize,
2215 ) -> EvalConfigResult {
2216 let res = self.cfg().cfg_true(&attr, node.node_id(), ShouldEmit::ErrorsAndLints);
2217 if res.as_bool() {
2218 let trace_attr = attr_into_trace(attr, sym::cfg_trace);
2221 node.visit_attrs(|attrs| attrs.insert(pos, trace_attr));
2222 }
2223
2224 res
2225 }
2226
2227 fn expand_cfg_attr(&self, node: &mut impl HasAttrs, attr: &ast::Attribute, pos: usize) {
2228 node.visit_attrs(|attrs| {
2229 for cfg in self.cfg().expand_cfg_attr(attr, false).into_iter().rev() {
2232 attrs.insert(pos, cfg)
2233 }
2234 });
2235 }
2236
2237 fn flat_map_node<Node: InvocationCollectorNode<OutputTy: Default>>(
2238 &mut self,
2239 mut node: Node,
2240 ) -> Node::OutputTy {
2241 loop {
2242 return match self.take_first_attr(&mut node) {
2243 Some((attr, pos, derives)) => match attr.name() {
2244 Some(sym::cfg) => {
2245 let res = self.expand_cfg_true(&mut node, attr, pos);
2246 match res {
2247 EvalConfigResult::True => continue,
2248 EvalConfigResult::False { reason, reason_span } => {
2249 for ident in node.declared_idents() {
2250 self.cx.resolver.append_stripped_cfg_item(
2251 self.cx.current_expansion.lint_node_id,
2252 ident,
2253 reason.clone(),
2254 reason_span,
2255 )
2256 }
2257 }
2258 }
2259
2260 Default::default()
2261 }
2262 Some(sym::cfg_attr) => {
2263 self.expand_cfg_attr(&mut node, &attr, pos);
2264 continue;
2265 }
2266 _ => {
2267 Node::pre_flat_map_node_collect_attr(&self.cfg(), &attr);
2268 self.collect_attr((attr, pos, derives), node.to_annotatable(), Node::KIND)
2269 .make_ast::<Node>()
2270 }
2271 },
2272 None if node.is_mac_call() => {
2273 let (mac, attrs, add_semicolon) = node.take_mac_call();
2274 self.check_attributes(&attrs, &mac);
2275 let mut res = self.collect_bang(mac, Node::KIND).make_ast::<Node>();
2276 Node::post_flat_map_node_collect_bang(&mut res, add_semicolon);
2277 res
2278 }
2279 None if let Some((deleg, item)) = node.delegation() => {
2280 let Some(suffixes) = &deleg.suffixes else {
2281 let traitless_qself =
2282 matches!(&deleg.qself, Some(qself) if qself.position == 0);
2283 let (item, of_trait) = match node.to_annotatable() {
2284 Annotatable::AssocItem(item, AssocCtxt::Impl { of_trait }) => {
2285 (item, of_trait)
2286 }
2287 ann @ (Annotatable::Item(_)
2288 | Annotatable::AssocItem(..)
2289 | Annotatable::Stmt(_)) => {
2290 let span = ann.span();
2291 self.cx.dcx().emit_err(GlobDelegationOutsideImpls { span });
2292 return Default::default();
2293 }
2294 _ => unreachable!(),
2295 };
2296 if traitless_qself {
2297 let span = item.span;
2298 self.cx.dcx().emit_err(GlobDelegationTraitlessQpath { span });
2299 return Default::default();
2300 }
2301 return self
2302 .collect_glob_delegation(item, of_trait, Node::KIND)
2303 .make_ast::<Node>();
2304 };
2305
2306 let single_delegations = build_single_delegations::<Node>(
2307 self.cx, deleg, item, suffixes, item.span, false,
2308 );
2309 Node::flatten_outputs(single_delegations.map(|item| {
2310 let mut item = Node::from_item(item);
2311 assign_id!(self, item.node_id_mut(), || item.walk_flat_map(self))
2312 }))
2313 }
2314 None => {
2315 match Node::wrap_flat_map_node_walk_flat_map(node, self, |mut node, this| {
2316 assign_id!(this, node.node_id_mut(), || node.walk_flat_map(this))
2317 }) {
2318 Ok(output) => output,
2319 Err(returned_node) => {
2320 node = returned_node;
2321 continue;
2322 }
2323 }
2324 }
2325 };
2326 }
2327 }
2328
2329 fn visit_node<Node: InvocationCollectorNode<OutputTy: Into<Node>> + DummyAstNode>(
2330 &mut self,
2331 node: &mut Node,
2332 ) {
2333 loop {
2334 return match self.take_first_attr(node) {
2335 Some((attr, pos, derives)) => match attr.name() {
2336 Some(sym::cfg) => {
2337 let span = attr.span;
2338 if self.expand_cfg_true(node, attr, pos).as_bool() {
2339 continue;
2340 }
2341
2342 node.expand_cfg_false(self, pos, span);
2343 continue;
2344 }
2345 Some(sym::cfg_attr) => {
2346 self.expand_cfg_attr(node, &attr, pos);
2347 continue;
2348 }
2349 _ => {
2350 let n = mem::replace(node, Node::dummy());
2351 *node = self
2352 .collect_attr((attr, pos, derives), n.to_annotatable(), Node::KIND)
2353 .make_ast::<Node>()
2354 .into()
2355 }
2356 },
2357 None if node.is_mac_call() => {
2358 let n = mem::replace(node, Node::dummy());
2359 let (mac, attrs, _) = n.take_mac_call();
2360 self.check_attributes(&attrs, &mac);
2361
2362 *node = self.collect_bang(mac, Node::KIND).make_ast::<Node>().into()
2363 }
2364 None if node.delegation().is_some() => unreachable!(),
2365 None => {
2366 assign_id!(self, node.node_id_mut(), || node.walk(self))
2367 }
2368 };
2369 }
2370 }
2371}
2372
2373impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
2374 fn flat_map_item(&mut self, node: Box<ast::Item>) -> SmallVec<[Box<ast::Item>; 1]> {
2375 self.flat_map_node(node)
2376 }
2377
2378 fn flat_map_assoc_item(
2379 &mut self,
2380 node: Box<ast::AssocItem>,
2381 ctxt: AssocCtxt,
2382 ) -> SmallVec<[Box<ast::AssocItem>; 1]> {
2383 match ctxt {
2384 AssocCtxt::Trait => self.flat_map_node(AstNodeWrapper::new(node, TraitItemTag)),
2385 AssocCtxt::Impl { of_trait: false } => {
2386 self.flat_map_node(AstNodeWrapper::new(node, ImplItemTag))
2387 }
2388 AssocCtxt::Impl { of_trait: true } => {
2389 self.flat_map_node(AstNodeWrapper::new(node, TraitImplItemTag))
2390 }
2391 }
2392 }
2393
2394 fn flat_map_foreign_item(
2395 &mut self,
2396 node: Box<ast::ForeignItem>,
2397 ) -> SmallVec<[Box<ast::ForeignItem>; 1]> {
2398 self.flat_map_node(node)
2399 }
2400
2401 fn flat_map_variant(&mut self, node: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
2402 self.flat_map_node(node)
2403 }
2404
2405 fn flat_map_where_predicate(
2406 &mut self,
2407 node: ast::WherePredicate,
2408 ) -> SmallVec<[ast::WherePredicate; 1]> {
2409 self.flat_map_node(node)
2410 }
2411
2412 fn flat_map_field_def(&mut self, node: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
2413 self.flat_map_node(node)
2414 }
2415
2416 fn flat_map_pat_field(&mut self, node: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
2417 self.flat_map_node(node)
2418 }
2419
2420 fn flat_map_expr_field(&mut self, node: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
2421 self.flat_map_node(node)
2422 }
2423
2424 fn flat_map_param(&mut self, node: ast::Param) -> SmallVec<[ast::Param; 1]> {
2425 self.flat_map_node(node)
2426 }
2427
2428 fn flat_map_generic_param(
2429 &mut self,
2430 node: ast::GenericParam,
2431 ) -> SmallVec<[ast::GenericParam; 1]> {
2432 self.flat_map_node(node)
2433 }
2434
2435 fn flat_map_arm(&mut self, node: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
2436 self.flat_map_node(node)
2437 }
2438
2439 fn flat_map_stmt(&mut self, node: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
2440 if node.is_expr() {
2443 return match &node.kind {
2451 StmtKind::Expr(expr)
2452 if matches!(**expr, ast::Expr { kind: ExprKind::MacCall(..), .. }) =>
2453 {
2454 self.cx.current_expansion.is_trailing_mac = true;
2455 let res = walk_flat_map_stmt(self, node);
2458 self.cx.current_expansion.is_trailing_mac = false;
2459 res
2460 }
2461 _ => walk_flat_map_stmt(self, node),
2462 };
2463 }
2464
2465 self.flat_map_node(node)
2466 }
2467
2468 fn visit_crate(&mut self, node: &mut ast::Crate) {
2469 self.visit_node(node)
2470 }
2471
2472 fn visit_ty(&mut self, node: &mut ast::Ty) {
2473 self.visit_node(node)
2474 }
2475
2476 fn visit_pat(&mut self, node: &mut ast::Pat) {
2477 self.visit_node(node)
2478 }
2479
2480 fn visit_expr(&mut self, node: &mut ast::Expr) {
2481 if let Some(attr) = node.attrs.first() {
2483 self.cfg().maybe_emit_expr_attr_err(attr);
2484 }
2485 ensure_sufficient_stack(|| self.visit_node(node))
2486 }
2487
2488 fn visit_method_receiver_expr(&mut self, node: &mut ast::Expr) {
2489 self.visit_node(AstNodeWrapper::from_mut(node, MethodReceiverTag))
2490 }
2491
2492 fn filter_map_expr(&mut self, node: Box<ast::Expr>) -> Option<Box<ast::Expr>> {
2493 self.flat_map_node(AstNodeWrapper::new(node, OptExprTag))
2494 }
2495
2496 fn visit_block(&mut self, node: &mut ast::Block) {
2497 let orig_dir_ownership = mem::replace(
2498 &mut self.cx.current_expansion.dir_ownership,
2499 DirOwnership::UnownedViaBlock,
2500 );
2501 walk_block(self, node);
2502 self.cx.current_expansion.dir_ownership = orig_dir_ownership;
2503 }
2504
2505 fn visit_id(&mut self, id: &mut NodeId) {
2506 if self.monotonic && *id == ast::DUMMY_NODE_ID {
2509 *id = self.cx.resolver.next_node_id();
2510 }
2511 }
2512}
2513
2514pub struct ExpansionConfig<'feat> {
2515 pub crate_name: Symbol,
2516 pub features: &'feat Features,
2517 pub recursion_limit: Limit,
2518 pub trace_mac: bool,
2519 pub should_test: bool,
2521 pub span_debug: bool,
2523 pub proc_macro_backtrace: bool,
2525}
2526
2527impl ExpansionConfig<'_> {
2528 pub fn default(crate_name: Symbol, features: &Features) -> ExpansionConfig<'_> {
2529 ExpansionConfig {
2530 crate_name,
2531 features,
2532 recursion_limit: Limit::new(1024),
2533 trace_mac: false,
2534 should_test: false,
2535 span_debug: false,
2536 proc_macro_backtrace: false,
2537 }
2538 }
2539}