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

nested named irrefutable typetest pattern causes exhaustivity warning #10994

Closed
bishabosha opened this issue Jan 4, 2021 · 3 comments · Fixed by #12847 or #16150
Closed

nested named irrefutable typetest pattern causes exhaustivity warning #10994

bishabosha opened this issue Jan 4, 2021 · 3 comments · Fixed by #12847 or #16150

Comments

@bishabosha
Copy link
Member

Minimized code

def foo = true match
  case (b: Boolean): Boolean => ()

Output

-- [E029] Pattern Match Exhaustivity Warning: sandbox/match/example.scala:1:10 -
1 |def foo = true match
  |          ^^^^
  |          match may not be exhaustive.
  |
  |          It would fail on pattern case: true, false

Expectation

no warning.

the wildcard case produces no warning:

def foo = true match
  case (_: Boolean): Boolean => ()
@liufengyun
Copy link
Contributor

It seems Scala 2 syntax does not support this and it's not in the Scala 3 language specification. Maybe we can disallow it in Scala 3 as well.

@bishabosha
Copy link
Member Author

bishabosha commented Jan 4, 2021

I have seen some code use a nested pattern in a type test in the wild but I'm not sure what made it necessary

@abgruszecki
Copy link
Contributor

I'd also be in favour of (at least temporarily) disallowing this in Scala3, since it apparently causes extra difficulties in the compiler. It's always possible to define a "fork" pattern which allows simultaneously matching one value against two patterns, so there's no real loss of functionality from forbidding this syntax.

liufengyun added a commit to dotty-staging/dotty that referenced this issue Jan 7, 2021
In Scala 2, a typed pattern `p: T` restricts that `p` can only be a
pattern variable.

In Dotty, scala#6919 allows `p` to be any pattern, in order to support
 pattern matching on generic number literals.

This PR aligns the syntax with Scala 2 by stipulating that in a typed
pattern `p: T`, either

- `p` is a pattern variable, or
- `p` is a number literal
liufengyun added a commit to dotty-staging/dotty that referenced this issue Jan 7, 2021
In Scala 2, a typed pattern `p: T` restricts that `p` can only be a
pattern variable.

In Dotty, scala#6919 allows `p` to be any pattern, in order to support
 pattern matching on generic number literals.

This PR aligns the syntax with Scala 2 by stipulating that in a typed
pattern `p: T`, either

- `p` is a pattern variable, or
- `p` is a number literal
dwijnand pushed a commit to dwijnand/scala3 that referenced this issue Oct 7, 2022
In Scala 2, a typed pattern `p: T` restricts that `p` can only be a
pattern variable.

In Dotty, scala#6919 allows `p` to be any pattern, in order to support
 pattern matching on generic number literals.

This PR aligns the syntax with Scala 2 by stipulating that in a typed
pattern `p: T`, either

- `p` is a pattern variable, or
- `p` is a number literal
little-inferno pushed a commit to little-inferno/dotty that referenced this issue Jan 25, 2023
In Scala 2, a typed pattern `p: T` restricts that `p` can only be a
pattern variable.

In Dotty, scala#6919 allows `p` to be any pattern, in order to support
 pattern matching on generic number literals.

This PR aligns the syntax with Scala 2 by stipulating that in a typed
pattern `p: T`, either

- `p` is a pattern variable, or
- `p` is a number literal
@Kordyjan Kordyjan added this to the 3.3.0 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment