Skip to content

Commit e38eaf2

Browse files
authored
Rollup merge of #75513 - estebank:confused-parser, r=davidtwco
Recover gracefully from `struct` parse errors Currently the parser tries to recover from finding a keyword where a field name was expected, but this causes extra knock down parse errors that are completely irrelevant. Instead, bail out early in the parsing of the field and consume the remaining tokens in the block. This can reduce output significantly. _Improvements based on the narrative in https://fasterthanli.me/articles/i-am-a-java-csharp-c-or-cplusplus-dev-time-to-do-some-rust_
2 parents 28b11ab + 2e9b45e commit e38eaf2

File tree

5 files changed

+4
-18
lines changed

5 files changed

+4
-18
lines changed

src/librustc_parse/parser/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,7 @@ impl<'a> Parser<'a> {
13131313
vis: Visibility,
13141314
attrs: Vec<Attribute>,
13151315
) -> PResult<'a, StructField> {
1316-
let name = self.parse_ident()?;
1316+
let name = self.parse_ident_common(false)?;
13171317
self.expect(&token::Colon)?;
13181318
let ty = self.parse_ty()?;
13191319
Ok(StructField {

src/test/ui/parser/mismatched-braces/missing-close-brace-in-struct.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ pub(crate) struct Bar<T> {
22
foo: T,
33

44
trait T { //~ ERROR expected identifier, found keyword `trait`
5-
//~^ ERROR expected `:`, found `T`
65
fn foo(&self);
76
}
87

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this file contains an unclosed delimiter
2-
--> $DIR/missing-close-brace-in-struct.rs:14:65
2+
--> $DIR/missing-close-brace-in-struct.rs:13:65
33
|
44
LL | pub(crate) struct Bar<T> {
55
| - unclosed delimiter
@@ -13,11 +13,5 @@ error: expected identifier, found keyword `trait`
1313
LL | trait T {
1414
| ^^^^^ expected identifier, found keyword
1515

16-
error: expected `:`, found `T`
17-
--> $DIR/missing-close-brace-in-struct.rs:4:7
18-
|
19-
LL | trait T {
20-
| ^ expected `:`
21-
22-
error: aborting due to 3 previous errors
16+
error: aborting due to 2 previous errors
2317

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
struct S {
22
let foo: (),
33
//~^ ERROR expected identifier, found keyword `let`
4-
//~^^ ERROR expected `:`, found `foo`
54
}
65

76
fn main() {}

src/test/ui/parser/removed-syntax-field-let.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,5 @@ error: expected identifier, found keyword `let`
44
LL | let foo: (),
55
| ^^^ expected identifier, found keyword
66

7-
error: expected `:`, found `foo`
8-
--> $DIR/removed-syntax-field-let.rs:2:9
9-
|
10-
LL | let foo: (),
11-
| ^^^ expected `:`
12-
13-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
148

0 commit comments

Comments
 (0)