Skip to content

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

Open
@pnkfelix

Description

@pnkfelix

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions