Navigation Menu

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

Incorrect exhaustiveness warning with tuple with Boolean values #7466

Closed
scabug opened this issue May 9, 2013 · 9 comments
Closed

Incorrect exhaustiveness warning with tuple with Boolean values #7466

scabug opened this issue May 9, 2013 · 9 comments

Comments

@scabug
Copy link

scabug commented May 9, 2013

The following code gives an incorrect dead code warning:

object Test extends App {
  val Yes1 = true
  val Yes2 = true
  val No1 = false
  val No2 = false

  def test(b1: Boolean, b2: Boolean) {
    (b1, b2) match {
      case (No1, No2) => println("1")
      case (No1, Yes2) => println("2")
      case (Yes1, No2) => println("3")
      case (Yes1, Yes2) => println("4")
    }   
  }

  test(No1, Yes2)
}

When compiling this, I get the wraning:

[error] test.scala:14: unreachable code
[error]       case (No1, Yes2) => println("2")

However, when run, it does print "2", so it is clearly not dead code.

It looks like it has something to do the Booleans. If I change all the Booleans to Int's with values of 0/1, the dead code error goes away.

This same code does not produce a warning in scala 2.9.2.

@scabug
Copy link
Author

scabug commented May 9, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7466?orig=1
Reporter: Steve Lawrence (slawrence)
Affected Versions: 2.10.1
See #7369

@scabug
Copy link
Author

scabug commented May 9, 2013

@paulp said (edited on May 9, 2013 2:16:53 PM UTC):
The warning is correct, because the types of Yes1/Yes2/No1/No2 are "Boolean", they are not true constants - so you haven't actually covered any of the boolean space. It goes away with Ints because the pattern matcher doesn't aspire to exhaustively checking four billion values, but it can check two.

Declare the constants so they will be seen as such, and the warning goes away:

final val Yes1 = true
final val Yes2 = true
final val No1 = false
final val No2 = false

@scabug
Copy link
Author

scabug commented May 9, 2013

@paulp said:
Oh, sorry "unreachable" is clearly not true, I was thinking it said "not exhaustive".

@scabug
Copy link
Author

scabug commented May 9, 2013

@paulp said:
However the remedy remains.

@scabug
Copy link
Author

scabug commented May 9, 2013

@paulp said:
Ah, I was thinking it says "not exhaustive" because that's all it says in master. So the unreachable warning is already fixed.

<console>:14: warning: match may not be exhaustive.
It would fail on the following inputs: (??, false), (??, true), (false, ??), (false, false), (false, true), (true, ??), (true, false), (true, true)
           (b1, b2) match {
           ^
defined object Test

@scabug
Copy link
Author

scabug commented May 9, 2013

@retronym said (edited on May 9, 2013 2:28:23 PM UTC):
Similar to #7369. After its fix, (scala/scala@62713964), the reachability warning is gone, but the spurious (or at least, over-reaching) exhaustiveness warning remains.

@scabug
Copy link
Author

scabug commented May 9, 2013

@paulp said:
Adriaan: when stable ids and not constants are involved, the warning should probably say "it may fail", not "it would fail".

@scabug
Copy link
Author

scabug commented May 9, 2013

@paulp said:
On the subject of the warning, I think a warning should remain, but it should be a lot more useful, such as suggesting "final" for any stable ids which may be constants, and perhaps not enumerating the cases but only saying it can't verify exhaustiveness without constants so either give it constants or write a default case (or mark it unchecked.)

@scala scala deleted a comment from scabug Mar 2, 2018
@SethTisue SethTisue added this to the Backlog milestone Mar 2, 2018
@dwijnand dwijnand changed the title Incorrect unreachable code warning with tuple with Boolean values Incorrect exhaustiveness warning with tuple with Boolean values Feb 4, 2021
@dwijnand dwijnand self-assigned this Feb 4, 2021
@dwijnand
Copy link
Member

Spent a good while studying this one and I can't think of a good way to fix it. It's not false unreachable warnings anymore, it's just saying that it can't prove exhaustiveness, and gives you the 8 ways it thinks 2 booleans can fail, which is a bit dumb.

@SethTisue SethTisue removed this from the Backlog milestone Feb 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants