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

"unreachable code" warning on reachable code #9505

Open
scabug opened this issue Oct 6, 2015 · 5 comments
Open

"unreachable code" warning on reachable code #9505

scabug opened this issue Oct 6, 2015 · 5 comments
Labels
errors and warnings fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) help wanted patmat
Milestone

Comments

@scabug
Copy link

scabug commented Oct 6, 2015

Consider the following code which I've put in a file "unreachable.scala":

trait T1 {}

sealed abstract class Base

case class A() extends Base
case class B() extends Base

object O1 {
  def fn[V](x: Base with T1) = {
    x match {
      case a: A => "A"
      case b: B => "B"
    }
  }
}

object Main {
  def main(args: Array[String]) = {
    import O1.fn

    println(s"fn(fn(new A() with T1)) => ${fn(new A() with T1)}")
    println(s"fn(fn(new B() with T1)) => ${fn(new B() with T1)}")
  }
}

I get a "warning: unreachable code" for line 14 (the "case b: B" statement above) when I compile this.

However when I run p1.Main that line is clearly reachable, because "B" is output.

[jluxenberg-workstation ~]$ ~/Downloads/scala-2.11.7/bin/scalac unreachable.scala
unreachable.scala:14: warning: unreachable code
      case b: B => "B"
                   ^
one warning found
[jluxenberg-workstation ~]$ ~/Downloads/scala-2.11.7/bin/scala -classpath . p1.Main
fn(fn(new A() with T1)) => A
fn(fn(new B() with T1)) => B

Might be related to #7369

@scabug
Copy link
Author

scabug commented Oct 6, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9505?orig=1
Reporter: Jared Luxenberg (jluxenberg)
Affected Versions: 2.11.7
See #7369
Attachments:

@scabug
Copy link
Author

scabug commented Apr 15, 2016

@JustinPihony said:
Just as a note that the below code seems to be similar and have the same problem, however it can be remedied by marking the abstract classes as sealed:

sealed trait Result

abstract class FullResult(values: Int) extends Result

abstract class PartialResult(values: Int) extends Result

case class GetResult(events: Int) extends FullResult(events) with Result

case class PGetResult(events: Int, isLast: Boolean) extends PartialResult(events) with Result

object Test {
  def handle(r: Result) = r match {
    case res: GetResult => res.events
    case res: PGetResult => res.events
    case res => 42
  }
}

@pabloazul pabloazul added the fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) label Nov 21, 2019
@pabloazul pabloazul added this to the Backlog milestone Nov 21, 2019
@pabloazul
Copy link

Still occurs in 2.13.0

@SethTisue
Copy link
Member

This seems pretty interesting and surprising, somebody want to look into it?

@Jasper-M
Copy link

There's also #11457 which looks like it could be a duplicate, but that one works correctly in 2.12 and 2.11. I think I've looked into that one for a bit, but didn't find anything. All I remember is that the code in this area is very scary.
There's a bunch of other seemingly related tickets if you search for "unreachable".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
errors and warnings fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) help wanted patmat
Projects
None yet
Development

No branches or pull requests

4 participants