Skip to content

...or a vertical bar to match bugs #143330

@A4-Tacks

Description

@A4-Tacks

Code

fn main() {
    struct Foo { x: i32, ch: char }
    let pos = Foo { x: 2, ch: 'x' };
    match pos {
        Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' } => (),
        _ => todo!(),
    }
}

Current output

Compiling test_ v0.1.0 (/home/lrne/Project/Rust/test_)
error: unexpected `,` in pattern
 --> src/main.rs:5:30
  |
5 |         Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' } => (),
  |                              ^
  |
help: try adding parentheses to match on a tuple...
  |
5 |         (Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' }) => (),
  |         +                                            +
help: ...or a vertical bar to match on multiple alternatives
  |
5 -         Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' } => (),
5 +         Foo { x: 2 | ch: ' |' } | Foo { x: 3 | ch: '@' } => (),
  |

error: could not compile `test_` (bin "test_") due to 1 previous error

Desired output

Compiling test_ v0.1.0 (/home/lrne/Project/Rust/test_)
error: unexpected `,` in pattern
 --> src/main.rs:5:30
  |
5 |         Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' } => (),
  |                              ^
  |
help: try adding parentheses to match on a tuple...
  |
5 |         (Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' }) => (),
  |         +                                            +
help: ...or a vertical bar to match on multiple alternatives
  |
5 -         Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' } => (),
5 +         Foo { x: 2, ch: ',' } | Foo { x: 3, ch: '@' } => (),
  |

error: could not compile `test_` (bin "test_") due to 1 previous error

Rationale and extra context

No response

Other cases

Rust Version

rustc 1.88.0-nightly (a15cce269 2025-04-17)
binary: rustc
commit-hash: a15cce2690e8fab72422515c9dc02c6fbc506733
commit-date: 2025-04-17
host: aarch64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-papercutDiagnostics: An error or lint that needs small tweaks.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