Skip to content

parse error recovery could be better with missing commas after struct fields #50636

Closed
@comex

Description

@comex

Test case: (playground link)

struct S {
    foo: u32 // <- missing comma
    bar: u32
}

fn main() {
    let s = S { foo: 5, bar: 6 };
}

Actual result (nightly):

error: expected `,`, or `}`, found `bar`
 --> src/main.rs:3:5
  |
3 |     bar: u32
  |     ^^^
  |
  = help: struct fields should be separated by commas

error[E0560]: struct `S` has no field named `foo`
 --> src/main.rs:7:17
  |
7 |     let s = S { foo: 5, bar: 6 };
  |                 ^^^ `S` does not have this field

error[E0560]: struct `S` has no field named `bar`
 --> src/main.rs:7:25
  |
7 |     let s = S { foo: 5, bar: 6 };
  |                         ^^^ `S` does not have this field

Expected result:

Ideally the parser would guess that there was meant to be a comma at the end of the line. Then:

  • The "expected comma" error could highlight the line missing a comma (and suggest a fix-it), rather than highlighting the first token in the next line.
  • After producing an error, it could recover by pretending the comma was there, adding the foo and bar fields to the struct to avoid the latter two errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions