Skip to content

Commit c3b9047

Browse files
committed
syntax: make match arms store the expr directly.
Previously `ast::Arm` was always storing a single `ast::Expr` wrapped in an `ast::Block` (for historical reasons, AIUI), so we might as just store that expr directly. Closes #3085.
1 parent 3f3425a commit c3b9047

File tree

15 files changed

+30
-57
lines changed

15 files changed

+30
-57
lines changed

Diff for: src/librustc/middle/cfg/construct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ impl CFGBuilder {
300300
guard_exit = self.opt_expr(arm.guard, guard_exit); // 2
301301
let pats_exit = self.pats_any(arm.pats.as_slice(),
302302
guard_exit); // 3
303-
let body_exit = self.block(arm.body, pats_exit); // 4
303+
let body_exit = self.expr(arm.body, pats_exit); // 4
304304
self.add_contained_edge(body_exit, expr_exit); // 5
305305
}
306306
expr_exit

Diff for: src/librustc/middle/dataflow.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ impl<'a, O:DataFlowOperator> PropagationContext<'a, O> {
534534
self.walk_pat_alternatives(arm.pats.as_slice(),
535535
body,
536536
loop_scopes);
537-
self.walk_block(arm.body, body, loop_scopes);
537+
self.walk_expr(arm.body, body, loop_scopes);
538538
join_bits(&self.dfcx.oper, body, in_out);
539539
}
540540
}
@@ -915,4 +915,3 @@ fn bit_str(bit: uint) -> ~str {
915915
let lobits = 1 << (bit & 0xFF);
916916
format!("[{}:{}-{:02x}]", bit, byte, lobits)
917917
}
918-

Diff for: src/librustc/middle/liveness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ impl Liveness {
11251125
let mut first_merge = true;
11261126
for arm in arms.iter() {
11271127
let body_succ =
1128-
self.propagate_through_block(arm.body, succ);
1128+
self.propagate_through_expr(arm.body, succ);
11291129
let guard_succ =
11301130
self.propagate_through_opt_expr(arm.guard, body_succ);
11311131
let arm_succ =

Diff for: src/librustc/middle/moves.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ impl VisitContext {
632632
self.consume_expr(*guard);
633633
}
634634

635-
self.consume_block(arm.body);
635+
self.consume_expr(arm.body);
636636
}
637637

638638
pub fn use_pat(&mut self, pat: @Pat) {

Diff for: src/librustc/middle/resolve.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4248,7 +4248,7 @@ impl Resolver {
42484248
self.check_consistent_bindings(arm);
42494249

42504250
visit::walk_expr_opt(self, arm.guard, ());
4251-
self.resolve_block(arm.body);
4251+
self.resolve_expr(arm.body);
42524252

42534253
let mut value_ribs = self.value_ribs.borrow_mut();
42544254
value_ribs.get().pop();

Diff for: src/librustc/middle/trans/_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1939,7 +1939,7 @@ fn trans_match_inner<'a>(scope_cx: &'a Block<'a>,
19391939
let cleanup_scope = fcx.push_custom_cleanup_scope();
19401940
bcx = insert_lllocals(bcx, arm_data.bindings_map,
19411941
cleanup::CustomScope(cleanup_scope));
1942-
bcx = controlflow::trans_block(bcx, arm_data.arm.body, dest);
1942+
bcx = expr::trans_into(bcx, arm_data.arm.body, dest);
19431943
bcx = fcx.pop_and_trans_custom_cleanup_scope(bcx, cleanup_scope);
19441944
arm_cxs.push(bcx);
19451945
}

Diff for: src/librustc/middle/trans/debuginfo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2665,7 +2665,7 @@ fn populate_scope_map(cx: &CrateContext,
26652665
walk_expr(cx, *guard_exp, scope_stack, scope_map)
26662666
}
26672667

2668-
walk_block(cx, arm_ref.body, scope_stack, scope_map);
2668+
walk_expr(cx, arm_ref.body, scope_stack, scope_map);
26692669
})
26702670
}
26712671
}

Diff for: src/librustc/middle/typeck/check/_match.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use middle::pat_util::{PatIdMap, pat_id_map, pat_is_binding, pat_is_const};
1414
use middle::ty;
1515
use middle::typeck::check::demand;
16-
use middle::typeck::check::{check_block, check_expr_has_type, FnCtxt};
16+
use middle::typeck::check::{check_expr, check_expr_has_type, FnCtxt};
1717
use middle::typeck::check::{instantiate_path, lookup_def};
1818
use middle::typeck::check::{structure_of, valid_range_bounds};
1919
use middle::typeck::infer;
@@ -74,7 +74,7 @@ pub fn check_match(fcx: @FnCtxt,
7474
},
7575
None => ()
7676
}
77-
check_block(fcx, arm.body);
77+
check_expr(fcx, arm.body);
7878
let bty = fcx.node_ty(arm.body.id);
7979
saw_err = saw_err || ty::type_is_error(bty);
8080
if guard_err {

Diff for: src/libsyntax/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ pub enum Decl_ {
491491
pub struct Arm {
492492
pats: Vec<@Pat> ,
493493
guard: Option<@Expr>,
494-
body: P<Block>,
494+
body: @Expr,
495495
}
496496

497497
#[deriving(Clone, Eq, Encodable, Decodable, Hash)]

Diff for: src/libsyntax/ext/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
679679
ast::Arm {
680680
pats: pats,
681681
guard: None,
682-
body: self.block_expr(expr)
682+
body: expr
683683
}
684684
}
685685

Diff for: src/libsyntax/ext/deriving/primitive.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn cs_from(name: &str, cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure
110110
let arm = ast::Arm {
111111
pats: vec!(cx.pat_wild(span)),
112112
guard: Some(guard),
113-
body: cx.block_expr(body),
113+
body: body,
114114
};
115115

116116
arms.push(arm);
@@ -129,7 +129,7 @@ fn cs_from(name: &str, cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure
129129
let arm = ast::Arm {
130130
pats: vec!(cx.pat_wild(trait_span)),
131131
guard: None,
132-
body: cx.block_expr(cx.expr_none(trait_span)),
132+
body: cx.expr_none(trait_span),
133133
};
134134
arms.push(arm);
135135

Diff for: src/libsyntax/fold.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub trait Folder {
117117
Arm {
118118
pats: a.pats.map(|x| self.fold_pat(*x)),
119119
guard: a.guard.map(|x| self.fold_expr(x)),
120-
body: self.fold_block(a.body),
120+
body: self.fold_expr(a.body),
121121
}
122122
}
123123

@@ -933,4 +933,3 @@ mod test {
933933
~"zz!zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)))");
934934
}
935935
}
936-

Diff for: src/libsyntax/parse/parser.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -2634,16 +2634,7 @@ impl Parser {
26342634
self.eat(&token::COMMA);
26352635
}
26362636

2637-
let blk = P(ast::Block {
2638-
view_items: Vec::new(),
2639-
stmts: Vec::new(),
2640-
expr: Some(expr),
2641-
id: ast::DUMMY_NODE_ID,
2642-
rules: DefaultBlock,
2643-
span: expr.span,
2644-
});
2645-
2646-
arms.push(ast::Arm { pats: pats, guard: guard, body: blk });
2637+
arms.push(ast::Arm { pats: pats, guard: guard, body: expr });
26472638
}
26482639
let hi = self.span.hi;
26492640
self.bump();

Diff for: src/libsyntax/print/pprust.rs

+14-30
Original file line numberDiff line numberDiff line change
@@ -1352,38 +1352,22 @@ pub fn print_expr(s: &mut State, expr: &ast::Expr) -> io::IoResult<()> {
13521352
}
13531353
try!(word_space(s, "=>"));
13541354

1355-
// Extract the expression from the extra block the parser adds
1356-
// in the case of foo => expr
1357-
if arm.body.view_items.is_empty() &&
1358-
arm.body.stmts.is_empty() &&
1359-
arm.body.rules == ast::DefaultBlock &&
1360-
arm.body.expr.is_some()
1361-
{
1362-
match arm.body.expr {
1363-
Some(expr) => {
1364-
match expr.node {
1365-
ast::ExprBlock(blk) => {
1366-
// the block will close the pattern's ibox
1367-
try!(print_block_unclosed_indent(
1368-
s, blk, indent_unit));
1369-
}
1370-
_ => {
1371-
try!(end(s)); // close the ibox for the pattern
1372-
try!(print_expr(s, expr));
1373-
}
1374-
}
1375-
if !expr_is_simple_block(expr)
1376-
&& i < len - 1 {
1377-
try!(word(&mut s.s, ","));
1378-
}
1379-
try!(end(s)); // close enclosing cbox
1380-
}
1381-
None => fail!()
1355+
match arm.body.node {
1356+
ast::ExprBlock(blk) => {
1357+
// the block will close the pattern's ibox
1358+
try!(print_block_unclosed_indent(
1359+
s, blk, indent_unit));
13821360
}
1383-
} else {
1384-
// the block will close the pattern's ibox
1385-
try!(print_block_unclosed_indent(s, arm.body, indent_unit));
1361+
_ => {
1362+
try!(end(s)); // close the ibox for the pattern
1363+
try!(print_expr(s, arm.body));
1364+
}
1365+
}
1366+
if !expr_is_simple_block(expr)
1367+
&& i < len - 1 {
1368+
try!(word(&mut s.s, ","));
13861369
}
1370+
try!(end(s)); // close enclosing cbox
13871371
}
13881372
try!(bclose_(s, expr.span, indent_unit));
13891373
}

Diff for: src/libsyntax/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -765,5 +765,5 @@ pub fn walk_arm<E: Clone, V: Visitor<E>>(visitor: &mut V, arm: &Arm, env: E) {
765765
visitor.visit_pat(*pattern, env.clone())
766766
}
767767
walk_expr_opt(visitor, arm.guard, env.clone());
768-
visitor.visit_block(arm.body, env)
768+
visitor.visit_expr(arm.body, env)
769769
}

0 commit comments

Comments
 (0)