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

Misleading error for missing comma/semicolon in array literal #96791

Closed
LyricLy opened this issue May 6, 2022 · 0 comments · Fixed by #98796
Closed

Misleading error for missing comma/semicolon in array literal #96791

LyricLy opened this issue May 6, 2022 · 0 comments · Fixed by #98796
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@LyricLy
Copy link

LyricLy commented May 6, 2022

Given the following code: Playground

fn main() {
    let v = [1
    2];
}

The current output is:

error: expected `;`, found `2`
 --> test.rs:2:15
  |
2 |     let v = [1
  |               ^ help: add `;` here
3 |     2];
  |     - unexpected token

error: expected one of `.`, `;`, `?`, `}`, or an operator, found `]`
 --> test.rs:3:6
  |
3 |     2];
  |      ^ expected one of `.`, `;`, `?`, `}`, or an operator

error: aborting due to 2 previous errors

Ideally the output should look like:

error: expected one of `,`, `.`, `;`, `?`, `]`, or an operator, found `2`
 --> test.rs:3:5
  |
2 |     let v = [1
  |               -
  |               |
  |               expected one of `,`, `.`, `;`, `?`, `]`, or an operator
3 |     2];
  |     ^ unexpected token

The error implies that ; is the only valid token at the end of the second line, and that the third line is being considered a separate statement from the second line, probably due to a consequence of the fault-tolerant parsing machinery.

Reasonable output is given when the newline is removed or there is more than 2 elements:

error: expected one of `,`, `.`, `;`, `?`, `]`, or an operator, found `2`
 --> test.rs:2:16
  |
2 |     let v = [1 2
  |                ^ expected one of `,`, `.`, `;`, `?`, `]`, or an operator

error: aborting due to previous error
error: expected one of `,`, `.`, `?`, `]`, or an operator, found `3`
 --> test.rs:3:5
  |
2 |     let v = [1, 2
  |                  -
  |                  |
  |                  expected one of `,`, `.`, `?`, `]`, or an operator
  |                  help: missing `,`
3 |     3];
  |     ^ unexpected token

error: aborting due to previous error

This should be fixed because it is confusing in less clear situations such as the following, especially where someone might expect the comma to be implicit due to the behaviour of match.

let v = [
    {
        1
    }
    {
        2
    }
];

This happens in every edition and on both stable and nightly.

@LyricLy LyricLy added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 6, 2022
@compiler-errors compiler-errors added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels May 7, 2022
@bors bors closed this as completed in f6ea143 Aug 4, 2022
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-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants