Skip to content

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

@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.

Activity

added
area:reportingError reporting including formatting, implicit suggestions, etc
better-errorsIssues concerned with improving confusing/unhelpful diagnostic messages
and removed
stat:needs triageEvery issue needs to have an "area" and "itype" label
on Jun 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @theosotr@Gedochao

        Issue actions

          inconsistency in the reports of pattern-match exhaustiveness checks · Issue #23407 · scala/scala3