Skip to content

Explicit type specification in match worsens type checks #20705

Open
@mohe2015

Description

@mohe2015

Compiler version

3.3.3

Minimized code

@main
def main(): Unit = {
  val example: Tuple2[Int | String, Int | String] = ("test", 2)
  example match {
    case newExample@Tuple2(a: Int, b: Int) => println("hi")
    case other => println("unmatched")
  }
}

prints "unmatched" but

@main
def main(): Unit = {
  val example: Tuple2[Int | String, Int | String] = ("test", 2)
  example match {
    case newExample@Tuple2[Int, Int](a: Int, b: Int) => println("hi")
    case other => println("unmatched")
  }
}

throws a ClassCastException.

Expectation

While it's understandable that the compiler can't infer the types correctly (this seems to be #15972), I think it's unexpected that just adding a type specifier removes the runtime type checks.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions