From 215c1460f68a5c47f40ea173484cd791166ebc94 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Fri, 20 May 2016 23:55:39 +0000 Subject: [PATCH] Check attributes in `expand_mac_invoc` --- src/libsyntax/ext/expand.rs | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 8a5689daca51..aa5d93d82695 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -16,7 +16,7 @@ use ast; use ext::mtwt; use ext::build::AstBuilder; use attr; -use attr::{AttrMetaMethods, WithAttrs}; +use attr::{AttrMetaMethods, WithAttrs, ThinAttributesExt}; use codemap; use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute}; use ext::base::*; @@ -86,14 +86,7 @@ pub fn expand_expr(e: P, fld: &mut MacroExpander) -> P { // expr_mac should really be expr_ext or something; it's the // entry-point for all syntax extensions. ast::ExprKind::Mac(mac) => { - if let Some(ref attrs) = attrs { - check_attributes(attrs, fld); - } - - // Assert that we drop any macro attributes on the floor here - drop(attrs); - - expand_mac_invoc(mac, span, fld) + expand_mac_invoc(mac, attrs.into_attr_vec(), span, fld) } ast::ExprKind::InPlace(placer, value_expr) => { @@ -204,7 +197,12 @@ pub fn expand_expr(e: P, fld: &mut MacroExpander) -> P { } /// Expand a (not-ident-style) macro invocation. Returns the result of expansion. -fn expand_mac_invoc(mac: ast::Mac, span: Span, fld: &mut MacroExpander) -> T { +fn expand_mac_invoc(mac: ast::Mac, attrs: Vec, span: Span, + fld: &mut MacroExpander) -> T + where T: MacroGenerable, +{ + check_attributes(&attrs, fld); + // it would almost certainly be cleaner to pass the whole // macro invocation in, rather than pulling it apart and // marking the tts and the ctxt separately. This also goes @@ -527,15 +525,8 @@ fn expand_stmt(stmt: Stmt, fld: &mut MacroExpander) -> SmallVector { _ => return expand_non_macro_stmt(stmt, fld) }; - if let Some(ref attrs) = attrs { - check_attributes(attrs, fld); - } - - // Assert that we drop any macro attributes on the floor here - drop(attrs); - let mut fully_expanded: SmallVector = - expand_mac_invoc(mac.unwrap(), stmt.span, fld); + expand_mac_invoc(mac.unwrap(), attrs.into_attr_vec(), stmt.span, fld); // If this is a macro invocation with a semicolon, then apply that // semicolon to the final statement produced by expansion. @@ -752,7 +743,7 @@ fn expand_pat(p: P, fld: &mut MacroExpander) -> P { } p.and_then(|ast::Pat {node, span, ..}| { match node { - PatKind::Mac(mac) => expand_mac_invoc(mac, span, fld), + PatKind::Mac(mac) => expand_mac_invoc(mac, Vec::new(), span, fld), _ => unreachable!() } }) @@ -1007,8 +998,7 @@ fn expand_impl_item(ii: ast::ImplItem, fld: &mut MacroExpander) span: fld.new_span(ii.span) }), ast::ImplItemKind::Macro(mac) => { - check_attributes(&ii.attrs, fld); - expand_mac_invoc(mac, ii.span, fld) + expand_mac_invoc(mac, ii.attrs, ii.span, fld) } _ => fold::noop_fold_impl_item(ii, fld) } @@ -1052,7 +1042,7 @@ pub fn expand_type(t: P, fld: &mut MacroExpander) -> P { let t = match t.node.clone() { ast::TyKind::Mac(mac) => { if fld.cx.ecfg.features.unwrap().type_macros { - expand_mac_invoc(mac, t.span, fld) + expand_mac_invoc(mac, Vec::new(), t.span, fld) } else { feature_gate::emit_feature_err( &fld.cx.parse_sess.span_diagnostic,