2 use crate::config::StripUnconfigured;
4 use crate::hygiene::{ExpnData, ExpnKind, SyntaxContext};
5 use crate::mbe::macro_rules::annotate_err_with_kind;
6 use crate::module::{parse_external_mod, push_directory, Directory, DirectoryOwnership};
7 use crate::placeholders::{placeholder, PlaceholderExpander};
8 use crate::proc_macro::collect_derives;
10 use rustc_ast::mut_visit::*;
11 use rustc_ast::ptr::P;
13 use rustc_ast::tokenstream::TokenStream;
14 use rustc_ast::visit::{self, AssocCtxt, Visitor};
15 use rustc_ast::{self as ast, AttrItem, AttrStyle, Block, LitKind, NodeId, PatKind, Path};
16 use rustc_ast::{ItemKind, MacArgs, MacCallStmt, MacStmtStyle, StmtKind, Unsafe};
17 use rustc_ast_pretty::pprust;
18 use rustc_attr::{self as attr, is_builtin_attr, HasAttrs};
19 use rustc_data_structures::map_in_place::MapInPlace;
20 use rustc_data_structures::stack::ensure_sufficient_stack;
21 use rustc_errors::{struct_span_err, Applicability, PResult};
22 use rustc_feature::Features;
23 use rustc_parse::parser::{AttemptLocalParseRecovery, Parser};
24 use rustc_parse::validate_attr;
25 use rustc_session::lint::builtin::UNUSED_DOC_COMMENTS;
26 use rustc_session::lint::BuiltinLintDiagnostics;
27 use rustc_session::parse::{feature_err, ParseSess};
28 use rustc_session::Limit;
29 use rustc_span::symbol::{sym, Ident, Symbol};
30 use rustc_span::{ExpnId, FileName, Span, DUMMY_SP};
32 use smallvec::{smallvec, SmallVec};
33 use std::io::ErrorKind;
34 use std::ops::DerefMut;
35 use std::path::PathBuf;
37 use std::{iter, mem, slice};
39 macro_rules! ast_fragments {
41 $($Kind:ident($AstTy:ty) {
43 $(one fn $mut_visit_ast:ident; fn $visit_ast:ident;)?
44 $(many fn $flat_map_ast_elt:ident; fn $visit_ast_elt:ident($($args:tt)*);)?
48 /// A fragment of AST that can be produced by a single macro expansion.
49 /// Can also serve as an input and intermediate result for macro expansion operations.
50 pub enum AstFragment {
51 OptExpr(Option<P<ast::Expr>>),
55 /// "Discriminant" of an AST fragment.
56 #[derive(Copy, Clone, PartialEq, Eq)]
57 pub enum AstFragmentKind {
62 impl AstFragmentKind {
63 pub fn name(self) -> &'static str {
65 AstFragmentKind::OptExpr => "expression",
66 $(AstFragmentKind::$Kind => $kind_name,)*
70 fn make_from<'a>(self, result: Box<dyn MacResult + 'a>) -> Option<AstFragment> {
72 AstFragmentKind::OptExpr =>
73 result.make_expr().map(Some).map(AstFragment::OptExpr),
74 $(AstFragmentKind::$Kind => result.$make_ast().map(AstFragment::$Kind),)*
80 pub fn add_placeholders(&mut self, placeholders: &[NodeId]) {
81 if placeholders.is_empty() {
85 $($(AstFragment::$Kind(ast) => ast.extend(placeholders.iter().flat_map(|id| {
86 // We are repeating through arguments with `many`, to do that we have to
87 // mention some macro variable from those arguments even if it's not used.
88 macro _repeating($flat_map_ast_elt) {}
89 placeholder(AstFragmentKind::$Kind, *id, None).$make_ast()
91 _ => panic!("unexpected AST fragment kind")
95 pub fn make_opt_expr(self) -> Option<P<ast::Expr>> {
97 AstFragment::OptExpr(expr) => expr,
98 _ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
102 $(pub fn $make_ast(self) -> $AstTy {
104 AstFragment::$Kind(ast) => ast,
105 _ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
109 pub fn mut_visit_with<F: MutVisitor>(&mut self, vis: &mut F) {
111 AstFragment::OptExpr(opt_expr) => {
112 visit_clobber(opt_expr, |opt_expr| {
113 if let Some(expr) = opt_expr {
114 vis.filter_map_expr(expr)
120 $($(AstFragment::$Kind(ast) => vis.$mut_visit_ast(ast),)?)*
121 $($(AstFragment::$Kind(ast) =>
122 ast.flat_map_in_place(|ast| vis.$flat_map_ast_elt(ast)),)?)*
126 pub fn visit_with<'a, V: Visitor<'a>>(&'a self, visitor: &mut V) {
128 AstFragment::OptExpr(Some(ref expr)) => visitor.visit_expr(expr),
129 AstFragment::OptExpr(None) => {}
130 $($(AstFragment::$Kind(ref ast) => visitor.$visit_ast(ast),)?)*
131 $($(AstFragment::$Kind(ref ast) => for ast_elt in &ast[..] {
132 visitor.$visit_ast_elt(ast_elt, $($args)*);
138 impl<'a> MacResult for crate::mbe::macro_rules::ParserAnyMacro<'a> {
139 $(fn $make_ast(self: Box<crate::mbe::macro_rules::ParserAnyMacro<'a>>)
141 Some(self.make(AstFragmentKind::$Kind).$make_ast())
148 Expr(P<ast::Expr>) { "expression"; one fn visit_expr; fn visit_expr; fn make_expr; }
149 Pat(P<ast::Pat>) { "pattern"; one fn visit_pat; fn visit_pat; fn make_pat; }
150 Ty(P<ast::Ty>) { "type"; one fn visit_ty; fn visit_ty; fn make_ty; }
151 Stmts(SmallVec<[ast::Stmt; 1]>) {
152 "statement"; many fn flat_map_stmt; fn visit_stmt(); fn make_stmts;
154 Items(SmallVec<[P<ast::Item>; 1]>) {
155 "item"; many fn flat_map_item; fn visit_item(); fn make_items;
157 TraitItems(SmallVec<[P<ast::AssocItem>; 1]>) {
159 many fn flat_map_trait_item;
160 fn visit_assoc_item(AssocCtxt::Trait);
163 ImplItems(SmallVec<[P<ast::AssocItem>; 1]>) {
165 many fn flat_map_impl_item;
166 fn visit_assoc_item(AssocCtxt::Impl);
169 ForeignItems(SmallVec<[P<ast::ForeignItem>; 1]>) {
171 many fn flat_map_foreign_item;
172 fn visit_foreign_item();
173 fn make_foreign_items;
175 Arms(SmallVec<[ast::Arm; 1]>) {
176 "match arm"; many fn flat_map_arm; fn visit_arm(); fn make_arms;
178 Fields(SmallVec<[ast::Field; 1]>) {
179 "field expression"; many fn flat_map_field; fn visit_field(); fn make_fields;
181 FieldPats(SmallVec<[ast::FieldPat; 1]>) {
183 many fn flat_map_field_pattern;
184 fn visit_field_pattern();
185 fn make_field_patterns;
187 GenericParams(SmallVec<[ast::GenericParam; 1]>) {
189 many fn flat_map_generic_param;
190 fn visit_generic_param();
191 fn make_generic_params;
193 Params(SmallVec<[ast::Param; 1]>) {
194 "function parameter"; many fn flat_map_param; fn visit_param(); fn make_params;
196 StructFields(SmallVec<[ast::StructField; 1]>) {
198 many fn flat_map_struct_field;
199 fn visit_struct_field();
200 fn make_struct_fields;
202 Variants(SmallVec<[ast::Variant; 1]>) {
203 "variant"; many fn flat_map_variant; fn visit_variant(); fn make_variants;
207 impl AstFragmentKind {
208 crate fn dummy(self, span: Span) -> AstFragment {
209 self.make_from(DummyResult::any(span)).expect("couldn't create a dummy AST fragment")
212 /// Fragment supports macro expansion and not just inert attributes, `cfg` and `cfg_attr`.
213 pub fn supports_macro_expansion(self) -> bool {
215 AstFragmentKind::OptExpr
216 | AstFragmentKind::Expr
217 | AstFragmentKind::Pat
218 | AstFragmentKind::Ty
219 | AstFragmentKind::Stmts
220 | AstFragmentKind::Items
221 | AstFragmentKind::TraitItems
222 | AstFragmentKind::ImplItems
223 | AstFragmentKind::ForeignItems => true,
224 AstFragmentKind::Arms
225 | AstFragmentKind::Fields
226 | AstFragmentKind::FieldPats
227 | AstFragmentKind::GenericParams
228 | AstFragmentKind::Params
229 | AstFragmentKind::StructFields
230 | AstFragmentKind::Variants => false,
234 fn expect_from_annotatables<I: IntoIterator<Item = Annotatable>>(
238 let mut items = items.into_iter();
240 AstFragmentKind::Arms => {
241 AstFragment::Arms(items.map(Annotatable::expect_arm).collect())
243 AstFragmentKind::Fields => {
244 AstFragment::Fields(items.map(Annotatable::expect_field).collect())
246 AstFragmentKind::FieldPats => {
247 AstFragment::FieldPats(items.map(Annotatable::expect_field_pattern).collect())
249 AstFragmentKind::GenericParams => {
250 AstFragment::GenericParams(items.map(Annotatable::expect_generic_param).collect())
252 AstFragmentKind::Params => {
253 AstFragment::Params(items.map(Annotatable::expect_param).collect())
255 AstFragmentKind::StructFields => {
256 AstFragment::StructFields(items.map(Annotatable::expect_struct_field).collect())
258 AstFragmentKind::Variants => {
259 AstFragment::Variants(items.map(Annotatable::expect_variant).collect())
261 AstFragmentKind::Items => {
262 AstFragment::Items(items.map(Annotatable::expect_item).collect())
264 AstFragmentKind::ImplItems => {
265 AstFragment::ImplItems(items.map(Annotatable::expect_impl_item).collect())
267 AstFragmentKind::TraitItems => {
268 AstFragment::TraitItems(items.map(Annotatable::expect_trait_item).collect())
270 AstFragmentKind::ForeignItems => {
271 AstFragment::ForeignItems(items.map(Annotatable::expect_foreign_item).collect())
273 AstFragmentKind::Stmts => {
274 AstFragment::Stmts(items.map(Annotatable::expect_stmt).collect())
276 AstFragmentKind::Expr => AstFragment::Expr(
277 items.next().expect("expected exactly one expression").expect_expr(),
279 AstFragmentKind::OptExpr => {
280 AstFragment::OptExpr(items.next().map(Annotatable::expect_expr))
282 AstFragmentKind::Pat | AstFragmentKind::Ty => {
283 panic!("patterns and types aren't annotatable")
289 pub struct Invocation {
290 pub kind: InvocationKind,
291 pub fragment_kind: AstFragmentKind,
292 pub expansion_data: ExpansionData,
295 pub enum InvocationKind {
301 attr: ast::Attribute,
303 // Required for resolving derive helper attributes.
305 // We temporarily report errors for attribute macros placed after derives
312 /// "Invocation" that contains all derives from an item,
313 /// broken into multiple `Derive` invocations when expanded.
314 /// FIXME: Find a way to remove it.
321 impl InvocationKind {
322 fn placeholder_visibility(&self) -> Option<ast::Visibility> {
323 // HACK: For unnamed fields placeholders should have the same visibility as the actual
324 // fields because for tuple structs/variants resolve determines visibilities of their
325 // constructor using these field visibilities before attributes on them are are expanded.
326 // The assumption is that the attribute expansion cannot change field visibilities,
327 // and it holds because only inert attributes are supported in this position.
329 InvocationKind::Attr { item: Annotatable::StructField(field), .. }
330 | InvocationKind::Derive { item: Annotatable::StructField(field), .. }
331 | InvocationKind::DeriveContainer { item: Annotatable::StructField(field), .. }
332 if field.ident.is_none() =>
334 Some(field.vis.clone())
342 pub fn span(&self) -> Span {
344 InvocationKind::Bang { span, .. } => *span,
345 InvocationKind::Attr { attr, .. } => attr.span,
346 InvocationKind::Derive { path, .. } => path.span,
347 InvocationKind::DeriveContainer { item, .. } => item.span(),
352 pub struct MacroExpander<'a, 'b> {
353 pub cx: &'a mut ExtCtxt<'b>,
354 monotonic: bool, // cf. `cx.monotonic_expander()`
357 impl<'a, 'b> MacroExpander<'a, 'b> {
358 pub fn new(cx: &'a mut ExtCtxt<'b>, monotonic: bool) -> Self {
359 MacroExpander { cx, monotonic }
362 pub fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
363 let mut module = ModuleData {
364 mod_path: vec![Ident::from_str(&self.cx.ecfg.crate_name)],
365 directory: match self.cx.source_map().span_to_unmapped_path(krate.span) {
366 FileName::Real(name) => name.into_local_path(),
367 other => PathBuf::from(other.to_string()),
370 module.directory.pop();
371 self.cx.root_path = module.directory.clone();
372 self.cx.current_expansion.module = Rc::new(module);
374 let orig_mod_span = krate.module.inner;
376 let krate_item = AstFragment::Items(smallvec![P(ast::Item {
379 kind: ast::ItemKind::Mod(krate.module),
380 ident: Ident::invalid(),
381 id: ast::DUMMY_NODE_ID,
382 vis: ast::Visibility {
383 span: krate.span.shrink_to_lo(),
384 kind: ast::VisibilityKind::Public,
390 match self.fully_expand_fragment(krate_item).make_items().pop().map(P::into_inner) {
391 Some(ast::Item { attrs, kind: ast::ItemKind::Mod(module), .. }) => {
393 krate.module = module;
396 // Resolution failed so we return an empty expansion
397 krate.attrs = vec![];
398 krate.module = ast::Mod {
399 inner: orig_mod_span,
400 unsafety: Unsafe::No,
405 Some(ast::Item { span, kind, .. }) => {
406 krate.attrs = vec![];
407 krate.module = ast::Mod {
408 inner: orig_mod_span,
409 unsafety: Unsafe::No,
416 "expected crate top-level item to be a module after macro expansion, found {} {}",
417 kind.article(), kind.descr()
422 self.cx.trace_macros_diag();
426 // Recursively expand all macro invocations in this AST fragment.
427 pub fn fully_expand_fragment(&mut self, input_fragment: AstFragment) -> AstFragment {
428 let orig_expansion_data = self.cx.current_expansion.clone();
429 let orig_force_mode = self.cx.force_mode;
430 self.cx.current_expansion.depth = 0;
432 // Collect all macro invocations and replace them with placeholders.
433 let (mut fragment_with_placeholders, mut invocations) =
434 self.collect_invocations(input_fragment, &[]);
436 // Optimization: if we resolve all imports now,
437 // we'll be able to immediately resolve most of imported macros.
438 self.resolve_imports();
440 // Resolve paths in all invocations and produce output expanded fragments for them, but
441 // do not insert them into our input AST fragment yet, only store in `expanded_fragments`.
442 // The output fragments also go through expansion recursively until no invocations are left.
443 // Unresolved macros produce dummy outputs as a recovery measure.
444 invocations.reverse();
445 let mut expanded_fragments = Vec::new();
446 let mut undetermined_invocations = Vec::new();
447 let (mut progress, mut force) = (false, !self.monotonic);
449 let (invoc, res) = if let Some(invoc) = invocations.pop() {
452 self.resolve_imports();
453 if undetermined_invocations.is_empty() {
456 invocations = mem::take(&mut undetermined_invocations);
457 force = !mem::replace(&mut progress, false);
458 if force && self.monotonic {
459 self.cx.sess.delay_span_bug(
460 invocations.last().unwrap().0.span(),
461 "expansion entered force mode without producing any errors",
467 let res = match res {
470 let eager_expansion_root = if self.monotonic {
471 invoc.expansion_data.id
473 orig_expansion_data.id
475 match self.cx.resolver.resolve_macro_invocation(
477 eager_expansion_root,
481 Err(Indeterminate) => {
482 // Cannot resolve, will retry this invocation later.
483 undetermined_invocations.push((invoc, None));
490 let ExpansionData { depth, id: expn_id, .. } = invoc.expansion_data;
491 self.cx.current_expansion = invoc.expansion_data.clone();
492 self.cx.force_mode = force;
494 // FIXME(jseyfried): Refactor out the following logic
495 let fragment_kind = invoc.fragment_kind;
496 let (expanded_fragment, new_invocations) = match res {
497 InvocationRes::Single(ext) => match self.expand_invoc(invoc, &ext.kind) {
498 ExpandResult::Ready(fragment) => self.collect_invocations(fragment, &[]),
499 ExpandResult::Retry(invoc) => {
503 "expansion entered force mode but is still stuck",
506 // Cannot expand, will retry this invocation later.
507 undetermined_invocations
508 .push((invoc, Some(InvocationRes::Single(ext))));
513 InvocationRes::DeriveContainer(_exts) => {
514 // FIXME: Consider using the derive resolutions (`_exts`) immediately,
515 // instead of enqueuing the derives to be resolved again later.
516 let (derives, mut item) = match invoc.kind {
517 InvocationKind::DeriveContainer { derives, item } => (derives, item),
520 let (item, derive_placeholders) = if !item.derive_allowed() {
521 self.error_derive_forbidden_on_non_adt(&derives, &item);
522 item.visit_attrs(|attrs| attrs.retain(|a| !a.has_name(sym::derive)));
525 let mut visitor = StripUnconfigured {
527 features: self.cx.ecfg.features,
530 let mut item = visitor.fully_configure(item);
531 item.visit_attrs(|attrs| attrs.retain(|a| !a.has_name(sym::derive)));
532 if visitor.modified && !derives.is_empty() {
533 // Erase the tokens if cfg-stripping modified the item
534 // This will cause us to synthesize fake tokens
535 // when `nt_to_tokenstream` is called on this item.
537 Annotatable::Item(item) => item.tokens = None,
538 Annotatable::Stmt(stmt) => {
539 if let StmtKind::Item(item) = &mut stmt.kind {
542 panic!("Unexpected stmt {:?}", stmt);
545 _ => panic!("Unexpected annotatable {:?}", item),
549 invocations.reserve(derives.len());
550 let derive_placeholders = derives
553 let expn_id = ExpnId::fresh(None);
556 kind: InvocationKind::Derive { path, item: item.clone() },
558 expansion_data: ExpansionData {
560 ..self.cx.current_expansion.clone()
565 NodeId::placeholder_from_expn_id(expn_id)
567 .collect::<Vec<_>>();
568 (item, derive_placeholders)
571 let fragment = fragment_kind.expect_from_annotatables(::std::iter::once(item));
572 self.collect_invocations(fragment, &derive_placeholders)
577 if expanded_fragments.len() < depth {
578 expanded_fragments.push(Vec::new());
580 expanded_fragments[depth - 1].push((expn_id, expanded_fragment));
581 invocations.extend(new_invocations.into_iter().rev());
584 self.cx.current_expansion = orig_expansion_data;
585 self.cx.force_mode = orig_force_mode;
587 // Finally incorporate all the expanded macros into the input AST fragment.
588 let mut placeholder_expander = PlaceholderExpander::new(self.cx, self.monotonic);
589 while let Some(expanded_fragments) = expanded_fragments.pop() {
590 for (expn_id, expanded_fragment) in expanded_fragments.into_iter().rev() {
592 .add(NodeId::placeholder_from_expn_id(expn_id), expanded_fragment);
595 fragment_with_placeholders.mut_visit_with(&mut placeholder_expander);
596 fragment_with_placeholders
599 fn error_derive_forbidden_on_non_adt(&self, derives: &[Path], item: &Annotatable) {
600 let attr = self.cx.sess.find_by_name(item.attrs(), sym::derive);
601 let span = attr.map_or(item.span(), |attr| attr.span);
602 let mut err = struct_span_err!(
606 "`derive` may only be applied to structs, enums and unions",
608 if let Some(ast::Attribute { style: ast::AttrStyle::Inner, .. }) = attr {
609 let trait_list = derives.iter().map(|t| pprust::path_to_string(t)).collect::<Vec<_>>();
610 let suggestion = format!("#[derive({})]", trait_list.join(", "));
613 "try an outer attribute",
615 // We don't 𝑘𝑛𝑜𝑤 that the following item is an ADT
616 Applicability::MaybeIncorrect,
622 fn resolve_imports(&mut self) {
624 self.cx.resolver.resolve_imports();
628 /// Collects all macro invocations reachable at this time in this AST fragment, and replace
629 /// them with "placeholders" - dummy macro invocations with specially crafted `NodeId`s.
630 /// Then call into resolver that builds a skeleton ("reduced graph") of the fragment and
631 /// prepares data for resolving paths of macro invocations.
632 fn collect_invocations(
634 mut fragment: AstFragment,
635 extra_placeholders: &[NodeId],
636 ) -> (AstFragment, Vec<(Invocation, Option<InvocationRes>)>) {
637 // Resolve `$crate`s in the fragment for pretty-printing.
638 self.cx.resolver.resolve_dollar_crates();
641 let mut collector = InvocationCollector {
642 cfg: StripUnconfigured {
644 features: self.cx.ecfg.features,
648 invocations: Vec::new(),
649 monotonic: self.monotonic,
651 fragment.mut_visit_with(&mut collector);
652 fragment.add_placeholders(extra_placeholders);
653 collector.invocations
659 .visit_ast_fragment_with_placeholders(self.cx.current_expansion.id, &fragment);
662 (fragment, invocations)
665 fn error_recursion_limit_reached(&mut self) {
666 let expn_data = self.cx.current_expansion.id.expn_data();
667 let suggested_limit = self.cx.ecfg.recursion_limit * 2;
671 &format!("recursion limit reached while expanding `{}`", expn_data.kind.descr()),
674 "consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate (`{}`)",
675 suggested_limit, self.cx.ecfg.crate_name,
678 self.cx.trace_macros_diag();
681 /// A macro's expansion does not fit in this fragment kind.
682 /// For example, a non-type macro in a type position.
683 fn error_wrong_fragment_kind(&mut self, kind: AstFragmentKind, mac: &ast::MacCall, span: Span) {
685 "non-{kind} macro in {kind} position: {path}",
687 path = pprust::path_to_string(&mac.path),
689 self.cx.span_err(span, &msg);
690 self.cx.trace_macros_diag();
696 ext: &SyntaxExtensionKind,
697 ) -> ExpandResult<AstFragment, Invocation> {
698 let recursion_limit =
699 self.cx.reduced_recursion_limit.unwrap_or(self.cx.ecfg.recursion_limit);
700 if !recursion_limit.value_within_limit(self.cx.current_expansion.depth) {
701 if self.cx.reduced_recursion_limit.is_none() {
702 self.error_recursion_limit_reached();
705 // Reduce the recursion limit by half each time it triggers.
706 self.cx.reduced_recursion_limit = Some(recursion_limit / 2);
708 return ExpandResult::Ready(invoc.fragment_kind.dummy(invoc.span()));
711 let (fragment_kind, span) = (invoc.fragment_kind, invoc.span());
712 ExpandResult::Ready(match invoc.kind {
713 InvocationKind::Bang { mac, .. } => match ext {
714 SyntaxExtensionKind::Bang(expander) => {
715 let tok_result = match expander.expand(self.cx, span, mac.args.inner_tokens()) {
716 Err(_) => return ExpandResult::Ready(fragment_kind.dummy(span)),
719 self.parse_ast_fragment(tok_result, fragment_kind, &mac.path, span)
721 SyntaxExtensionKind::LegacyBang(expander) => {
722 let prev = self.cx.current_expansion.prior_type_ascription;
723 self.cx.current_expansion.prior_type_ascription = mac.prior_type_ascription;
724 let tok_result = expander.expand(self.cx, span, mac.args.inner_tokens());
725 let result = if let Some(result) = fragment_kind.make_from(tok_result) {
728 self.error_wrong_fragment_kind(fragment_kind, &mac, span);
729 fragment_kind.dummy(span)
731 self.cx.current_expansion.prior_type_ascription = prev;
736 InvocationKind::Attr { attr, mut item, derives, after_derive } => match ext {
737 SyntaxExtensionKind::Attr(expander) => {
738 self.gate_proc_macro_input(&item);
739 self.gate_proc_macro_attr_item(span, &item);
740 let tokens = match attr.style {
741 AttrStyle::Outer => item.into_tokens(&self.cx.sess.parse_sess),
742 // FIXME: Properly collect tokens for inner attributes
743 AttrStyle::Inner => rustc_parse::fake_token_stream(
744 &self.cx.sess.parse_sess,
745 &item.into_nonterminal(),
749 let attr_item = attr.unwrap_normal_item();
750 if let MacArgs::Eq(..) = attr_item.args {
751 self.cx.span_err(span, "key-value macro attributes are not supported");
753 let inner_tokens = attr_item.args.inner_tokens();
754 let tok_result = match expander.expand(self.cx, span, inner_tokens, tokens) {
755 Err(_) => return ExpandResult::Ready(fragment_kind.dummy(span)),
758 self.parse_ast_fragment(tok_result, fragment_kind, &attr_item.path, span)
760 SyntaxExtensionKind::LegacyAttr(expander) => {
761 match validate_attr::parse_meta(&self.cx.sess.parse_sess, &attr) {
763 let items = match expander.expand(self.cx, span, &meta, item) {
764 ExpandResult::Ready(items) => items,
765 ExpandResult::Retry(item) => {
766 // Reassemble the original invocation for retrying.
767 return ExpandResult::Retry(Invocation {
768 kind: InvocationKind::Attr {
778 fragment_kind.expect_from_annotatables(items)
782 fragment_kind.dummy(span)
786 SyntaxExtensionKind::NonMacroAttr { mark_used } => {
787 self.cx.sess.mark_attr_known(&attr);
789 self.cx.sess.mark_attr_used(&attr);
791 item.visit_attrs(|attrs| attrs.push(attr));
792 fragment_kind.expect_from_annotatables(iter::once(item))
796 InvocationKind::Derive { path, item } => match ext {
797 SyntaxExtensionKind::Derive(expander)
798 | SyntaxExtensionKind::LegacyDerive(expander) => {
799 if let SyntaxExtensionKind::Derive(..) = ext {
800 self.gate_proc_macro_input(&item);
802 let meta = ast::MetaItem { kind: ast::MetaItemKind::Word, span, path };
803 let items = match expander.expand(self.cx, span, &meta, item) {
804 ExpandResult::Ready(items) => items,
805 ExpandResult::Retry(item) => {
806 // Reassemble the original invocation for retrying.
807 return ExpandResult::Retry(Invocation {
808 kind: InvocationKind::Derive { path: meta.path, item },
813 fragment_kind.expect_from_annotatables(items)
817 InvocationKind::DeriveContainer { .. } => unreachable!(),
821 fn gate_proc_macro_attr_item(&self, span: Span, item: &Annotatable) {
822 let kind = match item {
824 | Annotatable::TraitItem(_)
825 | Annotatable::ImplItem(_)
826 | Annotatable::ForeignItem(_) => return,
827 Annotatable::Stmt(stmt) => {
828 // Attributes are stable on item statements,
829 // but unstable on all other kinds of statements
835 Annotatable::Expr(_) => "expressions",
837 | Annotatable::Field(..)
838 | Annotatable::FieldPat(..)
839 | Annotatable::GenericParam(..)
840 | Annotatable::Param(..)
841 | Annotatable::StructField(..)
842 | Annotatable::Variant(..) => panic!("unexpected annotatable"),
844 if self.cx.ecfg.proc_macro_hygiene() {
848 &self.cx.sess.parse_sess,
849 sym::proc_macro_hygiene,
851 &format!("custom attributes cannot be applied to {}", kind),
856 fn gate_proc_macro_input(&self, annotatable: &Annotatable) {
857 struct GateProcMacroInput<'a> {
858 parse_sess: &'a ParseSess,
861 impl<'ast, 'a> Visitor<'ast> for GateProcMacroInput<'a> {
862 fn visit_item(&mut self, item: &'ast ast::Item) {
864 ast::ItemKind::Mod(module) if !module.inline => {
867 sym::proc_macro_hygiene,
869 "non-inline modules in proc macro input are unstable",
876 visit::walk_item(self, item);
880 if !self.cx.ecfg.proc_macro_hygiene() {
882 .visit_with(&mut GateProcMacroInput { parse_sess: &self.cx.sess.parse_sess });
886 fn parse_ast_fragment(
889 kind: AstFragmentKind,
893 let mut parser = self.cx.new_parser_from_tts(toks);
894 match parse_ast_fragment(&mut parser, kind) {
896 ensure_complete_parse(&mut parser, path, kind.name(), span);
901 annotate_err_with_kind(&mut err, kind, span);
903 self.cx.trace_macros_diag();
910 pub fn parse_ast_fragment<'a>(
911 this: &mut Parser<'a>,
912 kind: AstFragmentKind,
913 ) -> PResult<'a, AstFragment> {
915 AstFragmentKind::Items => {
916 let mut items = SmallVec::new();
917 while let Some(item) = this.parse_item()? {
920 AstFragment::Items(items)
922 AstFragmentKind::TraitItems => {
923 let mut items = SmallVec::new();
924 while let Some(item) = this.parse_trait_item()? {
927 AstFragment::TraitItems(items)
929 AstFragmentKind::ImplItems => {
930 let mut items = SmallVec::new();
931 while let Some(item) = this.parse_impl_item()? {
934 AstFragment::ImplItems(items)
936 AstFragmentKind::ForeignItems => {
937 let mut items = SmallVec::new();
938 while let Some(item) = this.parse_foreign_item()? {
941 AstFragment::ForeignItems(items)
943 AstFragmentKind::Stmts => {
944 let mut stmts = SmallVec::new();
945 // Won't make progress on a `}`.
946 while this.token != token::Eof && this.token != token::CloseDelim(token::Brace) {
947 if let Some(stmt) = this.parse_full_stmt(AttemptLocalParseRecovery::Yes)? {
951 AstFragment::Stmts(stmts)
953 AstFragmentKind::Expr => AstFragment::Expr(this.parse_expr()?),
954 AstFragmentKind::OptExpr => {
955 if this.token != token::Eof {
956 AstFragment::OptExpr(Some(this.parse_expr()?))
958 AstFragment::OptExpr(None)
961 AstFragmentKind::Ty => AstFragment::Ty(this.parse_ty()?),
962 AstFragmentKind::Pat => AstFragment::Pat(this.parse_pat(None)?),
963 AstFragmentKind::Arms
964 | AstFragmentKind::Fields
965 | AstFragmentKind::FieldPats
966 | AstFragmentKind::GenericParams
967 | AstFragmentKind::Params
968 | AstFragmentKind::StructFields
969 | AstFragmentKind::Variants => panic!("unexpected AST fragment kind"),
973 pub fn ensure_complete_parse<'a>(
974 this: &mut Parser<'a>,
979 if this.token != token::Eof {
980 let token = pprust::token_to_string(&this.token);
981 let msg = format!("macro expansion ignores token `{}` and any following", token);
982 // Avoid emitting backtrace info twice.
983 let def_site_span = this.token.span.with_ctxt(SyntaxContext::root());
984 let mut err = this.struct_span_err(def_site_span, &msg);
985 err.span_label(span, "caused by the macro expansion here");
987 "the usage of `{}!` is likely invalid in {} context",
988 pprust::path_to_string(macro_path),
992 let semi_span = this.sess.source_map().next_point(span);
994 let semi_full_span = semi_span.to(this.sess.source_map().next_point(semi_span));
995 match this.sess.source_map().span_to_snippet(semi_full_span) {
996 Ok(ref snippet) if &snippet[..] != ";" && kind_name == "expression" => {
999 "you might be missing a semicolon here",
1001 Applicability::MaybeIncorrect,
1010 struct InvocationCollector<'a, 'b> {
1011 cx: &'a mut ExtCtxt<'b>,
1012 cfg: StripUnconfigured<'a>,
1013 invocations: Vec<(Invocation, Option<InvocationRes>)>,
1017 impl<'a, 'b> InvocationCollector<'a, 'b> {
1018 fn collect(&mut self, fragment_kind: AstFragmentKind, kind: InvocationKind) -> AstFragment {
1019 // Expansion data for all the collected invocations is set upon their resolution,
1020 // with exception of the derive container case which is not resolved and can get
1021 // its expansion data immediately.
1022 let expn_data = match &kind {
1023 InvocationKind::DeriveContainer { item, .. } => {
1024 let mut expn_data = ExpnData::default(
1025 ExpnKind::Macro(MacroKind::Attr, sym::derive),
1027 self.cx.sess.parse_sess.edition,
1030 expn_data.parent = self.cx.current_expansion.id;
1035 let expn_id = ExpnId::fresh(expn_data);
1036 let vis = kind.placeholder_visibility();
1037 self.invocations.push((
1041 expansion_data: ExpansionData {
1043 depth: self.cx.current_expansion.depth + 1,
1044 ..self.cx.current_expansion.clone()
1049 placeholder(fragment_kind, NodeId::placeholder_from_expn_id(expn_id), vis)
1056 kind: AstFragmentKind,
1058 self.collect(kind, InvocationKind::Bang { mac, span })
1063 (attr, derives, after_derive): (Option<ast::Attribute>, Vec<Path>, bool),
1065 kind: AstFragmentKind,
1070 Some(attr) => InvocationKind::Attr { attr, item, derives, after_derive },
1071 None => InvocationKind::DeriveContainer { derives, item },
1078 attrs: &mut Vec<ast::Attribute>,
1079 after_derive: &mut bool,
1080 ) -> Option<ast::Attribute> {
1084 if a.has_name(sym::derive) {
1085 *after_derive = true;
1087 !self.cx.sess.is_attr_known(a) && !is_builtin_attr(a)
1089 .map(|i| attrs.remove(i))
1092 /// If `item` is an attr invocation, remove and return the macro attribute and derive traits.
1095 item: &mut impl HasAttrs,
1096 ) -> Option<(Option<ast::Attribute>, Vec<Path>, /* after_derive */ bool)> {
1097 let (mut attr, mut traits, mut after_derive) = (None, Vec::new(), false);
1099 item.visit_attrs(|mut attrs| {
1100 attr = self.find_attr_invoc(&mut attrs, &mut after_derive);
1101 traits = collect_derives(&mut self.cx, &mut attrs);
1104 if attr.is_some() || !traits.is_empty() { Some((attr, traits, after_derive)) } else { None }
1107 /// Alternative to `take_first_attr()` that ignores `#[derive]` so invocations fallthrough
1108 /// to the unused-attributes lint (making it an error on statements and expressions
1109 /// is a breaking change)
1110 fn take_first_attr_no_derive(
1112 nonitem: &mut impl HasAttrs,
1113 ) -> Option<(Option<ast::Attribute>, Vec<Path>, /* after_derive */ bool)> {
1114 let (mut attr, mut after_derive) = (None, false);
1116 nonitem.visit_attrs(|mut attrs| {
1117 attr = self.find_attr_invoc(&mut attrs, &mut after_derive);
1120 attr.map(|attr| (Some(attr), Vec::new(), after_derive))
1123 fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
1124 self.cfg.configure(node)
1127 // Detect use of feature-gated or invalid attributes on macro invocations
1128 // since they will not be detected after macro expansion.
1129 fn check_attributes(&mut self, attrs: &[ast::Attribute]) {
1130 let features = self.cx.ecfg.features.unwrap();
1131 for attr in attrs.iter() {
1132 rustc_ast_passes::feature_gate::check_attribute(attr, self.cx.sess, features);
1133 validate_attr::check_meta(&self.cx.sess.parse_sess, attr);
1135 // macros are expanded before any lint passes so this warning has to be hardcoded
1136 if attr.has_name(sym::derive) {
1140 .struct_span_warn(attr.span, "`#[derive]` does nothing on macro invocations")
1141 .note("this may become a hard error in a future release")
1145 if attr.doc_str().is_some() {
1146 self.cx.sess.parse_sess.buffer_lint_with_diagnostic(
1147 &UNUSED_DOC_COMMENTS,
1150 "unused doc comment",
1151 BuiltinLintDiagnostics::UnusedDocComment(attr.span),
1158 impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
1159 fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
1160 self.cfg.configure_expr(expr);
1161 visit_clobber(expr.deref_mut(), |mut expr| {
1162 self.cfg.configure_expr_kind(&mut expr.kind);
1164 if let Some(attr) = self.take_first_attr_no_derive(&mut expr) {
1165 // Collect the invoc regardless of whether or not attributes are permitted here
1166 // expansion will eat the attribute so it won't error later.
1167 if let Some(attr) = attr.0.as_ref() {
1168 self.cfg.maybe_emit_expr_attr_err(attr)
1171 // AstFragmentKind::Expr requires the macro to emit an expression.
1173 .collect_attr(attr, Annotatable::Expr(P(expr)), AstFragmentKind::Expr)
1178 if let ast::ExprKind::MacCall(mac) = expr.kind {
1179 self.check_attributes(&expr.attrs);
1180 self.collect_bang(mac, expr.span, AstFragmentKind::Expr).make_expr().into_inner()
1182 ensure_sufficient_stack(|| noop_visit_expr(&mut expr, self));
1188 fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
1189 let mut arm = configure!(self, arm);
1191 if let Some(attr) = self.take_first_attr(&mut arm) {
1193 .collect_attr(attr, Annotatable::Arm(arm), AstFragmentKind::Arms)
1197 noop_flat_map_arm(arm, self)
1200 fn flat_map_field(&mut self, field: ast::Field) -> SmallVec<[ast::Field; 1]> {
1201 let mut field = configure!(self, field);
1203 if let Some(attr) = self.take_first_attr(&mut field) {
1205 .collect_attr(attr, Annotatable::Field(field), AstFragmentKind::Fields)
1209 noop_flat_map_field(field, self)
1212 fn flat_map_field_pattern(&mut self, fp: ast::FieldPat) -> SmallVec<[ast::FieldPat; 1]> {
1213 let mut fp = configure!(self, fp);
1215 if let Some(attr) = self.take_first_attr(&mut fp) {
1217 .collect_attr(attr, Annotatable::FieldPat(fp), AstFragmentKind::FieldPats)
1218 .make_field_patterns();
1221 noop_flat_map_field_pattern(fp, self)
1224 fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
1225 let mut p = configure!(self, p);
1227 if let Some(attr) = self.take_first_attr(&mut p) {
1229 .collect_attr(attr, Annotatable::Param(p), AstFragmentKind::Params)
1233 noop_flat_map_param(p, self)
1236 fn flat_map_struct_field(&mut self, sf: ast::StructField) -> SmallVec<[ast::StructField; 1]> {
1237 let mut sf = configure!(self, sf);
1239 if let Some(attr) = self.take_first_attr(&mut sf) {
1241 .collect_attr(attr, Annotatable::StructField(sf), AstFragmentKind::StructFields)
1242 .make_struct_fields();
1245 noop_flat_map_struct_field(sf, self)
1248 fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
1249 let mut variant = configure!(self, variant);
1251 if let Some(attr) = self.take_first_attr(&mut variant) {
1253 .collect_attr(attr, Annotatable::Variant(variant), AstFragmentKind::Variants)
1257 noop_flat_map_variant(variant, self)
1260 fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
1261 let expr = configure!(self, expr);
1262 expr.filter_map(|mut expr| {
1263 self.cfg.configure_expr_kind(&mut expr.kind);
1265 if let Some(attr) = self.take_first_attr_no_derive(&mut expr) {
1266 if let Some(attr) = attr.0.as_ref() {
1267 self.cfg.maybe_emit_expr_attr_err(attr)
1271 .collect_attr(attr, Annotatable::Expr(P(expr)), AstFragmentKind::OptExpr)
1273 .map(|expr| expr.into_inner());
1276 if let ast::ExprKind::MacCall(mac) = expr.kind {
1277 self.check_attributes(&expr.attrs);
1278 self.collect_bang(mac, expr.span, AstFragmentKind::OptExpr)
1280 .map(|expr| expr.into_inner())
1283 noop_visit_expr(&mut expr, self);
1290 fn visit_pat(&mut self, pat: &mut P<ast::Pat>) {
1291 self.cfg.configure_pat(pat);
1293 PatKind::MacCall(_) => {}
1294 _ => return noop_visit_pat(pat, self),
1297 visit_clobber(pat, |mut pat| match mem::replace(&mut pat.kind, PatKind::Wild) {
1298 PatKind::MacCall(mac) => {
1299 self.collect_bang(mac, pat.span, AstFragmentKind::Pat).make_pat()
1301 _ => unreachable!(),
1305 fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
1306 let mut stmt = configure!(self, stmt);
1308 // we'll expand attributes on expressions separately
1309 if !stmt.is_expr() {
1310 let attr = if stmt.is_item() {
1311 self.take_first_attr(&mut stmt)
1313 // Ignore derives on non-item statements for backwards compatibility.
1314 // This will result in a unused attribute warning
1315 self.take_first_attr_no_derive(&mut stmt)
1318 if let Some(attr) = attr {
1320 .collect_attr(attr, Annotatable::Stmt(P(stmt)), AstFragmentKind::Stmts)
1325 if let StmtKind::MacCall(mac) = stmt.kind {
1326 let MacCallStmt { mac, style, attrs, tokens: _ } = mac.into_inner();
1327 self.check_attributes(&attrs);
1328 let mut placeholder =
1329 self.collect_bang(mac, stmt.span, AstFragmentKind::Stmts).make_stmts();
1331 // If this is a macro invocation with a semicolon, then apply that
1332 // semicolon to the final statement produced by expansion.
1333 if style == MacStmtStyle::Semicolon {
1334 if let Some(stmt) = placeholder.pop() {
1335 placeholder.push(stmt.add_trailing_semicolon());
1342 // The placeholder expander gives ids to statements, so we avoid folding the id here.
1343 let ast::Stmt { id, kind, span } = stmt;
1344 noop_flat_map_stmt_kind(kind, self)
1346 .map(|kind| ast::Stmt { id, kind, span })
1350 fn visit_block(&mut self, block: &mut P<Block>) {
1351 let old_directory_ownership = self.cx.current_expansion.directory_ownership;
1352 self.cx.current_expansion.directory_ownership = DirectoryOwnership::UnownedViaBlock;
1353 noop_visit_block(block, self);
1354 self.cx.current_expansion.directory_ownership = old_directory_ownership;
1357 fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
1358 let mut item = configure!(self, item);
1360 if let Some(attr) = self.take_first_attr(&mut item) {
1362 .collect_attr(attr, Annotatable::Item(item), AstFragmentKind::Items)
1366 let mut attrs = mem::take(&mut item.attrs); // We do this to please borrowck.
1367 let ident = item.ident;
1368 let span = item.span;
1371 ast::ItemKind::MacCall(..) => {
1373 self.check_attributes(&item.attrs);
1374 item.and_then(|item| match item.kind {
1375 ItemKind::MacCall(mac) => {
1376 self.collect_bang(mac, span, AstFragmentKind::Items).make_items()
1378 _ => unreachable!(),
1381 ast::ItemKind::Mod(ref mut old_mod @ ast::Mod { .. }) if ident != Ident::invalid() => {
1382 let sess = &self.cx.sess.parse_sess;
1383 let orig_ownership = self.cx.current_expansion.directory_ownership;
1384 let mut module = (*self.cx.current_expansion.module).clone();
1386 let pushed = &mut false; // Record `parse_external_mod` pushing so we can pop.
1387 let dir = Directory { ownership: orig_ownership, path: module.directory };
1388 let Directory { ownership, path } = if old_mod.inline {
1389 // Inline `mod foo { ... }`, but we still need to push directories.
1391 push_directory(&self.cx.sess, ident, &item.attrs, dir)
1393 // We have an outline `mod foo;` so we need to parse the file.
1394 let (new_mod, dir) = parse_external_mod(
1404 let krate = ast::Crate {
1405 span: new_mod.inner,
1408 proc_macros: vec![],
1410 if let Some(extern_mod_loaded) = self.cx.extern_mod_loaded {
1411 extern_mod_loaded(&krate);
1414 *old_mod = krate.module;
1415 item.attrs = krate.attrs;
1416 // File can have inline attributes, e.g., `#![cfg(...)]` & co. => Reconfigure.
1417 item = match self.configure(item) {
1421 sess.included_mod_stack.borrow_mut().pop();
1423 return Default::default();
1429 // Set the module info before we flat map.
1430 self.cx.current_expansion.directory_ownership = ownership;
1431 module.directory = path;
1432 module.mod_path.push(ident);
1434 mem::replace(&mut self.cx.current_expansion.module, Rc::new(module));
1436 let result = noop_flat_map_item(item, self);
1438 // Restore the module info.
1439 self.cx.current_expansion.module = orig_module;
1440 self.cx.current_expansion.directory_ownership = orig_ownership;
1442 sess.included_mod_stack.borrow_mut().pop();
1448 noop_flat_map_item(item, self)
1453 fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
1454 let mut item = configure!(self, item);
1456 if let Some(attr) = self.take_first_attr(&mut item) {
1458 .collect_attr(attr, Annotatable::TraitItem(item), AstFragmentKind::TraitItems)
1459 .make_trait_items();
1463 ast::AssocItemKind::MacCall(..) => {
1464 self.check_attributes(&item.attrs);
1465 item.and_then(|item| match item.kind {
1466 ast::AssocItemKind::MacCall(mac) => self
1467 .collect_bang(mac, item.span, AstFragmentKind::TraitItems)
1468 .make_trait_items(),
1469 _ => unreachable!(),
1472 _ => noop_flat_map_assoc_item(item, self),
1476 fn flat_map_impl_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
1477 let mut item = configure!(self, item);
1479 if let Some(attr) = self.take_first_attr(&mut item) {
1481 .collect_attr(attr, Annotatable::ImplItem(item), AstFragmentKind::ImplItems)
1486 ast::AssocItemKind::MacCall(..) => {
1487 self.check_attributes(&item.attrs);
1488 item.and_then(|item| match item.kind {
1489 ast::AssocItemKind::MacCall(mac) => self
1490 .collect_bang(mac, item.span, AstFragmentKind::ImplItems)
1492 _ => unreachable!(),
1495 _ => noop_flat_map_assoc_item(item, self),
1499 fn visit_ty(&mut self, ty: &mut P<ast::Ty>) {
1501 ast::TyKind::MacCall(_) => {}
1502 _ => return noop_visit_ty(ty, self),
1505 visit_clobber(ty, |mut ty| match mem::replace(&mut ty.kind, ast::TyKind::Err) {
1506 ast::TyKind::MacCall(mac) => {
1507 self.collect_bang(mac, ty.span, AstFragmentKind::Ty).make_ty()
1509 _ => unreachable!(),
1513 fn visit_foreign_mod(&mut self, foreign_mod: &mut ast::ForeignMod) {
1514 self.cfg.configure_foreign_mod(foreign_mod);
1515 noop_visit_foreign_mod(foreign_mod, self);
1518 fn flat_map_foreign_item(
1520 mut foreign_item: P<ast::ForeignItem>,
1521 ) -> SmallVec<[P<ast::ForeignItem>; 1]> {
1522 if let Some(attr) = self.take_first_attr(&mut foreign_item) {
1526 Annotatable::ForeignItem(foreign_item),
1527 AstFragmentKind::ForeignItems,
1529 .make_foreign_items();
1532 match foreign_item.kind {
1533 ast::ForeignItemKind::MacCall(..) => {
1534 self.check_attributes(&foreign_item.attrs);
1535 foreign_item.and_then(|item| match item.kind {
1536 ast::ForeignItemKind::MacCall(mac) => self
1537 .collect_bang(mac, item.span, AstFragmentKind::ForeignItems)
1538 .make_foreign_items(),
1539 _ => unreachable!(),
1542 _ => noop_flat_map_foreign_item(foreign_item, self),
1546 fn visit_item_kind(&mut self, item: &mut ast::ItemKind) {
1548 ast::ItemKind::MacroDef(..) => {}
1550 self.cfg.configure_item_kind(item);
1551 noop_visit_item_kind(item, self);
1556 fn flat_map_generic_param(
1558 param: ast::GenericParam,
1559 ) -> SmallVec<[ast::GenericParam; 1]> {
1560 let mut param = configure!(self, param);
1562 if let Some(attr) = self.take_first_attr(&mut param) {
1566 Annotatable::GenericParam(param),
1567 AstFragmentKind::GenericParams,
1569 .make_generic_params();
1572 noop_flat_map_generic_param(param, self)
1575 fn visit_attribute(&mut self, at: &mut ast::Attribute) {
1576 // turn `#[doc(include="filename")]` attributes into `#[doc(include(file="filename",
1577 // contents="file contents")]` attributes
1578 if !self.cx.sess.check_name(at, sym::doc) {
1579 return noop_visit_attribute(at, self);
1582 if let Some(list) = at.meta_item_list() {
1583 if !list.iter().any(|it| it.has_name(sym::include)) {
1584 return noop_visit_attribute(at, self);
1587 let mut items = vec![];
1589 for mut it in list {
1590 if !it.has_name(sym::include) {
1592 noop_visit_meta_list_item(&mut it, self);
1598 if let Some(file) = it.value_str() {
1599 let err_count = self.cx.sess.parse_sess.span_diagnostic.err_count();
1600 self.check_attributes(slice::from_ref(at));
1601 if self.cx.sess.parse_sess.span_diagnostic.err_count() > err_count {
1602 // avoid loading the file if they haven't enabled the feature
1603 return noop_visit_attribute(at, self);
1606 let filename = match self.cx.resolve_path(&*file.as_str(), it.span()) {
1607 Ok(filename) => filename,
1614 match self.cx.source_map().load_file(&filename) {
1615 Ok(source_file) => {
1616 let src = source_file
1619 .expect("freshly loaded file should have a source");
1620 let src_interned = Symbol::intern(src.as_str());
1622 let include_info = vec![
1623 ast::NestedMetaItem::MetaItem(attr::mk_name_value_item_str(
1624 Ident::with_dummy_span(sym::file),
1628 ast::NestedMetaItem::MetaItem(attr::mk_name_value_item_str(
1629 Ident::with_dummy_span(sym::contents),
1635 let include_ident = Ident::with_dummy_span(sym::include);
1636 let item = attr::mk_list_item(include_ident, include_info);
1637 items.push(ast::NestedMetaItem::MetaItem(item));
1640 let lit_span = it.name_value_literal_span().unwrap();
1642 if e.kind() == ErrorKind::InvalidData {
1646 &format!("{} wasn't a utf-8 file", filename.display()),
1648 .span_label(lit_span, "contains invalid utf-8")
1651 let mut err = self.cx.struct_span_err(
1653 &format!("couldn't read {}: {}", filename.display(), e),
1655 err.span_label(lit_span, "couldn't read file");
1664 .struct_span_err(it.span(), "expected path to external documentation");
1666 // Check if the user erroneously used `doc(include(...))` syntax.
1667 let literal = it.meta_item_list().and_then(|list| {
1668 if list.len() == 1 {
1669 list[0].literal().map(|literal| &literal.kind)
1675 let (path, applicability) = match &literal {
1676 Some(LitKind::Str(path, ..)) => {
1677 (path.to_string(), Applicability::MachineApplicable)
1679 _ => (String::from("<path>"), Applicability::HasPlaceholders),
1682 err.span_suggestion(
1684 "provide a file path with `=`",
1685 format!("include = \"{}\"", path),
1693 let meta = attr::mk_list_item(Ident::with_dummy_span(sym::doc), items);
1694 *at = ast::Attribute {
1695 kind: ast::AttrKind::Normal(
1696 AttrItem { path: meta.path, args: meta.kind.mac_args(meta.span), tokens: None },
1704 noop_visit_attribute(at, self)
1708 fn visit_id(&mut self, id: &mut ast::NodeId) {
1710 debug_assert_eq!(*id, ast::DUMMY_NODE_ID);
1711 *id = self.cx.resolver.next_node_id()
1715 fn visit_fn_decl(&mut self, mut fn_decl: &mut P<ast::FnDecl>) {
1716 self.cfg.configure_fn_decl(&mut fn_decl);
1717 noop_visit_fn_decl(fn_decl, self);
1721 pub struct ExpansionConfig<'feat> {
1722 pub crate_name: String,
1723 pub features: Option<&'feat Features>,
1724 pub recursion_limit: Limit,
1725 pub trace_mac: bool,
1726 pub should_test: bool, // If false, strip `#[test]` nodes
1727 pub keep_macs: bool,
1728 pub span_debug: bool, // If true, use verbose debugging for `proc_macro::Span`
1729 pub proc_macro_backtrace: bool, // If true, show backtraces for proc-macro panics
1732 impl<'feat> ExpansionConfig<'feat> {
1733 pub fn default(crate_name: String) -> ExpansionConfig<'static> {
1737 recursion_limit: Limit::new(1024),
1742 proc_macro_backtrace: false,
1746 fn proc_macro_hygiene(&self) -> bool {
1747 self.features.map_or(false, |features| features.proc_macro_hygiene)