Skip to content

Commit aedc1b6

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 0f6a51d commit aedc1b6

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
@@ -2733,13 +2733,35 @@ impl<'a> Parser<'a> {
27332733
let first_tok_span = self.token.span;
27342734
match self.parse_expr() {
27352735
Ok(cond)
2736-
// If it's not a free-standing expression, and is followed by a block,
2737-
// then it's very likely the condition to an `else if`.
2736+
// Try to guess the difference between a "condition-like" vs
2737+
// "statement-like" expression.
2738+
//
2739+
// We are seeing the following code, in which $cond is neither
2740+
// ExprKind::Block nor ExprKind::If (the 2 cases wherein this
2741+
// would be valid syntax).
2742+
//
2743+
// if ... {
2744+
// } else $cond
2745+
//
2746+
// If $cond is "condition-like" such as ExprKind::Binary, we
2747+
// want to suggest inserting `if`.
2748+
//
2749+
// if ... {
2750+
// } else if a == b {
2751+
// ^^
2752+
// }
2753+
//
2754+
// If $cond is "statement-like" such as ExprKind::While then we
2755+
// want to suggest wrapping in braces.
2756+
//
2757+
// if ... {
2758+
// } else {
2759+
// ^
2760+
// while true {}
2761+
// }
2762+
// ^
27382763
if self.check(&TokenKind::OpenDelim(Delimiter::Brace))
2739-
&& match cond.kind {
2740-
ExprKind::MacCall(_) => true,
2741-
_ => classify::expr_requires_semi_to_be_stmt(&cond),
2742-
} =>
2764+
&& classify::expr_requires_semi_to_be_stmt(&cond) =>
27432765
{
27442766
self.dcx().emit_err(errors::ExpectedElseBlock {
27452767
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)