Skip to content

Commit 3713512

Browse files
committed
Parse ident with allowing recovery when trying to recover in diagnosing
1 parent 844f131 commit 3713512

9 files changed

Lines changed: 102 additions & 22 deletions

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2264,7 +2264,7 @@ impl<'a> Parser<'a> {
22642264
&& self.look_ahead(1, |t| *t == token::Comma || *t == token::CloseParen)
22652265
{
22662266
// `fn foo(String s) {}`
2267-
let ident = self.parse_ident().unwrap();
2267+
let ident = self.parse_ident_common(true).unwrap();
22682268
let span = pat.span.with_hi(ident.span.hi());
22692269

22702270
err.span_suggestion(

compiler/rustc_parse/src/parser/item.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,11 @@ impl<'a> Parser<'a> {
408408
let insert_span = ident_span.shrink_to_lo();
409409

410410
let ident = if self.token.is_ident()
411-
&& self.token.is_non_reserved_ident()
412411
&& (!is_const || self.look_ahead(1, |t| *t == token::OpenParen))
413412
&& self.look_ahead(1, |t| {
414413
matches!(t.kind, token::Lt | token::OpenBrace | token::OpenParen)
415414
}) {
416-
self.parse_ident().unwrap()
415+
self.parse_ident_common(true).unwrap()
417416
} else {
418417
return Ok(());
419418
};

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ impl<'a> Parser<'a> {
469469
self.parse_ident_common(self.may_recover())
470470
}
471471

472-
fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, Ident> {
472+
pub(crate) fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, Ident> {
473473
let (ident, is_raw) = self.ident_or_err(recover)?;
474474

475475
if is_raw == IdentIsRaw::No && ident.is_reserved() {

tests/ui/parser/macro/kw-in-const-item-pos-recovery-149692.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ macro_rules! m {
66
}
77

88
m!(const Self());
9-
//~^ ERROR expected one of `!` or `::`, found `(`
9+
//~^ ERROR expected identifier, found keyword `Self`
10+
//~^^ ERROR missing `fn` or `struct` for function or struct definition
1011

1112
fn main() {}
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
error: expected one of `!` or `::`, found `(`
2-
--> $DIR/kw-in-const-item-pos-recovery-149692.rs:8:14
1+
error: expected identifier, found keyword `Self`
2+
--> $DIR/kw-in-const-item-pos-recovery-149692.rs:8:10
3+
|
4+
LL | m!(const Self());
5+
| ^^^^ expected identifier, found keyword
6+
7+
error: missing `fn` or `struct` for function or struct definition
8+
--> $DIR/kw-in-const-item-pos-recovery-149692.rs:8:10
39
|
410
LL | (const $id:item()) => {}
511
| -------- while parsing argument for this `item` macro fragment
612
...
713
LL | m!(const Self());
8-
| ^ expected one of `!` or `::`
14+
| ^^^^
15+
|
16+
help: if you meant to call a macro, try
17+
|
18+
LL | m!(const Self!());
19+
| +
920

10-
error: aborting due to 1 previous error
21+
error: aborting due to 2 previous errors
1122

tests/ui/parser/macro/kw-in-item-pos-recovery-149692.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ macro_rules! m {
88
}
99

1010
m!(Self());
11-
//~^ ERROR expected one of `!` or `::`, found `(`
11+
//~^ ERROR expected identifier, found keyword `Self`
12+
//~^^ ERROR missing `fn` or `struct` for function or struct definition
1213

1314
m!(Self{});
14-
//~^ ERROR expected one of `!` or `::`, found `{`
15+
//~^ ERROR expected identifier, found keyword `Self`
16+
//~^^ ERROR missing `enum` or `struct` for enum or struct definition
1517

1618
m!(crate());
17-
//~^ ERROR expected one of `!` or `::`, found `(`
19+
//~^ ERROR expected identifier, found keyword `crate`
20+
//~^^ ERROR missing `fn` or `struct` for function or struct definition
1821

1922
fn main() {}
Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,57 @@
1-
error: expected one of `!` or `::`, found `(`
2-
--> $DIR/kw-in-item-pos-recovery-149692.rs:10:8
1+
error: expected identifier, found keyword `Self`
2+
--> $DIR/kw-in-item-pos-recovery-149692.rs:10:4
3+
|
4+
LL | m!(Self());
5+
| ^^^^ expected identifier, found keyword
6+
7+
error: missing `fn` or `struct` for function or struct definition
8+
--> $DIR/kw-in-item-pos-recovery-149692.rs:10:4
39
|
410
LL | ($id:item()) => {}
511
| -------- while parsing argument for this `item` macro fragment
612
...
713
LL | m!(Self());
8-
| ^ expected one of `!` or `::`
14+
| ^^^^
15+
|
16+
help: if you meant to call a macro, try
17+
|
18+
LL | m!(Self!());
19+
| +
920

10-
error: expected one of `!` or `::`, found `{`
11-
--> $DIR/kw-in-item-pos-recovery-149692.rs:13:8
21+
error: expected identifier, found keyword `Self`
22+
--> $DIR/kw-in-item-pos-recovery-149692.rs:14:4
23+
|
24+
LL | m!(Self{});
25+
| ^^^^ expected identifier, found keyword
26+
27+
error: missing `enum` or `struct` for enum or struct definition
28+
--> $DIR/kw-in-item-pos-recovery-149692.rs:14:4
1229
|
1330
LL | ($id:item()) => {}
1431
| -------- while parsing argument for this `item` macro fragment
1532
...
1633
LL | m!(Self{});
17-
| ^ expected one of `!` or `::`
34+
| ^^^^
35+
36+
error: expected identifier, found keyword `crate`
37+
--> $DIR/kw-in-item-pos-recovery-149692.rs:18:4
38+
|
39+
LL | m!(crate());
40+
| ^^^^^ expected identifier, found keyword
1841

19-
error: expected one of `!` or `::`, found `(`
20-
--> $DIR/kw-in-item-pos-recovery-149692.rs:16:9
42+
error: missing `fn` or `struct` for function or struct definition
43+
--> $DIR/kw-in-item-pos-recovery-149692.rs:18:4
2144
|
2245
LL | ($id:item()) => {}
2346
| -------- while parsing argument for this `item` macro fragment
2447
...
2548
LL | m!(crate());
26-
| ^ expected one of `!` or `::`
49+
| ^^^^^
50+
|
51+
help: if you meant to call a macro, try
52+
|
53+
LL | m!(crate!());
54+
| +
2755

28-
error: aborting due to 3 previous errors
56+
error: aborting due to 6 previous errors
2957

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ edition: 2021
2+
3+
macro_rules! x {
4+
($ty : item) => {};
5+
}
6+
x! {
7+
trait MyTrait { fn bar(c self) }
8+
//~^ ERROR expected identifier, found keyword `self`
9+
//~^^ ERROR expected one of `:`, `@`, or `|`, found keyword `self`
10+
//~^^^ ERROR expected one of `->`, `;`, `where`, or `{`, found `}`
11+
}
12+
13+
fn main() {}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: expected identifier, found keyword `self`
2+
--> $DIR/kw-in-item-pos-recovery-151238.rs:7:28
3+
|
4+
LL | trait MyTrait { fn bar(c self) }
5+
| ^^^^ expected identifier, found keyword
6+
7+
error: expected one of `:`, `@`, or `|`, found keyword `self`
8+
--> $DIR/kw-in-item-pos-recovery-151238.rs:7:28
9+
|
10+
LL | trait MyTrait { fn bar(c self) }
11+
| --^^^^
12+
| | |
13+
| | expected one of `:`, `@`, or `|`
14+
| help: declare the type after the parameter binding: `<identifier>: <type>`
15+
16+
error: expected one of `->`, `;`, `where`, or `{`, found `}`
17+
--> $DIR/kw-in-item-pos-recovery-151238.rs:7:34
18+
|
19+
LL | trait MyTrait { fn bar(c self) }
20+
| --- ^ expected one of `->`, `;`, `where`, or `{`
21+
| |
22+
| while parsing this `fn`
23+
24+
error: aborting due to 3 previous errors
25+

0 commit comments

Comments
 (0)