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 #7369

Closed
scabug opened this issue Apr 14, 2013 · 4 comments
Closed

"unreachable code" warning on reachable code #7369

scabug opened this issue Apr 14, 2013 · 4 comments
Assignees
Labels
Milestone

Comments

@scabug
Copy link

@scabug scabug commented Apr 14, 2013

Another "unreachable code" issue; I've reproduced it in 2.10.1 and a 2.11 nightly build.

Welcome to Scala version 2.11.0-20130410-113912-f6323d866f (OpenJDK 64-Bit Server VM, Java 1.7.0_15).
Type in expressions to have them evaluated.
Type :help for more information.

scala>     val X = true
X: Boolean = true

scala>     val O = false
O: Boolean = false

scala>     def classify(neighbourhood: (Boolean, Boolean, Boolean)): String = {
     |         neighbourhood match {
     |         case (X, X, X) => "middle"
     |         case (X, X, O) => "right"
     |         case (O, X, X) => "left"
     |         case _ => throw new IllegalArgumentException("Invalid")
     |       }
     |     }
<console>:12: warning: unreachable code
               case (X, X, O) => "right"
                                 ^
classify: (neighbourhood: (Boolean, Boolean, Boolean))String

scala> classify(true, true, false)
res0: String = right

scala> classify(false, true, true)
res1: String = left
Welcome to Scala version 2.10.1 (OpenJDK 64-Bit Server VM, Java 1.7.0_15).
Type in expressions to have them evaluated.
Type :help for more information.

scala>     val X = true
X: Boolean = true

scala>     val O = false
O: Boolean = false

scala>     def classify(neighbourhood: (Boolean, Boolean, Boolean)): String = {
     |         neighbourhood match {
     |         case (X, X, X) => "middle"
     |         case (X, X, O) => "right"
     |         case (O, X, X) => "left"
     |         case _ => throw new IllegalArgumentException("Invalid")
     |       }
     |     }
<console>:12: warning: unreachable code
               case (X, X, O) => "right"
                                 ^
classify: (neighbourhood: (Boolean, Boolean, Boolean))String

scala> classify(true, true, false)
res0: String = right
@scabug
Copy link
Author

@scabug scabug commented Apr 14, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7369?orig=1
Reporter: Joaquin Caraballo (jcaraballo)
Affected Versions: 2.10.1
See #6771

@scabug
Copy link
Author

@scabug scabug commented Apr 15, 2013

@retronym said:
Cut down a little:

val X, Y = true
(null: Tuple1[Boolean]) match {
  case Tuple1(X) =>
  case Tuple1(Y) => // unreachable
  case _      =>
}

val X, Y = ""
(null: Tuple1[String]) match {
  case Tuple1(X) =>
  case Tuple1(Y) => // no warning
  case _      =>
}

@scabug
Copy link
Author

@scabug scabug commented Apr 15, 2013

@retronym said:
Also interesting for comparison:

sealed abstract class B;
case object True extends B;
case object False extends B;

object Test2 {

  val X: B = True
  val Y: B = False

  (null: Tuple1[B]) match {
    case Tuple1(X) =>
    case Tuple1(Y) => // no warning
    case _      =>
  }
}
reachable:
 -V1=null#1  /\
   V2=Y#3
under prefix:
 V2=False.type#4  \/ V2=True.type#5 \/ V2=null#6  /\
     -V2=Y#3      \/    -V2=X#2     \/     /\
     -V2=Y#3      \/   -V2=null#6   \/     /\
 -V2=False.type#4 \/   -V2=null#6   \/     /\
     -V2=X#2      \/   -V2=null#6   \/     /\
 -V2=True.type#5  \/   -V2=null#6   \/     /\
    V1=null#1     \/    -V2=X#2     \/     via Map(V2=Y#3 -> true, V2=False.type#4 -> true, V2=X#2 -> false, V1=null#1 -> false, V2=null#6 -> false)

Compare with the unsatisfiable model for:

object Test {
  val X, Y = true
  (null: Tuple1[Boolean]) match {
    case Tuple1(X) =>
    case Tuple1(Y) => // unreachable
    case _      =>
  }
}
unreachable:
 -V1=null#1  /\
   V2=Y#3
under prefix:
  V2=true#4  \/ V2=false#5   /\
   -V2=X#2   \/ -V2=false#5  /\
   -V2=X#2   \/   -V2=Y#3    /\
   -V2=X#2   \/ -V2=true#4   /\
 -V2=false#5 \/   -V2=Y#3    /\
 -V2=false#5 \/ -V2=true#4   /\
   -V2=Y#3   \/ -V2=true#4   /\
  V1=null#1  \/   -V2=X#2

@scabug
Copy link
Author

@scabug scabug commented Apr 24, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants