Skip to content
New issue

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

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

False match exhaustiveness warning with case class containing enum #7206

Closed
scabug opened this issue Mar 2, 2013 · 4 comments
Closed

False match exhaustiveness warning with case class containing enum #7206

scabug opened this issue Mar 2, 2013 · 4 comments
Assignees

Comments

@scabug
Copy link

scabug commented Mar 2, 2013

object E extends Enumeration {
  val V = Value
}

sealed case class C(e: E.Value)

class Test {
  def foo(c: C) {
    c match {
      case C(E.V) => {}
    }
  }

  def foo2(e: E.Value) {
    e match {
      case E.V => {}
    }
  }
}

This code produces the following warning in Test.foo:

Test.scala:9: warning: match may not be exhaustive.
It would fail on the following input: C((x: E.Value forSome x not in V))
    c match {
    ^

Test.foo2 produces no warning (as expected), even though it's essentially the same match.

@scabug
Copy link
Author

scabug commented Mar 2, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7206?orig=1
Reporter: Роман Донченко (dpb)
Affected Versions: 2.10.0, 2.10.1-RC2, 2.10.1

@scabug
Copy link
Author

scabug commented Mar 3, 2013

@adriaanm said:
Scala's Enumeration is implemented purely as a library that the compiler has no knowledge of. I'm reluctant to special-case it in the pattern matcher.

@scabug
Copy link
Author

scabug commented Apr 5, 2014

Erik Allik (eallik) said (edited on Apr 5, 2014 3:00:24 AM UTC):
Would it be feasible to make Enumeration use case classes internally so as to benefit from the exhaustiveness checks performed on those?

@scabug
Copy link
Author

scabug commented Jan 29, 2016

@retronym said:
Closing as not-a-bug. There have been some efforts to make an alternative version of scala.Enumeration that would be amenable to pattern matching analysis, but we can't really do better with the legacy one.

I recommend using sealed heirarchies of case classes rather than scala.Enumeration. Plain old Java Enums are also a good choice (the pattern matcher can analyse these.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants