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