Skip to content

Match arm treats ranges and conditions differently #74277

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

Closed
kpp opened this issue Jul 12, 2020 · 1 comment
Closed

Match arm treats ranges and conditions differently #74277

kpp opened this issue Jul 12, 2020 · 1 comment

Comments

@kpp
Copy link
Contributor

kpp commented Jul 12, 2020

I tried this code:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b57dd6cc254167f09d1a51f753ddebb6

fn main() {
    match 42u32 {
        x if x >= 0 => dbg!(x)
    };
}

Gives error:

error[E0004]: non-exhaustive patterns: `_` not covered
 --> src/main.rs:2:11
  |
2 |     match 42u32 {
  |           ^^^^^ pattern `_` not covered
  |
  = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
  = note: the matched value is of type `u32`

However this one with ranges compiles fine:

fn main() {
    match 42u32 {
        x @ 0..=std::u32::MAX => dbg!(x)
    };
}

I would expect x @ 0..=std::u32::MAX and x if x >= 0 be equal in match arms.

@jonas-schievink
Copy link
Contributor

This is intentional – match guards are arbitrary expressions, and Rust doesn't look into the value of expressions to emit control-flow-related errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants