Skip to content

Rust match bound non-exhaustive patterns when all possible patterns for char are handled #108932

Open
@Arthurdw

Description

@Arthurdw

I tried this code:

fn main() {
    let val = "(hello({()}))";
    let mut destruct = Vec::new();

    for c in val.chars() {
        match c {
            sel @ ('(' | '{' | '[') => destruct.push(match sel {
                // If I'm correct, the only possible value `sel` can have here should be any of these 3
                // Yet rust wants me to add a catch-all arm
                '(' => ')',
                '{' => '}',
                '[' => ']'
            }),
            sel @ (')' | '}' | ']') => if destruct.len() > 0 && *destruct.last().unwrap() == sel { destruct.pop().unwrap(); },
            _ => ()
        }
    }

    println!("{}", destruct.len() == 0);
}

I expected to see this happen:
No error to occur and that it compiles peacefully

Instead, this happened:

error[E0004]: non-exhaustive patterns: `'\0'..='\''`, `')'..='Z'`, `'\\'..='z'` and 2 more not covered
 --> src\main.rs:7:60
  |
7 |             sel @ ('(' | '{' | '[') => destruct.push(match sel {
  |                                                            ^^^ patterns `'\0'..='\''`, `')'..='Z'`, `'\\'..='z'` and 2 more not covered
  |
  = note: the matched value is of type `char`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown, or multiple match arms
  |
12~                 '[' => ']',
13+                 _ => todo!()
  |

For more information about this error, try `rustc --explain E0004`.
error: could not compile `bug-match` due to previous error

Meta

rustc --version --verbose:

rustc 1.67.1 (d5a82bbd2 2023-02-07)
binary: rustc
commit-hash: d5a82bbd26e1ad8b7401f6a718a9c57c96905483
commit-date: 2023-02-07
host: x86_64-pc-windows-msvc
release: 1.67.1
LLVM version: 15.0.6

Also happens on:

rustc 1.70.0-nightly (900c35403 2023-03-08)
binary: rustc
commit-hash: 900c3540378c8422b8087ffa3db60fa6c8abfcad
commit-date: 2023-03-08
host: x86_64-pc-windows-msvc
release: 1.70.0-nightly
LLVM version: 15.0.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-exhaustiveness-checkingRelating to exhaustiveness / usefulness checking of patternsA-patternsRelating to patterns and pattern matchingC-enhancementCategory: An issue proposing an enhancement or a PR with one.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