Skip to content

parser: [elem; count] is strong signal that an array literal was intended #107518

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

Open
pnkfelix opened this issue Jan 31, 2023 · 0 comments
Open
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@pnkfelix
Copy link
Member

pnkfelix commented Jan 31, 2023

Code

fn main() {
    (a_function_that_takes_an_array[0; 10]);
}

fn a_function_that_takes_an_array(arg: [u8; 10]) {
    let _ = arg;
}

Current output

error: expected one of `.`, `?`, `]`, or an operator, found `;`
 --> src/main.rs:2:38
  |
2 |     (a_function_that_takes_an_array[0; 10]);
  |                                      ^ expected one of `.`, `?`, `]`, or an operator

Desired output

error: expected one of `.`, `?`, `]`, or an operator, found `;`
 --> src/main.rs:2:38
  |
2 |     (a_function_that_takes_an_array[0; 10]);
  |                                      ^ expected one of `.`, `?`, `]`, or an operator

note: `[0; 10]` would construct an array literal, but not immediately following the identifier `a_function_that_takes_an_array`

that is, In addition to the current parser error, it would be nice if the diagnostic included a note or suggestion that while [elem; count] can be used to construct an array literal, the tokens above cannot be an array literal because of the immediately preceding identifier.

(I suppose in an ideal world it would even suggest adding the parentheses to turn it into a function call, but that would require more context than I think we can expect to have at this point in the context of where the parser is.)

Rationale and extra context

I'll be honest, I left out the call parentheses by accident, and then kept staring at the rustc error message trying to understand why it wouldn't accept a semi-colon there.

Other cases

No response

Anything else?

No response

@pnkfelix pnkfelix 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 Jan 31, 2023
@estebank estebank added the A-parser Area: The parsing of Rust source code to an AST label Feb 1, 2023
@obeis obeis removed their assignment Jan 14, 2024
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 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