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

Scala Generics are Unsound #9912

Open
scabug opened this issue Sep 4, 2016 · 4 comments
Open

Scala Generics are Unsound #9912

scabug opened this issue Sep 4, 2016 · 4 comments
Labels
fixed in Scala 3 regression should not compile typer
Milestone

Comments

@scabug
Copy link

@scabug scabug commented Sep 4, 2016

The following code

class A {
  def compareTo(o: Any): Int = 0
}
class B extends A with Comparable[B] {
  def compareTo(b: B): Int = 0
}
object C {
  def main(args: Array[String]): Unit = {
    println(new B().compareTo(new Object()))
  }
}

compiles without warnings. And it should be executed without any runtime error. However, ClassCastException occurs when the code is executed.

This problem is also known as an already fixed bug of Java Generics:

http://www.concurrentaffair.org/2006/10/10/java-generics-unsound/

@scabug
Copy link
Author

@scabug scabug commented Sep 4, 2016

Imported From: https://issues.scala-lang.org/browse/SI-9912?orig=1
Reporter: Kota Mizushima (kmizushima)
Affected Versions: 2.11.8, 2.12.0-M5

@scabug
Copy link
Author

@scabug scabug commented Sep 5, 2016

@szeiger said:
I think this should not compile in the first place. From http://www.scala-lang.org/files/archive/spec/2.11/05-classes-and-objects.html#class-members :

{quote}It is also an error if a template contains two members (directly defined or inherited) with the same name and the same erased type.{quote}

While compareTo in B has a different erasure than compareTo in A, B also inherits (and overrides) compareTo from Comparable, which has the same erasure.

@scabug
Copy link
Author

@scabug scabug commented Sep 5, 2016

kenji yoshida said:
compile error in Scala 2.9.3 and 2.10.6

Welcome to Scala version 2.9.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_79).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :paste
// Entering paste mode (ctrl-D to finish)

class A {
  def compareTo(o: Any): Int = 0
}
class B extends A with Comparable[B] {
  def compareTo(b: B): Int = 0
}
object C {
  def main(args: Array[String]): Unit = {
    println(new B().compareTo(new Object()))
  }
}

// Exiting paste mode, now interpreting.

<console>:10: error: name clash between inherited members:
method compareTo:(o: Any)Int in class A and
method compareTo:(x$1: B)Int in trait Comparable
have same type after erasure: (o: java.lang.Object)Int
       class B extends A with Comparable[B] {
             ^
Welcome to Scala version 2.10.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_102).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :paste
// Entering paste mode (ctrl-D to finish)

class A {
  def compareTo(o: Any): Int = 0
}
class B extends A with Comparable[B] {
  def compareTo(b: B): Int = 0
}
object C {
  def main(args: Array[String]): Unit = {
    println(new B().compareTo(new Object()))
  }
}

// Exiting paste mode, now interpreting.

<console>:10: error: name clash between inherited members:
method compareTo:(o: Any)Int in class A and
method compareTo:(x$1: B)Int in trait Comparable
have same type after erasure: (o: Object)Int
       class B extends A with Comparable[B] {
             ^

@scabug
Copy link
Author

@scabug scabug commented Oct 28, 2016

@adriaanm said:
We should fix this, but it likely won't be possible in 2.12 due to compatibility constraints.

@scabug scabug added this to the 2.13.0-M1 milestone Apr 7, 2017
@adriaanm adriaanm removed this from the 2.13.0-M1 milestone Apr 14, 2017
@adriaanm adriaanm added this to the 2.13.0-M2 milestone Apr 14, 2017
@adriaanm adriaanm added this to the 2.13.0-M2 milestone Apr 14, 2017
@adriaanm adriaanm removed this from the 2.13.0-M1 milestone Apr 14, 2017
@adriaanm adriaanm removed this from the 2.13.0-M2 milestone Jun 26, 2017
@adriaanm adriaanm added this to the 2.13.0-M3 milestone Jun 26, 2017
@adriaanm adriaanm added this to the 2.13.0-M3 milestone Jun 26, 2017
@adriaanm adriaanm removed this from the 2.13.0-M2 milestone Jun 26, 2017
@adriaanm adriaanm removed this from the 2.13.0-M3 milestone Jan 30, 2018
@adriaanm adriaanm added this to the 2.13.0-M4 milestone Jan 30, 2018
@lrytz lrytz removed this from the 2.13.0-M4 milestone Apr 23, 2018
@lrytz lrytz added this to the 2.13.0-M5 milestone Apr 23, 2018
@adriaanm adriaanm removed this from the 2.13.0-M5 milestone Aug 8, 2018
@adriaanm adriaanm added this to the 2.13.0-RC1 milestone Aug 8, 2018
@adriaanm adriaanm removed their assignment Sep 28, 2018
@adriaanm adriaanm removed this from the 2.13.0-RC1 milestone Nov 13, 2018
@adriaanm adriaanm added this to the 2.14.0-M1 milestone Nov 13, 2018
@smarter smarter added the fixed in Scala 3 label Nov 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in Scala 3 regression should not compile typer
Development

No branches or pull requests

4 participants