2 use crate::config::StripUnconfigured;
4 IncompleteParse, RecursionLimitReached, RemoveExprNotSupported, RemoveNodeNotSupported,
5 UnsupportedKeyValue, WrongFragmentKind,
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};
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::map_in_place::MapInPlace;
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,
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};
38 use smallvec::SmallVec;
40 use std::path::PathBuf;
44 macro_rules! ast_fragments {
46 $($Kind:ident($AstTy:ty) {
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)*);)?
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>),
61 /// "Discriminant" of an AST fragment.
62 #[derive(Copy, Clone, PartialEq, Eq)]
63 pub enum AstFragmentKind {
69 impl AstFragmentKind {
70 pub fn name(self) -> &'static str {
72 AstFragmentKind::OptExpr => "expression",
73 AstFragmentKind::MethodReceiverExpr => "expression",
74 $(AstFragmentKind::$Kind => $kind_name,)*
78 fn make_from<'a>(self, result: Box<dyn MacResult + 'a>) -> Option<AstFragment> {
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),)*
90 pub fn add_placeholders(&mut self, placeholders: &[NodeId]) {
91 if placeholders.is_empty() {
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()
99 _ => panic!("unexpected AST fragment kind")
103 pub fn make_opt_expr(self) -> Option<P<ast::Expr>> {
105 AstFragment::OptExpr(expr) => expr,
106 _ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
110 pub fn make_method_receiver_expr(self) -> P<ast::Expr> {
112 AstFragment::MethodReceiverExpr(expr) => expr,
113 _ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
117 $(pub fn $make_ast(self) -> $AstTy {
119 AstFragment::$Kind(ast) => ast,
120 _ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
124 fn make_ast<T: InvocationCollectorNode>(self) -> T::OutputTy {
125 T::fragment_to_output(self)
128 pub fn mut_visit_with<F: MutVisitor>(&mut self, vis: &mut F) {
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)
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)),)?)*
146 pub fn visit_with<'a, V: Visitor<'a>>(&'a self, visitor: &mut V) {
148 AstFragment::OptExpr(Some(ref expr)) => visitor.visit_expr(expr),
149 AstFragment::OptExpr(None) => {}
150 AstFragment::MethodReceiverExpr(ref expr) => visitor.visit_method_receiver_expr(expr),
151 $($(AstFragment::$Kind(ref ast) => visitor.$visit_ast(ast),)?)*
152 $($(AstFragment::$Kind(ref ast) => for ast_elt in &ast[..] {
153 visitor.$visit_ast_elt(ast_elt, $($args)*);
159 impl<'a> MacResult for crate::mbe::macro_rules::ParserAnyMacro<'a> {
160 $(fn $make_ast(self: Box<crate::mbe::macro_rules::ParserAnyMacro<'a>>)
162 Some(self.make(AstFragmentKind::$Kind).$make_ast())
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;
175 Items(SmallVec<[P<ast::Item>; 1]>) {
176 "item"; many fn flat_map_item; fn visit_item(); fn make_items;
178 TraitItems(SmallVec<[P<ast::AssocItem>; 1]>) {
180 many fn flat_map_trait_item;
181 fn visit_assoc_item(AssocCtxt::Trait);
184 ImplItems(SmallVec<[P<ast::AssocItem>; 1]>) {
186 many fn flat_map_impl_item;
187 fn visit_assoc_item(AssocCtxt::Impl);
190 ForeignItems(SmallVec<[P<ast::ForeignItem>; 1]>) {
192 many fn flat_map_foreign_item;
193 fn visit_foreign_item();
194 fn make_foreign_items;
196 Arms(SmallVec<[ast::Arm; 1]>) {
197 "match arm"; many fn flat_map_arm; fn visit_arm(); fn make_arms;
199 ExprFields(SmallVec<[ast::ExprField; 1]>) {
200 "field expression"; many fn flat_map_expr_field; fn visit_expr_field(); fn make_expr_fields;
202 PatFields(SmallVec<[ast::PatField; 1]>) {
204 many fn flat_map_pat_field;
205 fn visit_pat_field();
208 GenericParams(SmallVec<[ast::GenericParam; 1]>) {
210 many fn flat_map_generic_param;
211 fn visit_generic_param();
212 fn make_generic_params;
214 Params(SmallVec<[ast::Param; 1]>) {
215 "function parameter"; many fn flat_map_param; fn visit_param(); fn make_params;
217 FieldDefs(SmallVec<[ast::FieldDef; 1]>) {
219 many fn flat_map_field_def;
220 fn visit_field_def();
223 Variants(SmallVec<[ast::Variant; 1]>) {
224 "variant"; many fn flat_map_variant; fn visit_variant(); fn make_variants;
226 Crate(ast::Crate) { "crate"; one fn visit_crate; fn visit_crate; fn make_crate; }
229 pub enum SupportsMacroExpansion {
231 Yes { supports_inner_attrs: bool },
234 impl AstFragmentKind {
235 pub(crate) fn dummy(self, span: Span) -> AstFragment {
236 self.make_from(DummyResult::any(span)).expect("couldn't create a dummy AST fragment")
239 pub fn supports_macro_expansion(self) -> SupportsMacroExpansion {
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,
262 fn expect_from_annotatables<I: IntoIterator<Item = Annotatable>>(
266 let mut items = items.into_iter();
268 AstFragmentKind::Arms => {
269 AstFragment::Arms(items.map(Annotatable::expect_arm).collect())
271 AstFragmentKind::ExprFields => {
272 AstFragment::ExprFields(items.map(Annotatable::expect_expr_field).collect())
274 AstFragmentKind::PatFields => {
275 AstFragment::PatFields(items.map(Annotatable::expect_pat_field).collect())
277 AstFragmentKind::GenericParams => {
278 AstFragment::GenericParams(items.map(Annotatable::expect_generic_param).collect())
280 AstFragmentKind::Params => {
281 AstFragment::Params(items.map(Annotatable::expect_param).collect())
283 AstFragmentKind::FieldDefs => {
284 AstFragment::FieldDefs(items.map(Annotatable::expect_field_def).collect())
286 AstFragmentKind::Variants => {
287 AstFragment::Variants(items.map(Annotatable::expect_variant).collect())
289 AstFragmentKind::Items => {
290 AstFragment::Items(items.map(Annotatable::expect_item).collect())
292 AstFragmentKind::ImplItems => {
293 AstFragment::ImplItems(items.map(Annotatable::expect_impl_item).collect())
295 AstFragmentKind::TraitItems => {
296 AstFragment::TraitItems(items.map(Annotatable::expect_trait_item).collect())
298 AstFragmentKind::ForeignItems => {
299 AstFragment::ForeignItems(items.map(Annotatable::expect_foreign_item).collect())
301 AstFragmentKind::Stmts => {
302 AstFragment::Stmts(items.map(Annotatable::expect_stmt).collect())
304 AstFragmentKind::Expr => AstFragment::Expr(
305 items.next().expect("expected exactly one expression").expect_expr(),
307 AstFragmentKind::MethodReceiverExpr => AstFragment::MethodReceiverExpr(
308 items.next().expect("expected exactly one expression").expect_expr(),
310 AstFragmentKind::OptExpr => {
311 AstFragment::OptExpr(items.next().map(Annotatable::expect_expr))
313 AstFragmentKind::Crate => {
314 AstFragment::Crate(items.next().expect("expected exactly one crate").expect_crate())
316 AstFragmentKind::Pat | AstFragmentKind::Ty => {
317 panic!("patterns and types aren't annotatable")
323 pub struct Invocation {
324 pub kind: InvocationKind,
325 pub fragment_kind: AstFragmentKind,
326 pub expansion_data: ExpansionData,
329 pub enum InvocationKind {
331 mac: P<ast::MacCall>,
335 attr: ast::Attribute,
336 // Re-insertion position for inert attributes.
339 // Required for resolving derive helper attributes.
340 derives: Vec<ast::Path>,
349 impl InvocationKind {
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.
357 InvocationKind::Attr { item: Annotatable::FieldDef(field), .. }
358 | InvocationKind::Derive { item: Annotatable::FieldDef(field), .. }
359 if field.ident.is_none() =>
361 Some(field.vis.clone())
369 pub fn span(&self) -> Span {
371 InvocationKind::Bang { span, .. } => *span,
372 InvocationKind::Attr { attr, .. } => attr.span,
373 InvocationKind::Derive { path, .. } => path.span,
378 pub struct MacroExpander<'a, 'b> {
379 pub cx: &'a mut ExtCtxt<'b>,
380 monotonic: bool, // cf. `cx.monotonic_expander()`
383 impl<'a, 'b> MacroExpander<'a, 'b> {
384 pub fn new(cx: &'a mut ExtCtxt<'b>, monotonic: bool) -> Self {
385 MacroExpander { cx, monotonic }
388 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
392 .expect("attempting to resolve a file path in an external file"),
393 other => PathBuf::from(other.prefer_local().to_string()),
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],
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();
408 /// Recursively expand all macro invocations in this AST fragment.
409 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;
413 // Collect all macro invocations and replace them with placeholders.
414 let (mut fragment_with_placeholders, mut invocations) =
415 self.collect_invocations(input_fragment, &[]);
417 // Optimization: if we resolve all imports now,
418 // we'll be able to immediately resolve most of imported macros.
419 self.resolve_imports();
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);
430 let Some((invoc, ext)) = invocations.pop() else {
431 self.resolve_imports();
432 if undetermined_invocations.is_empty() {
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",
446 let ext = match ext {
449 let eager_expansion_root = if self.monotonic {
450 invoc.expansion_data.id
452 orig_expansion_data.id
454 match self.cx.resolver.resolve_macro_invocation(
456 eager_expansion_root,
460 Err(Indeterminate) => {
461 // Cannot resolve, will retry this invocation later.
462 undetermined_invocations.push((invoc, None));
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;
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
481 .take_derive_resolutions(expn_id)
483 derive_invocations.reserve(derives.len());
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((
492 kind: InvocationKind::Derive { path, item, is_const },
494 expansion_data: ExpansionData {
496 ..self.cx.current_expansion.clone()
501 NodeId::placeholder_from_expn_id(expn_id)
505 .unwrap_or_default();
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)
514 ExpandResult::Retry(invoc) => {
518 "expansion entered force mode but is still stuck",
521 // Cannot expand, will retry this invocation later.
522 undetermined_invocations.push((invoc, Some(ext)));
529 if expanded_fragments.len() < depth {
530 expanded_fragments.push(Vec::new());
532 expanded_fragments[depth - 1].push((expn_id, expanded_fragment));
533 invocations.extend(new_invocations.into_iter().rev());
536 self.cx.current_expansion = orig_expansion_data;
537 self.cx.force_mode = orig_force_mode;
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() {
544 .add(NodeId::placeholder_from_expn_id(expn_id), expanded_fragment);
547 fragment_with_placeholders.mut_visit_with(&mut placeholder_expander);
548 fragment_with_placeholders
551 fn resolve_imports(&mut self) {
553 self.cx.resolver.resolve_imports();
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.
561 fn collect_invocations(
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();
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`
577 invocations: Vec::new(),
578 monotonic: self.monotonic,
580 fragment.mut_visit_with(&mut collector);
581 fragment.add_placeholders(extra_placeholders);
582 collector.invocations
588 .visit_ast_fragment_with_placeholders(self.cx.current_expansion.id, &fragment);
590 if self.cx.sess.opts.unstable_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 { ref mut span, .. } => span,
596 InvocationKind::Attr { attr, .. } => &mut attr.span,
597 InvocationKind::Derive { path, .. } => &mut path.span,
599 *span = span.with_parent(Some(parent_def));
604 (fragment, invocations)
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),
614 self.cx.emit_err(RecursionLimitReached {
615 span: expn_data.call_site,
616 descr: expn_data.kind.descr(),
618 crate_name: &self.cx.ecfg.crate_name,
621 self.cx.trace_macros_diag();
624 /// A macro's expansion does not fit in this fragment kind.
625 /// For example, a non-type macro in a type position.
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 });
629 self.cx.trace_macros_diag();
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();
644 // Reduce the recursion limit by half each time it triggers.
645 self.cx.reduced_recursion_limit = Some(recursion_limit / 2);
647 return ExpandResult::Ready(invoc.fragment_kind.dummy(invoc.span()));
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));
657 self.parse_ast_fragment(tok_result, fragment_kind, &mac.path, span)
659 SyntaxExtensionKind::LegacyBang(expander) => {
660 let prev = self.cx.current_expansion.prior_type_ascription;
661 self.cx.current_expansion.prior_type_ascription = mac.prior_type_ascription;
662 let tok_result = expander.expand(self.cx, span, mac.args.tokens.clone());
663 let result = if let Some(result) = fragment_kind.make_from(tok_result) {
666 self.error_wrong_fragment_kind(fragment_kind, &mac, span);
667 fragment_kind.dummy(span)
669 self.cx.current_expansion.prior_type_ascription = prev;
674 InvocationKind::Attr { attr, pos, mut item, derives } => match ext {
675 SyntaxExtensionKind::Attr(expander) => {
676 self.gate_proc_macro_input(&item);
677 self.gate_proc_macro_attr_item(span, &item);
678 let tokens = match &item {
679 // FIXME: Collect tokens and use them instead of generating
680 // fake ones. These are unstable, so it needs to be
681 // fixed prior to stabilization
682 // Fake tokens when we are invoking an inner attribute, and
683 // we are invoking it on an out-of-line module or crate.
684 Annotatable::Crate(krate) => rustc_parse::fake_token_stream_for_crate(
685 &self.cx.sess.parse_sess,
688 Annotatable::Item(item_inner)
689 if matches!(attr.style, AttrStyle::Inner)
694 ModKind::Unloaded | ModKind::Loaded(_, Inline::No, _),
698 rustc_parse::fake_token_stream_for_item(
699 &self.cx.sess.parse_sess,
703 _ => item.to_tokens(),
705 let attr_item = attr.unwrap_normal_item();
706 if let AttrArgs::Eq(..) = attr_item.args {
707 self.cx.emit_err(UnsupportedKeyValue { span });
709 let inner_tokens = attr_item.args.inner_tokens();
710 let Ok(tok_result) = expander.expand(self.cx, span, inner_tokens, tokens) else {
711 return ExpandResult::Ready(fragment_kind.dummy(span));
713 self.parse_ast_fragment(tok_result, fragment_kind, &attr_item.path, span)
715 SyntaxExtensionKind::LegacyAttr(expander) => {
716 match validate_attr::parse_meta(&self.cx.sess.parse_sess, &attr) {
718 let items = match expander.expand(self.cx, span, &meta, item, false) {
719 ExpandResult::Ready(items) => items,
720 ExpandResult::Retry(item) => {
721 // Reassemble the original invocation for retrying.
722 return ExpandResult::Retry(Invocation {
723 kind: InvocationKind::Attr { attr, pos, item, derives },
728 if fragment_kind == AstFragmentKind::Expr && items.is_empty() {
729 self.cx.emit_err(RemoveExprNotSupported { span });
730 fragment_kind.dummy(span)
732 fragment_kind.expect_from_annotatables(items)
737 fragment_kind.dummy(span)
741 SyntaxExtensionKind::NonMacroAttr => {
742 self.cx.expanded_inert_attrs.mark(&attr);
743 item.visit_attrs(|attrs| attrs.insert(pos, attr));
744 fragment_kind.expect_from_annotatables(iter::once(item))
748 InvocationKind::Derive { path, item, is_const } => match ext {
749 SyntaxExtensionKind::Derive(expander)
750 | SyntaxExtensionKind::LegacyDerive(expander) => {
751 if let SyntaxExtensionKind::Derive(..) = ext {
752 self.gate_proc_macro_input(&item);
754 let meta = ast::MetaItem { kind: MetaItemKind::Word, span, path };
755 let items = match expander.expand(self.cx, span, &meta, item, is_const) {
756 ExpandResult::Ready(items) => items,
757 ExpandResult::Retry(item) => {
758 // Reassemble the original invocation for retrying.
759 return ExpandResult::Retry(Invocation {
760 kind: InvocationKind::Derive { path: meta.path, item, is_const },
765 fragment_kind.expect_from_annotatables(items)
772 fn gate_proc_macro_attr_item(&self, span: Span, item: &Annotatable) {
773 let kind = match item {
775 | Annotatable::TraitItem(_)
776 | Annotatable::ImplItem(_)
777 | Annotatable::ForeignItem(_)
778 | Annotatable::Crate(..) => return,
779 Annotatable::Stmt(stmt) => {
780 // Attributes are stable on item statements,
781 // but unstable on all other kinds of statements
787 Annotatable::Expr(_) => "expressions",
789 | Annotatable::ExprField(..)
790 | Annotatable::PatField(..)
791 | Annotatable::GenericParam(..)
792 | Annotatable::Param(..)
793 | Annotatable::FieldDef(..)
794 | Annotatable::Variant(..) => panic!("unexpected annotatable"),
796 if self.cx.ecfg.proc_macro_hygiene() {
800 &self.cx.sess.parse_sess,
801 sym::proc_macro_hygiene,
803 &format!("custom attributes cannot be applied to {}", kind),
808 fn gate_proc_macro_input(&self, annotatable: &Annotatable) {
809 struct GateProcMacroInput<'a> {
810 parse_sess: &'a ParseSess,
813 impl<'ast, 'a> Visitor<'ast> for GateProcMacroInput<'a> {
814 fn visit_item(&mut self, item: &'ast ast::Item) {
816 ItemKind::Mod(_, mod_kind)
817 if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _)) =>
821 sym::proc_macro_hygiene,
823 "non-inline modules in proc macro input are unstable",
830 visit::walk_item(self, item);
834 if !self.cx.ecfg.proc_macro_hygiene() {
836 .visit_with(&mut GateProcMacroInput { parse_sess: &self.cx.sess.parse_sess });
840 fn parse_ast_fragment(
843 kind: AstFragmentKind,
847 let mut parser = self.cx.new_parser_from_tts(toks);
848 match parse_ast_fragment(&mut parser, kind) {
850 ensure_complete_parse(&mut parser, path, kind.name(), span);
854 if err.span.is_dummy() {
857 annotate_err_with_kind(&mut err, kind, span);
859 self.cx.trace_macros_diag();
866 pub fn parse_ast_fragment<'a>(
867 this: &mut Parser<'a>,
868 kind: AstFragmentKind,
869 ) -> PResult<'a, AstFragment> {
871 AstFragmentKind::Items => {
872 let mut items = SmallVec::new();
873 while let Some(item) = this.parse_item(ForceCollect::No)? {
876 AstFragment::Items(items)
878 AstFragmentKind::TraitItems => {
879 let mut items = SmallVec::new();
880 while let Some(item) = this.parse_trait_item(ForceCollect::No)? {
883 AstFragment::TraitItems(items)
885 AstFragmentKind::ImplItems => {
886 let mut items = SmallVec::new();
887 while let Some(item) = this.parse_impl_item(ForceCollect::No)? {
890 AstFragment::ImplItems(items)
892 AstFragmentKind::ForeignItems => {
893 let mut items = SmallVec::new();
894 while let Some(item) = this.parse_foreign_item(ForceCollect::No)? {
897 AstFragment::ForeignItems(items)
899 AstFragmentKind::Stmts => {
900 let mut stmts = SmallVec::new();
901 // Won't make progress on a `}`.
902 while this.token != token::Eof && this.token != token::CloseDelim(Delimiter::Brace) {
903 if let Some(stmt) = this.parse_full_stmt(AttemptLocalParseRecovery::Yes)? {
907 AstFragment::Stmts(stmts)
909 AstFragmentKind::Expr => AstFragment::Expr(this.parse_expr()?),
910 AstFragmentKind::MethodReceiverExpr => AstFragment::MethodReceiverExpr(this.parse_expr()?),
911 AstFragmentKind::OptExpr => {
912 if this.token != token::Eof {
913 AstFragment::OptExpr(Some(this.parse_expr()?))
915 AstFragment::OptExpr(None)
918 AstFragmentKind::Ty => AstFragment::Ty(this.parse_ty()?),
919 AstFragmentKind::Pat => AstFragment::Pat(this.parse_pat_allow_top_alt(
923 CommaRecoveryMode::LikelyTuple,
925 AstFragmentKind::Crate => AstFragment::Crate(this.parse_crate_mod()?),
926 AstFragmentKind::Arms
927 | AstFragmentKind::ExprFields
928 | AstFragmentKind::PatFields
929 | AstFragmentKind::GenericParams
930 | AstFragmentKind::Params
931 | AstFragmentKind::FieldDefs
932 | AstFragmentKind::Variants => panic!("unexpected AST fragment kind"),
936 pub fn ensure_complete_parse<'a>(
937 parser: &mut Parser<'a>,
938 macro_path: &ast::Path,
942 if parser.token != token::Eof {
943 let token = pprust::token_to_string(&parser.token);
944 // Avoid emitting backtrace info twice.
945 let def_site_span = parser.token.span.with_ctxt(SyntaxContext::root());
947 let semi_span = parser.sess.source_map().next_point(span);
948 let add_semicolon = match parser.sess.source_map().span_to_snippet(semi_span) {
949 Ok(ref snippet) if &snippet[..] != ";" && kind_name == "expression" => {
950 Some(span.shrink_to_hi())
955 parser.sess.emit_err(IncompleteParse {
966 /// Wraps a call to `noop_visit_*` / `noop_flat_map_*`
967 /// for an AST node that supports attributes
968 /// (see the `Annotatable` enum)
969 /// This method assigns a `NodeId`, and sets that `NodeId`
970 /// as our current 'lint node id'. If a macro call is found
971 /// inside this AST node, we will use this AST node's `NodeId`
972 /// to emit lints associated with that macro (allowing
973 /// `#[allow]` / `#[deny]` to be applied close to
974 /// the macro invocation).
976 /// Do *not* call this for a macro AST node
977 /// (e.g. `ExprKind::MacCall`) - we cannot emit lints
978 /// at these AST nodes, since they are removed and
979 /// replaced with the result of macro expansion.
981 /// All other `NodeId`s are assigned by `visit_id`.
982 /// * `self` is the 'self' parameter for the current method,
983 /// * `id` is a mutable reference to the `NodeId` field
984 /// of the current AST node.
985 /// * `closure` is a closure that executes the
986 /// `noop_visit_*` / `noop_flat_map_*` method
987 /// for the current AST node.
988 macro_rules! assign_id {
989 ($self:ident, $id:expr, $closure:expr) => {{
990 let old_id = $self.cx.current_expansion.lint_node_id;
992 debug_assert_eq!(*$id, ast::DUMMY_NODE_ID);
993 let new_id = $self.cx.resolver.next_node_id();
995 $self.cx.current_expansion.lint_node_id = new_id;
997 let ret = ($closure)();
998 $self.cx.current_expansion.lint_node_id = old_id;
1008 /// A trait implemented for all `AstFragment` nodes and providing all pieces
1009 /// of functionality used by `InvocationCollector`.
1010 trait InvocationCollectorNode: HasAttrs + HasNodeId + Sized {
1011 type OutputTy = SmallVec<[Self; 1]>;
1012 type AttrsTy: Deref<Target = [ast::Attribute]> = ast::AttrVec;
1013 const KIND: AstFragmentKind;
1014 fn to_annotatable(self) -> Annotatable;
1015 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy;
1016 fn descr() -> &'static str {
1019 fn noop_flat_map<V: MutVisitor>(self, _visitor: &mut V) -> Self::OutputTy {
1022 fn noop_visit<V: MutVisitor>(&mut self, _visitor: &mut V) {
1025 fn is_mac_call(&self) -> bool {
1028 fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1031 fn pre_flat_map_node_collect_attr(_cfg: &StripUnconfigured<'_>, _attr: &ast::Attribute) {}
1032 fn post_flat_map_node_collect_bang(_output: &mut Self::OutputTy, _add_semicolon: AddSemicolon) {
1034 fn wrap_flat_map_node_noop_flat_map(
1036 collector: &mut InvocationCollector<'_, '_>,
1037 noop_flat_map: impl FnOnce(Self, &mut InvocationCollector<'_, '_>) -> Self::OutputTy,
1038 ) -> Result<Self::OutputTy, Self> {
1039 Ok(noop_flat_map(node, collector))
1043 impl InvocationCollectorNode for P<ast::Item> {
1044 const KIND: AstFragmentKind = AstFragmentKind::Items;
1045 fn to_annotatable(self) -> Annotatable {
1046 Annotatable::Item(self)
1048 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1049 fragment.make_items()
1051 fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1052 noop_flat_map_item(self, visitor)
1054 fn is_mac_call(&self) -> bool {
1055 matches!(self.kind, ItemKind::MacCall(..))
1057 fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1058 let node = self.into_inner();
1060 ItemKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
1061 _ => unreachable!(),
1064 fn wrap_flat_map_node_noop_flat_map(
1066 collector: &mut InvocationCollector<'_, '_>,
1067 noop_flat_map: impl FnOnce(Self, &mut InvocationCollector<'_, '_>) -> Self::OutputTy,
1068 ) -> Result<Self::OutputTy, Self> {
1069 if !matches!(node.kind, ItemKind::Mod(..)) {
1070 return Ok(noop_flat_map(node, collector));
1073 // Work around borrow checker not seeing through `P`'s deref.
1074 let (ident, span, mut attrs) = (node.ident, node.span, mem::take(&mut node.attrs));
1075 let ItemKind::Mod(_, mod_kind) = &mut node.kind else {
1079 let ecx = &mut collector.cx;
1080 let (file_path, dir_path, dir_ownership) = match mod_kind {
1081 ModKind::Loaded(_, inline, _) => {
1082 // Inline `mod foo { ... }`, but we still need to push directories.
1083 let (dir_path, dir_ownership) = mod_dir_path(
1087 &ecx.current_expansion.module,
1088 ecx.current_expansion.dir_ownership,
1092 (None, dir_path, dir_ownership)
1094 ModKind::Unloaded => {
1095 // We have an outline `mod foo;` so we need to parse the file.
1096 let old_attrs_len = attrs.len();
1097 let ParsedExternalMod { items, spans, file_path, dir_path, dir_ownership } =
1102 &ecx.current_expansion.module,
1103 ecx.current_expansion.dir_ownership,
1107 if let Some(lint_store) = ecx.lint_store {
1108 lint_store.pre_expansion_lint(
1110 ecx.resolver.registered_tools(),
1111 ecx.current_expansion.lint_node_id,
1118 *mod_kind = ModKind::Loaded(items, Inline::No, spans);
1120 if node.attrs.len() > old_attrs_len {
1121 // If we loaded an out-of-line module and added some inner attributes,
1122 // then we need to re-configure it and re-collect attributes for
1123 // resolution and expansion.
1126 (Some(file_path), dir_path, dir_ownership)
1130 // Set the module info before we flat map.
1131 let mut module = ecx.current_expansion.module.with_dir_path(dir_path);
1132 module.mod_path.push(ident);
1133 if let Some(file_path) = file_path {
1134 module.file_path_stack.push(file_path);
1137 let orig_module = mem::replace(&mut ecx.current_expansion.module, Rc::new(module));
1138 let orig_dir_ownership =
1139 mem::replace(&mut ecx.current_expansion.dir_ownership, dir_ownership);
1141 let res = Ok(noop_flat_map(node, collector));
1143 collector.cx.current_expansion.dir_ownership = orig_dir_ownership;
1144 collector.cx.current_expansion.module = orig_module;
1149 struct TraitItemTag;
1150 impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, TraitItemTag> {
1151 type OutputTy = SmallVec<[P<ast::AssocItem>; 1]>;
1152 const KIND: AstFragmentKind = AstFragmentKind::TraitItems;
1153 fn to_annotatable(self) -> Annotatable {
1154 Annotatable::TraitItem(self.wrapped)
1156 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1157 fragment.make_trait_items()
1159 fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1160 noop_flat_map_assoc_item(self.wrapped, visitor)
1162 fn is_mac_call(&self) -> bool {
1163 matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
1165 fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1166 let item = self.wrapped.into_inner();
1168 AssocItemKind::MacCall(mac) => (mac, item.attrs, AddSemicolon::No),
1169 _ => unreachable!(),
1175 impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, ImplItemTag> {
1176 type OutputTy = SmallVec<[P<ast::AssocItem>; 1]>;
1177 const KIND: AstFragmentKind = AstFragmentKind::ImplItems;
1178 fn to_annotatable(self) -> Annotatable {
1179 Annotatable::ImplItem(self.wrapped)
1181 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1182 fragment.make_impl_items()
1184 fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1185 noop_flat_map_assoc_item(self.wrapped, visitor)
1187 fn is_mac_call(&self) -> bool {
1188 matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
1190 fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1191 let item = self.wrapped.into_inner();
1193 AssocItemKind::MacCall(mac) => (mac, item.attrs, AddSemicolon::No),
1194 _ => unreachable!(),
1199 impl InvocationCollectorNode for P<ast::ForeignItem> {
1200 const KIND: AstFragmentKind = AstFragmentKind::ForeignItems;
1201 fn to_annotatable(self) -> Annotatable {
1202 Annotatable::ForeignItem(self)
1204 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1205 fragment.make_foreign_items()
1207 fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1208 noop_flat_map_foreign_item(self, visitor)
1210 fn is_mac_call(&self) -> bool {
1211 matches!(self.kind, ForeignItemKind::MacCall(..))
1213 fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1214 let node = self.into_inner();
1216 ForeignItemKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
1217 _ => unreachable!(),
1222 impl InvocationCollectorNode for ast::Variant {
1223 const KIND: AstFragmentKind = AstFragmentKind::Variants;
1224 fn to_annotatable(self) -> Annotatable {
1225 Annotatable::Variant(self)
1227 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1228 fragment.make_variants()
1230 fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1231 noop_flat_map_variant(self, visitor)
1235 impl InvocationCollectorNode for ast::FieldDef {
1236 const KIND: AstFragmentKind = AstFragmentKind::FieldDefs;
1237 fn to_annotatable(self) -> Annotatable {
1238 Annotatable::FieldDef(self)
1240 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1241 fragment.make_field_defs()
1243 fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1244 noop_flat_map_field_def(self, visitor)
1248 impl InvocationCollectorNode for ast::PatField {
1249 const KIND: AstFragmentKind = AstFragmentKind::PatFields;
1250 fn to_annotatable(self) -> Annotatable {
1251 Annotatable::PatField(self)
1253 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1254 fragment.make_pat_fields()
1256 fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1257 noop_flat_map_pat_field(self, visitor)
1261 impl InvocationCollectorNode for ast::ExprField {
1262 const KIND: AstFragmentKind = AstFragmentKind::ExprFields;
1263 fn to_annotatable(self) -> Annotatable {
1264 Annotatable::ExprField(self)
1266 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1267 fragment.make_expr_fields()
1269 fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1270 noop_flat_map_expr_field(self, visitor)
1274 impl InvocationCollectorNode for ast::Param {
1275 const KIND: AstFragmentKind = AstFragmentKind::Params;
1276 fn to_annotatable(self) -> Annotatable {
1277 Annotatable::Param(self)
1279 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1280 fragment.make_params()
1282 fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1283 noop_flat_map_param(self, visitor)
1287 impl InvocationCollectorNode for ast::GenericParam {
1288 const KIND: AstFragmentKind = AstFragmentKind::GenericParams;
1289 fn to_annotatable(self) -> Annotatable {
1290 Annotatable::GenericParam(self)
1292 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1293 fragment.make_generic_params()
1295 fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1296 noop_flat_map_generic_param(self, visitor)
1300 impl InvocationCollectorNode for ast::Arm {
1301 const KIND: AstFragmentKind = AstFragmentKind::Arms;
1302 fn to_annotatable(self) -> Annotatable {
1303 Annotatable::Arm(self)
1305 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1306 fragment.make_arms()
1308 fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1309 noop_flat_map_arm(self, visitor)
1313 impl InvocationCollectorNode for ast::Stmt {
1314 type AttrsTy = ast::AttrVec;
1315 const KIND: AstFragmentKind = AstFragmentKind::Stmts;
1316 fn to_annotatable(self) -> Annotatable {
1317 Annotatable::Stmt(P(self))
1319 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1320 fragment.make_stmts()
1322 fn noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1323 noop_flat_map_stmt(self, visitor)
1325 fn is_mac_call(&self) -> bool {
1327 StmtKind::MacCall(..) => true,
1328 StmtKind::Item(item) => matches!(item.kind, ItemKind::MacCall(..)),
1329 StmtKind::Semi(expr) => matches!(expr.kind, ExprKind::MacCall(..)),
1330 StmtKind::Expr(..) => unreachable!(),
1331 StmtKind::Local(..) | StmtKind::Empty => false,
1334 fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1335 // We pull macro invocations (both attributes and fn-like macro calls) out of their
1336 // `StmtKind`s and treat them as statement macro invocations, not as items or expressions.
1337 let (add_semicolon, mac, attrs) = match self.kind {
1338 StmtKind::MacCall(mac) => {
1339 let ast::MacCallStmt { mac, style, attrs, .. } = mac.into_inner();
1340 (style == MacStmtStyle::Semicolon, mac, attrs)
1342 StmtKind::Item(item) => match item.into_inner() {
1343 ast::Item { kind: ItemKind::MacCall(mac), attrs, .. } => {
1344 (mac.args.need_semicolon(), mac, attrs)
1346 _ => unreachable!(),
1348 StmtKind::Semi(expr) => match expr.into_inner() {
1349 ast::Expr { kind: ExprKind::MacCall(mac), attrs, .. } => {
1350 (mac.args.need_semicolon(), mac, attrs)
1352 _ => unreachable!(),
1354 _ => unreachable!(),
1356 (mac, attrs, if add_semicolon { AddSemicolon::Yes } else { AddSemicolon::No })
1358 fn post_flat_map_node_collect_bang(stmts: &mut Self::OutputTy, add_semicolon: AddSemicolon) {
1359 // If this is a macro invocation with a semicolon, then apply that
1360 // semicolon to the final statement produced by expansion.
1361 if matches!(add_semicolon, AddSemicolon::Yes) {
1362 if let Some(stmt) = stmts.pop() {
1363 stmts.push(stmt.add_trailing_semicolon());
1369 impl InvocationCollectorNode for ast::Crate {
1370 type OutputTy = ast::Crate;
1371 const KIND: AstFragmentKind = AstFragmentKind::Crate;
1372 fn to_annotatable(self) -> Annotatable {
1373 Annotatable::Crate(self)
1375 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1376 fragment.make_crate()
1378 fn noop_visit<V: MutVisitor>(&mut self, visitor: &mut V) {
1379 noop_visit_crate(self, visitor)
1383 impl InvocationCollectorNode for P<ast::Ty> {
1384 type OutputTy = P<ast::Ty>;
1385 const KIND: AstFragmentKind = AstFragmentKind::Ty;
1386 fn to_annotatable(self) -> Annotatable {
1389 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1392 fn noop_visit<V: MutVisitor>(&mut self, visitor: &mut V) {
1393 noop_visit_ty(self, visitor)
1395 fn is_mac_call(&self) -> bool {
1396 matches!(self.kind, ast::TyKind::MacCall(..))
1398 fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1399 let node = self.into_inner();
1401 TyKind::MacCall(mac) => (mac, AttrVec::new(), AddSemicolon::No),
1402 _ => unreachable!(),
1407 impl InvocationCollectorNode for P<ast::Pat> {
1408 type OutputTy = P<ast::Pat>;
1409 const KIND: AstFragmentKind = AstFragmentKind::Pat;
1410 fn to_annotatable(self) -> Annotatable {
1413 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1416 fn noop_visit<V: MutVisitor>(&mut self, visitor: &mut V) {
1417 noop_visit_pat(self, visitor)
1419 fn is_mac_call(&self) -> bool {
1420 matches!(self.kind, PatKind::MacCall(..))
1422 fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1423 let node = self.into_inner();
1425 PatKind::MacCall(mac) => (mac, AttrVec::new(), AddSemicolon::No),
1426 _ => unreachable!(),
1431 impl InvocationCollectorNode for P<ast::Expr> {
1432 type OutputTy = P<ast::Expr>;
1433 type AttrsTy = ast::AttrVec;
1434 const KIND: AstFragmentKind = AstFragmentKind::Expr;
1435 fn to_annotatable(self) -> Annotatable {
1436 Annotatable::Expr(self)
1438 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1439 fragment.make_expr()
1441 fn descr() -> &'static str {
1444 fn noop_visit<V: MutVisitor>(&mut self, visitor: &mut V) {
1445 noop_visit_expr(self, visitor)
1447 fn is_mac_call(&self) -> bool {
1448 matches!(self.kind, ExprKind::MacCall(..))
1450 fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1451 let node = self.into_inner();
1453 ExprKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
1454 _ => unreachable!(),
1460 impl InvocationCollectorNode for AstNodeWrapper<P<ast::Expr>, OptExprTag> {
1461 type OutputTy = Option<P<ast::Expr>>;
1462 type AttrsTy = ast::AttrVec;
1463 const KIND: AstFragmentKind = AstFragmentKind::OptExpr;
1464 fn to_annotatable(self) -> Annotatable {
1465 Annotatable::Expr(self.wrapped)
1467 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1468 fragment.make_opt_expr()
1470 fn noop_flat_map<V: MutVisitor>(mut self, visitor: &mut V) -> Self::OutputTy {
1471 noop_visit_expr(&mut self.wrapped, visitor);
1474 fn is_mac_call(&self) -> bool {
1475 matches!(self.wrapped.kind, ast::ExprKind::MacCall(..))
1477 fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1478 let node = self.wrapped.into_inner();
1480 ExprKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
1481 _ => unreachable!(),
1484 fn pre_flat_map_node_collect_attr(cfg: &StripUnconfigured<'_>, attr: &ast::Attribute) {
1485 cfg.maybe_emit_expr_attr_err(&attr);
1489 /// This struct is a hack to workaround unstable of `stmt_expr_attributes`.
1490 /// It can be removed once that feature is stabilized.
1491 struct MethodReceiverTag;
1492 impl DummyAstNode for MethodReceiverTag {
1493 fn dummy() -> MethodReceiverTag {
1497 impl InvocationCollectorNode for AstNodeWrapper<P<ast::Expr>, MethodReceiverTag> {
1498 type OutputTy = Self;
1499 type AttrsTy = ast::AttrVec;
1500 const KIND: AstFragmentKind = AstFragmentKind::MethodReceiverExpr;
1501 fn descr() -> &'static str {
1504 fn to_annotatable(self) -> Annotatable {
1505 Annotatable::Expr(self.wrapped)
1507 fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
1508 AstNodeWrapper::new(fragment.make_method_receiver_expr(), MethodReceiverTag)
1510 fn noop_visit<V: MutVisitor>(&mut self, visitor: &mut V) {
1511 noop_visit_expr(&mut self.wrapped, visitor)
1513 fn is_mac_call(&self) -> bool {
1514 matches!(self.wrapped.kind, ast::ExprKind::MacCall(..))
1516 fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1517 let node = self.wrapped.into_inner();
1519 ExprKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
1520 _ => unreachable!(),
1525 struct InvocationCollector<'a, 'b> {
1526 cx: &'a mut ExtCtxt<'b>,
1527 invocations: Vec<(Invocation, Option<Lrc<SyntaxExtension>>)>,
1531 impl<'a, 'b> InvocationCollector<'a, 'b> {
1532 fn cfg(&self) -> StripUnconfigured<'_> {
1534 sess: &self.cx.sess,
1535 features: self.cx.ecfg.features,
1536 config_tokens: false,
1537 lint_node_id: self.cx.current_expansion.lint_node_id,
1541 fn collect(&mut self, fragment_kind: AstFragmentKind, kind: InvocationKind) -> AstFragment {
1542 let expn_id = LocalExpnId::fresh_empty();
1543 let vis = kind.placeholder_visibility();
1544 self.invocations.push((
1548 expansion_data: ExpansionData {
1550 depth: self.cx.current_expansion.depth + 1,
1551 ..self.cx.current_expansion.clone()
1556 placeholder(fragment_kind, NodeId::placeholder_from_expn_id(expn_id), vis)
1559 fn collect_bang(&mut self, mac: P<ast::MacCall>, kind: AstFragmentKind) -> AstFragment {
1560 // cache the macro call span so that it can be
1561 // easily adjusted for incremental compilation
1562 let span = mac.span();
1563 self.collect(kind, InvocationKind::Bang { mac, span })
1568 (attr, pos, derives): (ast::Attribute, usize, Vec<ast::Path>),
1570 kind: AstFragmentKind,
1572 self.collect(kind, InvocationKind::Attr { attr, pos, item, derives })
1575 /// If `item` is an attribute invocation, remove the attribute and return it together with
1576 /// its position and derives following it. We have to collect the derives in order to resolve
1577 /// legacy derive helpers (helpers written before derives that introduce them).
1580 item: &mut impl HasAttrs,
1581 ) -> Option<(ast::Attribute, usize, Vec<ast::Path>)> {
1582 let mut attr = None;
1584 let mut cfg_pos = None;
1585 let mut attr_pos = None;
1586 for (pos, attr) in item.attrs().iter().enumerate() {
1587 if !attr.is_doc_comment() && !self.cx.expanded_inert_attrs.is_marked(attr) {
1588 let name = attr.ident().map(|ident| ident.name);
1589 if name == Some(sym::cfg) || name == Some(sym::cfg_attr) {
1590 cfg_pos = Some(pos); // a cfg attr found, no need to search anymore
1592 } else if attr_pos.is_none()
1593 && !name.map_or(false, rustc_feature::is_builtin_attr_name)
1595 attr_pos = Some(pos); // a non-cfg attr found, still may find a cfg attr
1600 item.visit_attrs(|attrs| {
1601 attr = Some(match (cfg_pos, attr_pos) {
1602 (Some(pos), _) => (attrs.remove(pos), pos, Vec::new()),
1604 let attr = attrs.remove(pos);
1605 let following_derives = attrs[pos..]
1607 .filter(|a| a.has_name(sym::derive))
1608 .flat_map(|a| a.meta_item_list().unwrap_or_default())
1609 .filter_map(|nested_meta| match nested_meta {
1610 NestedMetaItem::MetaItem(ast::MetaItem {
1611 kind: MetaItemKind::Word,
1619 (attr, pos, following_derives)
1628 // Detect use of feature-gated or invalid attributes on macro invocations
1629 // since they will not be detected after macro expansion.
1630 fn check_attributes(&self, attrs: &[ast::Attribute], call: &ast::MacCall) {
1631 let features = self.cx.ecfg.features.unwrap();
1632 let mut attrs = attrs.iter().peekable();
1633 let mut span: Option<Span> = None;
1634 while let Some(attr) = attrs.next() {
1635 rustc_ast_passes::feature_gate::check_attribute(attr, self.cx.sess, features);
1636 validate_attr::check_attr(&self.cx.sess.parse_sess, attr);
1638 let current_span = if let Some(sp) = span { sp.to(attr.span) } else { attr.span };
1639 span = Some(current_span);
1641 if attrs.peek().map_or(false, |next_attr| next_attr.doc_str().is_some()) {
1645 if attr.is_doc_comment() {
1646 self.cx.sess.parse_sess.buffer_lint_with_diagnostic(
1647 &UNUSED_DOC_COMMENTS,
1649 self.cx.current_expansion.lint_node_id,
1650 "unused doc comment",
1651 BuiltinLintDiagnostics::UnusedDocComment(attr.span),
1653 } else if rustc_attr::is_builtin_attr(attr) {
1654 let attr_name = attr.ident().unwrap().name;
1655 // `#[cfg]` and `#[cfg_attr]` are special - they are
1656 // eagerly evaluated.
1657 if attr_name != sym::cfg && attr_name != sym::cfg_attr {
1658 self.cx.sess.parse_sess.buffer_lint_with_diagnostic(
1661 self.cx.current_expansion.lint_node_id,
1662 &format!("unused attribute `{}`", attr_name),
1663 BuiltinLintDiagnostics::UnusedBuiltinAttribute {
1665 macro_name: pprust::path_to_string(&call.path),
1666 invoc_span: call.path.span,
1676 node: &mut impl HasAttrs,
1677 attr: ast::Attribute,
1680 let res = self.cfg().cfg_true(&attr);
1682 // FIXME: `cfg(TRUE)` attributes do not currently remove themselves during expansion,
1683 // and some tools like rustdoc and clippy rely on that. Find a way to remove them
1684 // while keeping the tools working.
1685 self.cx.expanded_inert_attrs.mark(&attr);
1686 node.visit_attrs(|attrs| attrs.insert(pos, attr));
1691 fn expand_cfg_attr(&self, node: &mut impl HasAttrs, attr: ast::Attribute, pos: usize) {
1692 node.visit_attrs(|attrs| {
1693 // Repeated `insert` calls is inefficient, but the number of
1694 // insertions is almost always 0 or 1 in practice.
1695 for cfg in self.cfg().expand_cfg_attr(attr, false).into_iter().rev() {
1696 attrs.insert(pos, cfg)
1701 fn flat_map_node<Node: InvocationCollectorNode<OutputTy: Default>>(
1704 ) -> Node::OutputTy {
1706 return match self.take_first_attr(&mut node) {
1707 Some((attr, pos, derives)) => match attr.name_or_empty() {
1709 if self.expand_cfg_true(&mut node, attr, pos) {
1715 self.expand_cfg_attr(&mut node, attr, pos);
1719 Node::pre_flat_map_node_collect_attr(&self.cfg(), &attr);
1720 self.collect_attr((attr, pos, derives), node.to_annotatable(), Node::KIND)
1724 None if node.is_mac_call() => {
1725 let (mac, attrs, add_semicolon) = node.take_mac_call();
1726 self.check_attributes(&attrs, &mac);
1727 let mut res = self.collect_bang(mac, Node::KIND).make_ast::<Node>();
1728 Node::post_flat_map_node_collect_bang(&mut res, add_semicolon);
1732 match Node::wrap_flat_map_node_noop_flat_map(node, self, |mut node, this| {
1733 assign_id!(this, node.node_id_mut(), || node.noop_flat_map(this))
1735 Ok(output) => output,
1736 Err(returned_node) => {
1737 node = returned_node;
1746 fn visit_node<Node: InvocationCollectorNode<OutputTy = Node> + DummyAstNode>(
1751 return match self.take_first_attr(node) {
1752 Some((attr, pos, derives)) => match attr.name_or_empty() {
1754 let span = attr.span;
1755 if self.expand_cfg_true(node, attr, pos) {
1759 self.cx.emit_err(RemoveNodeNotSupported { span, descr: Node::descr() });
1763 self.expand_cfg_attr(node, attr, pos);
1766 _ => visit_clobber(node, |node| {
1767 self.collect_attr((attr, pos, derives), node.to_annotatable(), Node::KIND)
1771 None if node.is_mac_call() => {
1772 visit_clobber(node, |node| {
1773 // Do not clobber unless it's actually a macro (uncommon case).
1774 let (mac, attrs, _) = node.take_mac_call();
1775 self.check_attributes(&attrs, &mac);
1776 self.collect_bang(mac, Node::KIND).make_ast::<Node>()
1780 assign_id!(self, node.node_id_mut(), || node.noop_visit(self))
1787 impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
1788 fn flat_map_item(&mut self, node: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
1789 self.flat_map_node(node)
1792 fn flat_map_trait_item(&mut self, node: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
1793 self.flat_map_node(AstNodeWrapper::new(node, TraitItemTag))
1796 fn flat_map_impl_item(&mut self, node: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
1797 self.flat_map_node(AstNodeWrapper::new(node, ImplItemTag))
1800 fn flat_map_foreign_item(
1802 node: P<ast::ForeignItem>,
1803 ) -> SmallVec<[P<ast::ForeignItem>; 1]> {
1804 self.flat_map_node(node)
1807 fn flat_map_variant(&mut self, node: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
1808 self.flat_map_node(node)
1811 fn flat_map_field_def(&mut self, node: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
1812 self.flat_map_node(node)
1815 fn flat_map_pat_field(&mut self, node: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
1816 self.flat_map_node(node)
1819 fn flat_map_expr_field(&mut self, node: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
1820 self.flat_map_node(node)
1823 fn flat_map_param(&mut self, node: ast::Param) -> SmallVec<[ast::Param; 1]> {
1824 self.flat_map_node(node)
1827 fn flat_map_generic_param(
1829 node: ast::GenericParam,
1830 ) -> SmallVec<[ast::GenericParam; 1]> {
1831 self.flat_map_node(node)
1834 fn flat_map_arm(&mut self, node: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
1835 self.flat_map_node(node)
1838 fn flat_map_stmt(&mut self, node: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
1839 // FIXME: invocations in semicolon-less expressions positions are expanded as expressions,
1840 // changing that requires some compatibility measures.
1842 // The only way that we can end up with a `MacCall` expression statement,
1843 // (as opposed to a `StmtKind::MacCall`) is if we have a macro as the
1844 // trailing expression in a block (e.g. `fn foo() { my_macro!() }`).
1845 // Record this information, so that we can report a more specific
1846 // `SEMICOLON_IN_EXPRESSIONS_FROM_MACROS` lint if needed.
1847 // See #78991 for an investigation of treating macros in this position
1848 // as statements, rather than expressions, during parsing.
1849 return match &node.kind {
1850 StmtKind::Expr(expr)
1851 if matches!(**expr, ast::Expr { kind: ExprKind::MacCall(..), .. }) =>
1853 self.cx.current_expansion.is_trailing_mac = true;
1854 // Don't use `assign_id` for this statement - it may get removed
1855 // entirely due to a `#[cfg]` on the contained expression
1856 let res = noop_flat_map_stmt(node, self);
1857 self.cx.current_expansion.is_trailing_mac = false;
1860 _ => noop_flat_map_stmt(node, self),
1864 self.flat_map_node(node)
1867 fn visit_crate(&mut self, node: &mut ast::Crate) {
1868 self.visit_node(node)
1871 fn visit_ty(&mut self, node: &mut P<ast::Ty>) {
1872 self.visit_node(node)
1875 fn visit_pat(&mut self, node: &mut P<ast::Pat>) {
1876 self.visit_node(node)
1879 fn visit_expr(&mut self, node: &mut P<ast::Expr>) {
1880 // FIXME: Feature gating is performed inconsistently between `Expr` and `OptExpr`.
1881 if let Some(attr) = node.attrs.first() {
1882 self.cfg().maybe_emit_expr_attr_err(attr);
1884 self.visit_node(node)
1887 fn visit_method_receiver_expr(&mut self, node: &mut P<ast::Expr>) {
1888 visit_clobber(node, |node| {
1889 let mut wrapper = AstNodeWrapper::new(node, MethodReceiverTag);
1890 self.visit_node(&mut wrapper);
1895 fn filter_map_expr(&mut self, node: P<ast::Expr>) -> Option<P<ast::Expr>> {
1896 self.flat_map_node(AstNodeWrapper::new(node, OptExprTag))
1899 fn visit_block(&mut self, node: &mut P<ast::Block>) {
1900 let orig_dir_ownership = mem::replace(
1901 &mut self.cx.current_expansion.dir_ownership,
1902 DirOwnership::UnownedViaBlock,
1904 noop_visit_block(node, self);
1905 self.cx.current_expansion.dir_ownership = orig_dir_ownership;
1908 fn visit_id(&mut self, id: &mut NodeId) {
1909 // We may have already assigned a `NodeId`
1910 // by calling `assign_id`
1911 if self.monotonic && *id == ast::DUMMY_NODE_ID {
1912 *id = self.cx.resolver.next_node_id();
1917 pub struct ExpansionConfig<'feat> {
1918 pub crate_name: String,
1919 pub features: Option<&'feat Features>,
1920 pub recursion_limit: Limit,
1921 pub trace_mac: bool,
1922 /// If false, strip `#[test]` nodes
1923 pub should_test: bool,
1924 /// If true, use verbose debugging for `proc_macro::Span`
1925 pub span_debug: bool,
1926 /// If true, show backtraces for proc-macro panics
1927 pub proc_macro_backtrace: bool,
1930 impl<'feat> ExpansionConfig<'feat> {
1931 pub fn default(crate_name: String) -> ExpansionConfig<'static> {
1935 recursion_limit: Limit::new(1024),
1939 proc_macro_backtrace: false,
1943 fn proc_macro_hygiene(&self) -> bool {
1944 self.features.map_or(false, |features| features.proc_macro_hygiene)