Skip to content

Commit bab4cae

Browse files
committed
Differentiate statement boundary rules for stmt vs match arm
1 parent 9cb0f98 commit bab4cae

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

src/classify.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ use crate::ty::{ReturnType, Type};
66
use proc_macro2::{Delimiter, TokenStream, TokenTree};
77
use std::ops::ControlFlow;
88

9-
pub(crate) fn requires_terminator(expr: &Expr) -> bool {
10-
// see https://github.com/rust-lang/rust/blob/9a19e7604/compiler/rustc_ast/src/util/classify.rs#L7-L26
9+
pub(crate) fn requires_semi_to_be_stmt(expr: &Expr) -> bool {
10+
match expr {
11+
Expr::Macro(expr) => !expr.mac.delimiter.is_brace(),
12+
_ => requires_comma_to_be_match_arm(expr),
13+
}
14+
}
15+
16+
pub(crate) fn requires_comma_to_be_match_arm(expr: &Expr) -> bool {
1117
match expr {
1218
Expr::If(_)
1319
| Expr::Match(_)

src/expr.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2873,7 +2873,7 @@ pub(crate) mod parsing {
28732873
fat_arrow_token: input.parse()?,
28742874
body: {
28752875
let body = Expr::parse_with_earlier_boundary_rule(input)?;
2876-
requires_comma = classify::requires_terminator(&body);
2876+
requires_comma = classify::requires_comma_to_be_match_arm(&body);
28772877
Box::new(body)
28782878
},
28792879
comma: {
@@ -3312,7 +3312,10 @@ pub(crate) mod printing {
33123312
// Ensure that we have a comma after a non-block arm, except
33133313
// for the last one.
33143314
let is_last = i == self.arms.len() - 1;
3315-
if !is_last && classify::requires_terminator(&arm.body) && arm.comma.is_none() {
3315+
if !is_last
3316+
&& classify::requires_comma_to_be_match_arm(&arm.body)
3317+
&& arm.comma.is_none()
3318+
{
33163319
<Token![,]>::default().to_tokens(tokens);
33173320
}
33183321
}

src/stmt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ pub(crate) mod parsing {
159159
}
160160
let stmt = parse_stmt(input, AllowNoSemi(true))?;
161161
let requires_semicolon = match &stmt {
162-
Stmt::Expr(stmt, None) => classify::requires_terminator(stmt),
162+
Stmt::Expr(stmt, None) => classify::requires_semi_to_be_stmt(stmt),
163163
Stmt::Macro(stmt) => {
164164
stmt.semi_token.is_none() && !stmt.mac.delimiter.is_brace()
165165
}
@@ -401,7 +401,7 @@ pub(crate) mod parsing {
401401

402402
if semi_token.is_some() {
403403
Ok(Stmt::Expr(e, semi_token))
404-
} else if allow_nosemi.0 || !classify::requires_terminator(&e) {
404+
} else if allow_nosemi.0 || !classify::requires_semi_to_be_stmt(&e) {
405405
Ok(Stmt::Expr(e, None))
406406
} else {
407407
Err(input.error("expected semicolon"))

0 commit comments

Comments
 (0)