Skip to content

"non-exhaustive patterns: _ not covered" message could be more helpful for bitwise matching #56635

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No 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? No Sign in to your account

Closed
indygreg opened this issue Dec 8, 2018 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@indygreg
Copy link
Contributor

indygreg commented Dec 8, 2018

I have code resembling the following:

pub fn parse_initial_byte(initial: u8) -> Result<InitialByte, &'static str> {
    match initial {
        0b000_00000...0b000_10111 => Ok(InitialByte::UIntInline(initial)),
        0b000_11000 => Ok(InitialByte::UInt8),
        0b000_11001 => Ok(InitialByte::UInt16),
        0b000_11010 => Ok(InitialByte::UInt32),
        0b000_11011 => Ok(InitialByte::UInt64),
        0b000_11100...0b000_11111 => Err("reserved major type 0 value"),
        0b001_00000...0b001_10111 => Ok(InitialByte::NegIntInline(-1 - (initial - 0x20) as i8)),
        0b001_11000 => Ok(InitialByte::NegInt8),
        0b001_11001 => Ok(InitialByte::NegInt16),
        0b001_11010 => Ok(InitialByte::NegInt32),
        0b001_11011 => Ok(InitialByte::NegInt64),
        0b001_11100...0b001_11111 => Err("reserved major type 1 value"),
        ...
    }

When compiling with Rust 1.31.0, I get the following output:

error[E0004]: non-exhaustive patterns: `_` not covered
  --> src/cbor.rs:70:11
   |
70 |     match initial {
   |           ^^^^^^^ pattern `_` not covered

error: aborting due to previous error

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

My bitwise matching patterns are apparently non-exhaustive :/

The error message is useful in that it is telling me where the error is. But it isn't telling me exactly which value(s) aren't covered by the match. It would be extremely useful if the compiler could do that for me. As it stands, I'll likely have to add a match for _ and write a test that identifies which value(s) are hitting it. The amount of work is relatively small. But it would be ideal if the compiler gave me enough info to avoid this overhead.

@indygreg
Copy link
Contributor Author

indygreg commented Dec 8, 2018

Talking with @withoutboats IRL, my understanding was slightly wrong here. Apparently the compiler can't figure out things and needs the _ match.

This wasn't at all intuitive to me: I thought the compiler was that smart and I was missing something in the match!

@nikic
Copy link
Contributor

nikic commented Dec 8, 2018

Exhaustive integer patterns recently got stabilized (on nightly). This will both enable the compiler to analyze this type of code and output a precise missing range, if one is missing.

The relevant tracking issue is #50907.

@estebank estebank added the A-diagnostics Area: Messages for errors, warnings, and lints label Dec 10, 2018
@estebank
Copy link
Contributor

Fixed in nightly by the perviously mentioned PR:

error[E0004]: non-exhaustive patterns: `26u8..=247u8` and `249u8..=255u8` not covered
 --> src/lib.rs:2:11
  |
2 |     match initial {
  |           ^^^^^^^ patterns `26u8..=247u8` and `249u8..=255u8` not covered

@fintelia
Copy link
Contributor

fintelia commented Mar 1, 2019

Now that the feature is stable, shouldn't this be closed?

No Sign up for free to join this conversation on GitHub. Already have an account? No Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

No branches or pull requests

4 participants