2

I have the following enum:

pub enum Game {
    Match(GameWorker),
    #[cfg(feature = "cups")]
    Cup(CupWorker),
}

So, this enum consists of one item if cups feature is disabled. The code below with match compiles okay but in place where I use if lets on matching this enum there is a error:

Working match:

fn clear(&mut self, silent: bool) {
    match *self {
        Game::Match(ref mut gm) => gm.clear(silent),
        #[cfg(feature = "cups")]
        Game::Cup(ref mut c) => c.clear(silent),
    }
}

if let which leads to a compile error:

let m: &mut Game = Game::Match(...);
if let Game::Match(ref mut gamematch) = *m {
    // ...
}

Error:

error[E0162]: irrefutable if-let pattern
   --> src/game.rs:436:32
    |
436 |                         if let Game::Match(ref mut gamematch) = *m {
    |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ irrefutable pattern

Minimal example

Is there a way to allow such if lets ? I like this construction but somewhy it is not allowed to use it, I don't understand why. As shown above, match construction works okay in the same case. In my personal opinion here should be a silenceable warning instead of error.

  • 2
    I agree with you that I'd rather it be a warning. You may want to start a discussion on the Rust internals forum (internals.rust-lang.org) or the reddit channel (reddit.com/r/rust) to gauge the mood about moving this to a warning. – Matthieu M. Mar 28 '17 at 11:32
1

if let expects a refutable pattern, similar to how if expects a bool. You can't write if () { something }, even though () is "valid" in some sense. If you had if () {} else { something_else } it would be statically known that the else cannot occur.

Arguably if true { something } is also statically known, but there's a difference: The condition is a bool, which has two values, so even if you statically know the value, the type still offers multiple variants.

With if let it's the same, but you can use user defined types instead of just bool. If your enum has multiple variants, you can't statically decide that the if let is always taken. If the enum has a single variant, you know for a fact that the if condition is always true, so even if you had an else branch, it would not make any sense at all to exist.

  • I think that there's a big difference between the two though. It is known that bool will never get any other value, however it is not known that an enum will not. – Matthieu M. Mar 28 '17 at 11:26
  • Though I understand your answer, personally I do not treat this as a error. Yes, this branch will always be executed, so what? Why not to let it go? And example of enum shows good that we can't guarantee that the if let will compile because someday the enum may be changed because of conditional compilation and it will break everything. I don't think this is a good error, even more - that this is a problem. This makes sense only in certain well-defined situations where conditional compilation is absent. – Victor Polevoy Mar 28 '17 at 11:28
  • "Why not to let it go?" Because always taking one branch over the other is quite possibly a mistake the programmer isn't aware of. cfg!(...) exists, though, which makes unconditional branching "useful" for if, but if let has no such use. "... someday the enum may be changed ..." Which is why Rust makes non-exhaustive matching a hard error, so that you're far less likely to forget to update matches after changing the enum. Unconditional if let being a hard error makes sense if you consider the full context: Rust errs on the side of disallowing probable mistakes. – DK. Mar 28 '17 at 12:03
  • @DK. that sounds good but only sounds good. So, I have disabled some features and now my code does not compile. I have to rewrite it by using matches instead of if let. And so will happen more and more times because I will not trust if let anymore. Also, however, this may lead to a problem you wrote, this also may not lead to such problems in other cases, it depends on what engineer intentions were. So this definitely should be a silenceable warning instead of a error. – Victor Polevoy Mar 28 '17 at 12:16
  • 2
    @VictorPolevoy I didn't say it shouldn't be; I was trying to provide context as to why it's designed the way it is. Degenerate enums caused by conditional compilation + if let is such a niche combination that it's possible very few people have ever had this problem. – DK. Mar 28 '17 at 12:18

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.