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

Account for match arms statements without a block #82524

Closed
estebank opened this issue Feb 25, 2021 · 0 comments · Fixed by #82714
Closed

Account for match arms statements without a block #82524

estebank opened this issue Feb 25, 2021 · 0 comments · Fixed by #82714
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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

@estebank
Copy link
Contributor

Given the following code:

    match S::get(1) {
        Some(Val::Foo) => ();
        _ => (),
    }

The current output is:

error: expected one of `,`, `.`, `?`, `}`, or an operator, found `;`
  --> src/main.rs:26:29
   |
26 |         Some(Val::Foo) => ();
   |                        --   ^ expected one of `,`, `.`, `?`, `}`, or an operator
   |                        |
   |                        while parsing the `match` arm starting here

error[E0283]: type annotations needed
  --> src/main.rs:25:11
   |
4  |     fn get<K, V: Default>(_: K) -> Option<V> {
   |                  ------- required by this bound in `S::get`
...
25 |     match S::get(1) {
   |           ^^^^^^ cannot infer type for type parameter `V` declared on the associated function `get`
   |
   = note: cannot satisfy `_: Default`
help: consider specifying the type arguments in the function call
   |
25 |     match S::get::<K, V>(1) {
   |                 ^^^^^^^^

The first error is a generic parse error that we could account for. The second is an inference error that would be completely solved if the parse error was recovered more gracefully.

Ideally, for a generic case the output should look like:

error: expected one of `,`, `.`, `?`, `}`, or an operator, found `;`
  --> src/main.rs:26:29
   |
26 |         Some(Val::Foo) => ();
   |                        --   ^ expected one of `,`, `.`, `?`, `}`, or an operator
   |                        |
   |                        while parsing the `match` arm starting here
help: you likely meant to write a match arm block
   |
26 |         Some(Val::Foo) => { (); }
   |                           ^     ^

while for a case where a single statement is found we could also suggest:

error: expected one of `,`, `.`, `?`, `}`, or an operator, found `;`
  --> src/main.rs:26:29
   |
26 |         Some(Val::Foo) => ();
   |                        --   ^ expected one of `,`, `.`, `?`, `}`, or an operator
   |                        |
   |                        while parsing the `match` arm starting here
help: you might have meant to write a match arm expression separator
   |
26 |         Some(Val::Foo) => (),
   |                             ^

Taken from https://twitter.com/mcclure111/status/1364998004852236289

@estebank estebank added 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. D-papercut Diagnostics: An error or lint that needs small tweaks. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Feb 25, 2021
@estebank estebank changed the title Account for match arms without a block Account for match arms statements without a block Feb 25, 2021
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Mar 5, 2021
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Mar 5, 2021
@bors bors closed this as completed in ae494d1 Mar 6, 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 A-parser Area: The parsing of Rust source code to an AST. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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.

1 participant