Skip to content
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

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

Closed
comex opened this issue May 10, 2018 · 2 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@comex
Copy link
Contributor

comex commented May 10, 2018

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.
@estebank estebank added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST labels May 14, 2018
@simartin
Copy link
Contributor

I propose to work on that one.

simartin added a commit to simartin/rust that referenced this issue May 20, 2018
kennytm added a commit to kennytm/rust that referenced this issue May 22, 2018
Issue rust-lang#50636: Improve error diagnostic with missing commas after struct fields.

Fixes rust-lang#50636
bors added a commit that referenced this issue May 22, 2018
Rollup of 15 pull requests

Successful merges:

 - #50846 (Add E0665)
 - #50849 (CheckLoopVisitor: also visit closure arguments)
 - #50863 (Make `[T]::len` and `str::len` const fn)
 - #50875 (rustdoc: use "short form" doc(cfg) printing even when combined with other conditionals)
 - #50913 (Fix typo in cell.rs)
 - #50914 (Issue #50636: Improve error diagnostic with missing commas after struct fields.)
 - #50931 (Inline `try_get`.)
 - #50932 (Optimize seen Predicate filtering.)
 - #50945 (Stabilize feature from_ref)
 - #50946 (rustc: Fix procedural macros generating lifetime tokens)
 - #50947 (rustdoc: set tab width in rust source blocks)
 - #50952 (Add the 2018 edition of the book to doc.rust-lang.org)
 - #50958 (Micro-optimization on PR#50697)
 - #50961 (Fix FileCheck finding with MSVC)
 - #50963 (Right-size the `VecDeque` in `coerce_unsized`.)

Failed merges:
@comex
Copy link
Contributor Author

comex commented May 22, 2018

@simartin ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

3 participants