Skip to content

Commit a8e47f5

Browse files
committed
Macro call with braces does not require semicolon to be statement
This commit by itself is supposed to have no effect on behavior. All of the call sites are updated to preserve their previous behavior. The behavior changes are in the commits that follow.
1 parent 14e5ba0 commit a8e47f5

File tree

6 files changed

+50
-23
lines changed

6 files changed

+50
-23
lines changed

compiler/rustc_ast/src/util/classify.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,23 @@ use crate::{ast, token::Delimiter};
4242
/// _ => m! {} - 1, // binary subtraction operator
4343
/// }
4444
/// ```
45-
#[allow(non_snake_case)]
46-
pub fn expr_requires_semi_to_be_stmt_FIXME(e: &ast::Expr) -> bool {
47-
!matches!(
48-
e.kind,
49-
ast::ExprKind::If(..)
50-
| ast::ExprKind::Match(..)
51-
| ast::ExprKind::Block(..)
52-
| ast::ExprKind::While(..)
53-
| ast::ExprKind::Loop(..)
54-
| ast::ExprKind::ForLoop { .. }
55-
| ast::ExprKind::TryBlock(..)
56-
| ast::ExprKind::ConstBlock(..)
57-
)
45+
pub fn expr_requires_semi_to_be_stmt(e: &ast::Expr) -> bool {
46+
use ast::ExprKind::*;
47+
48+
match &e.kind {
49+
If(..)
50+
| Match(..)
51+
| Block(..)
52+
| While(..)
53+
| Loop(..)
54+
| ForLoop { .. }
55+
| TryBlock(..)
56+
| ConstBlock(..) => false,
57+
58+
MacCall(mac_call) => mac_call.args.delim != Delimiter::Brace,
59+
60+
_ => true,
61+
}
5862
}
5963

6064
/// If an expression ends with `}`, returns the innermost expression ending in the `}`

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,10 @@ impl<'a> State<'a> {
12531253
ast::StmtKind::Expr(expr) => {
12541254
self.space_if_not_bol();
12551255
self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
1256-
if classify::expr_requires_semi_to_be_stmt_FIXME(expr) {
1256+
if match expr.kind {
1257+
ast::ExprKind::MacCall(_) => true,
1258+
_ => classify::expr_requires_semi_to_be_stmt(expr),
1259+
} {
12571260
self.word(";");
12581261
}
12591262
}

compiler/rustc_ast_pretty/src/pprust/state/fixup.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ impl FixupContext {
128128
/// The documentation on `FixupContext::leftmost_subexpression_in_stmt` has
129129
/// examples.
130130
pub fn would_cause_statement_boundary(self, expr: &Expr) -> bool {
131-
self.leftmost_subexpression_in_stmt && !classify::expr_requires_semi_to_be_stmt_FIXME(expr)
131+
self.leftmost_subexpression_in_stmt
132+
&& match expr.kind {
133+
ExprKind::MacCall(_) => false,
134+
_ => !classify::expr_requires_semi_to_be_stmt(expr),
135+
}
132136
}
133137

134138
/// Determine whether parentheses are needed around the given `let`

compiler/rustc_lint/src/unused.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,10 @@ trait UnusedDelimLint {
686686
ExprKind::Index(base, _subscript, _) => base,
687687
_ => break,
688688
};
689-
if !classify::expr_requires_semi_to_be_stmt_FIXME(innermost) {
689+
if match innermost.kind {
690+
ExprKind::MacCall(_) => false,
691+
_ => !classify::expr_requires_semi_to_be_stmt(innermost),
692+
} {
690693
return true;
691694
}
692695
}

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,10 @@ impl<'a> Parser<'a> {
484484
/// Checks if this expression is a successfully parsed statement.
485485
fn expr_is_complete(&self, e: &Expr) -> bool {
486486
self.restrictions.contains(Restrictions::STMT_EXPR)
487-
&& !classify::expr_requires_semi_to_be_stmt_FIXME(e)
487+
&& match e.kind {
488+
ExprKind::MacCall(_) => false,
489+
_ => !classify::expr_requires_semi_to_be_stmt(e),
490+
}
488491
}
489492

490493
/// Parses `x..y`, `x..=y`, and `x..`/`x..=`.
@@ -2672,7 +2675,10 @@ impl<'a> Parser<'a> {
26722675
// If it's not a free-standing expression, and is followed by a block,
26732676
// then it's very likely the condition to an `else if`.
26742677
if self.check(&TokenKind::OpenDelim(Delimiter::Brace))
2675-
&& classify::expr_requires_semi_to_be_stmt_FIXME(&cond) =>
2678+
&& match cond.kind {
2679+
ExprKind::MacCall(_) => true,
2680+
_ => classify::expr_requires_semi_to_be_stmt(&cond),
2681+
} =>
26762682
{
26772683
self.dcx().emit_err(errors::ExpectedElseBlock {
26782684
first_tok_span,
@@ -3114,8 +3120,10 @@ impl<'a> Parser<'a> {
31143120
err
31153121
})?;
31163122

3117-
let require_comma = classify::expr_requires_semi_to_be_stmt_FIXME(&expr)
3118-
&& this.token != token::CloseDelim(Delimiter::Brace);
3123+
let require_comma = match expr.kind {
3124+
ExprKind::MacCall(_) => true,
3125+
_ => classify::expr_requires_semi_to_be_stmt(&expr),
3126+
} && this.token != token::CloseDelim(Delimiter::Brace);
31193127

31203128
if !require_comma {
31213129
arm_body = Some(expr);

compiler/rustc_parse/src/parser/stmt.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -649,8 +649,10 @@ impl<'a> Parser<'a> {
649649
match &mut stmt.kind {
650650
// Expression without semicolon.
651651
StmtKind::Expr(expr)
652-
if classify::expr_requires_semi_to_be_stmt_FIXME(expr)
653-
&& !expr.attrs.is_empty()
652+
if match expr.kind {
653+
ExprKind::MacCall(_) => true,
654+
_ => classify::expr_requires_semi_to_be_stmt(expr),
655+
} && !expr.attrs.is_empty()
654656
&& ![token::Eof, token::Semi, token::CloseDelim(Delimiter::Brace)]
655657
.contains(&self.token.kind) =>
656658
{
@@ -664,7 +666,10 @@ impl<'a> Parser<'a> {
664666
// Expression without semicolon.
665667
StmtKind::Expr(expr)
666668
if self.token != token::Eof
667-
&& classify::expr_requires_semi_to_be_stmt_FIXME(expr) =>
669+
&& match expr.kind {
670+
ExprKind::MacCall(_) => true,
671+
_ => classify::expr_requires_semi_to_be_stmt(expr),
672+
} =>
668673
{
669674
// Just check for errors and recover; do not eat semicolon yet.
670675

0 commit comments

Comments
 (0)