Skip to content

Unify rules about commas in match arms and semicolons in expressions #40989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions src/libsyntax/parse/classify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

// 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
/// as a statement? The negation of this: 'can this expression
Expand All @@ -30,18 +30,12 @@ pub fn expr_requires_semi_to_be_stmt(e: &ast::Expr) -> bool {
ast::ExprKind::While(..) |
ast::ExprKind::WhileLet(..) |
ast::ExprKind::Loop(..) |
ast::ExprKind::ForLoop(..) => false,
ast::ExprKind::ForLoop(..) |
ast::ExprKind::Catch(..) => false,
_ => true,
}
}

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.
/// note that in one case (`stmt_semi`), we've already
/// seen the semicolon, and thus don't need another.
Expand Down
3 changes: 1 addition & 2 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3209,8 +3209,7 @@ impl<'a> Parser<'a> {
self.expect(&token::FatArrow)?;
let expr = self.parse_expr_res(RESTRICTION_STMT_EXPR, None)?;

let require_comma =
!classify::expr_is_simple_block(&expr)
let require_comma = classify::expr_requires_semi_to_be_stmt(&expr)
&& self.token != token::CloseDelim(token::Brace);

if require_comma {
Expand Down
6 changes: 6 additions & 0 deletions src/test/run-pass/catch-expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,10 @@ pub fn main() {
Ok(&my_string)
};
assert_eq!(res, Ok("test"));

do catch {
()
}

();
}
47 changes: 47 additions & 0 deletions src/test/run-pass/optional_comma_in_match_arm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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.

// ignore-pretty issue #37199

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 => (),
_ =>()
}
}