Skip to content

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

Closed
@bfil

Description

@bfil

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions