Skip to content

Commit 5e25dc9

Browse files
committed
Unify rules about commas in match arms and semicolons in expressions
1 parent 2652ce6 commit 5e25dc9

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

src/libsyntax/parse/classify.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
// Predicates on exprs and stmts that the pretty-printer and parser use
1414

15-
use ast::{self, BlockCheckMode};
15+
use ast;
1616

1717
/// Does this expression require a semicolon to be treated
1818
/// as a statement? The negation of this: 'can this expression
@@ -35,13 +35,6 @@ pub fn expr_requires_semi_to_be_stmt(e: &ast::Expr) -> bool {
3535
}
3636
}
3737

38-
pub fn expr_is_simple_block(e: &ast::Expr) -> bool {
39-
match e.node {
40-
ast::ExprKind::Block(ref block) => block.rules == BlockCheckMode::Default,
41-
_ => false,
42-
}
43-
}
44-
4538
/// this statement requires a semicolon after it.
4639
/// note that in one case (`stmt_semi`), we've already
4740
/// seen the semicolon, and thus don't need another.

src/libsyntax/parse/parser.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3209,8 +3209,7 @@ impl<'a> Parser<'a> {
32093209
self.expect(&token::FatArrow)?;
32103210
let expr = self.parse_expr_res(RESTRICTION_STMT_EXPR, None)?;
32113211

3212-
let require_comma =
3213-
!classify::expr_is_simple_block(&expr)
3212+
let require_comma = classify::expr_requires_semi_to_be_stmt(&expr)
32143213
&& self.token != token::CloseDelim(token::Brace);
32153214

32163215
if require_comma {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let x = 1;
13+
14+
match x {
15+
1 => loop { break; },
16+
2 => while true { break; },
17+
3 => if true { () },
18+
4 => if true { () } else { () },
19+
5 => match () { () => () },
20+
6 => { () },
21+
7 => unsafe { () },
22+
_ => (),
23+
}
24+
25+
match x {
26+
1 => loop { break; }
27+
2 => while true { break; }
28+
3 => if true { () }
29+
4 => if true { () } else { () }
30+
5 => match () { () => () }
31+
6 => { () }
32+
7 => unsafe { () }
33+
_ => ()
34+
}
35+
36+
let r: &i32 = &x;
37+
38+
match r {
39+
// Absence of comma should not cause confusion between a pattern
40+
// and a bitwise and.
41+
&1 => if true { () } else { () }
42+
&2 => (),
43+
_ =>()
44+
}
45+
}

0 commit comments

Comments
 (0)