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

Comments

Projects
None yet
4 participants
@scabug
Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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 modified the milestones: 2.13.0-M1, 2.13.0-M2 Apr 14, 2017

@adriaanm adriaanm modified the milestones: 2.13.0-M2, 2.13.0-M3 Jun 26, 2017

@adriaanm adriaanm modified the milestones: 2.13.0-M3, 2.13.0-M4 Jan 30, 2018

@lrytz lrytz modified the milestones: 2.13.0-M4, 2.13.0-M5 Apr 23, 2018

@adriaanm adriaanm modified the milestones: 2.13.0-M5, 2.13.0-RC1 Aug 8, 2018

@adriaanm adriaanm removed their assignment Sep 28, 2018

@adriaanm adriaanm modified the milestones: 2.13.0-RC1, 2.14.0-M1 Nov 13, 2018

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