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

Pattern-matcher spuriously spouts "unreachable code" #9411

Closed
scabug opened this issue Jul 22, 2015 · 3 comments
Closed

Pattern-matcher spuriously spouts "unreachable code" #9411

scabug opened this issue Jul 22, 2015 · 3 comments
Assignees
Labels
Milestone

Comments

@scabug
Copy link

scabug commented Jul 22, 2015

In following snippet:

object OhNoes {

  sealed trait F
  sealed abstract class FA extends F
  sealed abstract class FB extends F

  case object FA1 extends FA
  case object FB1 extends FB
  case object FB2 extends FB

  sealed trait G
  case object G1 extends G
  case object G2 extends G

  sealed trait H
  case class H1(a: FB, b: G) extends H
  case class H2(a: F)        extends H

  val demo: H => Unit = {
    case H1(FB1, G1) =>
    case H1(FB2, G2) =>
    case H2(_: FB) =>
    case H2(_: FA) =>
    case H1(FB1, G2) =>
    case H1(FB2, G1) =>
  }
}

the naughty little compiler lies and reports:

[warn] /tmp/ah/demo.scala:23: unreachable code
[warn]     case H2(_: FA) =>

Very interestingly, H1 and H2 both having a parameter with the same name seems to be pertinent. If you rename H2's "a" to "b" you get a different (incorrect) warning:

[warn] It would fail on the following input: H2(_)
[warn]   val demo: H => Unit = {

Renaming H2's "a" to "x" the warnings disappear.

Finally, rearranging the order of cases produces different, incorrect warnings, likely related to #9399 and #9398. Example:

  val demo2: H => Unit = {
    case H2(_: FA) =>
    case H2(_: FB) =>
    case H1(FB1, G1) =>
    case H1(FB2, G1) =>
    case H1(FB1, G2) =>
    case H1(FB2, G2) =>
  }

produces

[warn] It would fail on the following inputs: H1(_, G1), H1(_, G2)
[warn]   val demo2: H => Unit = {
@scabug
Copy link
Author

scabug commented Jul 22, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9411?orig=1
Reporter: David Barri (japgolly)
Affected Versions: 2.11.7
See #9398, #9399

@scabug
Copy link
Author

scabug commented Jan 25, 2016

@scabug
Copy link
Author

scabug commented Jan 27, 2016

@retronym said:
scala/scala#4919

@scabug scabug closed this as completed Jan 29, 2016
@scabug scabug added the patmat label Apr 7, 2017
@scabug scabug added this to the 2.12.0-M4 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants