Avoid including attributes in bang macro invocations.

This commit is contained in:
Jeffrey Seyfried
2016-12-01 11:54:01 +00:00
parent 421c5d11c1
commit 6f040b48ef
2 changed files with 19 additions and 25 deletions

View File

@@ -217,8 +217,7 @@ pub trait IdentMacroExpander {
cx: &'cx mut ExtCtxt, cx: &'cx mut ExtCtxt,
sp: Span, sp: Span,
ident: ast::Ident, ident: ast::Ident,
token_tree: Vec<tokenstream::TokenTree>, token_tree: Vec<tokenstream::TokenTree>)
attrs: Vec<ast::Attribute>)
-> Box<MacResult+'cx>; -> Box<MacResult+'cx>;
} }
@@ -234,8 +233,7 @@ impl<F> IdentMacroExpander for F
cx: &'cx mut ExtCtxt, cx: &'cx mut ExtCtxt,
sp: Span, sp: Span,
ident: ast::Ident, ident: ast::Ident,
token_tree: Vec<tokenstream::TokenTree>, token_tree: Vec<tokenstream::TokenTree>)
_attrs: Vec<ast::Attribute>)
-> Box<MacResult+'cx> -> Box<MacResult+'cx>
{ {
(*self)(cx, sp, ident, token_tree) (*self)(cx, sp, ident, token_tree)

View File

@@ -158,7 +158,6 @@ pub struct Invocation {
pub enum InvocationKind { pub enum InvocationKind {
Bang { Bang {
attrs: Vec<ast::Attribute>,
mac: ast::Mac, mac: ast::Mac,
ident: Option<Ident>, ident: Option<Ident>,
span: Span, span: Span,
@@ -386,8 +385,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
/// Expand a macro invocation. Returns the result of expansion. /// Expand a macro invocation. Returns the result of expansion.
fn expand_bang_invoc(&mut self, invoc: Invocation, ext: Rc<SyntaxExtension>) -> Expansion { fn expand_bang_invoc(&mut self, invoc: Invocation, ext: Rc<SyntaxExtension>) -> Expansion {
let (mark, kind) = (invoc.expansion_data.mark, invoc.expansion_kind); let (mark, kind) = (invoc.expansion_data.mark, invoc.expansion_kind);
let (attrs, mac, ident, span) = match invoc.kind { let (mac, ident, span) = match invoc.kind {
InvocationKind::Bang { attrs, mac, ident, span } => (attrs, mac, ident, span), InvocationKind::Bang { mac, ident, span } => (mac, ident, span),
_ => unreachable!(), _ => unreachable!(),
}; };
let Mac_ { path, tts, .. } = mac.node; let Mac_ { path, tts, .. } = mac.node;
@@ -432,7 +431,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
} }
}); });
kind.make_from(expander.expand(self.cx, span, ident, marked_tts, attrs)) kind.make_from(expander.expand(self.cx, span, ident, marked_tts))
} }
MultiDecorator(..) | MultiModifier(..) | SyntaxExtension::AttrProcMacro(..) => { MultiDecorator(..) | MultiModifier(..) | SyntaxExtension::AttrProcMacro(..) => {
@@ -590,11 +589,8 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
placeholder(expansion_kind, ast::NodeId::from_u32(mark.as_u32())) placeholder(expansion_kind, ast::NodeId::from_u32(mark.as_u32()))
} }
fn collect_bang( fn collect_bang(&mut self, mac: ast::Mac, span: Span, kind: ExpansionKind) -> Expansion {
&mut self, mac: ast::Mac, attrs: Vec<ast::Attribute>, span: Span, kind: ExpansionKind, self.collect(kind, InvocationKind::Bang { mac: mac, ident: None, span: span })
) -> Expansion {
self.check_attributes(&attrs);
self.collect(kind, InvocationKind::Bang { attrs: attrs, mac: mac, ident: None, span: span })
} }
fn collect_attr(&mut self, attr: ast::Attribute, item: Annotatable, kind: ExpansionKind) fn collect_attr(&mut self, attr: ast::Attribute, item: Annotatable, kind: ExpansionKind)
@@ -663,7 +659,8 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
expr.node = self.cfg.configure_expr_kind(expr.node); expr.node = self.cfg.configure_expr_kind(expr.node);
if let ast::ExprKind::Mac(mac) = expr.node { if let ast::ExprKind::Mac(mac) = expr.node {
self.collect_bang(mac, expr.attrs.into(), expr.span, ExpansionKind::Expr).make_expr() self.check_attributes(&expr.attrs);
self.collect_bang(mac, expr.span, ExpansionKind::Expr).make_expr()
} else { } else {
P(noop_fold_expr(expr, self)) P(noop_fold_expr(expr, self))
} }
@@ -674,8 +671,8 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
expr.node = self.cfg.configure_expr_kind(expr.node); expr.node = self.cfg.configure_expr_kind(expr.node);
if let ast::ExprKind::Mac(mac) = expr.node { if let ast::ExprKind::Mac(mac) = expr.node {
self.collect_bang(mac, expr.attrs.into(), expr.span, ExpansionKind::OptExpr) self.check_attributes(&expr.attrs);
.make_opt_expr() self.collect_bang(mac, expr.span, ExpansionKind::OptExpr).make_opt_expr()
} else { } else {
Some(P(noop_fold_expr(expr, self))) Some(P(noop_fold_expr(expr, self)))
} }
@@ -688,8 +685,7 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
} }
pat.and_then(|pat| match pat.node { pat.and_then(|pat| match pat.node {
PatKind::Mac(mac) => PatKind::Mac(mac) => self.collect_bang(mac, pat.span, ExpansionKind::Pat).make_pat(),
self.collect_bang(mac, Vec::new(), pat.span, ExpansionKind::Pat).make_pat(),
_ => unreachable!(), _ => unreachable!(),
}) })
} }
@@ -710,8 +706,8 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
}).collect() }).collect()
}; };
let mut placeholder = self.check_attributes(&attrs);
self.collect_bang(mac, attrs.into(), stmt.span, ExpansionKind::Stmts).make_stmts(); let mut placeholder = self.collect_bang(mac, stmt.span, ExpansionKind::Stmts).make_stmts();
// If this is a macro invocation with a semicolon, then apply that // If this is a macro invocation with a semicolon, then apply that
// semicolon to the final statement produced by expansion. // semicolon to the final statement produced by expansion.
@@ -758,7 +754,6 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
ItemKind::Mac(mac) => { ItemKind::Mac(mac) => {
self.collect(ExpansionKind::Items, InvocationKind::Bang { self.collect(ExpansionKind::Items, InvocationKind::Bang {
mac: mac, mac: mac,
attrs: item.attrs,
ident: Some(item.ident), ident: Some(item.ident),
span: item.span, span: item.span,
}).make_items() }).make_items()
@@ -830,7 +825,8 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
match item.node { match item.node {
ast::TraitItemKind::Macro(mac) => { ast::TraitItemKind::Macro(mac) => {
let ast::TraitItem { attrs, span, .. } = item; let ast::TraitItem { attrs, span, .. } = item;
self.collect_bang(mac, attrs, span, ExpansionKind::TraitItems).make_trait_items() self.check_attributes(&attrs);
self.collect_bang(mac, span, ExpansionKind::TraitItems).make_trait_items()
} }
_ => fold::noop_fold_trait_item(item, self), _ => fold::noop_fold_trait_item(item, self),
} }
@@ -848,7 +844,8 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
match item.node { match item.node {
ast::ImplItemKind::Macro(mac) => { ast::ImplItemKind::Macro(mac) => {
let ast::ImplItem { attrs, span, .. } = item; let ast::ImplItem { attrs, span, .. } = item;
self.collect_bang(mac, attrs, span, ExpansionKind::ImplItems).make_impl_items() self.check_attributes(&attrs);
self.collect_bang(mac, span, ExpansionKind::ImplItems).make_impl_items()
} }
_ => fold::noop_fold_impl_item(item, self), _ => fold::noop_fold_impl_item(item, self),
} }
@@ -861,8 +858,7 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
}; };
match ty.node { match ty.node {
ast::TyKind::Mac(mac) => ast::TyKind::Mac(mac) => self.collect_bang(mac, ty.span, ExpansionKind::Ty).make_ty(),
self.collect_bang(mac, Vec::new(), ty.span, ExpansionKind::Ty).make_ty(),
_ => unreachable!(), _ => unreachable!(),
} }
} }