Skip to content

Commit f6c131b

Browse files
committed
Don't parse keyword as identifier when trying to diagnose
1 parent 844f131 commit f6c131b

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,6 +2261,7 @@ impl<'a> Parser<'a> {
22612261
// If we find a pattern followed by an identifier, it could be an (incorrect)
22622262
// C-style parameter declaration.
22632263
if self.check_ident()
2264+
&& self.token.is_non_reserved_ident()
22642265
&& self.look_ahead(1, |t| *t == token::Comma || *t == token::CloseParen)
22652266
{
22662267
// `fn foo(String s) {}`
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ edition: 2021
2+
3+
macro_rules! x {
4+
($ty : item) => {};
5+
}
6+
x! {
7+
trait MyTrait { fn bar(c self) }
8+
//~^ ERROR expected one of `:`, `@`, or `|`, found keyword `self`
9+
//~^^ ERROR expected one of `->`, `;`, `where`, or `{`, found `}`
10+
}
11+
12+
fn main() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: expected one of `:`, `@`, or `|`, 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 one of `:`, `@`, or `|`
6+
7+
error: expected one of `->`, `;`, `where`, or `{`, found `}`
8+
--> $DIR/kw-in-item-pos-recovery-151238.rs:7:34
9+
|
10+
LL | trait MyTrait { fn bar(c self) }
11+
| --- ^ expected one of `->`, `;`, `where`, or `{`
12+
| |
13+
| while parsing this `fn`
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)