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 matching on Booleans doesn't check exhaustiveness #3111

Closed
scabug opened this issue Feb 26, 2010 · 8 comments
Closed

Pattern matching on Booleans doesn't check exhaustiveness #3111

scabug opened this issue Feb 26, 2010 · 8 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Feb 26, 2010

Consider the following:

val bool: Boolean = false
bool match {
  case true => "true!"
}

Expectation: I'd hope for a non-exhaustive match warning.
Actual result: run-time MatchError

Likewise for:

val bool: Boolean = true
bool match {
  case true => "true!"
  case false => "false!"
  case _ => "cats and dogs living together... mass hysteria!"
}

Expectation: I would expect to receive an unreachable code error.
Actual: Silent, happy, deluded compiler.

Tested on 2.8.0 nightly as of Feb 26, 2010.

@scabug
Copy link
Author

scabug commented Feb 26, 2010

Imported From: https://issues.scala-lang.org/browse/SI-3111?orig=1
Reporter: Aaron Harnly (aaronharnly)

@scabug
Copy link
Author

scabug commented Feb 26, 2010

@paulp said:
I've tried to do this before and it was harder than I thought. Reclassifying as enhancement.

@scabug
Copy link
Author

scabug commented Mar 2, 2010

@lrytz said:
scala_devteam is probably the wrong owner for this ticket.

@scabug
Copy link
Author

scabug commented Mar 5, 2010

@axel22 said:
Right.

@scabug
Copy link
Author

scabug commented Mar 9, 2010

@axel22 said:
Paul, could you take a look at this?

@scabug
Copy link
Author

scabug commented Mar 10, 2010

@paulp said:
Does my first comment not imply previous looking? I'll take ownership of it, but I won't be doing it anytime soon. I agree it is desirable.

@scabug
Copy link
Author

scabug commented May 25, 2012

@retronym said:
The new pattern matcher should deliver this. It already checks exhaustiveness for booleans, reachability isn't quite there yet.

Welcome to Scala version 2.10.0-20120524-094715-123050cf07 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_31).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val b: Boolean = false
b: Boolean = false

scala> b match { case false => true }
<console>:9: warning: match may not be exhaustive.
It would fail on the following input: true
              b match { case false => true }
              ^
res0: Boolean = true

scala> b match { case true => true; case false => false; case _ => true }
res1: Boolean = false

@scabug
Copy link
Author

scabug commented Jun 3, 2012

@retronym said:
Fixed, and adequately tested here:

scala/scala@379384c#L6R5

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

2 participants