Skip to content

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

Description

@PatchMixolydic

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-exhaustiveness-checkingRelating to exhaustiveness / usefulness checking of patternsA-patternsRelating to patterns and pattern matchingD-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.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