Skip to content

Commit 9ffde14

Browse files
committed
Recover on misspelled item keyword
1 parent f9b30df commit 9ffde14

File tree

9 files changed

+111
-59
lines changed

9 files changed

+111
-59
lines changed

compiler/rustc_parse/src/parser/item.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ impl<'a> Parser<'a> {
228228
body,
229229
define_opaque: None,
230230
}))
231-
} else if self.eat_keyword(exp!(Extern)) {
232-
if self.eat_keyword(exp!(Crate)) {
231+
} else if self.eat_keyword_case(exp!(Extern), case) {
232+
if self.eat_keyword_case(exp!(Crate), case) {
233233
// EXTERN CRATE
234234
self.parse_item_extern_crate()?
235235
} else {
@@ -241,19 +241,17 @@ impl<'a> Parser<'a> {
241241
let safety = self.parse_safety(Case::Sensitive);
242242
self.expect_keyword(exp!(Extern))?;
243243
self.parse_item_foreign_mod(attrs, safety)?
244-
} else if self.is_static_global() {
245-
let safety = self.parse_safety(Case::Sensitive);
244+
} else if let Some(safety) = self.parse_global_static_front_matter(case) {
246245
// STATIC ITEM
247-
self.bump(); // `static`
248246
let mutability = self.parse_mutability();
249247
self.parse_static_item(safety, mutability)?
250-
} else if self.check_keyword(exp!(Trait)) || self.check_trait_front_matter() {
248+
} else if self.check_keyword_case(exp!(Trait), case) || self.check_trait_front_matter() {
251249
// TRAIT ITEM
252250
self.parse_item_trait(attrs, lo)?
253251
} else if self.check_impl_frontmatter() {
254252
// IMPL ITEM
255253
self.parse_item_impl(attrs, def_())?
256-
} else if let Const::Yes(const_span) = self.parse_constness(Case::Sensitive) {
254+
} else if let Const::Yes(const_span) = self.parse_constness(case) {
257255
// CONST ITEM
258256
self.recover_const_mut(const_span);
259257
self.recover_missing_kw_before_item()?;
@@ -268,18 +266,18 @@ impl<'a> Parser<'a> {
268266
}))
269267
} else if self.is_reuse_path_item() {
270268
self.parse_item_delegation()?
271-
} else if self.check_keyword(exp!(Mod))
272-
|| self.check_keyword(exp!(Unsafe)) && self.is_keyword_ahead(1, &[kw::Mod])
269+
} else if self.check_keyword_case(exp!(Mod), case)
270+
|| self.check_keyword_case(exp!(Unsafe), case) && self.is_keyword_ahead(1, &[kw::Mod])
273271
{
274272
// MODULE ITEM
275273
self.parse_item_mod(attrs)?
276-
} else if self.eat_keyword(exp!(Type)) {
274+
} else if self.eat_keyword_case(exp!(Type), case) {
277275
// TYPE ITEM
278276
self.parse_type_alias(def_())?
279-
} else if self.eat_keyword(exp!(Enum)) {
277+
} else if self.eat_keyword_case(exp!(Enum), case) {
280278
// ENUM ITEM
281279
self.parse_item_enum()?
282-
} else if self.eat_keyword(exp!(Struct)) {
280+
} else if self.eat_keyword_case(exp!(Struct), case) {
283281
// STRUCT ITEM
284282
self.parse_item_struct()?
285283
} else if self.is_kw_followed_by_ident(kw::Union) {
@@ -289,7 +287,7 @@ impl<'a> Parser<'a> {
289287
} else if self.is_builtin() {
290288
// BUILTIN# ITEM
291289
return self.parse_item_builtin();
292-
} else if self.eat_keyword(exp!(Macro)) {
290+
} else if self.eat_keyword_case(exp!(Macro), case) {
293291
// MACROS 2.0 ITEM
294292
self.parse_item_decl_macro(lo)?
295293
} else if let IsMacroRulesItem::Yes { has_bang } = self.is_macro_rules_item() {
@@ -1324,19 +1322,28 @@ impl<'a> Parser<'a> {
13241322
== Some(true)
13251323
}
13261324

1327-
fn is_static_global(&mut self) -> bool {
1328-
if self.check_keyword(exp!(Static)) {
1325+
fn parse_global_static_front_matter(&mut self, case: Case) -> Option<Safety> {
1326+
let is_global_static = if self.check_keyword_case(exp!(Static), case) {
13291327
// Check if this could be a closure.
13301328
!self.look_ahead(1, |token| {
1331-
if token.is_keyword(kw::Move) || token.is_keyword(kw::Use) {
1329+
if token.is_keyword_case(kw::Move, case) || token.is_keyword_case(kw::Use, case) {
13321330
return true;
13331331
}
13341332
matches!(token.kind, token::Or | token::OrOr)
13351333
})
13361334
} else {
13371335
// `$qual static`
1338-
(self.check_keyword(exp!(Unsafe)) || self.check_keyword(exp!(Safe)))
1339-
&& self.look_ahead(1, |t| t.is_keyword(kw::Static))
1336+
(self.check_keyword_case(exp!(Unsafe), case)
1337+
|| self.check_keyword_case(exp!(Safe), case))
1338+
&& self.look_ahead(1, |t| t.is_keyword_case(kw::Static, case))
1339+
};
1340+
1341+
if is_global_static {
1342+
let safety = self.parse_safety(case);
1343+
let _ = self.eat_keyword_case(exp!(Static), case);
1344+
Some(safety)
1345+
} else {
1346+
None
13401347
}
13411348
}
13421349

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
#![feature(associated_type_defaults)]
2+
13
trait Animal {
24
Type Result = u8;
3-
//~^ ERROR expected one of
5+
//~^ ERROR keyword `type` is written in the wrong case
46
}
57

68
fn main() {}

tests/ui/parser/misspelled-keywords/assoc-type.stderr

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
error: expected one of `!` or `::`, found `Result`
2-
--> $DIR/assoc-type.rs:2:10
1+
error: keyword `type` is written in the wrong case
2+
--> $DIR/assoc-type.rs:4:5
33
|
4-
LL | trait Animal {
5-
| - while parsing this item list starting here
64
LL | Type Result = u8;
7-
| ^^^^^^ expected one of `!` or `::`
8-
LL |
9-
LL | }
10-
| - the item list ends here
5+
| ^^^^
116
|
12-
help: write keyword `type` in lowercase
7+
help: write it in lowercase
138
|
149
LL - Type Result = u8;
1510
LL + type Result = u8;

tests/ui/parser/misspelled-keywords/recovery.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@
44

55
trait Animal {
66
Type Result = u8;
7-
//~^ ERROR expected one of
7+
//~^ ERROR keyword `type` is written in the wrong case
88
}
99

1010
Struct Foor {
11-
//~^ ERROR expected one of
11+
//~^ ERROR keyword `struct` is written in the wrong case
1212
hello: String,
1313
}
1414

1515
Const A: u8 = 10;
16+
//~^ ERROR keyword `const` is written in the wrong case
1617

1718
Fn code() {}
19+
//~^ ERROR keyword `fn` is written in the wrong case
1820

1921
Static a: u8 = 0;
22+
//~^ ERROR keyword `static` is written in the wrong case
2023

2124
usee a::b;
25+
//~^ ERROR expected one of
2226

2327
fn main() {}
Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,74 @@
1-
error: expected one of `!` or `::`, found `Result`
2-
--> $DIR/recovery.rs:6:10
1+
error: keyword `type` is written in the wrong case
2+
--> $DIR/recovery.rs:6:5
33
|
4-
LL | trait Animal {
5-
| - while parsing this item list starting here
64
LL | Type Result = u8;
7-
| ^^^^^^ expected one of `!` or `::`
8-
LL |
9-
LL | }
10-
| - the item list ends here
5+
| ^^^^
116
|
12-
help: write keyword `type` in lowercase
7+
help: write it in lowercase
138
|
149
LL - Type Result = u8;
1510
LL + type Result = u8;
1611
|
1712

18-
error: expected one of `!` or `::`, found `Foor`
19-
--> $DIR/recovery.rs:10:8
13+
error: keyword `struct` is written in the wrong case
14+
--> $DIR/recovery.rs:10:1
2015
|
2116
LL | Struct Foor {
22-
| ^^^^ expected one of `!` or `::`
17+
| ^^^^^^
2318
|
24-
help: write keyword `struct` in lowercase (notice the capitalization)
19+
help: write it in lowercase (notice the capitalization)
2520
|
2621
LL - Struct Foor {
2722
LL + struct Foor {
2823
|
2924

30-
error: aborting due to 2 previous errors
25+
error: keyword `const` is written in the wrong case
26+
--> $DIR/recovery.rs:15:1
27+
|
28+
LL | Const A: u8 = 10;
29+
| ^^^^^
30+
|
31+
help: write it in lowercase (notice the capitalization)
32+
|
33+
LL - Const A: u8 = 10;
34+
LL + const A: u8 = 10;
35+
|
36+
37+
error: keyword `fn` is written in the wrong case
38+
--> $DIR/recovery.rs:18:1
39+
|
40+
LL | Fn code() {}
41+
| ^^
42+
|
43+
help: write it in lowercase (notice the capitalization)
44+
|
45+
LL - Fn code() {}
46+
LL + fn code() {}
47+
|
48+
49+
error: keyword `static` is written in the wrong case
50+
--> $DIR/recovery.rs:21:1
51+
|
52+
LL | Static a: u8 = 0;
53+
| ^^^^^^
54+
|
55+
help: write it in lowercase (notice the capitalization)
56+
|
57+
LL - Static a: u8 = 0;
58+
LL + static a: u8 = 0;
59+
|
60+
61+
error: expected one of `!` or `::`, found `a`
62+
--> $DIR/recovery.rs:24:6
63+
|
64+
LL | usee a::b;
65+
| ^ expected one of `!` or `::`
66+
|
67+
help: there is a keyword `use` with a similar name
68+
|
69+
LL - usee a::b;
70+
LL + use a::b;
71+
|
72+
73+
error: aborting due to 6 previous errors
3174

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
Static a = 0;
2-
//~^ ERROR expected one of
3-
1+
Static a: u32 = 0;
2+
//~^ ERROR keyword `static` is written in the wrong case
43
fn main() {}

tests/ui/parser/misspelled-keywords/static.stderr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
error: expected one of `!` or `::`, found `a`
2-
--> $DIR/static.rs:1:8
1+
error: keyword `static` is written in the wrong case
2+
--> $DIR/static.rs:1:1
33
|
4-
LL | Static a = 0;
5-
| ^ expected one of `!` or `::`
4+
LL | Static a: u32 = 0;
5+
| ^^^^^^
66
|
7-
help: write keyword `static` in lowercase (notice the capitalization)
7+
help: write it in lowercase (notice the capitalization)
88
|
9-
LL - Static a = 0;
10-
LL + static a = 0;
9+
LL - Static a: u32 = 0;
10+
LL + static a: u32 = 0;
1111
|
1212

1313
error: aborting due to 1 previous error
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
Struct Foor {
2-
//~^ ERROR expected one of
2+
//~^ ERROR keyword `struct` is written in the wrong case
33
hello: String,
44
}
5+
6+
fn main() {}

tests/ui/parser/misspelled-keywords/struct.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: expected one of `!` or `::`, found `Foor`
2-
--> $DIR/struct.rs:1:8
1+
error: keyword `struct` is written in the wrong case
2+
--> $DIR/struct.rs:1:1
33
|
44
LL | Struct Foor {
5-
| ^^^^ expected one of `!` or `::`
5+
| ^^^^^^
66
|
7-
help: write keyword `struct` in lowercase (notice the capitalization)
7+
help: write it in lowercase (notice the capitalization)
88
|
99
LL - Struct Foor {
1010
LL + struct Foor {

0 commit comments

Comments
 (0)