Skip to content

Commit 1418311

Browse files
committed
syntax: aesthetic improvements to the for desugaring.
1 parent 07351b4 commit 1418311

File tree

1 file changed

+38
-130
lines changed

1 file changed

+38
-130
lines changed

src/libsyntax/ext/expand.rs

Lines changed: 38 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ use ast::{item_mac, Mrk, Stmt_, StmtDecl, StmtMac, StmtExpr, StmtSemi};
1414
use ast::{token_tree};
1515
use ast;
1616
use ast_util::{mtwt_outer_mark, new_rename, new_mark};
17+
use ext::build::AstBuilder;
1718
use attr;
1819
use attr::AttrMetaMethods;
1920
use codemap;
20-
use codemap::{Span, Spanned, spanned, ExpnInfo, NameAndSpan};
21+
use codemap::{Span, Spanned, ExpnInfo, NameAndSpan};
2122
use ext::base::*;
2223
use fold::*;
23-
use opt_vec;
2424
use parse;
2525
use parse::{parse_item_from_source_str};
2626
use parse::token;
@@ -33,7 +33,7 @@ use std::vec;
3333
pub fn expand_expr(extsbox: @mut SyntaxEnv,
3434
cx: @ExtCtxt,
3535
e: &Expr_,
36-
s: Span,
36+
span: Span,
3737
fld: @ast_fold,
3838
orig: @fn(&Expr_, Span, @ast_fold) -> (Expr_, Span))
3939
-> (Expr_, Span) {
@@ -66,7 +66,7 @@ pub fn expand_expr(extsbox: @mut SyntaxEnv,
6666
}
6767
Some(@SE(NormalTT(expandfun, exp_span))) => {
6868
cx.bt_push(ExpnInfo {
69-
call_site: s,
69+
call_site: span,
7070
callee: NameAndSpan {
7171
name: extnamestr,
7272
span: exp_span,
@@ -98,7 +98,7 @@ pub fn expand_expr(extsbox: @mut SyntaxEnv,
9898
fld.fold_expr(marked_after).node.clone();
9999
cx.bt_pop();
100100

101-
(fully_expanded, s)
101+
(fully_expanded, span)
102102
}
103103
_ => {
104104
cx.span_fatal(
@@ -112,62 +112,18 @@ pub fn expand_expr(extsbox: @mut SyntaxEnv,
112112
}
113113

114114
// Desugar expr_for_loop
115-
// From: `for <src_pat> in <src_expr> <src_loop_block>`
115+
// From: `['<ident>:] for <src_pat> in <src_expr> <src_loop_block>`
116116
ast::ExprForLoop(src_pat, src_expr, ref src_loop_block, opt_ident) => {
117-
let src_pat = src_pat.clone();
118-
let src_expr = src_expr.clone();
119-
120117
// Expand any interior macros etc.
121118
// NB: we don't fold pats yet. Curious.
122-
let src_expr = fld.fold_expr(src_expr).clone();
123-
let src_loop_block = fld.fold_block(src_loop_block).clone();
124-
125-
let span = s;
126-
let lo = s.lo;
127-
let hi = s.hi;
128-
129-
pub fn mk_expr(cx: @ExtCtxt, span: Span,
130-
node: Expr_) -> @ast::Expr {
131-
@ast::Expr {
132-
id: cx.next_id(),
133-
node: node,
134-
span: span,
135-
}
136-
}
137-
138-
fn mk_block(cx: @ExtCtxt,
139-
stmts: &[@ast::Stmt],
140-
expr: Option<@ast::Expr>,
141-
span: Span) -> ast::Block {
142-
ast::Block {
143-
view_items: ~[],
144-
stmts: stmts.to_owned(),
145-
expr: expr,
146-
id: cx.next_id(),
147-
rules: ast::DefaultBlock,
148-
span: span,
149-
}
150-
}
151-
152-
fn mk_simple_path(ident: ast::Ident, span: Span) -> ast::Path {
153-
ast::Path {
154-
span: span,
155-
global: false,
156-
segments: ~[
157-
ast::PathSegment {
158-
identifier: ident,
159-
lifetime: None,
160-
types: opt_vec::Empty,
161-
}
162-
],
163-
}
164-
}
119+
let src_expr = fld.fold_expr(src_expr);
120+
let src_loop_block = fld.fold_block(src_loop_block);
165121

166122
// to:
167123
//
168124
// {
169125
// let _i = &mut <src_expr>;
170-
// loop {
126+
// ['<ident>:] loop {
171127
// match i.next() {
172128
// None => break,
173129
// Some(<src_pat>) => <src_loop_block>
@@ -176,98 +132,50 @@ pub fn expand_expr(extsbox: @mut SyntaxEnv,
176132
// }
177133

178134
let local_ident = token::gensym_ident("i");
179-
let some_ident = token::str_to_ident("Some");
180-
let none_ident = token::str_to_ident("None");
181-
let next_ident = token::str_to_ident("next");
135+
let next_ident = cx.ident_of("next");
136+
let none_ident = cx.ident_of("None");
182137

183-
let local_path_1 = mk_simple_path(local_ident, span);
184-
let local_path_2 = mk_simple_path(local_ident, span);
185-
let some_path = mk_simple_path(some_ident, span);
186-
let none_path = mk_simple_path(none_ident, span);
138+
let local_path = cx.path_ident(span, local_ident);
139+
let some_path = cx.path_ident(span, cx.ident_of("Some"));
187140

188141
// `let i = &mut <src_expr>`
189-
let iter_decl_stmt = {
190-
let ty = ast::Ty {
191-
id: cx.next_id(),
192-
node: ast::ty_infer,
193-
span: span
194-
};
195-
let local = @ast::Local {
196-
is_mutbl: false,
197-
ty: ty,
198-
pat: @ast::Pat {
199-
id: cx.next_id(),
200-
node: ast::PatIdent(ast::BindInfer, local_path_1, None),
201-
span: src_expr.span
202-
},
203-
init: Some(mk_expr(cx, src_expr.span,
204-
ast::ExprAddrOf(ast::MutMutable, src_expr))),
205-
id: cx.next_id(),
206-
span: src_expr.span,
207-
};
208-
let e = @spanned(src_expr.span.lo,
209-
src_expr.span.hi,
210-
ast::DeclLocal(local));
211-
@spanned(lo, hi, ast::StmtDecl(e, cx.next_id()))
212-
};
142+
let iter_decl_stmt = cx.stmt_let(span, false, local_ident,
143+
cx.expr_mut_addr_of(span, src_expr));
213144

214-
// `None => break;`
145+
// `None => break ['<ident>];`
215146
let none_arm = {
216-
let break_expr = mk_expr(cx, span, ast::ExprBreak(None));
217-
let break_stmt = @spanned(lo, hi, ast::StmtExpr(break_expr, cx.next_id()));
218-
let none_block = mk_block(cx, [break_stmt], None, span);
219-
let none_pat = @ast::Pat {
220-
id: cx.next_id(),
221-
node: ast::PatIdent(ast::BindInfer, none_path, None),
222-
span: span
223-
};
224-
ast::Arm {
225-
pats: ~[none_pat],
226-
guard: None,
227-
body: none_block
228-
}
147+
let break_expr = cx.expr(span, ast::ExprBreak(opt_ident));
148+
let none_pat = cx.pat_ident(span, none_ident);
149+
cx.arm(span, ~[none_pat], break_expr)
229150
};
230151

231152
// `Some(<src_pat>) => <src_loop_block>`
232-
let some_arm = {
233-
let pat = @ast::Pat {
234-
id: cx.next_id(),
235-
node: ast::PatEnum(some_path, Some(~[src_pat])),
236-
span: src_pat.span
237-
};
238-
ast::Arm {
239-
pats: ~[pat],
240-
guard: None,
241-
body: src_loop_block
242-
}
243-
};
153+
let some_arm =
154+
cx.arm(span,
155+
~[cx.pat_enum(span, some_path, ~[src_pat])],
156+
cx.expr_block(src_loop_block));
244157

245158
// `match i.next() { ... }`
246-
let match_stmt = {
247-
let local_expr = mk_expr(cx, span, ast::ExprPath(local_path_2));
248-
let next_call_expr = mk_expr(cx, span,
249-
ast::ExprMethodCall(cx.next_id(),
250-
local_expr, next_ident,
251-
~[], ~[], ast::NoSugar));
252-
let match_expr = mk_expr(cx, span, ast::ExprMatch(next_call_expr,
253-
~[none_arm, some_arm]));
254-
@spanned(lo, hi, ast::StmtExpr(match_expr, cx.next_id()))
255-
};
159+
let match_expr = {
160+
let next_call_expr =
161+
cx.expr_method_call(span, cx.expr_path(local_path), next_ident, ~[]);
256162

257-
// `loop { ... }`
258-
let loop_block = {
259-
let loop_body_block = mk_block(cx, [match_stmt], None, span);
260-
let loop_body_expr = mk_expr(cx, span, ast::ExprLoop(loop_body_block, opt_ident));
261-
let loop_body_stmt = @spanned(lo, hi, ast::StmtExpr(loop_body_expr, cx.next_id()));
262-
mk_block(cx, [iter_decl_stmt,
263-
loop_body_stmt],
264-
None, span)
163+
cx.expr_match(span, next_call_expr, ~[none_arm, some_arm])
265164
};
266165

267-
(ast::ExprBlock(loop_block), span)
166+
// ['ident:] loop { ... }
167+
let loop_expr = cx.expr(span,
168+
ast::ExprLoop(cx.block_expr(match_expr), opt_ident));
169+
170+
// `{ let ... ; loop { ... } }`
171+
let block = cx.block(span,
172+
~[iter_decl_stmt],
173+
Some(loop_expr));
174+
175+
(ast::ExprBlock(block), span)
268176
}
269177

270-
_ => orig(e, s, fld)
178+
_ => orig(e, span, fld)
271179
}
272180
}
273181

0 commit comments

Comments
 (0)