Unify rules about commas in match arms and semicolons in expressions
This commit is contained in:
parent
2652ce6771
commit
5e25dc9966
3 changed files with 47 additions and 10 deletions
|
@ -12,7 +12,7 @@
|
||||||
|
|
||||||
// Predicates on exprs and stmts that the pretty-printer and parser use
|
// Predicates on exprs and stmts that the pretty-printer and parser use
|
||||||
|
|
||||||
use ast::{self, BlockCheckMode};
|
use ast;
|
||||||
|
|
||||||
/// Does this expression require a semicolon to be treated
|
/// Does this expression require a semicolon to be treated
|
||||||
/// as a statement? The negation of this: 'can this expression
|
/// 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 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expr_is_simple_block(e: &ast::Expr) -> bool {
|
|
||||||
match e.node {
|
|
||||||
ast::ExprKind::Block(ref block) => block.rules == BlockCheckMode::Default,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// this statement requires a semicolon after it.
|
/// this statement requires a semicolon after it.
|
||||||
/// note that in one case (`stmt_semi`), we've already
|
/// note that in one case (`stmt_semi`), we've already
|
||||||
/// seen the semicolon, and thus don't need another.
|
/// seen the semicolon, and thus don't need another.
|
||||||
|
|
|
@ -3209,8 +3209,7 @@ impl<'a> Parser<'a> {
|
||||||
self.expect(&token::FatArrow)?;
|
self.expect(&token::FatArrow)?;
|
||||||
let expr = self.parse_expr_res(RESTRICTION_STMT_EXPR, None)?;
|
let expr = self.parse_expr_res(RESTRICTION_STMT_EXPR, None)?;
|
||||||
|
|
||||||
let require_comma =
|
let require_comma = classify::expr_requires_semi_to_be_stmt(&expr)
|
||||||
!classify::expr_is_simple_block(&expr)
|
|
||||||
&& self.token != token::CloseDelim(token::Brace);
|
&& self.token != token::CloseDelim(token::Brace);
|
||||||
|
|
||||||
if require_comma {
|
if require_comma {
|
||||||
|
|
45
src/test/run-pass/optional_comma_in_match_arm.rs
Normal file
45
src/test/run-pass/optional_comma_in_match_arm.rs
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let x = 1;
|
||||||
|
|
||||||
|
match x {
|
||||||
|
1 => loop { break; },
|
||||||
|
2 => while true { break; },
|
||||||
|
3 => if true { () },
|
||||||
|
4 => if true { () } else { () },
|
||||||
|
5 => match () { () => () },
|
||||||
|
6 => { () },
|
||||||
|
7 => unsafe { () },
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
|
||||||
|
match x {
|
||||||
|
1 => loop { break; }
|
||||||
|
2 => while true { break; }
|
||||||
|
3 => if true { () }
|
||||||
|
4 => if true { () } else { () }
|
||||||
|
5 => match () { () => () }
|
||||||
|
6 => { () }
|
||||||
|
7 => unsafe { () }
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
|
||||||
|
let r: &i32 = &x;
|
||||||
|
|
||||||
|
match r {
|
||||||
|
// Absence of comma should not cause confusion between a pattern
|
||||||
|
// and a bitwise and.
|
||||||
|
&1 => if true { () } else { () }
|
||||||
|
&2 => (),
|
||||||
|
_ =>()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue