Skip to content

Commit 36b11cf

Browse files
committed
Add expectation for { when parsing lone couroutine qualifiers
1 parent 2b0274c commit 36b11cf

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,22 +1520,20 @@ impl<'a> Parser<'a> {
15201520
Ok(this.mk_expr(this.prev_token.span, ExprKind::Underscore))
15211521
} else if this.token_uninterpolated_span().at_least_rust_2018() {
15221522
// `Span::at_least_rust_2018()` is somewhat expensive; don't get it repeatedly.
1523+
let at_async = this.check_keyword(exp!(Async));
1524+
// check for `gen {}` and `gen move {}`
1525+
// or `async gen {}` and `async gen move {}`
1526+
// FIXME: (async) gen closures aren't yet parsed.
1527+
// FIXME(gen_blocks): Parse `gen async` and suggest swap
15231528
if this.token_uninterpolated_span().at_least_rust_2024()
1524-
// check for `gen {}` and `gen move {}`
1525-
// or `async gen {}` and `async gen move {}`
1526-
&& (this.is_gen_block(kw::Gen, 0)
1527-
|| (this.check_keyword(exp!(Async)) && this.is_gen_block(kw::Gen, 1)))
1529+
&& this.is_gen_block(kw::Gen, at_async as usize)
15281530
{
1529-
// FIXME: (async) gen closures aren't yet parsed.
15301531
this.parse_gen_block()
1531-
} else if this.check_keyword(exp!(Async)) {
1532-
// FIXME(gen_blocks): Parse `gen async` and suggest swap
1533-
if this.is_gen_block(kw::Async, 0) {
1534-
// Check for `async {` and `async move {`,
1535-
this.parse_gen_block()
1536-
} else {
1537-
this.parse_expr_closure()
1538-
}
1532+
// Check for `async {` and `async move {`,
1533+
} else if this.is_gen_block(kw::Async, 0) {
1534+
this.parse_gen_block()
1535+
} else if at_async {
1536+
this.parse_expr_closure()
15391537
} else if this.eat_keyword_noexpect(kw::Await) {
15401538
this.recover_incorrect_await_syntax(lo)
15411539
} else {
@@ -2407,6 +2405,14 @@ impl<'a> Parser<'a> {
24072405
None
24082406
};
24092407

2408+
if let ClosureBinder::NotPresent = binder
2409+
&& coroutine_kind.is_some()
2410+
{
2411+
// couroutine closures and generators can have the same qualifiers, so we might end up
2412+
// in here if there is a missing `|` but also no `{`. Adjust the expectations in that case.
2413+
self.expected_token_types.insert(TokenType::OpenBrace);
2414+
}
2415+
24102416
let capture_clause = self.parse_capture_clause()?;
24112417
let (fn_decl, fn_arg_span) = self.parse_fn_block_decl()?;
24122418
let decl_hi = self.prev_token.span;

tests/ui/parser/block-no-opening-brace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ fn in_try() {
2727
let x = 0;
2828
}
2929

30-
// FIXME(#80931)
3130
fn in_async() {
3231
async
33-
let x = 0; //~ ERROR expected one of `move`, `use`, `|`, or `||`, found keyword `let`
32+
let x = 0;
33+
//~^ ERROR expected one of `move`, `use`, `{`, `|`, or `||`, found keyword `let`
3434
}
3535

3636
// FIXME(#78168)

tests/ui/parser/block-no-opening-brace.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ error: expected expression, found reserved keyword `try`
4343
LL | try
4444
| ^^^ expected expression
4545

46-
error: expected one of `move`, `use`, `|`, or `||`, found keyword `let`
47-
--> $DIR/block-no-opening-brace.rs:33:9
46+
error: expected one of `move`, `use`, `{`, `|`, or `||`, found keyword `let`
47+
--> $DIR/block-no-opening-brace.rs:32:9
4848
|
4949
LL | async
50-
| - expected one of `move`, `use`, `|`, or `||`
50+
| - expected one of `move`, `use`, `{`, `|`, or `||`
5151
LL | let x = 0;
5252
| ^^^ unexpected token
5353

tests/ui/parser/misspelled-keywords/async-move.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected one of `move`, `use`, `|`, or `||`, found `Move`
1+
error: expected one of `move`, `use`, `{`, `|`, or `||`, found `Move`
22
--> $DIR/async-move.rs:4:11
33
|
44
LL | async Move {}
5-
| ^^^^ expected one of `move`, `use`, `|`, or `||`
5+
| ^^^^ expected one of `move`, `use`, `{`, `|`, or `||`
66
|
77
help: write keyword `move` in lowercase
88
|

0 commit comments

Comments
 (0)