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

Provide warning for exhaustive match on sealed traits with an unapply method #8511

Closed
scabug opened this issue Apr 17, 2014 · 1 comment · Fixed by scala/scala#9474
Closed
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Apr 17, 2014

Given the following sealed trait, it is possible to see that at runtime there is the possibility of a MatchError being thrown -

sealed trait Expr
final case class Foo(other: Option[String]) extends Expr
final case class Bar(someConstant: String) extends Expr
final case class Baz() extends Expr
final case class EatsExhaustiveWarning(other: Reference) extends Expr
 
sealed trait Reference {
  val value: String
}
 
object Reference {
  def unapply(reference: Reference): Option[(String)] = {
    Some(reference.value)
  }
}
 
object EntryPoint {
  def main(args: Array[String]) {
    println("Successfully ran")
  }
 
  private def logic(head: Expr): String = head match {
    case Foo(_) =>
      ???
    // Commenting this line only causes the exhaustive search warning to be emitted
    case EatsExhaustiveWarning(Reference(text)) =>
      ???
  }
}

From the scala docs

If the selector of a pattern match is an instance of a sealed class (s5.2), the compilation of pattern matching can emit warnings which diagnose that a given set of patterns is not exhaustive, i.e. that there is a possibility of a MatchError being raised at run-time.

It seems that when the unapply method is provided for the Reference trait, no compiler warnings are emitted.

@scabug
Copy link
Author

scabug commented Apr 17, 2014

Imported From: https://issues.scala-lang.org/browse/SI-8511?orig=1
Reporter: Alan Foster (alanfoster)
Affected Versions: 2.11.0-RC4

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

Successfully merging a pull request may close this issue.

3 participants