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

Invalid syntax (missing comma) in non-exhaustive patterns error's suggestion involving macros #94866

Closed
dtolnay opened this issue Mar 11, 2022 · 0 comments · Fixed by #98267
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dtolnay
Copy link
Member

dtolnay commented Mar 11, 2022

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=b417dca9b7f8cab72fc583d6a97f97de

macro_rules! m {
    () => {
        {}
    };
}

enum Enum { A, B }

fn main() {
    match Enum::A {
        Enum::A => m!()
    }
}

The current output is:

error[E0004]: non-exhaustive patterns: `B` not covered
  --> src/main.rs:10:11
   |
10 |     match Enum::A {
   |           ^^^^^^^ pattern `B` not covered
   |
note: `Enum` defined here
  --> src/main.rs:7:16
   |
7  | enum Enum { A, B }
   |      ----      ^ not covered
   = note: the matched value is of type `Enum`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
   |
11 ~         Enum::A => m!()
12 +         B => todo!()
   |

Rustc's suggested fix is:

    match Enum::A {
        Enum::A => m!()
        B => todo!()
    }

which is not valid syntax:

error: expected one of `,`, `.`, `?`, `}`, or an operator, found `B`
  --> src/main.rs:12:9
   |
11 |         Enum::A => m!()
   |                 --     - expected one of `,`, `.`, `?`, `}`, or an operator
   |                 |
   |                 while parsing the `match` arm starting here
12 |         B => todo!()
   |         ^ unexpected token

The diagnostic appears to be getting misled by the fact that m!() expands to a braced block, which would not ordinarily require a trailing comma if written inline in the match arm. The diagnostic correctly suggests the comma if the macro expands to something different than a braced block.

macro_rules! m {
    () => {
        ()
    };
}
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
   |
11 ~         Enum::A => m!(),
12 +         B => todo!()
   |
@dtolnay dtolnay 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. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Mar 11, 2022
@bors bors closed this as completed in 3e5800b Jun 21, 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 D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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