Skip to content

Commit 683e480

Browse files
committed
Refactor away WithAttrs trait
1 parent 5bf7970 commit 683e480

File tree

2 files changed

+15
-39
lines changed

2 files changed

+15
-39
lines changed

src/libsyntax/attr.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -889,21 +889,6 @@ pub trait HasAttrs: Sized {
889889
fn map_attrs<F: FnOnce(Vec<ast::Attribute>) -> Vec<ast::Attribute>>(self, f: F) -> Self;
890890
}
891891

892-
/// A cheap way to add Attributes to an AST node.
893-
pub trait WithAttrs {
894-
// FIXME: Could be extended to anything IntoIter<Item=Attribute>
895-
fn with_attrs(self, attrs: ThinAttributes) -> Self;
896-
}
897-
898-
impl<T: HasAttrs> WithAttrs for T {
899-
fn with_attrs(self, attrs: ThinAttributes) -> Self {
900-
self.map_attrs(|mut orig_attrs| {
901-
orig_attrs.extend(attrs.into_attr_vec());
902-
orig_attrs
903-
})
904-
}
905-
}
906-
907892
impl HasAttrs for Vec<Attribute> {
908893
fn attrs(&self) -> &[Attribute] {
909894
&self

src/libsyntax/ext/expand.rs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ use ast::{MacStmtStyle, Mrk, Stmt, StmtKind, ItemKind};
1414
use ast::TokenTree;
1515
use ast;
1616
use ext::mtwt;
17-
use ext::build::AstBuilder;
1817
use attr;
19-
use attr::{AttrMetaMethods, WithAttrs, ThinAttributesExt};
18+
use attr::{AttrMetaMethods, ThinAttributesExt};
2019
use codemap;
2120
use codemap::{Span, Spanned, ExpnInfo, ExpnId, NameAndSpan, MacroBang, MacroAttribute};
2221
use config::StripUnconfigured;
@@ -87,19 +86,18 @@ impl MacroGenerable for Option<P<ast::Expr>> {
8786
}
8887
}
8988

90-
pub fn expand_expr(expr: ast::Expr, fld: &mut MacroExpander) -> P<ast::Expr> {
89+
pub fn expand_expr(mut expr: ast::Expr, fld: &mut MacroExpander) -> P<ast::Expr> {
9190
match expr.node {
9291
// expr_mac should really be expr_ext or something; it's the
9392
// entry-point for all syntax extensions.
9493
ast::ExprKind::Mac(mac) => {
95-
expand_mac_invoc(mac, None, expr.attrs.into_attr_vec(), expr.span, fld)
94+
return expand_mac_invoc(mac, None, expr.attrs.into_attr_vec(), expr.span, fld);
9695
}
9796

9897
ast::ExprKind::While(cond, body, opt_ident) => {
9998
let cond = fld.fold_expr(cond);
10099
let (body, opt_ident) = expand_loop_block(body, opt_ident, fld);
101-
fld.cx.expr(expr.span, ast::ExprKind::While(cond, body, opt_ident))
102-
.with_attrs(fold_thin_attrs(expr.attrs, fld))
100+
expr.node = ast::ExprKind::While(cond, body, opt_ident);
103101
}
104102

105103
ast::ExprKind::WhileLet(pat, cond, body, opt_ident) => {
@@ -116,14 +114,12 @@ pub fn expand_expr(expr: ast::Expr, fld: &mut MacroExpander) -> P<ast::Expr> {
116114
});
117115
assert!(rewritten_pats.len() == 1);
118116

119-
let wl = ast::ExprKind::WhileLet(rewritten_pats.remove(0), cond, body, opt_ident);
120-
fld.cx.expr(expr.span, wl).with_attrs(fold_thin_attrs(expr.attrs, fld))
117+
expr.node = ast::ExprKind::WhileLet(rewritten_pats.remove(0), cond, body, opt_ident);
121118
}
122119

123120
ast::ExprKind::Loop(loop_block, opt_ident) => {
124121
let (loop_block, opt_ident) = expand_loop_block(loop_block, opt_ident, fld);
125-
fld.cx.expr(expr.span, ast::ExprKind::Loop(loop_block, opt_ident))
126-
.with_attrs(fold_thin_attrs(expr.attrs, fld))
122+
expr.node = ast::ExprKind::Loop(loop_block, opt_ident);
127123
}
128124

129125
ast::ExprKind::ForLoop(pat, head, body, opt_ident) => {
@@ -140,8 +136,7 @@ pub fn expand_expr(expr: ast::Expr, fld: &mut MacroExpander) -> P<ast::Expr> {
140136
assert!(rewritten_pats.len() == 1);
141137

142138
let head = fld.fold_expr(head);
143-
let fl = ast::ExprKind::ForLoop(rewritten_pats.remove(0), head, body, opt_ident);
144-
fld.cx.expr(expr.span, fl).with_attrs(fold_thin_attrs(expr.attrs, fld))
139+
expr.node = ast::ExprKind::ForLoop(rewritten_pats.remove(0), head, body, opt_ident);
145140
}
146141

147142
ast::ExprKind::IfLet(pat, sub_expr, body, else_opt) => {
@@ -159,25 +154,21 @@ pub fn expand_expr(expr: ast::Expr, fld: &mut MacroExpander) -> P<ast::Expr> {
159154

160155
let else_opt = else_opt.map(|else_opt| fld.fold_expr(else_opt));
161156
let sub_expr = fld.fold_expr(sub_expr);
162-
let il = ast::ExprKind::IfLet(rewritten_pats.remove(0), sub_expr, body, else_opt);
163-
fld.cx.expr(expr.span, il).with_attrs(fold_thin_attrs(expr.attrs, fld))
157+
expr.node = ast::ExprKind::IfLet(rewritten_pats.remove(0), sub_expr, body, else_opt);
164158
}
165159

166160
ast::ExprKind::Closure(capture_clause, fn_decl, block, fn_decl_span) => {
167161
let (rewritten_fn_decl, rewritten_block)
168162
= expand_and_rename_fn_decl_and_block(fn_decl, block, fld);
169-
let new_node = ast::ExprKind::Closure(capture_clause,
170-
rewritten_fn_decl,
171-
rewritten_block,
172-
fn_decl_span);
173-
P(ast::Expr{ id: expr.id,
174-
node: new_node,
175-
span: expr.span,
176-
attrs: fold_thin_attrs(expr.attrs, fld) })
163+
expr.node = ast::ExprKind::Closure(capture_clause,
164+
rewritten_fn_decl,
165+
rewritten_block,
166+
fn_decl_span);
177167
}
178168

179-
_ => P(noop_fold_expr(expr, fld)),
180-
}
169+
_ => expr = noop_fold_expr(expr, fld),
170+
};
171+
P(expr)
181172
}
182173

183174
/// Expand a macro invocation. Returns the result of expansion.

0 commit comments

Comments
 (0)