1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use ast::{self, Block, Ident, NodeId, PatKind, Path};
12 use ast::{MacStmtStyle, StmtKind, ItemKind};
13 use attr::{self, HasAttrs};
14 use codemap::{ExpnInfo, NameAndSpan, MacroBang, MacroAttribute, dummy_spanned, respan};
15 use config::{is_test_or_bench, StripUnconfigured};
16 use errors::FatalError;
18 use ext::derive::{add_derived_markers, collect_derives};
19 use ext::hygiene::{Mark, SyntaxContext};
20 use ext::placeholders::{placeholder, PlaceholderExpander};
21 use feature_gate::{self, Features, GateIssue, is_builtin_attr, emit_feature_err};
24 use parse::{DirectoryOwnership, PResult};
25 use parse::token::{self, Token};
26 use parse::parser::Parser;
30 use syntax_pos::{Span, DUMMY_SP, FileName};
31 use syntax_pos::hygiene::ExpnFormat;
32 use tokenstream::{TokenStream, TokenTree};
33 use util::small_vector::SmallVector;
36 use std::collections::HashMap;
41 use std::path::PathBuf;
43 macro_rules! expansions {
44 ($($kind:ident: $ty:ty [$($vec:ident, $ty_elt:ty)*], $kind_name:expr, .$make:ident,
45 $(.$fold:ident)* $(lift .$fold_elt:ident)*,
46 $(.$visit:ident)* $(lift .$visit_elt:ident)*;)*) => {
47 #[derive(Copy, Clone, PartialEq, Eq)]
48 pub enum ExpansionKind { OptExpr, $( $kind, )* }
49 pub enum Expansion { OptExpr(Option<P<ast::Expr>>), $( $kind($ty), )* }
52 pub fn name(self) -> &'static str {
54 ExpansionKind::OptExpr => "expression",
55 $( ExpansionKind::$kind => $kind_name, )*
59 fn make_from<'a>(self, result: Box<MacResult + 'a>) -> Option<Expansion> {
61 ExpansionKind::OptExpr => result.make_expr().map(Some).map(Expansion::OptExpr),
62 $( ExpansionKind::$kind => result.$make().map(Expansion::$kind), )*
68 pub fn make_opt_expr(self) -> Option<P<ast::Expr>> {
70 Expansion::OptExpr(expr) => expr,
71 _ => panic!("Expansion::make_* called on the wrong kind of expansion"),
74 $( pub fn $make(self) -> $ty {
76 Expansion::$kind(ast) => ast,
77 _ => panic!("Expansion::make_* called on the wrong kind of expansion"),
81 pub fn fold_with<F: Folder>(self, folder: &mut F) -> Self {
82 use self::Expansion::*;
84 OptExpr(expr) => OptExpr(expr.and_then(|expr| folder.fold_opt_expr(expr))),
85 $($( $kind(ast) => $kind(folder.$fold(ast)), )*)*
87 $kind(ast.into_iter().flat_map(|ast| folder.$fold_elt(ast)).collect())
92 pub fn visit_with<'a, V: Visitor<'a>>(&'a self, visitor: &mut V) {
94 Expansion::OptExpr(Some(ref expr)) => visitor.visit_expr(expr),
95 Expansion::OptExpr(None) => {}
96 $($( Expansion::$kind(ref ast) => visitor.$visit(ast), )*)*
97 $($( Expansion::$kind(ref ast) => for ast in &ast[..] {
98 visitor.$visit_elt(ast);
104 impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
105 fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
106 self.expand(Expansion::OptExpr(Some(expr))).make_opt_expr()
108 $($(fn $fold(&mut self, node: $ty) -> $ty {
109 self.expand(Expansion::$kind(node)).$make()
111 $($(fn $fold_elt(&mut self, node: $ty_elt) -> $ty {
112 self.expand(Expansion::$kind(SmallVector::one(node))).$make()
116 impl<'a> MacResult for ::ext::tt::macro_rules::ParserAnyMacro<'a> {
117 $(fn $make(self: Box<::ext::tt::macro_rules::ParserAnyMacro<'a>>) -> Option<$ty> {
118 Some(self.make(ExpansionKind::$kind).$make())
125 Expr: P<ast::Expr> [], "expression", .make_expr, .fold_expr, .visit_expr;
126 Pat: P<ast::Pat> [], "pattern", .make_pat, .fold_pat, .visit_pat;
127 Ty: P<ast::Ty> [], "type", .make_ty, .fold_ty, .visit_ty;
128 Stmts: SmallVector<ast::Stmt> [SmallVector, ast::Stmt],
129 "statement", .make_stmts, lift .fold_stmt, lift .visit_stmt;
130 Items: SmallVector<P<ast::Item>> [SmallVector, P<ast::Item>],
131 "item", .make_items, lift .fold_item, lift .visit_item;
132 TraitItems: SmallVector<ast::TraitItem> [SmallVector, ast::TraitItem],
133 "trait item", .make_trait_items, lift .fold_trait_item, lift .visit_trait_item;
134 ImplItems: SmallVector<ast::ImplItem> [SmallVector, ast::ImplItem],
135 "impl item", .make_impl_items, lift .fold_impl_item, lift .visit_impl_item;
136 ForeignItems: SmallVector<ast::ForeignItem> [SmallVector, ast::ForeignItem],
137 "foreign item", .make_foreign_items, lift .fold_foreign_item, lift .visit_foreign_item;
141 fn dummy(self, span: Span) -> Option<Expansion> {
142 self.make_from(DummyResult::any(span))
145 fn expect_from_annotatables<I: IntoIterator<Item = Annotatable>>(self, items: I) -> Expansion {
146 let mut items = items.into_iter();
148 ExpansionKind::Items =>
149 Expansion::Items(items.map(Annotatable::expect_item).collect()),
150 ExpansionKind::ImplItems =>
151 Expansion::ImplItems(items.map(Annotatable::expect_impl_item).collect()),
152 ExpansionKind::TraitItems =>
153 Expansion::TraitItems(items.map(Annotatable::expect_trait_item).collect()),
154 ExpansionKind::ForeignItems =>
155 Expansion::ForeignItems(items.map(Annotatable::expect_foreign_item).collect()),
156 ExpansionKind::Stmts => Expansion::Stmts(items.map(Annotatable::expect_stmt).collect()),
157 ExpansionKind::Expr => Expansion::Expr(
158 items.next().expect("expected exactly one expression").expect_expr()
160 ExpansionKind::OptExpr =>
161 Expansion::OptExpr(items.next().map(Annotatable::expect_expr)),
162 ExpansionKind::Pat | ExpansionKind::Ty =>
163 panic!("patterns and types aren't annotatable"),
168 fn macro_bang_format(path: &ast::Path) -> ExpnFormat {
169 // We don't want to format a path using pretty-printing,
170 // `format!("{}", path)`, because that tries to insert
171 // line-breaks and is slow.
172 let mut path_str = String::with_capacity(64);
173 for (i, segment) in path.segments.iter().enumerate() {
175 path_str.push_str("::");
178 if segment.ident.name != keywords::CrateRoot.name() &&
179 segment.ident.name != keywords::DollarCrate.name()
181 path_str.push_str(&segment.ident.name.as_str())
185 MacroBang(Symbol::intern(&path_str))
188 pub struct Invocation {
189 pub kind: InvocationKind,
190 expansion_kind: ExpansionKind,
191 pub expansion_data: ExpansionData,
194 pub enum InvocationKind {
197 ident: Option<Ident>,
201 attr: Option<ast::Attribute>,
212 fn span(&self) -> Span {
214 InvocationKind::Bang { span, .. } => span,
215 InvocationKind::Attr { attr: Some(ref attr), .. } => attr.span,
216 InvocationKind::Attr { attr: None, .. } => DUMMY_SP,
217 InvocationKind::Derive { ref path, .. } => path.span,
222 pub struct MacroExpander<'a, 'b:'a> {
223 pub cx: &'a mut ExtCtxt<'b>,
224 monotonic: bool, // c.f. `cx.monotonic_expander()`
227 impl<'a, 'b> MacroExpander<'a, 'b> {
228 pub fn new(cx: &'a mut ExtCtxt<'b>, monotonic: bool) -> Self {
229 MacroExpander { cx: cx, monotonic: monotonic }
232 pub fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
233 let mut module = ModuleData {
234 mod_path: vec![Ident::from_str(&self.cx.ecfg.crate_name)],
235 directory: match self.cx.codemap().span_to_unmapped_path(krate.span) {
236 FileName::Real(path) => path,
237 other => PathBuf::from(other.to_string()),
240 module.directory.pop();
241 self.cx.root_path = module.directory.clone();
242 self.cx.current_expansion.module = Rc::new(module);
243 self.cx.current_expansion.crate_span = Some(krate.span);
245 let orig_mod_span = krate.module.inner;
247 let krate_item = Expansion::Items(SmallVector::one(P(ast::Item {
250 node: ast::ItemKind::Mod(krate.module),
251 ident: keywords::Invalid.ident(),
252 id: ast::DUMMY_NODE_ID,
253 vis: respan(krate.span.shrink_to_lo(), ast::VisibilityKind::Public),
257 match self.expand(krate_item).make_items().pop().map(P::into_inner) {
258 Some(ast::Item { attrs, node: ast::ItemKind::Mod(module), .. }) => {
260 krate.module = module;
263 // Resolution failed so we return an empty expansion
264 krate.attrs = vec![];
265 krate.module = ast::Mod {
266 inner: orig_mod_span,
272 self.cx.trace_macros_diag();
276 // Fully expand all the invocations in `expansion`.
277 fn expand(&mut self, expansion: Expansion) -> Expansion {
278 let orig_expansion_data = self.cx.current_expansion.clone();
279 self.cx.current_expansion.depth = 0;
281 let (expansion, mut invocations) = self.collect_invocations(expansion, &[]);
282 self.resolve_imports();
283 invocations.reverse();
285 let mut expansions = Vec::new();
286 let mut derives = HashMap::new();
287 let mut undetermined_invocations = Vec::new();
288 let (mut progress, mut force) = (false, !self.monotonic);
290 let mut invoc = if let Some(invoc) = invocations.pop() {
293 self.resolve_imports();
294 if undetermined_invocations.is_empty() { break }
295 invocations = mem::replace(&mut undetermined_invocations, Vec::new());
296 force = !mem::replace(&mut progress, false);
301 if self.monotonic { invoc.expansion_data.mark } else { orig_expansion_data.mark };
302 let ext = match self.cx.resolver.resolve_invoc(&mut invoc, scope, force) {
303 Ok(ext) => Some(ext),
304 Err(Determinacy::Determined) => None,
305 Err(Determinacy::Undetermined) => {
306 undetermined_invocations.push(invoc);
312 let ExpansionData { depth, mark, .. } = invoc.expansion_data;
313 self.cx.current_expansion = invoc.expansion_data.clone();
315 self.cx.current_expansion.mark = scope;
316 // FIXME(jseyfried): Refactor out the following logic
317 let (expansion, new_invocations) = if let Some(ext) = ext {
318 if let Some(ext) = ext {
319 let dummy = invoc.expansion_kind.dummy(invoc.span()).unwrap();
320 let expansion = self.expand_invoc(invoc, &*ext).unwrap_or(dummy);
321 self.collect_invocations(expansion, &[])
322 } else if let InvocationKind::Attr { attr: None, traits, item } = invoc.kind {
323 if !item.derive_allowed() {
324 let attr = attr::find_by_name(item.attrs(), "derive")
325 .expect("`derive` attribute should exist");
326 let span = attr.span;
327 let mut err = self.cx.mut_span_err(span,
328 "`derive` may only be applied to \
329 structs, enums and unions");
330 if let ast::AttrStyle::Inner = attr.style {
331 let trait_list = traits.iter()
332 .map(|t| format!("{}", t)).collect::<Vec<_>>();
333 let suggestion = format!("#[derive({})]", trait_list.join(", "));
334 err.span_suggestion(span, "try an outer attribute", suggestion);
339 let item = self.fully_configure(item)
340 .map_attrs(|mut attrs| { attrs.retain(|a| a.path != "derive"); attrs });
341 let item_with_markers =
342 add_derived_markers(&mut self.cx, item.span(), &traits, item.clone());
343 let derives = derives.entry(invoc.expansion_data.mark).or_insert_with(Vec::new);
345 for path in &traits {
346 let mark = Mark::fresh(self.cx.current_expansion.mark);
348 let item = match self.cx.resolver.resolve_macro(
349 Mark::root(), path, MacroKind::Derive, false) {
350 Ok(ext) => match *ext {
351 BuiltinDerive(..) => item_with_markers.clone(),
356 invocations.push(Invocation {
357 kind: InvocationKind::Derive { path: path.clone(), item: item },
358 expansion_kind: invoc.expansion_kind,
359 expansion_data: ExpansionData {
361 ..invoc.expansion_data.clone()
365 let expansion = invoc.expansion_kind
366 .expect_from_annotatables(::std::iter::once(item_with_markers));
367 self.collect_invocations(expansion, derives)
372 self.collect_invocations(invoc.expansion_kind.dummy(invoc.span()).unwrap(), &[])
375 if expansions.len() < depth {
376 expansions.push(Vec::new());
378 expansions[depth - 1].push((mark, expansion));
379 if !self.cx.ecfg.single_step {
380 invocations.extend(new_invocations.into_iter().rev());
384 self.cx.current_expansion = orig_expansion_data;
386 let mut placeholder_expander = PlaceholderExpander::new(self.cx, self.monotonic);
387 while let Some(expansions) = expansions.pop() {
388 for (mark, expansion) in expansions.into_iter().rev() {
389 let derives = derives.remove(&mark).unwrap_or_else(Vec::new);
390 placeholder_expander.add(NodeId::placeholder_from_mark(mark), expansion, derives);
394 expansion.fold_with(&mut placeholder_expander)
397 fn resolve_imports(&mut self) {
399 let err_count = self.cx.parse_sess.span_diagnostic.err_count();
400 self.cx.resolver.resolve_imports();
401 self.cx.resolve_err_count += self.cx.parse_sess.span_diagnostic.err_count() - err_count;
405 fn collect_invocations(&mut self, expansion: Expansion, derives: &[Mark])
406 -> (Expansion, Vec<Invocation>) {
408 let mut collector = InvocationCollector {
409 cfg: StripUnconfigured {
410 should_test: self.cx.ecfg.should_test,
411 sess: self.cx.parse_sess,
412 features: self.cx.ecfg.features,
415 invocations: Vec::new(),
416 monotonic: self.monotonic,
418 (expansion.fold_with(&mut collector), collector.invocations)
422 let err_count = self.cx.parse_sess.span_diagnostic.err_count();
423 let mark = self.cx.current_expansion.mark;
424 self.cx.resolver.visit_expansion(mark, &result.0, derives);
425 self.cx.resolve_err_count += self.cx.parse_sess.span_diagnostic.err_count() - err_count;
431 fn fully_configure(&mut self, item: Annotatable) -> Annotatable {
432 let mut cfg = StripUnconfigured {
433 should_test: self.cx.ecfg.should_test,
434 sess: self.cx.parse_sess,
435 features: self.cx.ecfg.features,
437 // Since the item itself has already been configured by the InvocationCollector,
438 // we know that fold result vector will contain exactly one element
440 Annotatable::Item(item) => {
441 Annotatable::Item(cfg.fold_item(item).pop().unwrap())
443 Annotatable::TraitItem(item) => {
444 Annotatable::TraitItem(item.map(|item| cfg.fold_trait_item(item).pop().unwrap()))
446 Annotatable::ImplItem(item) => {
447 Annotatable::ImplItem(item.map(|item| cfg.fold_impl_item(item).pop().unwrap()))
449 Annotatable::ForeignItem(item) => {
450 Annotatable::ForeignItem(
451 item.map(|item| cfg.fold_foreign_item(item).pop().unwrap())
454 Annotatable::Stmt(stmt) => {
455 Annotatable::Stmt(stmt.map(|stmt| cfg.fold_stmt(stmt).pop().unwrap()))
457 Annotatable::Expr(expr) => {
458 Annotatable::Expr(cfg.fold_expr(expr))
463 fn expand_invoc(&mut self, invoc: Invocation, ext: &SyntaxExtension) -> Option<Expansion> {
464 let result = match invoc.kind {
465 InvocationKind::Bang { .. } => self.expand_bang_invoc(invoc, ext)?,
466 InvocationKind::Attr { .. } => self.expand_attr_invoc(invoc, ext)?,
467 InvocationKind::Derive { .. } => self.expand_derive_invoc(invoc, ext)?,
470 if self.cx.current_expansion.depth > self.cx.ecfg.recursion_limit {
471 let info = self.cx.current_expansion.mark.expn_info().unwrap();
472 let suggested_limit = self.cx.ecfg.recursion_limit * 2;
473 let mut err = self.cx.struct_span_err(info.call_site,
474 &format!("recursion limit reached while expanding the macro `{}`",
475 info.callee.name()));
477 "consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
480 self.cx.trace_macros_diag();
487 fn expand_attr_invoc(&mut self,
489 ext: &SyntaxExtension)
490 -> Option<Expansion> {
491 let Invocation { expansion_kind: kind, .. } = invoc;
492 let (attr, item) = match invoc.kind {
493 InvocationKind::Attr { attr, item, .. } => (attr?, item),
497 attr::mark_used(&attr);
498 invoc.expansion_data.mark.set_expn_info(ExpnInfo {
499 call_site: attr.span,
500 callee: NameAndSpan {
501 format: MacroAttribute(Symbol::intern(&format!("{}", attr.path))),
503 allow_internal_unstable: false,
504 allow_internal_unsafe: false,
509 MultiModifier(ref mac) => {
510 let meta = attr.parse_meta(self.cx.parse_sess)
511 .map_err(|mut e| { e.emit(); }).ok()?;
512 let item = mac.expand(self.cx, attr.span, &meta, item);
513 Some(kind.expect_from_annotatables(item))
515 MultiDecorator(ref mac) => {
516 let mut items = Vec::new();
517 let meta = attr.parse_meta(self.cx.parse_sess)
518 .expect("derive meta should already have been parsed");
519 mac.expand(self.cx, attr.span, &meta, &item, &mut |item| items.push(item));
521 Some(kind.expect_from_annotatables(items))
523 AttrProcMacro(ref mac) => {
524 self.gate_proc_macro_attr_item(attr.span, &item);
525 let item_tok = TokenTree::Token(DUMMY_SP, Token::interpolated(match item {
526 Annotatable::Item(item) => token::NtItem(item),
527 Annotatable::TraitItem(item) => token::NtTraitItem(item.into_inner()),
528 Annotatable::ImplItem(item) => token::NtImplItem(item.into_inner()),
529 Annotatable::ForeignItem(item) => token::NtForeignItem(item.into_inner()),
530 Annotatable::Stmt(stmt) => token::NtStmt(stmt.into_inner()),
531 Annotatable::Expr(expr) => token::NtExpr(expr),
533 let input = self.extract_proc_macro_attr_input(attr.tokens, attr.span);
534 let tok_result = mac.expand(self.cx, attr.span, input, item_tok);
535 self.parse_expansion(tok_result, kind, &attr.path, attr.span)
537 ProcMacroDerive(..) | BuiltinDerive(..) => {
538 self.cx.span_err(attr.span, &format!("`{}` is a derive mode", attr.path));
539 self.cx.trace_macros_diag();
540 kind.dummy(attr.span)
543 let msg = &format!("macro `{}` may not be used in attributes", attr.path);
544 self.cx.span_err(attr.span, msg);
545 self.cx.trace_macros_diag();
546 kind.dummy(attr.span)
551 fn extract_proc_macro_attr_input(&self, tokens: TokenStream, span: Span) -> TokenStream {
552 let mut trees = tokens.trees();
554 Some(TokenTree::Delimited(_, delim)) => {
555 if trees.next().is_none() {
556 return delim.tts.into()
559 Some(TokenTree::Token(..)) => {}
560 None => return TokenStream::empty(),
562 self.cx.span_err(span, "custom attribute invocations must be \
563 of the form #[foo] or #[foo(..)], the macro name must only be \
564 followed by a delimiter token");
568 fn gate_proc_macro_attr_item(&self, span: Span, item: &Annotatable) {
569 let (kind, gate) = match *item {
570 Annotatable::Item(ref item) => {
572 ItemKind::Mod(_) if self.cx.ecfg.proc_macro_mod() => return,
573 ItemKind::Mod(_) => ("modules", "proc_macro_mod"),
577 Annotatable::TraitItem(_) => return,
578 Annotatable::ImplItem(_) => return,
579 Annotatable::ForeignItem(_) => return,
580 Annotatable::Stmt(_) |
581 Annotatable::Expr(_) if self.cx.ecfg.proc_macro_expr() => return,
582 Annotatable::Stmt(_) => ("statements", "proc_macro_expr"),
583 Annotatable::Expr(_) => ("expressions", "proc_macro_expr"),
590 &format!("custom attributes cannot be applied to {}", kind),
594 /// Expand a macro invocation. Returns the result of expansion.
595 fn expand_bang_invoc(&mut self,
597 ext: &SyntaxExtension)
598 -> Option<Expansion> {
599 let (mark, kind) = (invoc.expansion_data.mark, invoc.expansion_kind);
600 let (mac, ident, span) = match invoc.kind {
601 InvocationKind::Bang { mac, ident, span } => (mac, ident, span),
604 let path = &mac.node.path;
606 let ident = ident.unwrap_or_else(|| keywords::Invalid.ident());
607 let validate_and_set_expn_info = |this: &mut Self, // arg instead of capture
608 def_site_span: Option<Span>,
609 allow_internal_unstable,
610 allow_internal_unsafe,
611 // can't infer this type
612 unstable_feature: Option<(Symbol, u32)>| {
614 // feature-gate the macro invocation
615 if let Some((feature, issue)) = unstable_feature {
616 let crate_span = this.cx.current_expansion.crate_span.unwrap();
617 // don't stability-check macros in the same crate
618 // (the only time this is null is for syntax extensions registered as macros)
619 if def_site_span.map_or(false, |def_span| !crate_span.contains(def_span))
620 && !span.allows_unstable() && this.cx.ecfg.features.map_or(true, |feats| {
621 // macro features will count as lib features
622 !feats.declared_lib_features.iter().any(|&(feat, _)| feat == feature)
624 let explain = format!("macro {}! is unstable", path);
625 emit_feature_err(this.cx.parse_sess, &*feature.as_str(), span,
626 GateIssue::Library(Some(issue)), &explain);
627 this.cx.trace_macros_diag();
628 return Err(kind.dummy(span));
632 if ident.name != keywords::Invalid.name() {
633 let msg = format!("macro {}! expects no ident argument, given '{}'", path, ident);
634 this.cx.span_err(path.span, &msg);
635 this.cx.trace_macros_diag();
636 return Err(kind.dummy(span));
638 mark.set_expn_info(ExpnInfo {
640 callee: NameAndSpan {
641 format: macro_bang_format(path),
643 allow_internal_unstable,
644 allow_internal_unsafe,
650 let opt_expanded = match *ext {
651 DeclMacro(ref expand, def_span) => {
652 if let Err(dummy_span) = validate_and_set_expn_info(self, def_span.map(|(_, s)| s),
653 false, false, None) {
656 kind.make_from(expand.expand(self.cx, span, mac.node.stream()))
663 allow_internal_unstable,
664 allow_internal_unsafe,
667 if let Err(dummy_span) = validate_and_set_expn_info(self, def_info.map(|(_, s)| s),
668 allow_internal_unstable,
669 allow_internal_unsafe,
673 kind.make_from(expander.expand(self.cx, span, mac.node.stream()))
677 IdentTT(ref expander, tt_span, allow_internal_unstable) => {
678 if ident.name == keywords::Invalid.name() {
679 self.cx.span_err(path.span,
680 &format!("macro {}! expects an ident argument", path));
681 self.cx.trace_macros_diag();
684 invoc.expansion_data.mark.set_expn_info(ExpnInfo {
686 callee: NameAndSpan {
687 format: macro_bang_format(path),
689 allow_internal_unstable,
690 allow_internal_unsafe: false,
694 let input: Vec<_> = mac.node.stream().into_trees().collect();
695 kind.make_from(expander.expand(self.cx, span, ident, input))
699 MultiDecorator(..) | MultiModifier(..) | AttrProcMacro(..) => {
700 self.cx.span_err(path.span,
701 &format!("`{}` can only be used in attributes", path));
702 self.cx.trace_macros_diag();
706 ProcMacroDerive(..) | BuiltinDerive(..) => {
707 self.cx.span_err(path.span, &format!("`{}` is a derive mode", path));
708 self.cx.trace_macros_diag();
712 ProcMacro(ref expandfun) => {
713 if ident.name != keywords::Invalid.name() {
715 format!("macro {}! expects no ident argument, given '{}'", path, ident);
716 self.cx.span_err(path.span, &msg);
717 self.cx.trace_macros_diag();
720 self.gate_proc_macro_expansion_kind(span, kind);
721 invoc.expansion_data.mark.set_expn_info(ExpnInfo {
723 callee: NameAndSpan {
724 format: macro_bang_format(path),
725 // FIXME procedural macros do not have proper span info
726 // yet, when they do, we should use it here.
728 // FIXME probably want to follow macro_rules macros here.
729 allow_internal_unstable: false,
730 allow_internal_unsafe: false,
734 let tok_result = expandfun.expand(self.cx, span, mac.node.stream());
735 self.parse_expansion(tok_result, kind, path, span)
740 if opt_expanded.is_some() {
743 let msg = format!("non-{kind} macro in {kind} position: {name}",
744 name = path.segments[0].ident.name, kind = kind.name());
745 self.cx.span_err(path.span, &msg);
746 self.cx.trace_macros_diag();
751 fn gate_proc_macro_expansion_kind(&self, span: Span, kind: ExpansionKind) {
752 let kind = match kind {
753 ExpansionKind::Expr => "expressions",
754 ExpansionKind::OptExpr => "expressions",
755 ExpansionKind::Pat => "patterns",
756 ExpansionKind::Ty => "types",
757 ExpansionKind::Stmts => "statements",
758 ExpansionKind::Items => return,
759 ExpansionKind::TraitItems => return,
760 ExpansionKind::ImplItems => return,
761 ExpansionKind::ForeignItems => return,
763 if self.cx.ecfg.proc_macro_non_items() {
768 "proc_macro_non_items",
771 &format!("procedural macros cannot be expanded to {}", kind),
775 /// Expand a derive invocation. Returns the result of expansion.
776 fn expand_derive_invoc(&mut self,
778 ext: &SyntaxExtension)
779 -> Option<Expansion> {
780 let Invocation { expansion_kind: kind, .. } = invoc;
781 let (path, item) = match invoc.kind {
782 InvocationKind::Derive { path, item } => (path, item),
785 if !item.derive_allowed() {
789 let pretty_name = Symbol::intern(&format!("derive({})", path));
790 let span = path.span;
791 let attr = ast::Attribute {
793 tokens: TokenStream::empty(),
795 id: ast::AttrId(0), style: ast::AttrStyle::Outer, is_sugared_doc: false,
798 let mut expn_info = ExpnInfo {
800 callee: NameAndSpan {
801 format: MacroAttribute(pretty_name),
803 allow_internal_unstable: false,
804 allow_internal_unsafe: false,
809 ProcMacroDerive(ref ext, _) => {
810 invoc.expansion_data.mark.set_expn_info(expn_info);
811 let span = span.with_ctxt(self.cx.backtrace());
812 let dummy = ast::MetaItem { // FIXME(jseyfried) avoid this
813 name: Path::from_ident(DUMMY_SP, keywords::Invalid.ident()),
815 node: ast::MetaItemKind::Word,
817 Some(kind.expect_from_annotatables(ext.expand(self.cx, span, &dummy, item)))
819 BuiltinDerive(func) => {
820 expn_info.callee.allow_internal_unstable = true;
821 invoc.expansion_data.mark.set_expn_info(expn_info);
822 let span = span.with_ctxt(self.cx.backtrace());
823 let mut items = Vec::new();
824 func(self.cx, span, &attr.meta()?, &item, &mut |a| items.push(a));
825 Some(kind.expect_from_annotatables(items))
828 let msg = &format!("macro `{}` may not be used for derive attributes", attr.path);
829 self.cx.span_err(span, msg);
830 self.cx.trace_macros_diag();
836 fn parse_expansion(&mut self,
841 -> Option<Expansion> {
842 let mut parser = self.cx.new_parser_from_tts(&toks.into_trees().collect::<Vec<_>>());
843 match parser.parse_expansion(kind, false) {
845 parser.ensure_complete_parse(path, kind.name(), span);
851 self.cx.trace_macros_diag();
858 impl<'a> Parser<'a> {
859 pub fn parse_expansion(&mut self, kind: ExpansionKind, macro_legacy_warnings: bool)
860 -> PResult<'a, Expansion> {
862 ExpansionKind::Items => {
863 let mut items = SmallVector::new();
864 while let Some(item) = self.parse_item()? {
867 Expansion::Items(items)
869 ExpansionKind::TraitItems => {
870 let mut items = SmallVector::new();
871 while self.token != token::Eof {
872 items.push(self.parse_trait_item(&mut false)?);
874 Expansion::TraitItems(items)
876 ExpansionKind::ImplItems => {
877 let mut items = SmallVector::new();
878 while self.token != token::Eof {
879 items.push(self.parse_impl_item(&mut false)?);
881 Expansion::ImplItems(items)
883 ExpansionKind::ForeignItems => {
884 let mut items = SmallVector::new();
885 while self.token != token::Eof {
886 if let Some(item) = self.parse_foreign_item()? {
890 Expansion::ForeignItems(items)
892 ExpansionKind::Stmts => {
893 let mut stmts = SmallVector::new();
894 while self.token != token::Eof &&
895 // won't make progress on a `}`
896 self.token != token::CloseDelim(token::Brace) {
897 if let Some(stmt) = self.parse_full_stmt(macro_legacy_warnings)? {
901 Expansion::Stmts(stmts)
903 ExpansionKind::Expr => Expansion::Expr(self.parse_expr()?),
904 ExpansionKind::OptExpr => {
905 if self.token != token::Eof {
906 Expansion::OptExpr(Some(self.parse_expr()?))
908 Expansion::OptExpr(None)
911 ExpansionKind::Ty => Expansion::Ty(self.parse_ty()?),
912 ExpansionKind::Pat => Expansion::Pat(self.parse_pat()?),
916 pub fn ensure_complete_parse(&mut self, macro_path: &Path, kind_name: &str, span: Span) {
917 if self.token != token::Eof {
918 let msg = format!("macro expansion ignores token `{}` and any following",
919 self.this_token_to_string());
920 // Avoid emitting backtrace info twice.
921 let def_site_span = self.span.with_ctxt(SyntaxContext::empty());
922 let mut err = self.diagnostic().struct_span_err(def_site_span, &msg);
923 let msg = format!("caused by the macro expansion here; the usage \
924 of `{}!` is likely invalid in {} context",
925 macro_path, kind_name);
926 err.span_note(span, &msg).emit();
931 struct InvocationCollector<'a, 'b: 'a> {
932 cx: &'a mut ExtCtxt<'b>,
933 cfg: StripUnconfigured<'a>,
934 invocations: Vec<Invocation>,
938 impl<'a, 'b> InvocationCollector<'a, 'b> {
939 fn collect(&mut self, expansion_kind: ExpansionKind, kind: InvocationKind) -> Expansion {
940 let mark = Mark::fresh(self.cx.current_expansion.mark);
941 self.invocations.push(Invocation {
944 expansion_data: ExpansionData {
946 depth: self.cx.current_expansion.depth + 1,
947 ..self.cx.current_expansion.clone()
950 placeholder(expansion_kind, NodeId::placeholder_from_mark(mark))
953 fn collect_bang(&mut self, mac: ast::Mac, span: Span, kind: ExpansionKind) -> Expansion {
954 self.collect(kind, InvocationKind::Bang { mac: mac, ident: None, span: span })
957 fn collect_attr(&mut self,
958 attr: Option<ast::Attribute>,
963 self.collect(kind, InvocationKind::Attr { attr, traits, item })
966 /// If `item` is an attr invocation, remove and return the macro attribute and derive traits.
967 fn classify_item<T>(&mut self, mut item: T) -> (Option<ast::Attribute>, Vec<Path>, T)
970 let (mut attr, mut traits) = (None, Vec::new());
972 item = item.map_attrs(|mut attrs| {
973 if let Some(legacy_attr_invoc) = self.cx.resolver.find_legacy_attr_invoc(&mut attrs,
975 attr = Some(legacy_attr_invoc);
979 if self.cx.ecfg.proc_macro_enabled() {
980 attr = find_attr_invoc(&mut attrs);
982 traits = collect_derives(&mut self.cx, &mut attrs);
989 /// Alternative of `classify_item()` that ignores `#[derive]` so invocations fallthrough
990 /// to the unused-attributes lint (making it an error on statements and expressions
991 /// is a breaking change)
992 fn classify_nonitem<T: HasAttrs>(&mut self, mut item: T) -> (Option<ast::Attribute>, T) {
995 item = item.map_attrs(|mut attrs| {
996 if let Some(legacy_attr_invoc) = self.cx.resolver.find_legacy_attr_invoc(&mut attrs,
998 attr = Some(legacy_attr_invoc);
1002 if self.cx.ecfg.proc_macro_enabled() {
1003 attr = find_attr_invoc(&mut attrs);
1011 fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
1012 self.cfg.configure(node)
1015 // Detect use of feature-gated or invalid attributes on macro invocations
1016 // since they will not be detected after macro expansion.
1017 fn check_attributes(&mut self, attrs: &[ast::Attribute]) {
1018 let features = self.cx.ecfg.features.unwrap();
1019 for attr in attrs.iter() {
1020 self.check_attribute_inner(attr, features);
1022 // macros are expanded before any lint passes so this warning has to be hardcoded
1023 if attr.path == "derive" {
1024 self.cx.struct_span_warn(attr.span, "`#[derive]` does nothing on macro invocations")
1025 .note("this may become a hard error in a future release")
1031 fn check_attribute(&mut self, at: &ast::Attribute) {
1032 let features = self.cx.ecfg.features.unwrap();
1033 self.check_attribute_inner(at, features);
1036 fn check_attribute_inner(&mut self, at: &ast::Attribute, features: &Features) {
1037 feature_gate::check_attribute(at, self.cx.parse_sess, features);
1041 pub fn find_attr_invoc(attrs: &mut Vec<ast::Attribute>) -> Option<ast::Attribute> {
1043 .position(|a| !attr::is_known(a) && !is_builtin_attr(a))
1044 .map(|i| attrs.remove(i))
1047 impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
1048 fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
1049 let mut expr = self.cfg.configure_expr(expr).into_inner();
1050 expr.node = self.cfg.configure_expr_kind(expr.node);
1052 // ignore derives so they remain unused
1053 let (attr, expr) = self.classify_nonitem(expr);
1056 // collect the invoc regardless of whether or not attributes are permitted here
1057 // expansion will eat the attribute so it won't error later
1058 attr.as_ref().map(|a| self.cfg.maybe_emit_expr_attr_err(a));
1060 // ExpansionKind::Expr requires the macro to emit an expression
1061 return self.collect_attr(attr, vec![], Annotatable::Expr(P(expr)), ExpansionKind::Expr)
1065 if let ast::ExprKind::Mac(mac) = expr.node {
1066 self.check_attributes(&expr.attrs);
1067 self.collect_bang(mac, expr.span, ExpansionKind::Expr).make_expr()
1069 P(noop_fold_expr(expr, self))
1073 fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
1074 let mut expr = configure!(self, expr).into_inner();
1075 expr.node = self.cfg.configure_expr_kind(expr.node);
1077 // ignore derives so they remain unused
1078 let (attr, expr) = self.classify_nonitem(expr);
1081 attr.as_ref().map(|a| self.cfg.maybe_emit_expr_attr_err(a));
1083 return self.collect_attr(attr, vec![], Annotatable::Expr(P(expr)),
1084 ExpansionKind::OptExpr)
1088 if let ast::ExprKind::Mac(mac) = expr.node {
1089 self.check_attributes(&expr.attrs);
1090 self.collect_bang(mac, expr.span, ExpansionKind::OptExpr).make_opt_expr()
1092 Some(P(noop_fold_expr(expr, self)))
1096 fn fold_pat(&mut self, pat: P<ast::Pat>) -> P<ast::Pat> {
1097 let pat = self.cfg.configure_pat(pat);
1099 PatKind::Mac(_) => {}
1100 _ => return noop_fold_pat(pat, self),
1103 pat.and_then(|pat| match pat.node {
1104 PatKind::Mac(mac) => self.collect_bang(mac, pat.span, ExpansionKind::Pat).make_pat(),
1105 _ => unreachable!(),
1109 fn fold_stmt(&mut self, stmt: ast::Stmt) -> SmallVector<ast::Stmt> {
1110 let mut stmt = match self.cfg.configure_stmt(stmt) {
1112 None => return SmallVector::new(),
1115 // we'll expand attributes on expressions separately
1116 if !stmt.is_expr() {
1117 let (attr, derives, stmt_) = if stmt.is_item() {
1118 self.classify_item(stmt)
1120 // ignore derives on non-item statements so it falls through
1121 // to the unused-attributes lint
1122 let (attr, stmt) = self.classify_nonitem(stmt);
1123 (attr, vec![], stmt)
1126 if attr.is_some() || !derives.is_empty() {
1127 return self.collect_attr(attr, derives,
1128 Annotatable::Stmt(P(stmt_)), ExpansionKind::Stmts)
1135 if let StmtKind::Mac(mac) = stmt.node {
1136 let (mac, style, attrs) = mac.into_inner();
1137 self.check_attributes(&attrs);
1138 let mut placeholder = self.collect_bang(mac, stmt.span, ExpansionKind::Stmts)
1141 // If this is a macro invocation with a semicolon, then apply that
1142 // semicolon to the final statement produced by expansion.
1143 if style == MacStmtStyle::Semicolon {
1144 if let Some(stmt) = placeholder.pop() {
1145 placeholder.push(stmt.add_trailing_semicolon());
1152 // The placeholder expander gives ids to statements, so we avoid folding the id here.
1153 let ast::Stmt { id, node, span } = stmt;
1154 noop_fold_stmt_kind(node, self).into_iter().map(|node| {
1155 ast::Stmt { id, node, span }
1160 fn fold_block(&mut self, block: P<Block>) -> P<Block> {
1161 let old_directory_ownership = self.cx.current_expansion.directory_ownership;
1162 self.cx.current_expansion.directory_ownership = DirectoryOwnership::UnownedViaBlock;
1163 let result = noop_fold_block(block, self);
1164 self.cx.current_expansion.directory_ownership = old_directory_ownership;
1168 fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
1169 let item = configure!(self, item);
1171 let (attr, traits, mut item) = self.classify_item(item);
1172 if attr.is_some() || !traits.is_empty() {
1173 let item = Annotatable::Item(item);
1174 return self.collect_attr(attr, traits, item, ExpansionKind::Items).make_items();
1178 ast::ItemKind::Mac(..) => {
1179 self.check_attributes(&item.attrs);
1180 item.and_then(|item| match item.node {
1181 ItemKind::Mac(mac) => {
1182 self.collect(ExpansionKind::Items, InvocationKind::Bang {
1184 ident: Some(item.ident),
1188 _ => unreachable!(),
1191 ast::ItemKind::Mod(ast::Mod { inner, .. }) => {
1192 if item.ident == keywords::Invalid.ident() {
1193 return noop_fold_item(item, self);
1196 let orig_directory_ownership = self.cx.current_expansion.directory_ownership;
1197 let mut module = (*self.cx.current_expansion.module).clone();
1198 module.mod_path.push(item.ident);
1200 // Detect if this is an inline module (`mod m { ... }` as opposed to `mod m;`).
1201 // In the non-inline case, `inner` is never the dummy span (c.f. `parse_item_mod`).
1202 // Thus, if `inner` is the dummy span, we know the module is inline.
1203 let inline_module = item.span.contains(inner) || inner == DUMMY_SP;
1206 if let Some(path) = attr::first_attr_value_str_by_name(&item.attrs, "path") {
1207 self.cx.current_expansion.directory_ownership =
1208 DirectoryOwnership::Owned { relative: None };
1209 module.directory.push(&*path.as_str());
1211 module.directory.push(&*item.ident.name.as_str());
1214 let path = self.cx.parse_sess.codemap().span_to_unmapped_path(inner);
1215 let mut path = match path {
1216 FileName::Real(path) => path,
1217 other => PathBuf::from(other.to_string()),
1219 let directory_ownership = match path.file_name().unwrap().to_str() {
1220 Some("mod.rs") => DirectoryOwnership::Owned { relative: None },
1221 Some(_) => DirectoryOwnership::Owned {
1222 relative: Some(item.ident),
1224 None => DirectoryOwnership::UnownedViaMod(false),
1227 module.directory = path;
1228 self.cx.current_expansion.directory_ownership = directory_ownership;
1232 mem::replace(&mut self.cx.current_expansion.module, Rc::new(module));
1233 let result = noop_fold_item(item, self);
1234 self.cx.current_expansion.module = orig_module;
1235 self.cx.current_expansion.directory_ownership = orig_directory_ownership;
1238 // Ensure that test functions are accessible from the test harness.
1239 ast::ItemKind::Fn(..) if self.cx.ecfg.should_test => {
1240 if item.attrs.iter().any(|attr| is_test_or_bench(attr)) {
1241 item = item.map(|mut item| {
1242 item.vis = respan(item.vis.span, ast::VisibilityKind::Public);
1246 noop_fold_item(item, self)
1248 _ => noop_fold_item(item, self),
1252 fn fold_trait_item(&mut self, item: ast::TraitItem) -> SmallVector<ast::TraitItem> {
1253 let item = configure!(self, item);
1255 let (attr, traits, item) = self.classify_item(item);
1256 if attr.is_some() || !traits.is_empty() {
1257 let item = Annotatable::TraitItem(P(item));
1258 return self.collect_attr(attr, traits, item, ExpansionKind::TraitItems)
1263 ast::TraitItemKind::Macro(mac) => {
1264 let ast::TraitItem { attrs, span, .. } = item;
1265 self.check_attributes(&attrs);
1266 self.collect_bang(mac, span, ExpansionKind::TraitItems).make_trait_items()
1268 _ => fold::noop_fold_trait_item(item, self),
1272 fn fold_impl_item(&mut self, item: ast::ImplItem) -> SmallVector<ast::ImplItem> {
1273 let item = configure!(self, item);
1275 let (attr, traits, item) = self.classify_item(item);
1276 if attr.is_some() || !traits.is_empty() {
1277 let item = Annotatable::ImplItem(P(item));
1278 return self.collect_attr(attr, traits, item, ExpansionKind::ImplItems)
1283 ast::ImplItemKind::Macro(mac) => {
1284 let ast::ImplItem { attrs, span, .. } = item;
1285 self.check_attributes(&attrs);
1286 self.collect_bang(mac, span, ExpansionKind::ImplItems).make_impl_items()
1288 _ => fold::noop_fold_impl_item(item, self),
1292 fn fold_ty(&mut self, ty: P<ast::Ty>) -> P<ast::Ty> {
1293 let ty = match ty.node {
1294 ast::TyKind::Mac(_) => ty.into_inner(),
1295 _ => return fold::noop_fold_ty(ty, self),
1299 ast::TyKind::Mac(mac) => self.collect_bang(mac, ty.span, ExpansionKind::Ty).make_ty(),
1300 _ => unreachable!(),
1304 fn fold_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
1305 noop_fold_foreign_mod(self.cfg.configure_foreign_mod(foreign_mod), self)
1308 fn fold_foreign_item(&mut self,
1309 foreign_item: ast::ForeignItem) -> SmallVector<ast::ForeignItem> {
1310 let (attr, traits, foreign_item) = self.classify_item(foreign_item);
1312 let explain = if self.cx.ecfg.proc_macro_enabled() {
1313 feature_gate::EXPLAIN_PROC_MACROS_IN_EXTERN
1315 feature_gate::EXPLAIN_MACROS_IN_EXTERN
1318 if attr.is_some() || !traits.is_empty() {
1319 if !self.cx.ecfg.macros_in_extern_enabled() {
1320 if let Some(ref attr) = attr {
1321 emit_feature_err(&self.cx.parse_sess, "macros_in_extern", attr.span,
1322 GateIssue::Language, explain);
1326 let item = Annotatable::ForeignItem(P(foreign_item));
1327 return self.collect_attr(attr, traits, item, ExpansionKind::ForeignItems)
1328 .make_foreign_items();
1331 if let ast::ForeignItemKind::Macro(mac) = foreign_item.node {
1332 self.check_attributes(&foreign_item.attrs);
1334 if !self.cx.ecfg.macros_in_extern_enabled() {
1335 emit_feature_err(&self.cx.parse_sess, "macros_in_extern", foreign_item.span,
1336 GateIssue::Language, explain);
1339 return self.collect_bang(mac, foreign_item.span, ExpansionKind::ForeignItems)
1340 .make_foreign_items();
1343 noop_fold_foreign_item(foreign_item, self)
1346 fn fold_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind {
1348 ast::ItemKind::MacroDef(..) => item,
1349 _ => noop_fold_item_kind(self.cfg.configure_item_kind(item), self),
1353 fn fold_attribute(&mut self, at: ast::Attribute) -> Option<ast::Attribute> {
1354 // turn `#[doc(include="filename")]` attributes into `#[doc(include(file="filename",
1355 // contents="file contents")]` attributes
1356 if !at.check_name("doc") {
1357 return noop_fold_attribute(at, self);
1360 if let Some(list) = at.meta_item_list() {
1361 if !list.iter().any(|it| it.check_name("include")) {
1362 return noop_fold_attribute(at, self);
1365 let mut items = vec![];
1368 if !it.check_name("include") {
1369 items.push(noop_fold_meta_list_item(it, self));
1373 if let Some(file) = it.value_str() {
1374 let err_count = self.cx.parse_sess.span_diagnostic.err_count();
1375 self.check_attribute(&at);
1376 if self.cx.parse_sess.span_diagnostic.err_count() > err_count {
1377 // avoid loading the file if they haven't enabled the feature
1378 return noop_fold_attribute(at, self);
1381 let mut buf = vec![];
1382 let filename = self.cx.root_path.join(file.to_string());
1384 match File::open(&filename).and_then(|mut f| f.read_to_end(&mut buf)) {
1387 self.cx.span_err(at.span,
1388 &format!("couldn't read {}: {}",
1394 match String::from_utf8(buf) {
1396 // Add this input file to the code map to make it available as
1397 // dependency information
1398 self.cx.codemap().new_filemap_and_lines(&filename, &src);
1400 let include_info = vec![
1401 dummy_spanned(ast::NestedMetaItemKind::MetaItem(
1402 attr::mk_name_value_item_str(Ident::from_str("file"),
1403 dummy_spanned(file)))),
1404 dummy_spanned(ast::NestedMetaItemKind::MetaItem(
1405 attr::mk_name_value_item_str(Ident::from_str("contents"),
1406 dummy_spanned(Symbol::intern(&src))))),
1409 let include_ident = Ident::from_str("include");
1410 let item = attr::mk_list_item(DUMMY_SP, include_ident, include_info);
1411 items.push(dummy_spanned(ast::NestedMetaItemKind::MetaItem(item)));
1414 self.cx.span_err(at.span,
1415 &format!("{} wasn't a utf-8 file",
1416 filename.display()));
1420 items.push(noop_fold_meta_list_item(it, self));
1424 let meta = attr::mk_list_item(DUMMY_SP, Ident::from_str("doc"), items);
1426 ast::AttrStyle::Inner =>
1427 Some(attr::mk_spanned_attr_inner(at.span, at.id, meta)),
1428 ast::AttrStyle::Outer =>
1429 Some(attr::mk_spanned_attr_outer(at.span, at.id, meta)),
1432 noop_fold_attribute(at, self)
1436 fn new_id(&mut self, id: ast::NodeId) -> ast::NodeId {
1438 assert_eq!(id, ast::DUMMY_NODE_ID);
1439 self.cx.resolver.next_node_id()
1446 pub struct ExpansionConfig<'feat> {
1447 pub crate_name: String,
1448 pub features: Option<&'feat Features>,
1449 pub recursion_limit: usize,
1450 pub trace_mac: bool,
1451 pub should_test: bool, // If false, strip `#[test]` nodes
1452 pub single_step: bool,
1453 pub keep_macs: bool,
1456 macro_rules! feature_tests {
1457 ($( fn $getter:ident = $field:ident, )*) => {
1459 pub fn $getter(&self) -> bool {
1460 match self.features {
1461 Some(&Features { $field: true, .. }) => true,
1469 impl<'feat> ExpansionConfig<'feat> {
1470 pub fn default(crate_name: String) -> ExpansionConfig<'static> {
1474 recursion_limit: 1024,
1483 fn enable_quotes = quote,
1484 fn enable_asm = asm,
1485 fn enable_global_asm = global_asm,
1486 fn enable_log_syntax = log_syntax,
1487 fn enable_concat_idents = concat_idents,
1488 fn enable_trace_macros = trace_macros,
1489 fn enable_allow_internal_unstable = allow_internal_unstable,
1490 fn enable_custom_derive = custom_derive,
1491 fn proc_macro_enabled = proc_macro,
1492 fn macros_in_extern_enabled = macros_in_extern,
1493 fn proc_macro_mod = proc_macro_mod,
1494 fn proc_macro_expr = proc_macro_expr,
1495 fn proc_macro_non_items = proc_macro_non_items,
1499 // A Marker adds the given mark to the syntax context.
1501 pub struct Marker(pub Mark);
1503 impl Folder for Marker {
1504 fn fold_ident(&mut self, mut ident: Ident) -> Ident {
1505 ident.span = ident.span.apply_mark(self.0);
1509 fn new_span(&mut self, span: Span) -> Span {
1510 span.apply_mark(self.0)
1513 fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
1514 noop_fold_mac(mac, self)