Skip to content

Commit db4d226

Browse files
committed
chore: remove old support for getClass in scala.AnyVal
1 parent 68396ca commit db4d226

File tree

14 files changed

+7
-69
lines changed

14 files changed

+7
-69
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -775,17 +775,7 @@ object desugar {
775775
case PatDef(_, ids: List[Ident] @ unchecked, _, _) => ids
776776
}
777777

778-
val stats0 = impl.body.map(expandConstructor)
779-
val stats =
780-
if (ctx.owner eq defn.ScalaPackageClass) && defn.hasProblematicGetClass(className) then
781-
stats0.filterConserve {
782-
case ddef: DefDef =>
783-
ddef.name ne nme.getClass_
784-
case _ =>
785-
true
786-
}
787-
else
788-
stats0
778+
val stats = impl.body.map(expandConstructor)
789779

790780
if (isEnum) {
791781
val (enumCases, enumStats) = stats.partition(DesugarEnums.isEnumCase)

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,27 +1940,6 @@ class Definitions {
19401940
if (isBoxedUnitClass(cls)) parents.filter(_.typeSymbol != JavaSerializableClass)
19411941
else parents
19421942

1943-
private val HasProblematicGetClass: Set[Name] = Set(
1944-
tpnme.AnyVal, tpnme.Byte, tpnme.Short, tpnme.Char, tpnme.Int, tpnme.Long, tpnme.Float, tpnme.Double,
1945-
tpnme.Unit, tpnme.Boolean)
1946-
1947-
/** When typing a primitive value class or AnyVal, we ignore the `getClass`
1948-
* member: it's supposed to be an override of the `getClass` defined on `Any`,
1949-
* but in dotty `Any#getClass` is polymorphic so it ends up being an overload.
1950-
* This is especially problematic because it means that when writing:
1951-
*
1952-
* 1.asInstanceOf[Int & AnyRef].getClass
1953-
*
1954-
* the `getClass` that returns `Class[Int]` defined in Int can be selected,
1955-
* but this call is specified to return `classOf[Integer]`, see
1956-
* tests/run/t5568.scala.
1957-
*
1958-
* FIXME: remove all the `getClass` methods defined in the standard library
1959-
* so we don't have to hot-patch it like this.
1960-
*/
1961-
def hasProblematicGetClass(className: Name): Boolean =
1962-
HasProblematicGetClass.contains(className)
1963-
19641943
@tu lazy val assumedTransparentNames: Map[Name, Set[Symbol]] =
19651944
// we should do a more through sweep through it then.
19661945
val strs = Map(

compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -444,11 +444,9 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
444444

445445
var flags = unpickleScalaFlags(readLongNat(), name.isTypeName)
446446

447-
if (name eq nme.getClass_) && defn.hasProblematicGetClass(owner.name)
448-
// Scala 2 sometimes pickle the same type parameter symbol multiple times
449-
// (see i11173 for an example), but we should only unpickle it once.
450-
|| tag == TYPEsym && flags.is(TypeParam) && symScope(owner).lookup(name.asTypeName).exists
451-
then
447+
// Scala 2 sometimes pickle the same type parameter symbol multiple times
448+
// (see i11173 for an example), but we should only unpickle it once.
449+
if tag == TYPEsym && flags.is(TypeParam) && symScope(owner).lookup(name.asTypeName).exists then
452450
// skip this member
453451
return NoSymbol
454452

compiler/src/dotty/tools/dotc/typer/RefChecks.scala

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,15 +1034,7 @@ object RefChecks {
10341034
nonMatching match {
10351035
case Nil =>
10361036
report.error(OverridesNothing(member), member.srcPos)
1037-
case ms =>
1038-
// getClass in primitive value classes is defined in the standard library as:
1039-
// override def getClass(): Class[Int] = ???
1040-
// However, it's not actually an override in Dotty because our Any#getClass
1041-
// is polymorphic (see `Definitions#Any_getClass`), so since we can't change
1042-
// the standard library, we need to drop the override flag without reporting
1043-
// an error.
1044-
if (!(member.name == nme.getClass_ && clazz.isPrimitiveValueClass))
1045-
report.error(OverridesNothingButNameExists(member, ms), member.srcPos)
1037+
case _ =>
10461038
}
10471039
member.resetFlag(Override)
10481040
member.resetFlag(AbsOverride)

library/src/scala/AnyVal.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,4 @@ import scala.language.`2.13`
5555
* still must allocate a value class instance at runtime. These limitations and circumstances are
5656
* explained in greater detail in the [[https://docs.scala-lang.org/overviews/core/value-classes.html Value Classes and Universal Traits]].
5757
*/
58-
transparent abstract class AnyVal extends Any, Matchable {
59-
def getClass(): Class[? <: AnyVal] = null.asInstanceOf[Class[? <: AnyVal]]
60-
}
58+
transparent abstract class AnyVal extends Any, Matchable

library/src/scala/Boolean.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ final abstract class Boolean private extends AnyVal {
108108
*/
109109
def ^(x: Boolean): Boolean
110110

111-
// Provide a more specific return type for Scaladoc
112-
override def getClass(): Class[Boolean] = ???
113111
}
114112

115113
object Boolean extends AnyValCompanion {

library/src/scala/Byte.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,6 @@ final abstract class Byte private extends AnyVal {
444444
/** Returns the remainder of the division of this value by `x`. */
445445
def %(x: Double): Double
446446

447-
// Provide a more specific return type for Scaladoc
448-
override def getClass(): Class[Byte] = ???
449447
}
450448

451449
object Byte extends AnyValCompanion {

library/src/scala/Char.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,6 @@ final abstract class Char private extends AnyVal {
444444
/** Returns the remainder of the division of this value by `x`. */
445445
def %(x: Double): Double
446446

447-
// Provide a more specific return type for Scaladoc
448-
override def getClass(): Class[Char] = ???
449447
}
450448

451449
object Char extends AnyValCompanion {

library/src/scala/Double.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,6 @@ final abstract class Double private extends AnyVal {
207207
/** Returns the remainder of the division of this value by `x`. */
208208
def %(x: Double): Double
209209

210-
// Provide a more specific return type for Scaladoc
211-
override def getClass(): Class[Double] = ???
212210
}
213211

214212
object Double extends AnyValCompanion {

library/src/scala/Float.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,6 @@ final abstract class Float private extends AnyVal {
207207
/** Returns the remainder of the division of this value by `x`. */
208208
def %(x: Double): Double
209209

210-
// Provide a more specific return type for Scaladoc
211-
override def getClass(): Class[Float] = ???
212210
}
213211

214212
object Float extends AnyValCompanion {

0 commit comments

Comments
 (0)