Skip to content

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

Closed
@dtolnay

Description

@dtolnay

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!()
   |

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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