Knocking down an invalid template error

When using the match statement, I came across a very confusing error message from the Rust compiler.

 enum Name { Known, } fn unreachable_pattern(n: Name) -> usize { use Name::*; match n { Unknown => 1, Known => 1, } } 

The Rust compiler complains about an unreachable template:

 error[E0001]: unreachable pattern --> src/main.rs:10:9 | 10 | Known => 1, | ^^^^^ this is an unreachable pattern | note: this pattern matches any value --> src/main.rs:9:9 | 9 | Unknown => 1, | ^^^^^^^ 

For humans, the real mistake is that Unknown missing from the Name definition, which is easier to detect if you don't have 40 other options.

+7
rust
source share
1 answer

This is currently a known issue; this is not a mistake, but a quality problem.

The problem boils down to irrefutable coincidences, that is:

 match variable { 1 => 2, i => 2 * i } 

Here i is an irrefutable match, that is, it always matches, regardless of the value of variable .


Well, we have the same problem with this strange report: because Unknown unknown, it becomes the name of the variable in an irrefutable match! Of course, this is unintentional, but it makes sense to the compiler.

The good news is that the compiler starts complaining right after the next match, so you can easily find out which match is irrefutable.

Expected

There are many variations of this error (see duplicates), it can also be caused by improper enum import and, therefore, not having its own options in scope.

+5
source share

All Articles