From 2a8f358de7ee71934b8129dff5d908730454d7b1 Mon Sep 17 00:00:00 2001 From: Marvin Löbel Date: Tue, 3 Nov 2015 17:39:51 +0100 Subject: Add syntax support for attributes on expressions and all syntax nodes in statement position. Extended #[cfg] folder to allow removal of statements, and of expressions in optional positions like expression lists and trailing block expressions. Extended lint checker to recognize lint levels on expressions and locals. --- src/libsyntax/ext/asm.rs | 3 ++- src/libsyntax/ext/base.rs | 1 + src/libsyntax/ext/build.rs | 3 +++ src/libsyntax/ext/concat_idents.rs | 1 + src/libsyntax/ext/deriving/debug.rs | 1 + src/libsyntax/ext/expand.rs | 37 ++++++++++++++++++++++++++++--------- src/libsyntax/ext/quote.rs | 1 + 7 files changed, 37 insertions(+), 10 deletions(-) (limited to 'src/libsyntax/ext') diff --git a/src/libsyntax/ext/asm.rs b/src/libsyntax/ext/asm.rs index ac18b9c0e49..d968858f634 100644 --- a/src/libsyntax/ext/asm.rs +++ b/src/libsyntax/ext/asm.rs @@ -233,6 +233,7 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) dialect: dialect, expn_id: expn_id, }), - span: sp + span: sp, + attrs: None, })) } diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 55f0fa5675a..8c93327c322 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -349,6 +349,7 @@ impl DummyResult { id: ast::DUMMY_NODE_ID, node: ast::ExprLit(P(codemap::respan(sp, ast::LitBool(false)))), span: sp, + attrs: None, }) } diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index 4c10a749683..806f5a7ee22 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -525,6 +525,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { init: Some(ex), id: ast::DUMMY_NODE_ID, span: sp, + attrs: None, }); let decl = respan(sp, ast::DeclLocal(local)); P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID))) @@ -548,6 +549,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { init: Some(ex), id: ast::DUMMY_NODE_ID, span: sp, + attrs: None, }); let decl = respan(sp, ast::DeclLocal(local)); P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID))) @@ -584,6 +586,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { id: ast::DUMMY_NODE_ID, node: node, span: span, + attrs: None, }) } diff --git a/src/libsyntax/ext/concat_idents.rs b/src/libsyntax/ext/concat_idents.rs index e9e36546ad6..c2233202b2f 100644 --- a/src/libsyntax/ext/concat_idents.rs +++ b/src/libsyntax/ext/concat_idents.rs @@ -67,6 +67,7 @@ pub fn expand_syntax_ext<'cx>(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree]) } ), span: sp, + attrs: None, }); MacEager::expr(e) } diff --git a/src/libsyntax/ext/deriving/debug.rs b/src/libsyntax/ext/deriving/debug.rs index 2b2e5309883..9488cfb86fc 100644 --- a/src/libsyntax/ext/deriving/debug.rs +++ b/src/libsyntax/ext/deriving/debug.rs @@ -148,6 +148,7 @@ fn stmt_let_undescore(cx: &mut ExtCtxt, init: Some(expr), id: ast::DUMMY_NODE_ID, span: sp, + attrs: None, }); let decl = respan(sp, ast::DeclLocal(local)); P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID))) diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 9b1a7a50201..132b29c7623 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -17,7 +17,7 @@ use ast; use ext::mtwt; use ext::build::AstBuilder; use attr; -use attr::AttrMetaMethods; +use attr::{AttrMetaMethods, WithAttrs}; use codemap; use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute}; use ext::base::*; @@ -37,11 +37,16 @@ use std::collections::HashSet; pub fn expand_expr(e: P, fld: &mut MacroExpander) -> P { let expr_span = e.span; - return e.and_then(|ast::Expr {id, node, span}| match node { + // FIXME: Drop attrs on the floor for now. + return e.and_then(|ast::Expr {id, node, span, attrs}| match node { // expr_mac should really be expr_ext or something; it's the // entry-point for all syntax extensions. ast::ExprMac(mac) => { + + // drop attributes on the macro itself + let _ = attrs; + let expanded_expr = match expand_mac_invoc(mac, span, |r| r.make_expr(), mark_expr, fld) { @@ -60,6 +65,7 @@ pub fn expand_expr(e: P, fld: &mut MacroExpander) -> P { id: ast::DUMMY_NODE_ID, node: e.node, span: span, + attrs: e.attrs, }) } @@ -73,12 +79,14 @@ pub fn expand_expr(e: P, fld: &mut MacroExpander) -> P { let placer = fld.fold_expr(placer); let value_expr = fld.fold_expr(value_expr); fld.cx.expr(span, ast::ExprInPlace(placer, value_expr)) + .with_attrs(attrs) } ast::ExprWhile(cond, body, opt_ident) => { let cond = fld.fold_expr(cond); let (body, opt_ident) = expand_loop_block(body, opt_ident, fld); fld.cx.expr(span, ast::ExprWhile(cond, body, opt_ident)) + .with_attrs(attrs) } ast::ExprWhileLet(pat, expr, body, opt_ident) => { @@ -96,11 +104,13 @@ pub fn expand_expr(e: P, fld: &mut MacroExpander) -> P { assert!(rewritten_pats.len() == 1); fld.cx.expr(span, ast::ExprWhileLet(rewritten_pats.remove(0), expr, body, opt_ident)) + .with_attrs(attrs) } ast::ExprLoop(loop_block, opt_ident) => { let (loop_block, opt_ident) = expand_loop_block(loop_block, opt_ident, fld); fld.cx.expr(span, ast::ExprLoop(loop_block, opt_ident)) + .with_attrs(attrs) } ast::ExprForLoop(pat, head, body, opt_ident) => { @@ -118,6 +128,7 @@ pub fn expand_expr(e: P, fld: &mut MacroExpander) -> P { let head = fld.fold_expr(head); fld.cx.expr(span, ast::ExprForLoop(rewritten_pats.remove(0), head, body, opt_ident)) + .with_attrs(attrs) } ast::ExprIfLet(pat, sub_expr, body, else_opt) => { @@ -136,6 +147,7 @@ pub fn expand_expr(e: P, fld: &mut MacroExpander) -> P { let else_opt = else_opt.map(|else_opt| fld.fold_expr(else_opt)); let sub_expr = fld.fold_expr(sub_expr); fld.cx.expr(span, ast::ExprIfLet(rewritten_pats.remove(0), sub_expr, body, else_opt)) + .with_attrs(attrs) } ast::ExprClosure(capture_clause, fn_decl, block) => { @@ -144,15 +156,18 @@ pub fn expand_expr(e: P, fld: &mut MacroExpander) -> P { let new_node = ast::ExprClosure(capture_clause, rewritten_fn_decl, rewritten_block); - P(ast::Expr{id:id, node: new_node, span: fld.new_span(span)}) + P(ast::Expr{id:id, node: new_node, span: fld.new_span(span), + attrs: None}) + .with_attrs(attrs) } _ => { P(noop_fold_expr(ast::Expr { id: id, node: node, - span: span - }, fld)) + span: span, + attrs: None + }, fld)).with_attrs(attrs) } }); } @@ -486,11 +501,14 @@ pub fn expand_item_mac(it: P, /// Expand a stmt fn expand_stmt(stmt: P, fld: &mut MacroExpander) -> SmallVector> { let stmt = stmt.and_then(|stmt| stmt); - let (mac, style) = match stmt.node { - StmtMac(mac, style) => (mac, style), + let (mac, style, attrs) = match stmt.node { + StmtMac(mac, style, attrs) => (mac, style, attrs), _ => return expand_non_macro_stmt(stmt, fld) }; + // FIXME: drop attrs for macros. + let _ = attrs; + let maybe_new_items = expand_mac_invoc(mac.and_then(|m| m), stmt.span, |r| r.make_stmts(), @@ -538,7 +556,7 @@ fn expand_non_macro_stmt(Spanned {node, span: stmt_span}: Stmt, fld: &mut MacroE StmtDecl(decl, node_id) => decl.and_then(|Spanned {node: decl, span}| match decl { DeclLocal(local) => { // take it apart: - let rewritten_local = local.map(|Local {id, pat, ty, init, span}| { + let rewritten_local = local.map(|Local {id, pat, ty, init, span, attrs}| { // expand the ty since TyFixedLengthVec contains an Expr // and thus may have a macro use let expanded_ty = ty.map(|t| fld.fold_ty(t)); @@ -568,7 +586,8 @@ fn expand_non_macro_stmt(Spanned {node, span: stmt_span}: Stmt, fld: &mut MacroE pat: rewritten_pat, // also, don't forget to expand the init: init: init.map(|e| fld.fold_expr(e)), - span: span + span: span, + attrs: attrs } }); SmallVector::one(P(Spanned { diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index 5e5b8158181..fc6cacb40f1 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -242,6 +242,7 @@ pub mod rt { id: ast::DUMMY_NODE_ID, node: ast::ExprLit(P(self.clone())), span: DUMMY_SP, + attrs: None, }).to_tokens(cx) } } -- cgit 1.4.1-3-g733a5