2 use crate::proc_macro::{collect_derives, MarkAttrs};
3 use crate::hygiene::{ExpnId, SyntaxContext, ExpnData, ExpnKind};
4 use crate::mbe::macro_rules::annotate_err_with_kind;
5 use crate::placeholders::{placeholder, PlaceholderExpander};
7 use syntax::ast::{self, AttrItem, Block, Ident, LitKind, NodeId, PatKind, Path};
8 use syntax::ast::{MacStmtStyle, StmtKind, ItemKind};
9 use syntax::attr::{self, HasAttrs};
10 use syntax::source_map::respan;
11 use syntax::configure;
12 use syntax::config::StripUnconfigured;
13 use syntax::feature_gate::{self, Features, GateIssue, is_builtin_attr, emit_feature_err};
14 use syntax::mut_visit::*;
15 use syntax::parse::{DirectoryOwnership, PResult};
16 use syntax::parse::token;
17 use syntax::parse::parser::Parser;
18 use syntax::print::pprust;
20 use syntax::symbol::{sym, Symbol};
21 use syntax::tokenstream::{TokenStream, TokenTree};
22 use syntax::visit::Visitor;
23 use syntax::util::map_in_place::MapInPlace;
25 use errors::{Applicability, FatalError};
26 use smallvec::{smallvec, SmallVec};
27 use syntax_pos::{Span, DUMMY_SP, FileName};
29 use rustc_data_structures::sync::Lrc;
30 use std::io::ErrorKind;
31 use std::{iter, mem, slice};
32 use std::ops::DerefMut;
34 use std::path::PathBuf;
36 macro_rules! ast_fragments {
38 $($Kind:ident($AstTy:ty) {
40 $(one fn $mut_visit_ast:ident; fn $visit_ast:ident;)?
41 $(many fn $flat_map_ast_elt:ident; fn $visit_ast_elt:ident;)?
45 /// A fragment of AST that can be produced by a single macro expansion.
46 /// Can also serve as an input and intermediate result for macro expansion operations.
47 pub enum AstFragment {
48 OptExpr(Option<P<ast::Expr>>),
52 /// "Discriminant" of an AST fragment.
53 #[derive(Copy, Clone, PartialEq, Eq)]
54 pub enum AstFragmentKind {
59 impl AstFragmentKind {
60 pub fn name(self) -> &'static str {
62 AstFragmentKind::OptExpr => "expression",
63 $(AstFragmentKind::$Kind => $kind_name,)*
67 fn make_from<'a>(self, result: Box<dyn MacResult + 'a>) -> Option<AstFragment> {
69 AstFragmentKind::OptExpr =>
70 result.make_expr().map(Some).map(AstFragment::OptExpr),
71 $(AstFragmentKind::$Kind => result.$make_ast().map(AstFragment::$Kind),)*
77 pub fn add_placeholders(&mut self, placeholders: &[NodeId]) {
78 if placeholders.is_empty() {
82 $($(AstFragment::$Kind(ast) => ast.extend(placeholders.iter().flat_map(|id| {
83 // We are repeating through arguments with `many`, to do that we have to
84 // mention some macro variable from those arguments even if it's not used.
85 #[cfg_attr(bootstrap, allow(unused_macros))]
86 macro _repeating($flat_map_ast_elt) {}
87 placeholder(AstFragmentKind::$Kind, *id).$make_ast()
89 _ => panic!("unexpected AST fragment kind")
93 pub fn make_opt_expr(self) -> Option<P<ast::Expr>> {
95 AstFragment::OptExpr(expr) => expr,
96 _ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
100 $(pub fn $make_ast(self) -> $AstTy {
102 AstFragment::$Kind(ast) => ast,
103 _ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
107 pub fn mut_visit_with<F: MutVisitor>(&mut self, vis: &mut F) {
109 AstFragment::OptExpr(opt_expr) => {
110 visit_clobber(opt_expr, |opt_expr| {
111 if let Some(expr) = opt_expr {
112 vis.filter_map_expr(expr)
118 $($(AstFragment::$Kind(ast) => vis.$mut_visit_ast(ast),)?)*
119 $($(AstFragment::$Kind(ast) =>
120 ast.flat_map_in_place(|ast| vis.$flat_map_ast_elt(ast)),)?)*
124 pub fn visit_with<'a, V: Visitor<'a>>(&'a self, visitor: &mut V) {
126 AstFragment::OptExpr(Some(ref expr)) => visitor.visit_expr(expr),
127 AstFragment::OptExpr(None) => {}
128 $($(AstFragment::$Kind(ref ast) => visitor.$visit_ast(ast),)?)*
129 $($(AstFragment::$Kind(ref ast) => for ast_elt in &ast[..] {
130 visitor.$visit_ast_elt(ast_elt);
136 impl<'a> MacResult for crate::mbe::macro_rules::ParserAnyMacro<'a> {
137 $(fn $make_ast(self: Box<crate::mbe::macro_rules::ParserAnyMacro<'a>>)
139 Some(self.make(AstFragmentKind::$Kind).$make_ast())
146 Expr(P<ast::Expr>) { "expression"; one fn visit_expr; fn visit_expr; fn make_expr; }
147 Pat(P<ast::Pat>) { "pattern"; one fn visit_pat; fn visit_pat; fn make_pat; }
148 Ty(P<ast::Ty>) { "type"; one fn visit_ty; fn visit_ty; fn make_ty; }
149 Stmts(SmallVec<[ast::Stmt; 1]>) {
150 "statement"; many fn flat_map_stmt; fn visit_stmt; fn make_stmts;
152 Items(SmallVec<[P<ast::Item>; 1]>) {
153 "item"; many fn flat_map_item; fn visit_item; fn make_items;
155 TraitItems(SmallVec<[ast::TraitItem; 1]>) {
156 "trait item"; many fn flat_map_trait_item; fn visit_trait_item; fn make_trait_items;
158 ImplItems(SmallVec<[ast::ImplItem; 1]>) {
159 "impl item"; many fn flat_map_impl_item; fn visit_impl_item; fn make_impl_items;
161 ForeignItems(SmallVec<[ast::ForeignItem; 1]>) {
163 many fn flat_map_foreign_item;
164 fn visit_foreign_item;
165 fn make_foreign_items;
167 Arms(SmallVec<[ast::Arm; 1]>) {
168 "match arm"; many fn flat_map_arm; fn visit_arm; fn make_arms;
170 Fields(SmallVec<[ast::Field; 1]>) {
171 "field expression"; many fn flat_map_field; fn visit_field; fn make_fields;
173 FieldPats(SmallVec<[ast::FieldPat; 1]>) {
175 many fn flat_map_field_pattern;
176 fn visit_field_pattern;
177 fn make_field_patterns;
179 GenericParams(SmallVec<[ast::GenericParam; 1]>) {
181 many fn flat_map_generic_param;
182 fn visit_generic_param;
183 fn make_generic_params;
185 Params(SmallVec<[ast::Param; 1]>) {
186 "function parameter"; many fn flat_map_param; fn visit_param; fn make_params;
188 StructFields(SmallVec<[ast::StructField; 1]>) {
190 many fn flat_map_struct_field;
191 fn visit_struct_field;
192 fn make_struct_fields;
194 Variants(SmallVec<[ast::Variant; 1]>) {
195 "variant"; many fn flat_map_variant; fn visit_variant; fn make_variants;
199 impl AstFragmentKind {
200 fn dummy(self, span: Span) -> AstFragment {
201 self.make_from(DummyResult::any(span)).expect("couldn't create a dummy AST fragment")
204 fn expect_from_annotatables<I: IntoIterator<Item = Annotatable>>(self, items: I)
206 let mut items = items.into_iter();
208 AstFragmentKind::Arms =>
209 AstFragment::Arms(items.map(Annotatable::expect_arm).collect()),
210 AstFragmentKind::Fields =>
211 AstFragment::Fields(items.map(Annotatable::expect_field).collect()),
212 AstFragmentKind::FieldPats =>
213 AstFragment::FieldPats(items.map(Annotatable::expect_field_pattern).collect()),
214 AstFragmentKind::GenericParams =>
215 AstFragment::GenericParams(items.map(Annotatable::expect_generic_param).collect()),
216 AstFragmentKind::Params =>
217 AstFragment::Params(items.map(Annotatable::expect_param).collect()),
218 AstFragmentKind::StructFields => AstFragment::StructFields(
219 items.map(Annotatable::expect_struct_field).collect()
221 AstFragmentKind::Variants =>
222 AstFragment::Variants(items.map(Annotatable::expect_variant).collect()),
223 AstFragmentKind::Items =>
224 AstFragment::Items(items.map(Annotatable::expect_item).collect()),
225 AstFragmentKind::ImplItems =>
226 AstFragment::ImplItems(items.map(Annotatable::expect_impl_item).collect()),
227 AstFragmentKind::TraitItems =>
228 AstFragment::TraitItems(items.map(Annotatable::expect_trait_item).collect()),
229 AstFragmentKind::ForeignItems =>
230 AstFragment::ForeignItems(items.map(Annotatable::expect_foreign_item).collect()),
231 AstFragmentKind::Stmts =>
232 AstFragment::Stmts(items.map(Annotatable::expect_stmt).collect()),
233 AstFragmentKind::Expr => AstFragment::Expr(
234 items.next().expect("expected exactly one expression").expect_expr()
236 AstFragmentKind::OptExpr =>
237 AstFragment::OptExpr(items.next().map(Annotatable::expect_expr)),
238 AstFragmentKind::Pat | AstFragmentKind::Ty =>
239 panic!("patterns and types aren't annotatable"),
244 pub struct Invocation {
245 pub kind: InvocationKind,
246 pub fragment_kind: AstFragmentKind,
247 pub expansion_data: ExpansionData,
250 pub enum InvocationKind {
256 attr: ast::Attribute,
258 // Required for resolving derive helper attributes.
260 // We temporarily report errors for attribute macros placed after derives
267 /// "Invocation" that contains all derives from an item,
268 /// broken into multiple `Derive` invocations when expanded.
269 /// FIXME: Find a way to remove it.
277 pub fn span(&self) -> Span {
279 InvocationKind::Bang { span, .. } => *span,
280 InvocationKind::Attr { attr, .. } => attr.span,
281 InvocationKind::Derive { path, .. } => path.span,
282 InvocationKind::DeriveContainer { item, .. } => item.span(),
287 pub struct MacroExpander<'a, 'b> {
288 pub cx: &'a mut ExtCtxt<'b>,
289 monotonic: bool, // cf. `cx.monotonic_expander()`
292 impl<'a, 'b> MacroExpander<'a, 'b> {
293 pub fn new(cx: &'a mut ExtCtxt<'b>, monotonic: bool) -> Self {
294 MacroExpander { cx, monotonic }
297 pub fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
298 let mut module = ModuleData {
299 mod_path: vec![Ident::from_str(&self.cx.ecfg.crate_name)],
300 directory: match self.cx.source_map().span_to_unmapped_path(krate.span) {
301 FileName::Real(path) => path,
302 other => PathBuf::from(other.to_string()),
305 module.directory.pop();
306 self.cx.root_path = module.directory.clone();
307 self.cx.current_expansion.module = Rc::new(module);
309 let orig_mod_span = krate.module.inner;
311 let krate_item = AstFragment::Items(smallvec![P(ast::Item {
314 kind: ast::ItemKind::Mod(krate.module),
315 ident: Ident::invalid(),
316 id: ast::DUMMY_NODE_ID,
317 vis: respan(krate.span.shrink_to_lo(), ast::VisibilityKind::Public),
321 match self.fully_expand_fragment(krate_item).make_items().pop().map(P::into_inner) {
322 Some(ast::Item { attrs, kind: ast::ItemKind::Mod(module), .. }) => {
324 krate.module = module;
327 // Resolution failed so we return an empty expansion
328 krate.attrs = vec![];
329 krate.module = ast::Mod {
330 inner: orig_mod_span,
337 self.cx.trace_macros_diag();
341 // Recursively expand all macro invocations in this AST fragment.
342 pub fn fully_expand_fragment(&mut self, input_fragment: AstFragment) -> AstFragment {
343 let orig_expansion_data = self.cx.current_expansion.clone();
344 self.cx.current_expansion.depth = 0;
346 // Collect all macro invocations and replace them with placeholders.
347 let (mut fragment_with_placeholders, mut invocations)
348 = self.collect_invocations(input_fragment, &[]);
350 // Optimization: if we resolve all imports now,
351 // we'll be able to immediately resolve most of imported macros.
352 self.resolve_imports();
354 // Resolve paths in all invocations and produce output expanded fragments for them, but
355 // do not insert them into our input AST fragment yet, only store in `expanded_fragments`.
356 // The output fragments also go through expansion recursively until no invocations are left.
357 // Unresolved macros produce dummy outputs as a recovery measure.
358 invocations.reverse();
359 let mut expanded_fragments = Vec::new();
360 let mut undetermined_invocations = Vec::new();
361 let (mut progress, mut force) = (false, !self.monotonic);
363 let invoc = if let Some(invoc) = invocations.pop() {
366 self.resolve_imports();
367 if undetermined_invocations.is_empty() { break }
368 invocations = mem::take(&mut undetermined_invocations);
369 force = !mem::replace(&mut progress, false);
373 let eager_expansion_root =
374 if self.monotonic { invoc.expansion_data.id } else { orig_expansion_data.id };
375 let res = match self.cx.resolver.resolve_macro_invocation(
376 &invoc, eager_expansion_root, force
379 Err(Indeterminate) => {
380 undetermined_invocations.push(invoc);
386 let ExpansionData { depth, id: expn_id, .. } = invoc.expansion_data;
387 self.cx.current_expansion = invoc.expansion_data.clone();
389 // FIXME(jseyfried): Refactor out the following logic
390 let (expanded_fragment, new_invocations) = match res {
391 InvocationRes::Single(ext) => {
392 let fragment = self.expand_invoc(invoc, &ext.kind);
393 self.collect_invocations(fragment, &[])
395 InvocationRes::DeriveContainer(exts) => {
396 let (derives, item) = match invoc.kind {
397 InvocationKind::DeriveContainer { derives, item } => (derives, item),
400 if !item.derive_allowed() {
401 let attr = attr::find_by_name(item.attrs(), sym::derive)
402 .expect("`derive` attribute should exist");
403 let span = attr.span;
404 let mut err = self.cx.struct_span_err(span,
405 "`derive` may only be applied to structs, enums and unions");
406 if let ast::AttrStyle::Inner = attr.style {
407 let trait_list = derives.iter()
408 .map(|t| pprust::path_to_string(t))
409 .collect::<Vec<_>>();
410 let suggestion = format!("#[derive({})]", trait_list.join(", "));
412 span, "try an outer attribute", suggestion,
413 // We don't 𝑘𝑛𝑜𝑤 that the following item is an ADT
414 Applicability::MaybeIncorrect
420 let mut item = self.fully_configure(item);
421 item.visit_attrs(|attrs| attrs.retain(|a| a.path != sym::derive));
422 let mut helper_attrs = Vec::new();
423 let mut has_copy = false;
425 helper_attrs.extend(&ext.helper_attrs);
426 has_copy |= ext.is_derive_copy;
428 // Mark derive helpers inside this item as known and used.
429 // FIXME: This is a hack, derive helpers should be integrated with regular name
430 // resolution instead. For example, helpers introduced by a derive container
431 // can be in scope for all code produced by that container's expansion.
432 item.visit_with(&mut MarkAttrs(&helper_attrs));
434 self.cx.resolver.add_derives(invoc.expansion_data.id, SpecialDerives::COPY);
437 let mut derive_placeholders = Vec::with_capacity(derives.len());
438 invocations.reserve(derives.len());
439 for path in derives {
440 let expn_id = ExpnId::fresh(None);
441 derive_placeholders.push(NodeId::placeholder_from_expn_id(expn_id));
442 invocations.push(Invocation {
443 kind: InvocationKind::Derive { path, item: item.clone() },
444 fragment_kind: invoc.fragment_kind,
445 expansion_data: ExpansionData {
447 ..invoc.expansion_data.clone()
451 let fragment = invoc.fragment_kind
452 .expect_from_annotatables(::std::iter::once(item));
453 self.collect_invocations(fragment, &derive_placeholders)
457 if expanded_fragments.len() < depth {
458 expanded_fragments.push(Vec::new());
460 expanded_fragments[depth - 1].push((expn_id, expanded_fragment));
461 if !self.cx.ecfg.single_step {
462 invocations.extend(new_invocations.into_iter().rev());
466 self.cx.current_expansion = orig_expansion_data;
468 // Finally incorporate all the expanded macros into the input AST fragment.
469 let mut placeholder_expander = PlaceholderExpander::new(self.cx, self.monotonic);
470 while let Some(expanded_fragments) = expanded_fragments.pop() {
471 for (expn_id, expanded_fragment) in expanded_fragments.into_iter().rev() {
472 placeholder_expander.add(NodeId::placeholder_from_expn_id(expn_id),
476 fragment_with_placeholders.mut_visit_with(&mut placeholder_expander);
477 fragment_with_placeholders
480 fn resolve_imports(&mut self) {
482 self.cx.resolver.resolve_imports();
486 /// Collects all macro invocations reachable at this time in this AST fragment, and replace
487 /// them with "placeholders" - dummy macro invocations with specially crafted `NodeId`s.
488 /// Then call into resolver that builds a skeleton ("reduced graph") of the fragment and
489 /// prepares data for resolving paths of macro invocations.
490 fn collect_invocations(&mut self, mut fragment: AstFragment, extra_placeholders: &[NodeId])
491 -> (AstFragment, Vec<Invocation>) {
492 // Resolve `$crate`s in the fragment for pretty-printing.
493 self.cx.resolver.resolve_dollar_crates();
496 let mut collector = InvocationCollector {
497 cfg: StripUnconfigured {
498 sess: self.cx.parse_sess,
499 features: self.cx.ecfg.features,
502 invocations: Vec::new(),
503 monotonic: self.monotonic,
505 fragment.mut_visit_with(&mut collector);
506 fragment.add_placeholders(extra_placeholders);
507 collector.invocations
511 self.cx.resolver.visit_ast_fragment_with_placeholders(
512 self.cx.current_expansion.id, &fragment
516 (fragment, invocations)
519 fn fully_configure(&mut self, item: Annotatable) -> Annotatable {
520 let mut cfg = StripUnconfigured {
521 sess: self.cx.parse_sess,
522 features: self.cx.ecfg.features,
524 // Since the item itself has already been configured by the InvocationCollector,
525 // we know that fold result vector will contain exactly one element
527 Annotatable::Item(item) => {
528 Annotatable::Item(cfg.flat_map_item(item).pop().unwrap())
530 Annotatable::TraitItem(item) => {
531 Annotatable::TraitItem(
532 item.map(|item| cfg.flat_map_trait_item(item).pop().unwrap()))
534 Annotatable::ImplItem(item) => {
535 Annotatable::ImplItem(item.map(|item| cfg.flat_map_impl_item(item).pop().unwrap()))
537 Annotatable::ForeignItem(item) => {
538 Annotatable::ForeignItem(
539 item.map(|item| cfg.flat_map_foreign_item(item).pop().unwrap())
542 Annotatable::Stmt(stmt) => {
543 Annotatable::Stmt(stmt.map(|stmt| cfg.flat_map_stmt(stmt).pop().unwrap()))
545 Annotatable::Expr(mut expr) => {
546 Annotatable::Expr({ cfg.visit_expr(&mut expr); expr })
548 Annotatable::Arm(arm) => {
549 Annotatable::Arm(cfg.flat_map_arm(arm).pop().unwrap())
551 Annotatable::Field(field) => {
552 Annotatable::Field(cfg.flat_map_field(field).pop().unwrap())
554 Annotatable::FieldPat(fp) => {
555 Annotatable::FieldPat(cfg.flat_map_field_pattern(fp).pop().unwrap())
557 Annotatable::GenericParam(param) => {
558 Annotatable::GenericParam(cfg.flat_map_generic_param(param).pop().unwrap())
560 Annotatable::Param(param) => {
561 Annotatable::Param(cfg.flat_map_param(param).pop().unwrap())
563 Annotatable::StructField(sf) => {
564 Annotatable::StructField(cfg.flat_map_struct_field(sf).pop().unwrap())
566 Annotatable::Variant(v) => {
567 Annotatable::Variant(cfg.flat_map_variant(v).pop().unwrap())
572 fn expand_invoc(&mut self, invoc: Invocation, ext: &SyntaxExtensionKind) -> AstFragment {
573 if self.cx.current_expansion.depth > self.cx.ecfg.recursion_limit {
574 let expn_data = self.cx.current_expansion.id.expn_data();
575 let suggested_limit = self.cx.ecfg.recursion_limit * 2;
576 let mut err = self.cx.struct_span_err(expn_data.call_site,
577 &format!("recursion limit reached while expanding the macro `{}`",
578 expn_data.kind.descr()));
580 "consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
583 self.cx.trace_macros_diag();
587 let (fragment_kind, span) = (invoc.fragment_kind, invoc.span());
589 InvocationKind::Bang { mac, .. } => match ext {
590 SyntaxExtensionKind::Bang(expander) => {
591 self.gate_proc_macro_expansion_kind(span, fragment_kind);
592 let tok_result = expander.expand(self.cx, span, mac.stream());
593 self.parse_ast_fragment(tok_result, fragment_kind, &mac.path, span)
595 SyntaxExtensionKind::LegacyBang(expander) => {
596 let prev = self.cx.current_expansion.prior_type_ascription;
597 self.cx.current_expansion.prior_type_ascription = mac.prior_type_ascription;
598 let tok_result = expander.expand(self.cx, span, mac.stream());
599 let result = if let Some(result) = fragment_kind.make_from(tok_result) {
603 "non-{kind} macro in {kind} position: {path}",
604 kind = fragment_kind.name(),
605 path = pprust::path_to_string(&mac.path),
607 self.cx.span_err(span, &msg);
608 self.cx.trace_macros_diag();
609 fragment_kind.dummy(span)
611 self.cx.current_expansion.prior_type_ascription = prev;
616 InvocationKind::Attr { attr, mut item, .. } => match ext {
617 SyntaxExtensionKind::Attr(expander) => {
618 self.gate_proc_macro_attr_item(span, &item);
619 let item_tok = TokenTree::token(token::Interpolated(Lrc::new(match item {
620 Annotatable::Item(item) => token::NtItem(item),
621 Annotatable::TraitItem(item) => token::NtTraitItem(item.into_inner()),
622 Annotatable::ImplItem(item) => token::NtImplItem(item.into_inner()),
623 Annotatable::ForeignItem(item) => token::NtForeignItem(item.into_inner()),
624 Annotatable::Stmt(stmt) => token::NtStmt(stmt.into_inner()),
625 Annotatable::Expr(expr) => token::NtExpr(expr),
627 | Annotatable::Field(..)
628 | Annotatable::FieldPat(..)
629 | Annotatable::GenericParam(..)
630 | Annotatable::Param(..)
631 | Annotatable::StructField(..)
632 | Annotatable::Variant(..)
633 => panic!("unexpected annotatable"),
634 })), DUMMY_SP).into();
635 let input = self.extract_proc_macro_attr_input(attr.item.tokens, span);
636 let tok_result = expander.expand(self.cx, span, input, item_tok);
637 self.parse_ast_fragment(tok_result, fragment_kind, &attr.item.path, span)
639 SyntaxExtensionKind::LegacyAttr(expander) => {
640 match attr.parse_meta(self.cx.parse_sess) {
642 let item = expander.expand(self.cx, span, &meta, item);
643 fragment_kind.expect_from_annotatables(item)
647 fragment_kind.dummy(span)
651 SyntaxExtensionKind::NonMacroAttr { mark_used } => {
652 attr::mark_known(&attr);
654 attr::mark_used(&attr);
656 item.visit_attrs(|attrs| attrs.push(attr));
657 fragment_kind.expect_from_annotatables(iter::once(item))
661 InvocationKind::Derive { path, item } => match ext {
662 SyntaxExtensionKind::Derive(expander) |
663 SyntaxExtensionKind::LegacyDerive(expander) => {
664 if !item.derive_allowed() {
665 return fragment_kind.dummy(span);
667 let meta = ast::MetaItem { kind: ast::MetaItemKind::Word, span, path };
668 let items = expander.expand(self.cx, span, &meta, item);
669 fragment_kind.expect_from_annotatables(items)
673 InvocationKind::DeriveContainer { .. } => unreachable!()
677 fn extract_proc_macro_attr_input(&self, tokens: TokenStream, span: Span) -> TokenStream {
678 let mut trees = tokens.trees();
680 Some(TokenTree::Delimited(_, _, tts)) => {
681 if trees.next().is_none() {
685 Some(TokenTree::Token(..)) => {}
686 None => return TokenStream::default(),
688 self.cx.span_err(span, "custom attribute invocations must be \
689 of the form `#[foo]` or `#[foo(..)]`, the macro name must only be \
690 followed by a delimiter token");
691 TokenStream::default()
694 fn gate_proc_macro_attr_item(&self, span: Span, item: &Annotatable) {
695 let (kind, gate) = match *item {
696 Annotatable::Item(ref item) => {
698 ItemKind::Mod(_) if self.cx.ecfg.proc_macro_hygiene() => return,
699 ItemKind::Mod(_) => ("modules", sym::proc_macro_hygiene),
703 Annotatable::TraitItem(_) => return,
704 Annotatable::ImplItem(_) => return,
705 Annotatable::ForeignItem(_) => return,
706 Annotatable::Stmt(_) |
707 Annotatable::Expr(_) if self.cx.ecfg.proc_macro_hygiene() => return,
708 Annotatable::Stmt(_) => ("statements", sym::proc_macro_hygiene),
709 Annotatable::Expr(_) => ("expressions", sym::proc_macro_hygiene),
711 | Annotatable::Field(..)
712 | Annotatable::FieldPat(..)
713 | Annotatable::GenericParam(..)
714 | Annotatable::Param(..)
715 | Annotatable::StructField(..)
716 | Annotatable::Variant(..)
717 => panic!("unexpected annotatable"),
724 &format!("custom attributes cannot be applied to {}", kind),
728 fn gate_proc_macro_expansion_kind(&self, span: Span, kind: AstFragmentKind) {
729 let kind = match kind {
730 AstFragmentKind::Expr |
731 AstFragmentKind::OptExpr => "expressions",
732 AstFragmentKind::Pat => "patterns",
733 AstFragmentKind::Stmts => "statements",
734 AstFragmentKind::Ty |
735 AstFragmentKind::Items |
736 AstFragmentKind::TraitItems |
737 AstFragmentKind::ImplItems |
738 AstFragmentKind::ForeignItems => return,
739 AstFragmentKind::Arms
740 | AstFragmentKind::Fields
741 | AstFragmentKind::FieldPats
742 | AstFragmentKind::GenericParams
743 | AstFragmentKind::Params
744 | AstFragmentKind::StructFields
745 | AstFragmentKind::Variants
746 => panic!("unexpected AST fragment kind"),
748 if self.cx.ecfg.proc_macro_hygiene() {
753 sym::proc_macro_hygiene,
756 &format!("procedural macros cannot be expanded to {}", kind),
760 fn parse_ast_fragment(
763 kind: AstFragmentKind,
767 let mut parser = self.cx.new_parser_from_tts(toks);
768 match parse_ast_fragment(&mut parser, kind, false) {
770 ensure_complete_parse(&mut parser, path, kind.name(), span);
775 annotate_err_with_kind(&mut err, kind, span);
777 self.cx.trace_macros_diag();
784 pub fn parse_ast_fragment<'a>(
785 this: &mut Parser<'a>,
786 kind: AstFragmentKind,
787 macro_legacy_warnings: bool,
788 ) -> PResult<'a, AstFragment> {
790 AstFragmentKind::Items => {
791 let mut items = SmallVec::new();
792 while let Some(item) = this.parse_item()? {
795 AstFragment::Items(items)
797 AstFragmentKind::TraitItems => {
798 let mut items = SmallVec::new();
799 while this.token != token::Eof {
800 items.push(this.parse_trait_item(&mut false)?);
802 AstFragment::TraitItems(items)
804 AstFragmentKind::ImplItems => {
805 let mut items = SmallVec::new();
806 while this.token != token::Eof {
807 items.push(this.parse_impl_item(&mut false)?);
809 AstFragment::ImplItems(items)
811 AstFragmentKind::ForeignItems => {
812 let mut items = SmallVec::new();
813 while this.token != token::Eof {
814 items.push(this.parse_foreign_item(DUMMY_SP)?);
816 AstFragment::ForeignItems(items)
818 AstFragmentKind::Stmts => {
819 let mut stmts = SmallVec::new();
820 while this.token != token::Eof &&
821 // won't make progress on a `}`
822 this.token != token::CloseDelim(token::Brace) {
823 if let Some(stmt) = this.parse_full_stmt(macro_legacy_warnings)? {
827 AstFragment::Stmts(stmts)
829 AstFragmentKind::Expr => AstFragment::Expr(this.parse_expr()?),
830 AstFragmentKind::OptExpr => {
831 if this.token != token::Eof {
832 AstFragment::OptExpr(Some(this.parse_expr()?))
834 AstFragment::OptExpr(None)
837 AstFragmentKind::Ty => AstFragment::Ty(this.parse_ty()?),
838 AstFragmentKind::Pat => AstFragment::Pat(this.parse_pat(None)?),
839 AstFragmentKind::Arms
840 | AstFragmentKind::Fields
841 | AstFragmentKind::FieldPats
842 | AstFragmentKind::GenericParams
843 | AstFragmentKind::Params
844 | AstFragmentKind::StructFields
845 | AstFragmentKind::Variants
846 => panic!("unexpected AST fragment kind"),
850 pub fn ensure_complete_parse<'a>(
851 this: &mut Parser<'a>,
856 if this.token != token::Eof {
857 let msg = format!("macro expansion ignores token `{}` and any following",
858 this.this_token_to_string());
859 // Avoid emitting backtrace info twice.
860 let def_site_span = this.token.span.with_ctxt(SyntaxContext::root());
861 let mut err = this.struct_span_err(def_site_span, &msg);
862 err.span_label(span, "caused by the macro expansion here");
864 "the usage of `{}!` is likely invalid in {} context",
865 pprust::path_to_string(macro_path),
869 let semi_span = this.sess.source_map().next_point(span);
871 let semi_full_span = semi_span.to(this.sess.source_map().next_point(semi_span));
872 match this.sess.source_map().span_to_snippet(semi_full_span) {
873 Ok(ref snippet) if &snippet[..] != ";" && kind_name == "expression" => {
876 "you might be missing a semicolon here",
878 Applicability::MaybeIncorrect,
887 struct InvocationCollector<'a, 'b> {
888 cx: &'a mut ExtCtxt<'b>,
889 cfg: StripUnconfigured<'a>,
890 invocations: Vec<Invocation>,
894 impl<'a, 'b> InvocationCollector<'a, 'b> {
895 fn collect(&mut self, fragment_kind: AstFragmentKind, kind: InvocationKind) -> AstFragment {
896 // Expansion data for all the collected invocations is set upon their resolution,
897 // with exception of the derive container case which is not resolved and can get
898 // its expansion data immediately.
899 let expn_data = match &kind {
900 InvocationKind::DeriveContainer { item, .. } => Some(ExpnData {
901 parent: self.cx.current_expansion.id,
903 ExpnKind::Macro(MacroKind::Attr, sym::derive),
904 item.span(), self.cx.parse_sess.edition,
909 let expn_id = ExpnId::fresh(expn_data);
910 self.invocations.push(Invocation {
913 expansion_data: ExpansionData {
915 depth: self.cx.current_expansion.depth + 1,
916 ..self.cx.current_expansion.clone()
919 placeholder(fragment_kind, NodeId::placeholder_from_expn_id(expn_id))
922 fn collect_bang(&mut self, mac: ast::Mac, span: Span, kind: AstFragmentKind) -> AstFragment {
923 self.collect(kind, InvocationKind::Bang { mac, span })
926 fn collect_attr(&mut self,
927 attr: Option<ast::Attribute>,
930 kind: AstFragmentKind,
933 self.collect(kind, match attr {
934 Some(attr) => InvocationKind::Attr { attr, item, derives, after_derive },
935 None => InvocationKind::DeriveContainer { derives, item },
939 fn find_attr_invoc(&self, attrs: &mut Vec<ast::Attribute>, after_derive: &mut bool)
940 -> Option<ast::Attribute> {
941 let attr = attrs.iter()
943 if a.path == sym::derive {
944 *after_derive = true;
946 !attr::is_known(a) && !is_builtin_attr(a)
948 .map(|i| attrs.remove(i));
949 if let Some(attr) = &attr {
950 if !self.cx.ecfg.custom_inner_attributes() &&
951 attr.style == ast::AttrStyle::Inner && attr.path != sym::test {
952 emit_feature_err(&self.cx.parse_sess, sym::custom_inner_attributes,
953 attr.span, GateIssue::Language,
954 "non-builtin inner attributes are unstable");
960 /// If `item` is an attr invocation, remove and return the macro attribute and derive traits.
961 fn classify_item<T>(&mut self, item: &mut T)
962 -> (Option<ast::Attribute>, Vec<Path>, /* after_derive */ bool)
965 let (mut attr, mut traits, mut after_derive) = (None, Vec::new(), false);
967 item.visit_attrs(|mut attrs| {
968 attr = self.find_attr_invoc(&mut attrs, &mut after_derive);
969 traits = collect_derives(&mut self.cx, &mut attrs);
972 (attr, traits, after_derive)
975 /// Alternative to `classify_item()` that ignores `#[derive]` so invocations fallthrough
976 /// to the unused-attributes lint (making it an error on statements and expressions
977 /// is a breaking change)
978 fn classify_nonitem<T: HasAttrs>(&mut self, nonitem: &mut T)
979 -> (Option<ast::Attribute>, /* after_derive */ bool) {
980 let (mut attr, mut after_derive) = (None, false);
982 nonitem.visit_attrs(|mut attrs| {
983 attr = self.find_attr_invoc(&mut attrs, &mut after_derive);
989 fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
990 self.cfg.configure(node)
993 // Detect use of feature-gated or invalid attributes on macro invocations
994 // since they will not be detected after macro expansion.
995 fn check_attributes(&mut self, attrs: &[ast::Attribute]) {
996 let features = self.cx.ecfg.features.unwrap();
997 for attr in attrs.iter() {
998 feature_gate::check_attribute(attr, self.cx.parse_sess, features);
1000 // macros are expanded before any lint passes so this warning has to be hardcoded
1001 if attr.path == sym::derive {
1002 self.cx.struct_span_warn(attr.span, "`#[derive]` does nothing on macro invocations")
1003 .note("this may become a hard error in a future release")
1010 impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
1011 fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
1012 self.cfg.configure_expr(expr);
1013 visit_clobber(expr.deref_mut(), |mut expr| {
1014 self.cfg.configure_expr_kind(&mut expr.kind);
1016 // ignore derives so they remain unused
1017 let (attr, after_derive) = self.classify_nonitem(&mut expr);
1020 // Collect the invoc regardless of whether or not attributes are permitted here
1021 // expansion will eat the attribute so it won't error later.
1022 attr.as_ref().map(|a| self.cfg.maybe_emit_expr_attr_err(a));
1024 // AstFragmentKind::Expr requires the macro to emit an expression.
1025 return self.collect_attr(attr, vec![], Annotatable::Expr(P(expr)),
1026 AstFragmentKind::Expr, after_derive)
1031 if let ast::ExprKind::Mac(mac) = expr.kind {
1032 self.check_attributes(&expr.attrs);
1033 self.collect_bang(mac, expr.span, AstFragmentKind::Expr)
1037 noop_visit_expr(&mut expr, self);
1043 fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
1044 let mut arm = configure!(self, arm);
1046 let (attr, traits, after_derive) = self.classify_item(&mut arm);
1047 if attr.is_some() || !traits.is_empty() {
1048 return self.collect_attr(attr, traits, Annotatable::Arm(arm),
1049 AstFragmentKind::Arms, after_derive)
1053 noop_flat_map_arm(arm, self)
1056 fn flat_map_field(&mut self, field: ast::Field) -> SmallVec<[ast::Field; 1]> {
1057 let mut field = configure!(self, field);
1059 let (attr, traits, after_derive) = self.classify_item(&mut field);
1060 if attr.is_some() || !traits.is_empty() {
1061 return self.collect_attr(attr, traits, Annotatable::Field(field),
1062 AstFragmentKind::Fields, after_derive)
1066 noop_flat_map_field(field, self)
1069 fn flat_map_field_pattern(&mut self, fp: ast::FieldPat) -> SmallVec<[ast::FieldPat; 1]> {
1070 let mut fp = configure!(self, fp);
1072 let (attr, traits, after_derive) = self.classify_item(&mut fp);
1073 if attr.is_some() || !traits.is_empty() {
1074 return self.collect_attr(attr, traits, Annotatable::FieldPat(fp),
1075 AstFragmentKind::FieldPats, after_derive)
1076 .make_field_patterns();
1079 noop_flat_map_field_pattern(fp, self)
1082 fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
1083 let mut p = configure!(self, p);
1085 let (attr, traits, after_derive) = self.classify_item(&mut p);
1086 if attr.is_some() || !traits.is_empty() {
1087 return self.collect_attr(attr, traits, Annotatable::Param(p),
1088 AstFragmentKind::Params, after_derive)
1092 noop_flat_map_param(p, self)
1095 fn flat_map_struct_field(&mut self, sf: ast::StructField) -> SmallVec<[ast::StructField; 1]> {
1096 let mut sf = configure!(self, sf);
1098 let (attr, traits, after_derive) = self.classify_item(&mut sf);
1099 if attr.is_some() || !traits.is_empty() {
1100 return self.collect_attr(attr, traits, Annotatable::StructField(sf),
1101 AstFragmentKind::StructFields, after_derive)
1102 .make_struct_fields();
1105 noop_flat_map_struct_field(sf, self)
1108 fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
1109 let mut variant = configure!(self, variant);
1111 let (attr, traits, after_derive) = self.classify_item(&mut variant);
1112 if attr.is_some() || !traits.is_empty() {
1113 return self.collect_attr(attr, traits, Annotatable::Variant(variant),
1114 AstFragmentKind::Variants, after_derive)
1118 noop_flat_map_variant(variant, self)
1121 fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
1122 let expr = configure!(self, expr);
1123 expr.filter_map(|mut expr| {
1124 self.cfg.configure_expr_kind(&mut expr.kind);
1126 // Ignore derives so they remain unused.
1127 let (attr, after_derive) = self.classify_nonitem(&mut expr);
1130 attr.as_ref().map(|a| self.cfg.maybe_emit_expr_attr_err(a));
1132 return self.collect_attr(attr, vec![], Annotatable::Expr(P(expr)),
1133 AstFragmentKind::OptExpr, after_derive)
1135 .map(|expr| expr.into_inner())
1138 if let ast::ExprKind::Mac(mac) = expr.kind {
1139 self.check_attributes(&expr.attrs);
1140 self.collect_bang(mac, expr.span, AstFragmentKind::OptExpr)
1142 .map(|expr| expr.into_inner())
1144 Some({ noop_visit_expr(&mut expr, self); expr })
1149 fn visit_pat(&mut self, pat: &mut P<ast::Pat>) {
1150 self.cfg.configure_pat(pat);
1152 PatKind::Mac(_) => {}
1153 _ => return noop_visit_pat(pat, self),
1156 visit_clobber(pat, |mut pat| {
1157 match mem::replace(&mut pat.kind, PatKind::Wild) {
1158 PatKind::Mac(mac) =>
1159 self.collect_bang(mac, pat.span, AstFragmentKind::Pat).make_pat(),
1160 _ => unreachable!(),
1165 fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
1166 let mut stmt = configure!(self, stmt);
1168 // we'll expand attributes on expressions separately
1169 if !stmt.is_expr() {
1170 let (attr, derives, after_derive) = if stmt.is_item() {
1171 self.classify_item(&mut stmt)
1173 // ignore derives on non-item statements so it falls through
1174 // to the unused-attributes lint
1175 let (attr, after_derive) = self.classify_nonitem(&mut stmt);
1176 (attr, vec![], after_derive)
1179 if attr.is_some() || !derives.is_empty() {
1180 return self.collect_attr(attr, derives, Annotatable::Stmt(P(stmt)),
1181 AstFragmentKind::Stmts, after_derive).make_stmts();
1185 if let StmtKind::Mac(mac) = stmt.kind {
1186 let (mac, style, attrs) = mac.into_inner();
1187 self.check_attributes(&attrs);
1188 let mut placeholder = self.collect_bang(mac, stmt.span, AstFragmentKind::Stmts)
1191 // If this is a macro invocation with a semicolon, then apply that
1192 // semicolon to the final statement produced by expansion.
1193 if style == MacStmtStyle::Semicolon {
1194 if let Some(stmt) = placeholder.pop() {
1195 placeholder.push(stmt.add_trailing_semicolon());
1202 // The placeholder expander gives ids to statements, so we avoid folding the id here.
1203 let ast::Stmt { id, kind, span } = stmt;
1204 noop_flat_map_stmt_kind(kind, self).into_iter().map(|kind| {
1205 ast::Stmt { id, kind, span }
1210 fn visit_block(&mut self, block: &mut P<Block>) {
1211 let old_directory_ownership = self.cx.current_expansion.directory_ownership;
1212 self.cx.current_expansion.directory_ownership = DirectoryOwnership::UnownedViaBlock;
1213 noop_visit_block(block, self);
1214 self.cx.current_expansion.directory_ownership = old_directory_ownership;
1217 fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
1218 let mut item = configure!(self, item);
1220 let (attr, traits, after_derive) = self.classify_item(&mut item);
1221 if attr.is_some() || !traits.is_empty() {
1222 return self.collect_attr(attr, traits, Annotatable::Item(item),
1223 AstFragmentKind::Items, after_derive).make_items();
1227 ast::ItemKind::Mac(..) => {
1228 self.check_attributes(&item.attrs);
1229 item.and_then(|item| match item.kind {
1230 ItemKind::Mac(mac) => self.collect(
1231 AstFragmentKind::Items, InvocationKind::Bang { mac, span: item.span }
1233 _ => unreachable!(),
1236 ast::ItemKind::Mod(ast::Mod { inner, .. }) => {
1237 if item.ident == Ident::invalid() {
1238 return noop_flat_map_item(item, self);
1241 let orig_directory_ownership = self.cx.current_expansion.directory_ownership;
1242 let mut module = (*self.cx.current_expansion.module).clone();
1243 module.mod_path.push(item.ident);
1245 // Detect if this is an inline module (`mod m { ... }` as opposed to `mod m;`).
1246 // In the non-inline case, `inner` is never the dummy span (cf. `parse_item_mod`).
1247 // Thus, if `inner` is the dummy span, we know the module is inline.
1248 let inline_module = item.span.contains(inner) || inner.is_dummy();
1251 if let Some(path) = attr::first_attr_value_str_by_name(&item.attrs, sym::path) {
1252 self.cx.current_expansion.directory_ownership =
1253 DirectoryOwnership::Owned { relative: None };
1254 module.directory.push(&*path.as_str());
1256 module.directory.push(&*item.ident.as_str());
1259 let path = self.cx.parse_sess.source_map().span_to_unmapped_path(inner);
1260 let mut path = match path {
1261 FileName::Real(path) => path,
1262 other => PathBuf::from(other.to_string()),
1264 let directory_ownership = match path.file_name().unwrap().to_str() {
1265 Some("mod.rs") => DirectoryOwnership::Owned { relative: None },
1266 Some(_) => DirectoryOwnership::Owned {
1267 relative: Some(item.ident),
1269 None => DirectoryOwnership::UnownedViaMod(false),
1272 module.directory = path;
1273 self.cx.current_expansion.directory_ownership = directory_ownership;
1277 mem::replace(&mut self.cx.current_expansion.module, Rc::new(module));
1278 let result = noop_flat_map_item(item, self);
1279 self.cx.current_expansion.module = orig_module;
1280 self.cx.current_expansion.directory_ownership = orig_directory_ownership;
1284 _ => noop_flat_map_item(item, self),
1288 fn flat_map_trait_item(&mut self, item: ast::TraitItem) -> SmallVec<[ast::TraitItem; 1]> {
1289 let mut item = configure!(self, item);
1291 let (attr, traits, after_derive) = self.classify_item(&mut item);
1292 if attr.is_some() || !traits.is_empty() {
1293 return self.collect_attr(attr, traits, Annotatable::TraitItem(P(item)),
1294 AstFragmentKind::TraitItems, after_derive).make_trait_items()
1298 ast::TraitItemKind::Macro(mac) => {
1299 let ast::TraitItem { attrs, span, .. } = item;
1300 self.check_attributes(&attrs);
1301 self.collect_bang(mac, span, AstFragmentKind::TraitItems).make_trait_items()
1303 _ => noop_flat_map_trait_item(item, self),
1307 fn flat_map_impl_item(&mut self, item: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]> {
1308 let mut item = configure!(self, item);
1310 let (attr, traits, after_derive) = self.classify_item(&mut item);
1311 if attr.is_some() || !traits.is_empty() {
1312 return self.collect_attr(attr, traits, Annotatable::ImplItem(P(item)),
1313 AstFragmentKind::ImplItems, after_derive).make_impl_items();
1317 ast::ImplItemKind::Macro(mac) => {
1318 let ast::ImplItem { attrs, span, .. } = item;
1319 self.check_attributes(&attrs);
1320 self.collect_bang(mac, span, AstFragmentKind::ImplItems).make_impl_items()
1322 _ => noop_flat_map_impl_item(item, self),
1326 fn visit_ty(&mut self, ty: &mut P<ast::Ty>) {
1328 ast::TyKind::Mac(_) => {}
1329 _ => return noop_visit_ty(ty, self),
1332 visit_clobber(ty, |mut ty| {
1333 match mem::replace(&mut ty.kind, ast::TyKind::Err) {
1334 ast::TyKind::Mac(mac) =>
1335 self.collect_bang(mac, ty.span, AstFragmentKind::Ty).make_ty(),
1336 _ => unreachable!(),
1341 fn visit_foreign_mod(&mut self, foreign_mod: &mut ast::ForeignMod) {
1342 self.cfg.configure_foreign_mod(foreign_mod);
1343 noop_visit_foreign_mod(foreign_mod, self);
1346 fn flat_map_foreign_item(&mut self, mut foreign_item: ast::ForeignItem)
1347 -> SmallVec<[ast::ForeignItem; 1]>
1349 let (attr, traits, after_derive) = self.classify_item(&mut foreign_item);
1351 if attr.is_some() || !traits.is_empty() {
1352 return self.collect_attr(attr, traits, Annotatable::ForeignItem(P(foreign_item)),
1353 AstFragmentKind::ForeignItems, after_derive)
1354 .make_foreign_items();
1357 if let ast::ForeignItemKind::Macro(mac) = foreign_item.kind {
1358 self.check_attributes(&foreign_item.attrs);
1359 return self.collect_bang(mac, foreign_item.span, AstFragmentKind::ForeignItems)
1360 .make_foreign_items();
1363 noop_flat_map_foreign_item(foreign_item, self)
1366 fn visit_item_kind(&mut self, item: &mut ast::ItemKind) {
1368 ast::ItemKind::MacroDef(..) => {}
1370 self.cfg.configure_item_kind(item);
1371 noop_visit_item_kind(item, self);
1376 fn flat_map_generic_param(
1378 param: ast::GenericParam
1379 ) -> SmallVec<[ast::GenericParam; 1]>
1381 let mut param = configure!(self, param);
1383 let (attr, traits, after_derive) = self.classify_item(&mut param);
1384 if attr.is_some() || !traits.is_empty() {
1385 return self.collect_attr(attr, traits, Annotatable::GenericParam(param),
1386 AstFragmentKind::GenericParams, after_derive)
1387 .make_generic_params();
1390 noop_flat_map_generic_param(param, self)
1393 fn visit_attribute(&mut self, at: &mut ast::Attribute) {
1394 // turn `#[doc(include="filename")]` attributes into `#[doc(include(file="filename",
1395 // contents="file contents")]` attributes
1396 if !at.check_name(sym::doc) {
1397 return noop_visit_attribute(at, self);
1400 if let Some(list) = at.meta_item_list() {
1401 if !list.iter().any(|it| it.check_name(sym::include)) {
1402 return noop_visit_attribute(at, self);
1405 let mut items = vec![];
1407 for mut it in list {
1408 if !it.check_name(sym::include) {
1409 items.push({ noop_visit_meta_list_item(&mut it, self); it });
1413 if let Some(file) = it.value_str() {
1414 let err_count = self.cx.parse_sess.span_diagnostic.err_count();
1415 self.check_attributes(slice::from_ref(at));
1416 if self.cx.parse_sess.span_diagnostic.err_count() > err_count {
1417 // avoid loading the file if they haven't enabled the feature
1418 return noop_visit_attribute(at, self);
1421 let filename = match self.cx.resolve_path(&*file.as_str(), it.span()) {
1422 Ok(filename) => filename,
1429 match self.cx.source_map().load_file(&filename) {
1430 Ok(source_file) => {
1431 let src = source_file.src.as_ref()
1432 .expect("freshly loaded file should have a source");
1433 let src_interned = Symbol::intern(src.as_str());
1435 let include_info = vec![
1436 ast::NestedMetaItem::MetaItem(
1437 attr::mk_name_value_item_str(
1438 Ident::with_dummy_span(sym::file),
1443 ast::NestedMetaItem::MetaItem(
1444 attr::mk_name_value_item_str(
1445 Ident::with_dummy_span(sym::contents),
1452 let include_ident = Ident::with_dummy_span(sym::include);
1453 let item = attr::mk_list_item(include_ident, include_info);
1454 items.push(ast::NestedMetaItem::MetaItem(item));
1459 .and_then(|item| item.name_value_literal())
1462 if e.kind() == ErrorKind::InvalidData {
1466 &format!("{} wasn't a utf-8 file", filename.display()),
1468 .span_label(lit.span, "contains invalid utf-8")
1471 let mut err = self.cx.struct_span_err(
1473 &format!("couldn't read {}: {}", filename.display(), e),
1475 err.span_label(lit.span, "couldn't read file");
1482 let mut err = self.cx.struct_span_err(
1484 &format!("expected path to external documentation"),
1487 // Check if the user erroneously used `doc(include(...))` syntax.
1488 let literal = it.meta_item_list().and_then(|list| {
1489 if list.len() == 1 {
1490 list[0].literal().map(|literal| &literal.kind)
1496 let (path, applicability) = match &literal {
1497 Some(LitKind::Str(path, ..)) => {
1498 (path.to_string(), Applicability::MachineApplicable)
1500 _ => (String::from("<path>"), Applicability::HasPlaceholders),
1503 err.span_suggestion(
1505 "provide a file path with `=`",
1506 format!("include = \"{}\"", path),
1514 let meta = attr::mk_list_item(Ident::with_dummy_span(sym::doc), items);
1515 *at = attr::Attribute {
1516 item: AttrItem { path: meta.path, tokens: meta.kind.tokens(meta.span) },
1520 is_sugared_doc: false,
1523 noop_visit_attribute(at, self)
1527 fn visit_id(&mut self, id: &mut ast::NodeId) {
1529 debug_assert_eq!(*id, ast::DUMMY_NODE_ID);
1530 *id = self.cx.resolver.next_node_id()
1534 fn visit_fn_decl(&mut self, mut fn_decl: &mut P<ast::FnDecl>) {
1535 self.cfg.configure_fn_decl(&mut fn_decl);
1536 noop_visit_fn_decl(fn_decl, self);
1540 pub struct ExpansionConfig<'feat> {
1541 pub crate_name: String,
1542 pub features: Option<&'feat Features>,
1543 pub recursion_limit: usize,
1544 pub trace_mac: bool,
1545 pub should_test: bool, // If false, strip `#[test]` nodes
1546 pub single_step: bool,
1547 pub keep_macs: bool,
1550 impl<'feat> ExpansionConfig<'feat> {
1551 pub fn default(crate_name: String) -> ExpansionConfig<'static> {
1555 recursion_limit: 1024,
1563 fn proc_macro_hygiene(&self) -> bool {
1564 self.features.map_or(false, |features| features.proc_macro_hygiene)
1566 fn custom_inner_attributes(&self) -> bool {
1567 self.features.map_or(false, |features| features.custom_inner_attributes)