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

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

Open
grindvoll opened this issue Sep 3, 2018 · 2 comments
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) patmat runtime crash
Milestone

Comments

@grindvoll
Copy link

grindvoll commented Sep 3, 2018

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.

@grindvoll
Copy link
Author

Just tested this case in newer Scala versions. It also fails for 2.13.0-M5. In Dotty 0.9.0-RC, it works OK.

@grindvoll
Copy link
Author

Another observation: By introducing a trait, the code works as expected:

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

@smarter smarter added the fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) label Oct 19, 2018
@SethTisue SethTisue added this to the Backlog milestone Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) patmat runtime crash
Projects
None yet
Development

No branches or pull requests

4 participants