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

Unsoundness involving generic type and same-named methods #9912

Closed
scabug opened this issue Sep 4, 2016 · 4 comments · Fixed by scala/scala#10100
Closed

Unsoundness involving generic type and same-named methods #9912

scabug opened this issue Sep 4, 2016 · 4 comments · Fixed by scala/scala#10100
Assignees
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) regression should not compile typer
Milestone

Comments

@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
Copy link
Author

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 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 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 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
@smarter smarter added the fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) label Nov 13, 2018
@SethTisue SethTisue removed this from the 3.x milestone Aug 10, 2022
@SethTisue SethTisue added this to the 2.13.9 milestone Aug 10, 2022
@SethTisue SethTisue changed the title Scala Generics are Unsound Unsoundness involving generic type and same-named methods Aug 10, 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/) regression should not compile typer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants