Skip to content

Type parameter variance not checked for coherence: ClassCastException #8241

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
scabug opened this issue Feb 5, 2014 · 2 comments
Closed

Comments

@scabug
Copy link

scabug commented Feb 5, 2014

This exploits a hole similar to the examples in #7278, but all of those use type members. Maybe it seems more serious if it can easily arise with the more frequently used type parameters.

class Covariant[+A](val x: A)
trait Invariant[A] extends Covariant[A]
object Foo extends Covariant[Any]("abc") with Invariant[Int] { def apply(x: Int): Int = x * 7 }
object Bar { def main(args: Array[String]) { Foo(Foo.x) } }
// java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
//   at scala.runtime.BoxesRunTime.unboxToInt(BoxesRunTime.java:105)
//   at Bar$.main(a.scala:4)
//   at Bar.main(a.scala)

The base type sequence of Foo.type is

Foo.type
Invariant[Int]
Covariant[Int] with Covariant[Any]
Object
Any
@scabug
Copy link
Author

scabug commented Feb 5, 2014

Imported From: https://issues.scala-lang.org/browse/SI-8241?orig=1
Reporter: @paulp
See #7278

@scabug
Copy link
Author

scabug commented Feb 5, 2014

@paulp said:
Damn, I guess this is #7886.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant