Skip to content

Incorrect non-exhaustivity warning in pattern matches #15289

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
nrinaudo opened this issue May 25, 2022 · 2 comments · Fixed by #15706
Closed

Incorrect non-exhaustivity warning in pattern matches #15289

nrinaudo opened this issue May 25, 2022 · 2 comments · Fixed by #15706

Comments

@nrinaudo
Copy link

Compiler version

3.1.2

Minimized code

enum Foo[A, B]:
  case Bar[A]() extends Foo[A, A]

def doFoo[A](foo: Foo[A, String]): String = foo match
  case Foo.Bar() => "baz"

Output

[warn] -- [E029] Pattern Match Exhaustivity Warning: /path/to/gadt/src/main/scala/repro.scala:6:44 
[warn] 6 |def doFoo[A](foo: Foo[A, String]): String = foo match
[warn]   |                                            ^^^
[warn]   |                                  match may not be exhaustive.
[warn]   |
[warn]   |                                  It would fail on pattern case: Foo.Bar()

Expectation

I would expect this not to warn, since there's a clear case Foo.Bar() => ... statement.

@nrinaudo nrinaudo added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels May 25, 2022
@szymon-rd szymon-rd added area:pattern-matching and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels May 25, 2022
@nrinaudo
Copy link
Author

This might also need a GADT tag. I didn't realise while minifying the reproduction scenario, but isn't it exactly Refl?

enum Eq[A, B]:
  case Refl[A]() extends Eq[A, A]

@dwijnand dwijnand self-assigned this May 25, 2022
@nrinaudo
Copy link
Author

Interestingly, this compiles without a warning:

enum Foo[A, B]:
  case Bar[A]() extends Foo[A, A]

def doFoo[A](foo: Foo[A, String]): Foo[A, String] = foo match
  case Foo.Bar() => Foo.Bar()

And this has a rather interesting warning:

enum Foo[A, B]:
  case Bar[A]() extends Foo[A, A]

def doFoo[A](foo: Foo[A, String]): Foo[A, String] = foo match
  case s @ Foo.Bar() => s

[warn] -- [E029] Pattern Match Exhaustivity Warning: /path/torefl/src/main/scala/repro.scala:6:52 
[warn] 6 |def doFoo[A](foo: Foo[A, String]): Foo[A, String] = foo match
[warn]   |                                                    ^^^
[warn]   |                                  match may not be exhaustive.
[warn]   |
[warn]   |                                  It would fail on pattern case: Foo.Bar()
[warn] -- Unchecked Warning: /path/to/refl/src/main/scala/repro.scala:7:7 
[warn] 7 |  case s @ Foo.Bar() => s
[warn]   |       ^
[warn]   |       the type test for repro.Foo.Bar[A] cannot be checked at runtime

@dwijnand dwijnand linked a pull request Jul 19, 2022 that will close this issue
@Kordyjan Kordyjan added this to the 3.2.1 milestone Aug 1, 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.

4 participants