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

Range patterns cannot satisfy the whole usize domain #113051

Closed
MultisampledNight opened this issue Jun 26, 2023 · 1 comment
Closed

Range patterns cannot satisfy the whole usize domain #113051

MultisampledNight opened this issue Jun 26, 2023 · 1 comment

Comments

@MultisampledNight
Copy link
Contributor

When trying to pattern match an usize, no ranges seem to be able to satisfy it, even if the ranges would automatically cover the entire added/removed range if usize's size changes. There seems to be a hard requirement of _.

MVCE

fn main() {
    match 4usize {
        0.. => println!("anything"),
    }
}

fails with

error[E0004]: non-exhaustive patterns: `_` not covered
 --> src/main.rs:2:11
  |
2 |     match 4usize {
  |           ^^^^^^ pattern `_` not covered
  |
  = note: the matched value is of type `usize`
  = note: `usize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
  |
3 ~         0.. => println!("anything else"),
4 ~         _ => todo!(),
  |

For more information about this error, try `rustc --explain E0004`.

However, switching to u64 makes it work again:

-    match 4usize {
+    match 4u64 {

Sidenote: Changing 0.. to 0..=usize::MAX yields the same error.

Workaround

Adding _ as a new branch on the last match branch. Clippy cries about it though.

Environment

rustc 1.72.0-nightly (8084f397c 2023-06-25)
binary: rustc
commit-hash: 8084f397c6710e4748994dd1488b1f399283d469
commit-date: 2023-06-25
host: x86_64-unknown-linux-gnu
release: 1.72.0-nightly
LLVM version: 16.0.5
@Noratrieb Noratrieb changed the title Ranges cannot satisfy the whole usize domain, but can do so on u64 Range patterns cannot satisfy the whole usize domain Jun 26, 2023
@Jules-Bertholet
Copy link
Contributor

Jules-Bertholet commented Jun 26, 2023

Dupe of #111859, see also #56354

@MultisampledNight MultisampledNight closed this as not planned Won't fix, can't repro, duplicate, stale Jun 26, 2023
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