Skip to content

Commit

Permalink
syntax: make match arms store the expr directly.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
huonw committed Mar 3, 2014
1 parent 3f3425a commit c3b9047
Show file tree
Hide file tree
Showing 15 changed files with 30 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/librustc/middle/cfg/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ impl CFGBuilder {
guard_exit = self.opt_expr(arm.guard, guard_exit); // 2
let pats_exit = self.pats_any(arm.pats.as_slice(),
guard_exit); // 3
let body_exit = self.block(arm.body, pats_exit); // 4
let body_exit = self.expr(arm.body, pats_exit); // 4
self.add_contained_edge(body_exit, expr_exit); // 5
}
expr_exit
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/middle/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ impl<'a, O:DataFlowOperator> PropagationContext<'a, O> {
self.walk_pat_alternatives(arm.pats.as_slice(),
body,
loop_scopes);
self.walk_block(arm.body, body, loop_scopes);
self.walk_expr(arm.body, body, loop_scopes);
join_bits(&self.dfcx.oper, body, in_out);
}
}
Expand Down Expand Up @@ -915,4 +915,3 @@ fn bit_str(bit: uint) -> ~str {
let lobits = 1 << (bit & 0xFF);
format!("[{}:{}-{:02x}]", bit, byte, lobits)
}

2 changes: 1 addition & 1 deletion src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ impl Liveness {
let mut first_merge = true;
for arm in arms.iter() {
let body_succ =
self.propagate_through_block(arm.body, succ);
self.propagate_through_expr(arm.body, succ);
let guard_succ =
self.propagate_through_opt_expr(arm.guard, body_succ);
let arm_succ =
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/moves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ impl VisitContext {
self.consume_expr(*guard);
}

self.consume_block(arm.body);
self.consume_expr(arm.body);
}

pub fn use_pat(&mut self, pat: @Pat) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4248,7 +4248,7 @@ impl Resolver {
self.check_consistent_bindings(arm);

visit::walk_expr_opt(self, arm.guard, ());
self.resolve_block(arm.body);
self.resolve_expr(arm.body);

let mut value_ribs = self.value_ribs.borrow_mut();
value_ribs.get().pop();
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1939,7 +1939,7 @@ fn trans_match_inner<'a>(scope_cx: &'a Block<'a>,
let cleanup_scope = fcx.push_custom_cleanup_scope();
bcx = insert_lllocals(bcx, arm_data.bindings_map,
cleanup::CustomScope(cleanup_scope));
bcx = controlflow::trans_block(bcx, arm_data.arm.body, dest);
bcx = expr::trans_into(bcx, arm_data.arm.body, dest);
bcx = fcx.pop_and_trans_custom_cleanup_scope(bcx, cleanup_scope);
arm_cxs.push(bcx);
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/debuginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2665,7 +2665,7 @@ fn populate_scope_map(cx: &CrateContext,
walk_expr(cx, *guard_exp, scope_stack, scope_map)
}

walk_block(cx, arm_ref.body, scope_stack, scope_map);
walk_expr(cx, arm_ref.body, scope_stack, scope_map);
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use middle::pat_util::{PatIdMap, pat_id_map, pat_is_binding, pat_is_const};
use middle::ty;
use middle::typeck::check::demand;
use middle::typeck::check::{check_block, check_expr_has_type, FnCtxt};
use middle::typeck::check::{check_expr, check_expr_has_type, FnCtxt};
use middle::typeck::check::{instantiate_path, lookup_def};
use middle::typeck::check::{structure_of, valid_range_bounds};
use middle::typeck::infer;
Expand Down Expand Up @@ -74,7 +74,7 @@ pub fn check_match(fcx: @FnCtxt,
},
None => ()
}
check_block(fcx, arm.body);
check_expr(fcx, arm.body);
let bty = fcx.node_ty(arm.body.id);
saw_err = saw_err || ty::type_is_error(bty);
if guard_err {
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ pub enum Decl_ {
pub struct Arm {
pats: Vec<@Pat> ,
guard: Option<@Expr>,
body: P<Block>,
body: @Expr,
}

#[deriving(Clone, Eq, Encodable, Decodable, Hash)]
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
ast::Arm {
pats: pats,
guard: None,
body: self.block_expr(expr)
body: expr
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ext/deriving/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn cs_from(name: &str, cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure
let arm = ast::Arm {
pats: vec!(cx.pat_wild(span)),
guard: Some(guard),
body: cx.block_expr(body),
body: body,
};

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

Expand Down
3 changes: 1 addition & 2 deletions src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub trait Folder {
Arm {
pats: a.pats.map(|x| self.fold_pat(*x)),
guard: a.guard.map(|x| self.fold_expr(x)),
body: self.fold_block(a.body),
body: self.fold_expr(a.body),
}
}

Expand Down Expand Up @@ -933,4 +933,3 @@ mod test {
~"zz!zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)))");
}
}

11 changes: 1 addition & 10 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2634,16 +2634,7 @@ impl Parser {
self.eat(&token::COMMA);
}

let blk = P(ast::Block {
view_items: Vec::new(),
stmts: Vec::new(),
expr: Some(expr),
id: ast::DUMMY_NODE_ID,
rules: DefaultBlock,
span: expr.span,
});

arms.push(ast::Arm { pats: pats, guard: guard, body: blk });
arms.push(ast::Arm { pats: pats, guard: guard, body: expr });
}
let hi = self.span.hi;
self.bump();
Expand Down
44 changes: 14 additions & 30 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1352,38 +1352,22 @@ pub fn print_expr(s: &mut State, expr: &ast::Expr) -> io::IoResult<()> {
}
try!(word_space(s, "=>"));

// Extract the expression from the extra block the parser adds
// in the case of foo => expr
if arm.body.view_items.is_empty() &&
arm.body.stmts.is_empty() &&
arm.body.rules == ast::DefaultBlock &&
arm.body.expr.is_some()
{
match arm.body.expr {
Some(expr) => {
match expr.node {
ast::ExprBlock(blk) => {
// the block will close the pattern's ibox
try!(print_block_unclosed_indent(
s, blk, indent_unit));
}
_ => {
try!(end(s)); // close the ibox for the pattern
try!(print_expr(s, expr));
}
}
if !expr_is_simple_block(expr)
&& i < len - 1 {
try!(word(&mut s.s, ","));
}
try!(end(s)); // close enclosing cbox
}
None => fail!()
match arm.body.node {
ast::ExprBlock(blk) => {
// the block will close the pattern's ibox
try!(print_block_unclosed_indent(
s, blk, indent_unit));
}
} else {
// the block will close the pattern's ibox
try!(print_block_unclosed_indent(s, arm.body, indent_unit));
_ => {
try!(end(s)); // close the ibox for the pattern
try!(print_expr(s, arm.body));
}
}
if !expr_is_simple_block(expr)
&& i < len - 1 {
try!(word(&mut s.s, ","));
}
try!(end(s)); // close enclosing cbox
}
try!(bclose_(s, expr.span, indent_unit));
}
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,5 +765,5 @@ pub fn walk_arm<E: Clone, V: Visitor<E>>(visitor: &mut V, arm: &Arm, env: E) {
visitor.visit_pat(*pattern, env.clone())
}
walk_expr_opt(visitor, arm.guard, env.clone());
visitor.visit_block(arm.body, env)
visitor.visit_expr(arm.body, env)
}

5 comments on commit c3b9047

@bors
Copy link
Contributor

@bors bors commented on c3b9047 Mar 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at huonw@c3b9047

@bors
Copy link
Contributor

@bors bors commented on c3b9047 Mar 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging huonw/rust/de-block-arms = c3b9047 into auto

@bors
Copy link
Contributor

@bors bors commented on c3b9047 Mar 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huonw/rust/de-block-arms = c3b9047 merged ok, testing candidate = dc18659

@bors
Copy link
Contributor

@bors bors commented on c3b9047 Mar 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on c3b9047 Mar 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = dc18659

Please sign in to comment.