Skip to content

No compilation error on invalid case statements when pattern matching on union types #8681

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
bfil opened this issue Apr 6, 2020 · 0 comments · Fixed by #8722
Closed

No compilation error on invalid case statements when pattern matching on union types #8681

bfil opened this issue Apr 6, 2020 · 0 comments · Fixed by #8722

Comments

@bfil
Copy link

bfil commented Apr 6, 2020

Minimized code

case class A(a: Int)
case class B(b: Int)
case class C(c: Int)

val a = (A(1): A) match {
  case A(_) => "OK"
  case B(_) => "NOT OK" // Compiler Error: this case is unreachable since class A and class B are unrelated
}

val b = (A(1): A | B) match {
  case A(_) => "OK"
  case B(_) => "OK"
  case C(_) => "NOT OK" // No Compiler Error
}

Output

[error]    |  case B(_) => "NOT OK"
[error]    |       ^
[error]    |       this case is unreachable since class A and class B are unrelated

Expectation

The compiler should also yield an unreachable code error for the second match statement when trying to match on C since it's not related to type A | B.

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