Skip to content

Commit

Permalink
Rollup merge of #75513 - estebank:confused-parser, r=davidtwco
Browse files Browse the repository at this point in the history
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_
  • Loading branch information
tmandry authored Aug 15, 2020
2 parents 28b11ab + 2e9b45e commit e38eaf2
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/librustc_parse/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ impl<'a> Parser<'a> {
vis: Visibility,
attrs: Vec<Attribute>,
) -> PResult<'a, StructField> {
let name = self.parse_ident()?;
let name = self.parse_ident_common(false)?;
self.expect(&token::Colon)?;
let ty = self.parse_ty()?;
Ok(StructField {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub(crate) struct Bar<T> {
foo: T,

trait T { //~ ERROR expected identifier, found keyword `trait`
//~^ ERROR expected `:`, found `T`
fn foo(&self);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this file contains an unclosed delimiter
--> $DIR/missing-close-brace-in-struct.rs:14:65
--> $DIR/missing-close-brace-in-struct.rs:13:65
|
LL | pub(crate) struct Bar<T> {
| - unclosed delimiter
Expand All @@ -13,11 +13,5 @@ error: expected identifier, found keyword `trait`
LL | trait T {
| ^^^^^ expected identifier, found keyword

error: expected `:`, found `T`
--> $DIR/missing-close-brace-in-struct.rs:4:7
|
LL | trait T {
| ^ expected `:`

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

1 change: 0 additions & 1 deletion src/test/ui/parser/removed-syntax-field-let.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
struct S {
let foo: (),
//~^ ERROR expected identifier, found keyword `let`
//~^^ ERROR expected `:`, found `foo`
}

fn main() {}
8 changes: 1 addition & 7 deletions src/test/ui/parser/removed-syntax-field-let.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,5 @@ error: expected identifier, found keyword `let`
LL | let foo: (),
| ^^^ expected identifier, found keyword

error: expected `:`, found `foo`
--> $DIR/removed-syntax-field-let.rs:2:9
|
LL | let foo: (),
| ^^^ expected `:`

error: aborting due to 2 previous errors
error: aborting due to previous error

0 comments on commit e38eaf2

Please sign in to comment.