Skip to content

Missing exhaustivity warning when pattern has a guard #8708

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

Closed
smarter opened this issue Apr 11, 2020 · 1 comment
Closed

Missing exhaustivity warning when pattern has a guard #8708

smarter opened this issue Apr 11, 2020 · 1 comment

Comments

@smarter
Copy link
Member

smarter commented Apr 11, 2020

The following code compiles without warning, even though the pattern isn't exhaustive:

object Main {
  def foo(x: Option[Int]): Int = x match {
    case Some(n) if n % 2 == 0 => n
    case None => 0
  }
  
  def main(args: Array[String]): Unit = println(foo(Some(1)))
}

It looks like we special cases guards for redundancy checks but not for exhaustivity checks (https://github.com/lampepfl/dotty/blob/85ab20fd874994634b909f0883760413f6e2ef12/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala#L812), but we need to do both to be safe.

See also previous discussions in scala/bug#5365

@liufengyun
Copy link
Contributor

This is now fixed in d5f9047 .

I agree with Richard Bradley:

scala/bug#5365 (comment)

Richard Bradley (richard.bradley) said:
I don't think that example stands up to scrutiny. If g1 || g2 really is constant true, then rewrite your proposed:

x match {
  case p if g1 => ...
  case p if g2 => ...
}

to just:

x match {
  case p if g1 => ...
  case p => ...
}

This will a) allow the compiler to verify exhaustiveness and b) will help the reader to understand the code.

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