Skip to content

inconsistency in the reports of pattern-match exhaustiveness checks #23407

Open
@theosotr

Description

@theosotr

Compiler version

3.7.1

Minimized code

sealed trait A[T]
case class CC_A[T](a: B[T]) extends A[T]
case class CC_B[T]() extends A[T]
sealed trait B[T]
case class CC_C() extends B[Int]

@main def test() = {
  val x: A[CC_C] = CC_A(null)
  val res: Int = x match{
    case CC_A(_) => 0
    case CC_B() => 1
  }
  print(res) // prints 0
}

Output

scala test.scala
Compiling project (Scala 3.7.1, JVM (22))
[warn] ./test.scala:10:10
[warn] Unreachable case
[warn]     case CC_A(_) => 0
[warn]          ^^^^^^^
Compiled project (Scala 3.7.1, JVM (22))
0

The above behavior suggests that CC_A(_) is unreachable and should be removed. However, if we remove it, we get the following warning from the compiler:

scala test.scala
Compiling project (Scala 3.7.1, JVM (22))
[warn] ./test.scala:9:18
[warn] match may not be exhaustive.
[warn] 
[warn] It would fail on pattern case: CC_A(_)
[warn]   val res: Int = x match{
[warn]                  ^
Compiled project (Scala 3.7.1, JVM (22))
Exception in thread "main" scala.MatchError: CC_A(null) (of class CC_A)
        at test$package$.test(test.scala:10)
        at test.main(test.scala:7)

which contradicts with the initial report that flags CC_A(_) as unreachable.

Expectation

CC_A(_) is actually reachable. Therefore, the compiler should not flag it as unreachable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:pattern-matchingarea:reportingError reporting including formatting, implicit suggestions, etcbetter-errorsIssues concerned with improving confusing/unhelpful diagnostic messagesitype:bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions