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

Better Diagnostics When Using Wrong Brackets to Create an Array #87672

Closed
Hpmason opened this issue Jul 31, 2021 · 1 comment · Fixed by #87830
Closed

Better Diagnostics When Using Wrong Brackets to Create an Array #87672

Hpmason opened this issue Jul 31, 2021 · 1 comment · Fixed by #87830
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Hpmason
Copy link

Hpmason commented Jul 31, 2021

I ended up spending way too much time debugging the following error, since I was trying to port some C code to rust.

const ARR: [u8; 3] = {
    1, 2, 3
}

Give the error:

error: expected one of `.`, `;`, `?`, `}`, or an operator, found `,`
 --> src/lib.rs:2:6
  |
2 |     1, 2, 3,
  |      ^ expected one of `.`, `;`, `?`, `}`, or an operator

error: aborting due to previous error

error: could not compile `playground`

The error message is very vague and hard to determine the actual error here.
Seeing as though some programming languages like Java and C use {} for arrays, I think it'd make sense for the compiler to suggest using [] if the user is trying to create an array.

@inquisitivecrystal inquisitivecrystal added A-diagnostics Area: Messages for errors, warnings, and lints D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Jul 31, 2021
@hkmatsumoto
Copy link
Member

I suppose adding manual diagnostic somewhere around rustc_parse::parse::parse_block_expr will get the job done.
@rustbot claim

bors added a commit to rust-lang-ci/rust that referenced this issue Sep 21, 2021
…y-esque-block-expr, r=estebank

Suggest replacing braces for brackets on array-esque invalid block expr

Newcomers may write `{1, 2, 3}` for making arrays, and the current error message is not informative enough to quickly convince them what is needed to fix the error.

This PR implements a diagnostic for this case, and its output looks like this:
```text
error: this code is interpreted as a block expression, not an array
 --> src/lib.rs:1:22
  |
1 |   const FOO: [u8; 3] = {
  |  ______________________^
2 | |     1, 2, 3
3 | | };
  | |_^
  |
  = note: to define an array, one would use square brackets instead of curly braces
help: try using [] instead of {}
  |
1 | const FOO: [u8; 3] = [
2 |     1, 2, 3
3 | ];
  |
```

Fix rust-lang#87672
@bors bors closed this as completed in 21eff8f Sep 21, 2021
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 C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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.

3 participants