Skip to content

Commit 0558df2

Browse files
committed
Refactor expand_expr
1 parent 1aa34e0 commit 0558df2

File tree

1 file changed

+22
-35
lines changed

1 file changed

+22
-35
lines changed

src/libsyntax/ext/expand.rs

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,24 @@ impl MacroGenerable for Option<P<ast::Expr>> {
8888
}
8989
}
9090

91-
pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
92-
return e.and_then(|ast::Expr {id, node, span, attrs}| match node {
93-
91+
pub fn expand_expr(expr: ast::Expr, fld: &mut MacroExpander) -> P<ast::Expr> {
92+
match expr.node {
9493
// expr_mac should really be expr_ext or something; it's the
9594
// entry-point for all syntax extensions.
9695
ast::ExprKind::Mac(mac) => {
97-
expand_mac_invoc(mac, None, attrs.into_attr_vec(), span, fld)
96+
expand_mac_invoc(mac, None, expr.attrs.into_attr_vec(), expr.span, fld)
9897
}
9998

10099
ast::ExprKind::While(cond, body, opt_ident) => {
101100
let cond = fld.fold_expr(cond);
102101
let (body, opt_ident) = expand_loop_block(body, opt_ident, fld);
103-
fld.cx.expr(span, ast::ExprKind::While(cond, body, opt_ident))
104-
.with_attrs(fold_thin_attrs(attrs, fld))
102+
fld.cx.expr(expr.span, ast::ExprKind::While(cond, body, opt_ident))
103+
.with_attrs(fold_thin_attrs(expr.attrs, fld))
105104
}
106105

107-
ast::ExprKind::WhileLet(pat, expr, body, opt_ident) => {
106+
ast::ExprKind::WhileLet(pat, cond, body, opt_ident) => {
108107
let pat = fld.fold_pat(pat);
109-
let expr = fld.fold_expr(expr);
108+
let cond = fld.fold_expr(cond);
110109

111110
// Hygienic renaming of the body.
112111
let ((body, opt_ident), mut rewritten_pats) =
@@ -118,14 +117,14 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
118117
});
119118
assert!(rewritten_pats.len() == 1);
120119

121-
let wl = ast::ExprKind::WhileLet(rewritten_pats.remove(0), expr, body, opt_ident);
122-
fld.cx.expr(span, wl).with_attrs(fold_thin_attrs(attrs, fld))
120+
let wl = ast::ExprKind::WhileLet(rewritten_pats.remove(0), cond, body, opt_ident);
121+
fld.cx.expr(expr.span, wl).with_attrs(fold_thin_attrs(expr.attrs, fld))
123122
}
124123

125124
ast::ExprKind::Loop(loop_block, opt_ident) => {
126125
let (loop_block, opt_ident) = expand_loop_block(loop_block, opt_ident, fld);
127-
fld.cx.expr(span, ast::ExprKind::Loop(loop_block, opt_ident))
128-
.with_attrs(fold_thin_attrs(attrs, fld))
126+
fld.cx.expr(expr.span, ast::ExprKind::Loop(loop_block, opt_ident))
127+
.with_attrs(fold_thin_attrs(expr.attrs, fld))
129128
}
130129

131130
ast::ExprKind::ForLoop(pat, head, body, opt_ident) => {
@@ -143,7 +142,7 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
143142

144143
let head = fld.fold_expr(head);
145144
let fl = ast::ExprKind::ForLoop(rewritten_pats.remove(0), head, body, opt_ident);
146-
fld.cx.expr(span, fl).with_attrs(fold_thin_attrs(attrs, fld))
145+
fld.cx.expr(expr.span, fl).with_attrs(fold_thin_attrs(expr.attrs, fld))
147146
}
148147

149148
ast::ExprKind::IfLet(pat, sub_expr, body, else_opt) => {
@@ -162,7 +161,7 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
162161
let else_opt = else_opt.map(|else_opt| fld.fold_expr(else_opt));
163162
let sub_expr = fld.fold_expr(sub_expr);
164163
let il = ast::ExprKind::IfLet(rewritten_pats.remove(0), sub_expr, body, else_opt);
165-
fld.cx.expr(span, il).with_attrs(fold_thin_attrs(attrs, fld))
164+
fld.cx.expr(expr.span, il).with_attrs(fold_thin_attrs(expr.attrs, fld))
166165
}
167166

168167
ast::ExprKind::Closure(capture_clause, fn_decl, block, fn_decl_span) => {
@@ -172,21 +171,14 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
172171
rewritten_fn_decl,
173172
rewritten_block,
174173
fn_decl_span);
175-
P(ast::Expr{ id:id,
174+
P(ast::Expr{ id: expr.id,
176175
node: new_node,
177-
span: span,
178-
attrs: fold_thin_attrs(attrs, fld) })
176+
span: expr.span,
177+
attrs: fold_thin_attrs(expr.attrs, fld) })
179178
}
180179

181-
_ => {
182-
P(noop_fold_expr(ast::Expr {
183-
id: id,
184-
node: node,
185-
span: span,
186-
attrs: attrs
187-
}, fld))
188-
}
189-
});
180+
_ => P(noop_fold_expr(expr, fld)),
181+
}
190182
}
191183

192184
/// Expand a macro invocation. Returns the result of expansion.
@@ -1015,19 +1007,14 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
10151007
}
10161008

10171009
fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
1018-
expand_expr(expr, self)
1010+
expr.and_then(|expr| expand_expr(expr, self))
10191011
}
10201012

10211013
fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
1022-
match expr.node {
1023-
ast::ExprKind::Mac(_) => {}
1024-
_ => return Some(expand_expr(expr, self)),
1025-
}
1026-
1027-
expr.and_then(|ast::Expr {node, span, attrs, ..}| match node {
1014+
expr.and_then(|expr| match expr.node {
10281015
ast::ExprKind::Mac(mac) =>
1029-
expand_mac_invoc(mac, None, attrs.into_attr_vec(), span, self),
1030-
_ => unreachable!(),
1016+
expand_mac_invoc(mac, None, expr.attrs.into_attr_vec(), expr.span, self),
1017+
_ => Some(expand_expr(expr, self)),
10311018
})
10321019
}
10331020

0 commit comments

Comments
 (0)