bug icon indicating copy to clipboard operation
bug copied to clipboard

Exhaustive pattern matching is found non-exhaustive for anonymous class

Open scabug opened this issue 9 years ago • 2 comments

The following two snippets must live in different files.

First a sealed Base hierarchy.

sealed abstract class Base

sealed trait A extends Base

object A {

  case object Root extends Base

  def apply(param: String): A = {
      new A {}
  }
}

Then trying to pattern match:

object ExhaustiveMatchWarning {

  def test() = {
    val b: Base = A("blabla")
    b match {
      case A.Root => println("Root")
      case path: A => println("Not root")
    }
  }
}

produces a warning

[warn] [...]/ExhaustiveMatchWarning.scala:5: match may not be exhaustive.
[warn] It would fail on the following input: (_ : this.<local child>)
[warn]     b match {
[warn]     ^
[warn] one warning found

scabug avatar Feb 26 '16 08:02 scabug

Imported From: https://issues.scala-lang.org/browse/SI-9677?orig=1 Reporter: Bruno Bieth (mustaghattack) Affected Versions: 2.11.7, 2.11.8

scabug avatar Feb 26 '16 08:02 scabug

behavior in 2.13.16 is unchanged

in Scala 3.6.3,

% /usr/local/scala/scala3-3.6.3/bin/scalac -classpath . Test.scala
-- [E029] Pattern Match Exhaustivity Warning: Test.scala:5:4 -------------------
5 |    b match {
  |    ^
  |    match may not be exhaustive.
  |
  |    It would fail on pattern case: _: Base (anonymous)
  |
  | longer explanation available when compiling with `-explain`
1 warning found

SethTisue avatar Feb 08 '25 22:02 SethTisue