Skip to content

Commit 412e7a9

Browse files
committed
Remove MacCall special case from recovery after missing 'if' after 'else'
The change to the test is a little goofy because the compiler was guessing "correctly" before that `falsy! {}` is the condition as opposed to the else body. But I believe this change is fundamentally correct. Braced macro invocations in statement position are most often item-like (`thread_local! {...}`) as opposed to parenthesized macro invocations which are condition-like (`cfg!(...)`).
1 parent ed5a1af commit 412e7a9

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

compiler/rustc_parse/src/parser/expr.rs

+28-6
Original file line numberDiff line numberDiff line change
@@ -2606,13 +2606,35 @@ impl<'a> Parser<'a> {
26062606
let first_tok_span = self.token.span;
26072607
match self.parse_expr() {
26082608
Ok(cond)
2609-
// If it's not a free-standing expression, and is followed by a block,
2610-
// then it's very likely the condition to an `else if`.
2609+
// Try to guess the difference between a "condition-like" vs
2610+
// "statement-like" expression.
2611+
//
2612+
// We are seeing the following code, in which $cond is neither
2613+
// ExprKind::Block nor ExprKind::If (the 2 cases wherein this
2614+
// would be valid syntax).
2615+
//
2616+
// if ... {
2617+
// } else $cond
2618+
//
2619+
// If $cond is "condition-like" such as ExprKind::Binary, we
2620+
// want to suggest inserting `if`.
2621+
//
2622+
// if ... {
2623+
// } else if a == b {
2624+
// ^^
2625+
// }
2626+
//
2627+
// If $cond is "statement-like" such as ExprKind::While then we
2628+
// want to suggest wrapping in braces.
2629+
//
2630+
// if ... {
2631+
// } else {
2632+
// ^
2633+
// while true {}
2634+
// }
2635+
// ^
26112636
if self.check(&TokenKind::OpenDelim(Delimiter::Brace))
2612-
&& match cond.kind {
2613-
ExprKind::MacCall(_) => true,
2614-
_ => classify::expr_requires_semi_to_be_stmt(&cond),
2615-
} =>
2637+
&& classify::expr_requires_semi_to_be_stmt(&cond) =>
26162638
{
26172639
self.dcx().emit_err(errors::ExpectedElseBlock {
26182640
first_tok_span,

tests/ui/parser/else-no-if.stderr

+4-6
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,12 @@ error: expected `{`, found `falsy`
7474
--> $DIR/else-no-if.rs:47:12
7575
|
7676
LL | } else falsy! {} {
77-
| ---- ^^^^^
78-
| |
79-
| expected an `if` or a block after this `else`
77+
| ^^^^^ expected `{`
8078
|
81-
help: add an `if` if this is the condition of a chained `else if` statement
79+
help: try placing this code inside a block
8280
|
83-
LL | } else if falsy! {} {
84-
| ++
81+
LL | } else { falsy! {} } {
82+
| + +
8583

8684
error: expected `{`, found `falsy`
8785
--> $DIR/else-no-if.rs:54:12

0 commit comments

Comments
 (0)