Skip to content

Commit 292aa87

Browse files
committed
Fix use closure parsing error message
1 parent 7c17bf8 commit 292aa87

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

compiler/rustc_parse/src/parser/item.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,7 @@ impl<'a> Parser<'a> {
209209
let check_pub = def == &Defaultness::Final;
210210
let mut def_ = || mem::replace(def, Defaultness::Final);
211211

212-
let info = if !self.look_ahead(1, |t| [token::OrOr, token::Or].contains(&t.kind))
213-
&& self.eat_keyword_case(exp!(Use), case)
214-
{
212+
let info = if !self.is_use_closure() && self.eat_keyword_case(exp!(Use), case) {
215213
self.parse_use_item()?
216214
} else if self.check_fn_front_matter(check_pub, case) {
217215
// FUNCTION ITEM
@@ -1279,6 +1277,21 @@ impl<'a> Parser<'a> {
12791277
None
12801278
}
12811279

1280+
fn is_use_closure(&self) -> bool {
1281+
if self.token.is_keyword(kw::Use) {
1282+
// Check if this could be a closure.
1283+
self.look_ahead(1, |token| {
1284+
// Move or Async here would be an error but still we're parsing a closure
1285+
let dist =
1286+
if token.is_keyword(kw::Move) || token.is_keyword(kw::Async) { 2 } else { 1 };
1287+
1288+
self.look_ahead(dist, |token| matches!(token.kind, token::Or | token::OrOr))
1289+
})
1290+
} else {
1291+
false
1292+
}
1293+
}
1294+
12821295
fn is_unsafe_foreign_mod(&self) -> bool {
12831296
self.token.is_keyword(kw::Unsafe)
12841297
&& self.is_keyword_ahead(1, &[kw::Extern])

tests/ui/ergonomic-clones/closure/parse.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ fn parse2() {
1414

1515
fn parse3() {
1616
use move || {
17-
//~^ ERROR expected identifier, found keyword `move`
18-
//~| ERROR expected one of `::`, `;`, or `as`, found `||`
19-
// FIXME ideally we should error like in the previous example
17+
//~^ ERROR expected one of `async`, `|`, or `||`, found keyword `move`
2018
};
2119
}
2220

tests/ui/ergonomic-clones/closure/parse.stderr

+3-9
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,11 @@ error: expected one of `async`, `|`, or `||`, found keyword `use`
1818
LL | move use || {
1919
| ^^^ expected one of `async`, `|`, or `||`
2020

21-
error: expected identifier, found keyword `move`
21+
error: expected one of `async`, `|`, or `||`, found keyword `move`
2222
--> $DIR/parse.rs:16:9
2323
|
2424
LL | use move || {
25-
| ^^^^ expected identifier, found keyword
25+
| ^^^^ expected one of `async`, `|`, or `||`
2626

27-
error: expected one of `::`, `;`, or `as`, found `||`
28-
--> $DIR/parse.rs:16:14
29-
|
30-
LL | use move || {
31-
| ^^ expected one of `::`, `;`, or `as`
32-
33-
error: aborting due to 4 previous errors
27+
error: aborting due to 3 previous errors
3428

0 commit comments

Comments
 (0)