Skip to content

No warning is triggered for non-exhaustive pattern matching on case class field #12337

Closed
@kevin-lee

Description

@kevin-lee

Compiler version

3.0.0-RC3 from print scalaVersion on sbt console.

Minimized code

sealed trait Status
object Status {
  case object Active   extends Status
  case object Inactive extends Status
}

final case class Foo(status: Status)
def bar(user: Foo): Unit = user match {
  case Foo(Status.Active) =>
    println("active")
    // no compile-time warning for missing Status.Inactive case
}

scalacOptions has

List(
  "-unchecked",
  "-deprecation",
  "-feature",
  "-Xfatal-warnings"
)

NOTE:

  • Replacing the Status above with the enum like below doesn't make any difference.

    enum Status {
      case Active
      case Inactive
    }
  • final case class (i.e. final case class Foo(status: Status)) makes no difference.

Output

It compiles without any warning or error even with -Xfatal-warnings, and it results in the following runtime error.

[error] (run-main-1) scala.MatchError: Foo(Inactive) (of class kevinlee.Example1$Foo)
[error] scala.MatchError: Foo(Inactive) (of class kevinlee.Example1$Foo)
...

Expectation

The compile-time warning like

match may not be exhaustive.
It would fail on the following input: Foo(Inactive)
    def bar(user: Foo): Unit = user match {

(or the compile-time error with "-Xfatal-warnings" compiler option)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions