Rust: Suboptimal error in case of duplicate `,` in struct constructor

Created on 22 May 2018  路  6Comments  路  Source: rust-lang/rust

struct Foo {
    a: i32,
    b: i32,
}

fn main() {
    let x = Foo {
        a: 42,,
        b: 3,
    };
}

results in

error: expected identifier, found `,`
 --> src/main.rs:9:15
  |
8 |     let x = Foo {
  |             --- while parsing this struct
9 |         a: 42,,
  |               ^ expected identifier

error[E0063]: missing field `b` in initializer of `Foo`
 --> src/main.rs:8:13
  |
8 |     let x = Foo {
  |             ^^^ missing `b`

we may want to add a parser recovery that just reports the error, eats the comma and continues parsing as normally

A-diagnostics E-easy

Most helpful comment

Missing commas have been fixed mere moments ago: https://github.com/rust-lang/rust/pull/50914#event-1640112916

All 6 comments

The parser gives up on parsing this error here:

https://github.com/estebank/rust/blob/47a8eb7c4e24b61a8a9ab4eaff60ef65291aaa56/src/libsyntax/parse/parser.rs#L2495-L2503

It could try to continue parsing if it encounters some simple tokens and a look ahead verifies that the next field is well formed. It could also recover from missing commas as well.

Missing commas have been fixed mere moments ago: https://github.com/rust-lang/rust/pull/50914#event-1640112916

@estebank @oli-obk I checked above issue in master branch after merge of #50914, this issue still persists, let me know if I am missing anything, else I would like to take on this issue if nobody is working on this.

@PramodBisht #50914 only addresses the case where a comma is missing in a struct definition.

The following broken code:

struct Foo {
    a: i32
    b: i32,
}

struct Bar {
    a: i32,,
    b: i32,
}

fn main() {
    let x = Foo {
        a: 42
        b: 3,
    };
    let y = Bar {
        a: 42,,
        b: 3,
    };
}

Should only have the following output:

error: expected `,`, or `}`, found `b`
 --> src/main.rs:2:11
  |
2 |     a: i32
  |           ^ help: try adding a comma: `,`

error: expected identifier, found `,`
 --> src/main.rs:7:12
  |
7 |     a: i32,,
  |            ^
  |            |
  |            expected identifier
  |            help: remove this comma

error: expected one of `,`, `.`, `?`, `}`, or an operator, found `b`
  --> src/main.rs:14:9
   |
13 |         a: 42
   |              - 
   |              |
   |              expected one of `,`, `.`, `?`, `}`, or an operator here
   |              help: add a comma here: `,`
14 |         b: 3,
   |         ^ unexpected token

error: expected identifier, found `,`
  --> src/main.rs:17:15
   |
16 |     let y = Bar {
   |             --- while parsing this struct
17 |         a: 42,,
   |               ^
   |               |
   |               expected identifier
   |               help: remove this comma

Note that there should be no error about missing fields, as the struct definition parser should recover after the parse error and include the following fields.

@estebank Ah, right. Got it now :)

@lambtowolf : I was already working on that, feel free to snatch it from me if I don't raise PR in next few days :P

Was this page helpful?
0 / 5 - 0 ratings