Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patternmatch Unreachable Warning #20123

Closed
CyrilFMoser opened this issue Apr 8, 2024 · 2 comments · Fixed by #21000
Closed

Patternmatch Unreachable Warning #20123

CyrilFMoser opened this issue Apr 8, 2024 · 2 comments · Fixed by #21000

Comments

@CyrilFMoser
Copy link

CyrilFMoser commented Apr 8, 2024

Compiler version

3.4.1

Minimized code

sealed trait T_A[A, B]
sealed trait T_B[C]
case class CC_D[A, C]() extends T_A[A, C]
case class CC_E() extends T_B
case class CC_G[A, C](c: C) extends T_A[A, C]
val v_a: T_A[Boolean, T_B[Boolean]] = CC_G(null)
val v_b = v_a match {
  case CC_D()  => 0
  case CC_G(_) => 1
}

Output

The value of v_b is 1

Expectation

The compiler gives an unreachable warning for case CC_G(_) , so it should not be able to reach that case.

@CyrilFMoser CyrilFMoser added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 8, 2024
@Gedochao Gedochao added area:pattern-matching and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 4, 2024
@Gedochao
Copy link
Contributor

Gedochao commented Jun 4, 2024

The unreachable case indeed is reached when this similar repro is run:

sealed trait T_A[A, B]
sealed trait T_B[C]
case class CC_D[A, C]() extends T_A[A, C]
case class CC_E() extends T_B
case class CC_G[A, C](c: C) extends T_A[A, C]
@main def main = {
  val v_a: T_A[Boolean, T_B[Boolean]] = CC_G(null)
  val v_b = v_a match {
    case CC_D()  => 0
    case CC_G(_) => 1
  }
  println(s"the unreachable case v_b: $v_b")
}
scala-cli run repro.scala               
# Compiling project (Scala 3.4.2, JVM (17))
# [warn] ./repro.scala:10:10
# [warn] Unreachable case
# [warn]     case CC_G(_) => 1
# [warn]          ^^^^^^^
# Compiled project (Scala 3.4.2, JVM (17))
# the unreachable case v_b: 1

@Gedochao
Copy link
Contributor

Gedochao commented Jun 4, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants