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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diagnostics emitted for disallowed exhaustive matches on types that contain a usize lack information about why the match cannot be considered exhaustive #85222

Closed
PatchMixolydic opened this issue May 12, 2021 · 3 comments 路 Fixed by #114397
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-exhaustiveness-checking Relating to exhaustiveness / usefulness checking of patterns A-patterns Relating to patterns and pattern matching D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@PatchMixolydic
Copy link
Contributor

Sorry about the long title 馃槄

Given the following code (playground):

fn main() {
    let x: usize = 4;
    let y = Some(x);

    match x {
        0 => (),
        1..=usize::MAX => (),
    }

    match y {
        Some(0) => (),
        Some(1..=usize::MAX) => (),
        None => (),
    }
}

The current output is:

error[E0004]: non-exhaustive patterns: `_` not covered
 --> src/main.rs:5:11
  |
5 |     match x {
  |           ^ 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 `usize`
  = note: `usize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively
  = help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching

error[E0004]: non-exhaustive patterns: `Some(_)` not covered
   --> src/main.rs:10:11
    |
10  |     match y {
    |           ^ pattern `Some(_)` 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 `Option<usize>`

Note how the first diagnostic, emitted when matching on a usize, contains additional information about why this exhaustive match does not pass the exhaustiveness check. Ideally, this information should be added to the second diagnostic as well:

error[E0004]: non-exhaustive patterns: `Some(_)` not covered
   --> src/main.rs:10:11
    |
10  |     match y {
    |           ^ pattern `Some(_)` 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 `Option<usize>`
    = note: `usize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively
    = help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching

@rustbot modify labels: +A-exhaustiveness-checking +A-patterns +D-terse

@PatchMixolydic PatchMixolydic added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 12, 2021
@rustbot rustbot added A-exhaustiveness-checking Relating to exhaustiveness / usefulness checking of patterns A-patterns Relating to patterns and pattern matching D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels May 12, 2021
@PatchMixolydic PatchMixolydic changed the title Diagnostics emitted for disallowed exhaustive matches on types that contain a usize lack information about precise_pointer_size_matching Diagnostics emitted for disallowed exhaustive matches on types that contain a usize lack information about why the match cannot be considered exhaustive May 12, 2021
@Nadrieril
Copy link
Member

Hm, even though usize has a different size on different architectures, 0..usize::MAX should always be exhaustive, right? Then that code would be accepted, but the following would still error:

match 0usize {
  0 => {}
  1..=18446744073709551615 => {}
}

We could have the error for this say "missing pattern: 18446744073709551616..=usize::MAX", which would also work if there's a Some around it.

That said, pretty sure what you describe also happens for non_exhaustive enums too, and there we'll want to do what you suggest.

@workingjubilee
Copy link
Contributor

see also #56354

@Nadrieril
Copy link
Member

Nadrieril commented Apr 10, 2023

I forgot this issue existed, but yeah this is a lint we want. I explained a bit about how this might be done in #105479 (comment), if anyone wants to tackle this feel free to ping me

@Nadrieril Nadrieril added the E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. label Apr 10, 2023
@bors bors closed this as completed in c75b6bd Aug 25, 2023
github-actions bot pushed a commit to rust-lang/miri that referenced this issue Aug 26, 2023
Add note when matching on tuples/ADTs containing non-exhaustive types

Fixes rust-lang/rust#85222

r? `@Nadrieril`
lnicola pushed a commit to lnicola/rust-analyzer that referenced this issue Apr 7, 2024
Add note when matching on tuples/ADTs containing non-exhaustive types

Fixes rust-lang/rust#85222

r? `@Nadrieril`
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this issue Apr 27, 2024
Add note when matching on tuples/ADTs containing non-exhaustive types

Fixes rust-lang/rust#85222

r? `@Nadrieril`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-exhaustiveness-checking Relating to exhaustiveness / usefulness checking of patterns A-patterns Relating to patterns and pattern matching D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants