scalascalac

Scalac bug or misunderstanding on my part?


I set the -Xfatal-warnings compiler flag in my SBT build and sure enough this code doesn't compile:

package example

sealed trait Errors

object Errors {
  case class BadFirstName(name: String) extends Errors
  case class BadLastName(name: String) extends Errors
}

...

def handleError(errors: Errors): String = errors match {
  case BadFirstName(name) => s"$name is bad"
}

I see the error [error] It would fail on the following input: BadLastName(_)

But if i add a guard to the BadFirstName match it compiles without error or warning:

def handleError(errors: Errors): String = errors match {
  case BadFirstName(name) if name.startsWith("t") => s"$name is bad"
}
...
[info] Done compiling.

And then blows up at runtime with the dreaded [error] scala.MatchError: BadLastName(...)

This looks like a bug to me but I wanted to check if I've missed something?


Solution

  • Yes, this is a bug, namely SI-5365.