Skip to content

Commit b7da35a

Browse files
committed
Remove field expr of ast::Block
1 parent 4960f2f commit b7da35a

File tree

19 files changed

+88
-120
lines changed

19 files changed

+88
-120
lines changed

src/librustc/hir/lowering.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,23 @@ impl<'a> LoweringContext<'a> {
587587
}
588588

589589
fn lower_block(&mut self, b: &Block) -> P<hir::Block> {
590+
let mut stmts = Vec::new();
591+
let mut expr = None;
592+
593+
if let Some((last, rest)) = b.stmts.split_last() {
594+
stmts = rest.iter().map(|s| self.lower_stmt(s)).collect::<Vec<_>>();
595+
let last = self.lower_stmt(last);
596+
if let hir::StmtExpr(e, _) = last.node {
597+
expr = Some(e);
598+
} else {
599+
stmts.push(last);
600+
}
601+
}
602+
590603
P(hir::Block {
591604
id: b.id,
592-
stmts: b.stmts.iter().map(|s| self.lower_stmt(s)).collect(),
593-
expr: b.expr.as_ref().map(|ref x| self.lower_expr(x)),
605+
stmts: stmts.into(),
606+
expr: expr,
594607
rules: self.lower_block_check_mode(&b.rules),
595608
span: b.span,
596609
})

src/librustc_driver/pretty.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,10 @@ impl fold::Folder for ReplaceBodyWithLoop {
657657
fn fold_block(&mut self, b: P<ast::Block>) -> P<ast::Block> {
658658
fn expr_to_block(rules: ast::BlockCheckMode, e: Option<P<ast::Expr>>) -> P<ast::Block> {
659659
P(ast::Block {
660-
expr: e,
661-
stmts: vec![],
660+
stmts: e.map(|e| codemap::Spanned {
661+
span: e.span,
662+
node: ast::StmtKind::Expr(e, ast::DUMMY_NODE_ID),
663+
}).into_iter().collect(),
662664
rules: rules,
663665
id: ast::DUMMY_NODE_ID,
664666
span: codemap::DUMMY_SP,

src/libsyntax/ast.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,9 +528,6 @@ impl PartialEq for MetaItemKind {
528528
pub struct Block {
529529
/// Statements in a block
530530
pub stmts: Vec<Stmt>,
531-
/// An expression at the end of the block
532-
/// without a semicolon, if any
533-
pub expr: Option<P<Expr>>,
534531
pub id: NodeId,
535532
/// Distinguishes between `unsafe { ... }` and `{ ... }`
536533
pub rules: BlockCheckMode,
@@ -803,7 +800,7 @@ pub enum StmtKind {
803800
/// Could be an item or a local (let) binding:
804801
Decl(P<Decl>, NodeId),
805802

806-
/// Expr without trailing semi-colon (must have unit type):
803+
/// Expr without trailing semi-colon
807804
Expr(P<Expr>, NodeId),
808805

809806
/// Expr with trailing semi-colon (may have any type):

src/libsyntax/ext/build.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,9 @@ pub trait AstBuilder {
9898
fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt;
9999

100100
// blocks
101-
fn block(&self, span: Span, stmts: Vec<ast::Stmt>,
102-
expr: Option<P<ast::Expr>>) -> P<ast::Block>;
101+
fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block>;
103102
fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block>;
104-
fn block_all(&self, span: Span,
105-
stmts: Vec<ast::Stmt>,
106-
expr: Option<P<ast::Expr>>) -> P<ast::Block>;
103+
fn block_all(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block>;
107104

108105
// expressions
109106
fn expr(&self, span: Span, node: ast::ExprKind) -> P<ast::Expr>;
@@ -508,7 +505,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
508505
}
509506

510507
fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt {
511-
respan(expr.span, ast::StmtKind::Semi(expr, ast::DUMMY_NODE_ID))
508+
respan(expr.span, ast::StmtKind::Expr(expr, ast::DUMMY_NODE_ID))
512509
}
513510

514511
fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
@@ -556,9 +553,8 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
556553
P(respan(sp, ast::StmtKind::Decl(P(decl), ast::DUMMY_NODE_ID)))
557554
}
558555

559-
fn block(&self, span: Span, stmts: Vec<ast::Stmt>,
560-
expr: Option<P<Expr>>) -> P<ast::Block> {
561-
self.block_all(span, stmts, expr)
556+
fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block> {
557+
self.block_all(span, stmts)
562558
}
563559

564560
fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt {
@@ -567,19 +563,18 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
567563
}
568564

569565
fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
570-
self.block_all(expr.span, Vec::new(), Some(expr))
571-
}
572-
fn block_all(&self,
573-
span: Span,
574-
stmts: Vec<ast::Stmt>,
575-
expr: Option<P<ast::Expr>>) -> P<ast::Block> {
576-
P(ast::Block {
577-
stmts: stmts,
578-
expr: expr,
579-
id: ast::DUMMY_NODE_ID,
580-
rules: BlockCheckMode::Default,
581-
span: span,
582-
})
566+
self.block_all(expr.span, vec![Spanned {
567+
span: expr.span,
568+
node: ast::StmtKind::Expr(expr, ast::DUMMY_NODE_ID),
569+
}])
570+
}
571+
fn block_all(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block> {
572+
P(ast::Block {
573+
stmts: stmts,
574+
id: ast::DUMMY_NODE_ID,
575+
rules: BlockCheckMode::Default,
576+
span: span,
577+
})
583578
}
584579

585580
fn expr(&self, span: Span, node: ast::ExprKind) -> P<ast::Expr> {
@@ -948,14 +943,14 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
948943
ids: Vec<ast::Ident>,
949944
stmts: Vec<ast::Stmt>)
950945
-> P<ast::Expr> {
951-
self.lambda(span, ids, self.block(span, stmts, None))
946+
self.lambda(span, ids, self.block(span, stmts))
952947
}
953948
fn lambda_stmts_0(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Expr> {
954-
self.lambda0(span, self.block(span, stmts, None))
949+
self.lambda0(span, self.block(span, stmts))
955950
}
956951
fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>,
957952
ident: ast::Ident) -> P<ast::Expr> {
958-
self.lambda1(span, self.block(span, stmts, None), ident)
953+
self.lambda1(span, self.block(span, stmts), ident)
959954
}
960955

961956
fn arg(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Arg {

src/libsyntax/ext/expand.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -636,23 +636,14 @@ pub fn expand_block(blk: P<Block>, fld: &mut MacroExpander) -> P<Block> {
636636

637637
// expand the elements of a block.
638638
pub fn expand_block_elts(b: P<Block>, fld: &mut MacroExpander) -> P<Block> {
639-
b.map(|Block {id, stmts, expr, rules, span}| {
639+
b.map(|Block {id, stmts, rules, span}| {
640640
let new_stmts = stmts.into_iter().flat_map(|x| {
641641
// perform pending renames and expand macros in the statement
642642
fld.fold_stmt(x).into_iter()
643643
}).collect();
644-
let new_expr = expr.map(|x| {
645-
let expr = {
646-
let pending_renames = &mut fld.cx.syntax_env.info().pending_renames;
647-
let mut rename_fld = IdentRenamer{renames:pending_renames};
648-
rename_fld.fold_expr(x)
649-
};
650-
fld.fold_expr(expr)
651-
});
652644
Block {
653645
id: fld.new_id(id),
654646
stmts: new_stmts,
655-
expr: new_expr,
656647
rules: rules,
657648
span: span
658649
}

src/libsyntax/ext/quote.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,8 @@ pub fn expand_quote_matcher(cx: &mut ExtCtxt,
512512
let (cx_expr, tts) = parse_arguments_to_quote(cx, tts);
513513
let mut vector = mk_stmts_let(cx, sp);
514514
vector.extend(statements_mk_tts(cx, &tts[..], true));
515-
let block = cx.expr_block(
516-
cx.block_all(sp,
517-
vector,
518-
Some(cx.expr_ident(sp, id_ext("tt")))));
515+
vector.push(cx.stmt_expr(cx.expr_ident(sp, id_ext("tt"))));
516+
let block = cx.expr_block(cx.block_all(sp, vector));
519517

520518
let expanded = expand_wrapper(cx, sp, cx_expr, block, &[&["syntax", "ext", "quote", "rt"]]);
521519
base::MacEager::expr(expanded)
@@ -765,8 +763,9 @@ fn statements_mk_tt(cx: &ExtCtxt, tt: &TokenTree, matcher: bool) -> Vec<ast::Stm
765763
let stmt_let_tt = cx.stmt_let(sp, true, id_ext("tt"), cx.expr_vec_ng(sp));
766764
let mut tts_stmts = vec![stmt_let_tt];
767765
tts_stmts.extend(statements_mk_tts(cx, &seq.tts[..], matcher));
768-
let e_tts = cx.expr_block(cx.block(sp, tts_stmts,
769-
Some(cx.expr_ident(sp, id_ext("tt")))));
766+
tts_stmts.push(cx.stmt_expr(cx.expr_ident(sp, id_ext("tt"))));
767+
let e_tts = cx.expr_block(cx.block(sp, tts_stmts));
768+
770769
let e_separator = match seq.separator {
771770
Some(ref sep) => cx.expr_some(sp, expr_mk_token(cx, sp, sep)),
772771
None => cx.expr_none(sp),
@@ -884,10 +883,8 @@ fn expand_tts(cx: &ExtCtxt, sp: Span, tts: &[TokenTree])
884883

885884
let mut vector = mk_stmts_let(cx, sp);
886885
vector.extend(statements_mk_tts(cx, &tts[..], false));
887-
let block = cx.expr_block(
888-
cx.block_all(sp,
889-
vector,
890-
Some(cx.expr_ident(sp, id_ext("tt")))));
886+
vector.push(cx.stmt_expr(cx.expr_ident(sp, id_ext("tt"))));
887+
let block = cx.expr_block(cx.block_all(sp, vector));
891888

892889
(cx_expr, block)
893890
}
@@ -901,13 +898,14 @@ fn expand_wrapper(cx: &ExtCtxt,
901898
let cx_expr_borrow = cx.expr_addr_of(sp, cx.expr_deref(sp, cx_expr));
902899
let stmt_let_ext_cx = cx.stmt_let(sp, false, id_ext("ext_cx"), cx_expr_borrow);
903900

904-
let stmts = imports.iter().map(|path| {
901+
let mut stmts = imports.iter().map(|path| {
905902
// make item: `use ...;`
906903
let path = path.iter().map(|s| s.to_string()).collect();
907904
cx.stmt_item(sp, cx.item_use_glob(sp, ast::Visibility::Inherited, ids_ext(path)))
908-
}).chain(Some(stmt_let_ext_cx)).collect();
905+
}).chain(Some(stmt_let_ext_cx)).collect::<Vec<_>>();
906+
stmts.push(cx.stmt_expr(expr));
909907

910-
cx.expr_block(cx.block_all(sp, stmts, Some(expr)))
908+
cx.expr_block(cx.block_all(sp, stmts))
911909
}
912910

913911
fn expand_parse_call(cx: &ExtCtxt,

src/libsyntax/fold.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,10 +845,9 @@ fn noop_fold_bounds<T: Folder>(bounds: TyParamBounds, folder: &mut T)
845845
}
846846

847847
pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> {
848-
b.map(|Block {id, stmts, expr, rules, span}| Block {
848+
b.map(|Block {id, stmts, rules, span}| Block {
849849
id: folder.new_id(id),
850850
stmts: stmts.move_flat_map(|s| folder.fold_stmt(s).into_iter()),
851-
expr: expr.and_then(|x| folder.fold_opt_expr(x)),
852851
rules: rules,
853852
span: folder.new_span(span),
854853
})

src/libsyntax/parse/parser.rs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3217,9 +3217,11 @@ impl<'a> Parser<'a> {
32173217
let body_expr = self.parse_expr()?;
32183218
P(ast::Block {
32193219
id: ast::DUMMY_NODE_ID,
3220-
stmts: vec![],
32213220
span: body_expr.span,
3222-
expr: Some(body_expr),
3221+
stmts: vec![Spanned {
3222+
span: body_expr.span,
3223+
node: StmtKind::Expr(body_expr, ast::DUMMY_NODE_ID),
3224+
}],
32233225
rules: BlockCheckMode::Default,
32243226
})
32253227
}
@@ -4082,7 +4084,6 @@ impl<'a> Parser<'a> {
40824084
/// Precondition: already parsed the '{'.
40834085
fn parse_block_tail(&mut self, lo: BytePos, s: BlockCheckMode) -> PResult<'a, P<Block>> {
40844086
let mut stmts = vec![];
4085-
let mut expr = None;
40864087

40874088
while !self.eat(&token::CloseDelim(token::Brace)) {
40884089
let Spanned {node, span} = if let Some(s) = self.parse_stmt_() {
@@ -4095,11 +4096,10 @@ impl<'a> Parser<'a> {
40954096
};
40964097
match node {
40974098
StmtKind::Expr(e, _) => {
4098-
self.handle_expression_like_statement(e, span, &mut stmts, &mut expr)?;
4099+
self.handle_expression_like_statement(e, span, &mut stmts)?;
40994100
}
41004101
StmtKind::Mac(mac, MacStmtStyle::NoBraces, attrs) => {
4101-
// statement macro without braces; might be an
4102-
// expr depending on whether a semicolon follows
4102+
// statement macro without braces
41034103
match self.token {
41044104
token::Semi => {
41054105
stmts.push(Spanned {
@@ -4115,11 +4115,7 @@ impl<'a> Parser<'a> {
41154115
let lo = e.span.lo;
41164116
let e = self.parse_dot_or_call_expr_with(e, lo, attrs)?;
41174117
let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?;
4118-
self.handle_expression_like_statement(
4119-
e,
4120-
span,
4121-
&mut stmts,
4122-
&mut expr)?;
4118+
self.handle_expression_like_statement(e, span, &mut stmts)?;
41234119
}
41244120
}
41254121
}
@@ -4133,13 +4129,6 @@ impl<'a> Parser<'a> {
41334129
});
41344130
self.bump();
41354131
}
4136-
token::CloseDelim(token::Brace) => {
4137-
// if a block ends in `m!(arg)` without
4138-
// a `;`, it must be an expr
4139-
expr = Some(self.mk_mac_expr(span.lo, span.hi,
4140-
m.and_then(|x| x.node),
4141-
attrs));
4142-
}
41434132
_ => {
41444133
stmts.push(Spanned {
41454134
node: StmtKind::Mac(m, style, attrs),
@@ -4165,7 +4154,6 @@ impl<'a> Parser<'a> {
41654154

41664155
Ok(P(ast::Block {
41674156
stmts: stmts,
4168-
expr: expr,
41694157
id: ast::DUMMY_NODE_ID,
41704158
rules: s,
41714159
span: mk_sp(lo, self.last_span.hi),
@@ -4175,8 +4163,7 @@ impl<'a> Parser<'a> {
41754163
fn handle_expression_like_statement(&mut self,
41764164
e: P<Expr>,
41774165
span: Span,
4178-
stmts: &mut Vec<Stmt>,
4179-
last_block_expr: &mut Option<P<Expr>>)
4166+
stmts: &mut Vec<Stmt>)
41804167
-> PResult<'a, ()> {
41814168
// expression without semicolon
41824169
if classify::expr_requires_semi_to_be_stmt(&e) {
@@ -4202,7 +4189,6 @@ impl<'a> Parser<'a> {
42024189
span: span_with_semi,
42034190
});
42044191
}
4205-
token::CloseDelim(token::Brace) => *last_block_expr = Some(e),
42064192
_ => {
42074193
stmts.push(Spanned {
42084194
node: StmtKind::Expr(e, ast::DUMMY_NODE_ID),

src/libsyntax/print/pprust.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,9 +1619,6 @@ impl<'a> State<'a> {
16191619
}
16201620
}
16211621
}
1622-
if parse::classify::stmt_ends_with_semi(&st.node) {
1623-
try!(word(&mut self.s, ";"));
1624-
}
16251622
self.maybe_print_trailing_comment(st.span, None)
16261623
}
16271624

@@ -1668,14 +1665,6 @@ impl<'a> State<'a> {
16681665
for st in &blk.stmts {
16691666
try!(self.print_stmt(st));
16701667
}
1671-
match blk.expr {
1672-
Some(ref expr) => {
1673-
try!(self.space_if_not_bol());
1674-
try!(self.print_expr_outer_attr_style(&expr, false));
1675-
try!(self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi)));
1676-
}
1677-
_ => ()
1678-
}
16791668
try!(self.bclose_maybe_open(blk.span, indented, close_box));
16801669
self.ann.post(self, NodeBlock(blk))
16811670
}
@@ -2084,24 +2073,23 @@ impl<'a> State<'a> {
20842073
_ => false
20852074
};
20862075

2087-
if !default_return || !body.stmts.is_empty() || body.expr.is_none() {
2088-
try!(self.print_block_unclosed(&body));
2089-
} else {
2090-
// we extract the block, so as not to create another set of boxes
2091-
let i_expr = body.expr.as_ref().unwrap();
2092-
match i_expr.node {
2093-
ast::ExprKind::Block(ref blk) => {
2076+
match body.stmts.last().map(|stmt| &stmt.node) {
2077+
Some(&ast::StmtKind::Expr(ref i_expr, _)) if default_return &&
2078+
body.stmts.len() == 1 => {
2079+
// we extract the block, so as not to create another set of boxes
2080+
if let ast::ExprKind::Block(ref blk) = i_expr.node {
20942081
try!(self.print_block_unclosed_with_attrs(
20952082
&blk,
20962083
i_expr.attrs.as_attr_slice()));
2097-
}
2098-
_ => {
2084+
} else {
20992085
// this is a bare expression
21002086
try!(self.print_expr(&i_expr));
21012087
try!(self.end()); // need to close a box
21022088
}
21032089
}
2090+
_ => try!(self.print_block_unclosed(&body)),
21042091
}
2092+
21052093
// a box will be closed by print_expr, but we didn't want an overall
21062094
// wrapper so we closed the corresponding opening. so create an
21072095
// empty box to satisfy the close.
@@ -2295,6 +2283,7 @@ impl<'a> State<'a> {
22952283
try!(self.word_space("="));
22962284
try!(self.print_expr(&init));
22972285
}
2286+
try!(word(&mut self.s, ";"));
22982287
self.end()
22992288
}
23002289
ast::DeclKind::Item(ref item) => self.print_item(&item)

0 commit comments

Comments
 (0)