Skip to content

Try to recover more from struct field parse error #127426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1753,11 +1753,18 @@ impl<'a> Parser<'a> {
fields.push(field);
}
Err(mut err) => {
self.consume_block(Delimiter::Brace, ConsumeClosingDelim::No);
err.span_label(ident_span, format!("while parsing this {adt_ty}"));
let guar = err.emit();
recovered = Recovered::Yes(guar);
break;
if self.look_ahead(1, |next_token| next_token == &token::Comma) {
self.bump();
self.eat(&TokenKind::Comma);
} else if self.prev_token.kind == token::Semi {
// we already suggested `;` -> `,`, here we try to recover and continue parse next field
} else {
self.consume_block(Delimiter::Brace, ConsumeClosingDelim::No);
break;
}
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions tests/ui/pattern/struct-parser-recovery-issue-126344.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
struct Wrong {
x: i32; //~ ERROR struct fields are separated by `,`
y: i32,
z: i32,
h: i32,
}

fn oops(w: &Wrong) {
w.x; //~ ERROR no field `x` on type `&Wrong`
}

fn foo(w: &Wrong) {
w.y;
}

fn haha(w: &Wrong) {
w.z;
}

struct WrongWithType {
x: 1, //~ ERROR expected type, found `1`
y: i32,
z: i32,
h: i32,
}

fn oops_type(w: &WrongWithType) {
w.x; //~ ERROR no field `x` on type `&WrongWithType`
}

fn foo_type(w: &WrongWithType) {
w.y;
}

fn haha_type(w: &WrongWithType) {
w.z;
}

fn main() {
let v = Wrong { x: 1, y: 2, z: 3, h: 4 };
let x = WrongWithType { x: 1, y: 2, z: 3, h: 4 };
}
41 changes: 41 additions & 0 deletions tests/ui/pattern/struct-parser-recovery-issue-126344.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
error: struct fields are separated by `,`
--> $DIR/struct-parser-recovery-issue-126344.rs:2:11
|
LL | struct Wrong {
| ----- while parsing this struct
LL | x: i32;
| ^ help: replace `;` with `,`

error: expected type, found `1`
--> $DIR/struct-parser-recovery-issue-126344.rs:21:8
|
LL | struct WrongWithType {
| ------------- while parsing this struct
LL | x: 1,
| ^ expected type

error[E0609]: no field `x` on type `&Wrong`
--> $DIR/struct-parser-recovery-issue-126344.rs:9:7
|
LL | w.x;
| ^ unknown field
|
help: a field with a similar name exists
|
LL | w.y;
| ~

error[E0609]: no field `x` on type `&WrongWithType`
--> $DIR/struct-parser-recovery-issue-126344.rs:28:7
|
LL | w.x;
| ^ unknown field
|
help: a field with a similar name exists
|
LL | w.y;
| ~

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0609`.
Loading