scala / scala3

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

Pattern Match Exhaustiveness Check Misses a Case #20132

Open CyrilFMoser opened 3 months ago

CyrilFMoser commented 3 months ago

Compiler version

3.4.1

Minimized code

sealed trait T_B[B]

case class CC_A()
case class CC_C[B]() extends T_B[B]
case class CC_D[B](b: T_B[B]) extends T_B[CC_A]

val v_a: T_B[CC_A] = CC_D(CC_D(null))
val v_b: Int = v_a match {
  case CC_C()       => 0
  case CC_D(CC_C()) => 1
}

Output

~Crashes~ runtime error (MatchError)

Expectation

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

Gedochao commented 1 month ago

Crashes

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

Gedochao commented 1 month 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_B[B]

case class CC_A()
case class CC_C[B]() extends T_B[B]
case class CC_D[B](b: T_B[B]) extends T_B[CC_A]

val v_a: T_B[CC_A] = CC_D(CC_D(null))
val v_b: Int = v_a match {
  case CC_C()       => 0
  case CC_D(CC_C()) => 1
}
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_D(CC_D(null)) (of class repro$minusscript$_$CC_D)
        at repro$minusscript$_.<init>(repro-script.sc:10)
        at repro$minusscript_sc$.script$lzyINIT1(repro-script.sc:25)
        at repro$minusscript_sc$.script(repro-script.sc:25)
        at repro$minusscript_sc$.main(repro-script.sc:29)
        at repro$minusscript_sc.main(repro-script.sc)
Gedochao commented 1 month ago

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

SethTisue commented 2 weeks ago

(Some of the other issues actually require null, but for this one, Dale has a reproducer that doesn't have null in it.)