scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.83k stars 1.05k forks source link

Pattern Match Exhaustiveness Check Misses a Case #20129

Closed CyrilFMoser closed 2 months ago

CyrilFMoser commented 6 months ago

Compiler version

3.4.1

Minimized code

sealed trait T_A[A]
case class CC_B[A](a: T_A[A], c: T_A[A]) extends T_A[Char]
case class CC_C[A]() extends T_A[A]
case class CC_G() extends T_A[Char]

val v_a: T_A[Char] = CC_B(CC_G(), CC_C())
val v_b: Int = v_a match {
  case CC_C()                   => 0
  case CC_G()                   => 1
  case CC_B(CC_B(_, _), CC_C()) => 2
  case CC_B(CC_C(), CC_C())     => 3
  case CC_B(_, CC_G())          => 4
  case CC_B(_, CC_B(_, _))      => 5
}

Output

Crashes

Expectation

Should give a warning at compile time that the pattern match is not exhaustive.

Gedochao commented 4 months ago

Crashes

I wasn't able to reproduce the crash. Assuming this to just be a reporting issue.

Gedochao commented 4 months ago

Ah, right. There's no crash, but the code fails by throwing an exception at runtime when run as a script.

// repro-script.sc
sealed trait T_A[A]
case class CC_B[A](a: T_A[A], c: T_A[A]) extends T_A[Char]
case class CC_C[A]() extends T_A[A]
case class CC_G() extends T_A[Char]

val v_a: T_A[Char] = CC_B(CC_G(), CC_C())
val v_b: Int = v_a match {
  case CC_C()                   => 0
  case CC_G()                   => 1
  case CC_B(CC_B(_, _), CC_C()) => 2
  case CC_B(CC_C(), CC_C())     => 3
  case CC_B(_, CC_G())          => 4
  case CC_B(_, CC_B(_, _))      => 5
}
scala-cli run repro-script.sc
Compiling project (Scala 3.4.2, JVM (17))
Compiled project (Scala 3.4.2, JVM (17))
Exception in thread "main" scala.MatchError: CC_B(CC_G(),CC_C()) (of class repro$minusscript$_$CC_B)
        at repro$minusscript$_.<init>(repro-script.sc:13)
        at repro$minusscript_sc$.script$lzyINIT1(repro-script.sc:28)
        at repro$minusscript_sc$.script(repro-script.sc:28)
        at repro$minusscript_sc$.main(repro-script.sc:32)
        at repro$minusscript_sc.main(repro-script.sc)
Gedochao commented 4 months ago

Very similar issues which were reported together with this, potentially duplicates: