Skip to content

Potential ClassCastException when pattern matching case class within generic trait #11130

Open
@grindvoll

Description

@grindvoll

Using Scala 2.12.6, the following code crashes with a ClassCastException:

trait Outer[T] {
  object Group {
    case class C1(x: T)
  }
}
object IntOuter extends Outer[Int]
object StringOuter extends Outer[String]

val c1: Any = IntOuter.Group.C1(123)
c1 match {
  case StringOuter.Group.C1(v) => println(v)
  case _ => println("Not a string")
}

// Results in error: Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

The exception occurs when trying to print the matched value v.

By removing the Group wrapper object, the code works as expected. It seems that the extra wrapper object makes the compiler skip checking the $outer reference that is present in the inner case class.

This bug is related to #9639 and Test1 in #6583. #9639 is still reported as open, but it seems to be fixed in Scala 2.12.6.

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixed in Scala 3This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/)patmatruntime crash

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions