Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upExhaustive match of number treated as non-exhaustive #12483
Comments
ghost
referenced this issue
Aug 2, 2014
Closed
Invalid non-exhaustive pattern error on scalar values #16199
This comment has been minimized.
This comment has been minimized.
jkleint
commented
Oct 12, 2014
|
I assume this is the same issue: fn main() {
println!("{}", match 1i {
n if n > 0 => "greater",
n if n < 0 => "less",
n if n == 0 => "equal",
});
}
// error: non-exhaustive patterns: `_` not covered [E0004] |
This comment has been minimized.
This comment has been minimized.
thecoshman
commented
Nov 10, 2014
|
Whilst I agree that your example is exhaustive, it's not a good example.
This is effectively the same, except it compiles. Keep in mind
you would just drop the 'if (n == 0)' from that last clause. |
This comment has been minimized.
This comment has been minimized.
|
Doing the test for n==0 is less efficient, but IMHO more readable. I would probably use your pattern but leave n==0 commented out. |
This comment has been minimized.
This comment has been minimized.
|
The compiler is never going to be able to fully determine these kinds of things, as they would involve evaluating certain things at compile time. For example:
This arm would be equivalent, but would report as non-exhaustive, too. As such, I'm giving this a close. I don't think we can improve this. |
steveklabnik
closed this
Jan 23, 2015
This comment has been minimized.
This comment has been minimized.
|
@steveklabnik I disagree with your closing of this issue; could you please open it again? The issue is entirely genuine, is blocking correct code (though admittedly there is an easy workaround: change the last pattern to an Moreover, your fabricated example is invalid, because you can’t do arithmetic in patterns. @jkleint Your example is a completely different issue and is not solvable. |
This comment has been minimized.
This comment has been minimized.
|
@chris-morgan I'm not understanding how this issue is solvable. To grok the range, we need to evaluate code at compile time, it can't work. |
This comment has been minimized.
This comment has been minimized.
|
@steveklabnik it’s a pattern, so the numbers are literals or constants. No additional evaluation required. The compiler needs to know them in order to compile the code anyway. |
steveklabnik
reopened this
Jul 22, 2015
This comment has been minimized.
This comment has been minimized.
jkleint
commented
Jul 25, 2015
|
@chris-morgan aren't |
steveklabnik
added
the
C-enhancement
label
Sep 3, 2015
arielb1
referenced this issue
Sep 18, 2015
Closed
match statements that cover all possible values for type not seen as exhaustive #28477
This comment has been minimized.
This comment has been minimized.
|
I've written a POC script that compares a set of ranges with 1 or more exhaustive ranges for the respective type and outputs any ranges that are not covered: https://gist.github.com/aarzee/4395cb34eef31ea4e5c4 I'm interested in implementing this logic in the compiler but looking at check_match.rs I have absolutely no idea how I would do this. Also, this code has opportunities to take advantage of more types in the future, as currently floats aren't supported by it, and specialization and native inclusive ranges have not yet been added to the language. |
This comment has been minimized.
This comment has been minimized.
|
Implementing this would cause the following to fail to compile since the last arm is unreachable: match 0u8 {
0...255 => {}
_ => {}
}Should a warning be emitted first? Is an RFC needed? Should this be ignored without warning, like it is now? Make it a deny-by-default Lint to prevent breaking dependencies? So many questions! |
This comment has been minimized.
This comment has been minimized.
|
To be sure, implementing this will cause certain code that compiles now to cease to compile, but it was bad code to begin with and a bug in the compiler is being fixed. And it is very unlikely to actually affect anyone in practice, because I doubt that anyone has actually written such code. Thus it fits easily inside the stability-retention guidelines. I do not believe any RFC is required, nor any ability to disable the new behaviour. |
This comment has been minimized.
This comment has been minimized.
Not this exact code, sure. But maybe an opcode dispatch switch that handles all possible byte values? Or one that handles all undefined opcodes with an arm like |
This comment has been minimized.
This comment has been minimized.
|
Hmm, good point. I myself have just changed the last branch into |
This comment has been minimized.
This comment has been minimized.
|
I wrote a bit of code that at least seems to work semi-correctly for a few test cases: https://play.rust-lang.org/?gist=f3d4e2e9559d84319432&version=stable It's Feel free to |
This comment has been minimized.
This comment has been minimized.
|
For reference, @quantheory wrote an RFC about this - rust-lang/rfcs#880, it was postponed. He wrote some implementation too https://github.com/quantheory/int_range_check. |
jonas-schievink
referenced this issue
Oct 28, 2015
Closed
Pattern mathing and fixed size decimals #29424
This comment has been minimized.
This comment has been minimized.
|
FWIW, GCC can do this: https://goo.gl/4f1i6H (no warning, but a warning appears if you change 255 to something else). Clang can't tell the difference. |
This comment has been minimized.
This comment has been minimized.
|
I'm going to close this in favor of an RFC, as this would be a pretty big change. @quantheory's might be a good starting point! |
ebiggers commentedFeb 23, 2014
The following function does not compile because rustc determines that the patterns in the match statement are non-exhaustive. However, the pattern matches every possible value of the integer type, and the compiler should be able to detect this.