Skip to content

Scrutinee type in pattern matches is widened, impairing exhaustivity and reachability analysis #13342

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
SethTisue opened this issue Aug 20, 2021 · 1 comment · Fixed by #13409

Comments

@SethTisue
Copy link
Member

Welcome to Scala 3.1.0-RC1-bin-20210818-868906d-NIGHTLY-git-868906d 
                                                                                                                        
scala> class C {
     |   def m(x: true) = x match {
     |     case true => println("the one true path")
     |   }
     | }
     | 
// defined class C
-- Warning:
2 |  def m(x: true) = x match {
  |                   ^
  |                   match may not be exhaustive.
  |
  |                   It would fail on pattern case: false

The warning is spurious; x has a singleton type that isn't taken into account.

This is a regression from Scala 2, where the corresponding bug is scala/bug#11603 and was fixed for Scala 2.13.4 by scala/scala#9209

@dwijnand
Copy link
Member

dwijnand commented Aug 20, 2021

Yeah, reachability for the opposite effect:

scala> def m(x: true) = x match { case true => 1 }
1 |def m(x: true) = x match { case true => 1 }
  |                 ^
  |                 match may not be exhaustive.
  |
  |                 It would fail on pattern case: false
def m(x: true): Int

scala> def m(x: true) = x match { case true => 1 case false => 2 }
def m(x: true): Int

It's not emitting a reachability warning on the case false.

@SethTisue SethTisue changed the title Scrutinee type in pattern matches is widened, impairing exhaustivity (and reachability?) analysis Scrutinee type in pattern matches is widened, impairing exhaustivity and reachability analysis Aug 20, 2021
@dwijnand dwijnand linked a pull request Aug 28, 2021 that will close this issue
@Kordyjan Kordyjan added this to the 3.1.1 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants