Skip to content

Commit 0ea05aa

Browse files
authored
Align erasure of Array[Nothing] and Array[Null] with Scala 2 (#22517)
Closes #22515
2 parents a3dde8b + f77b1f9 commit 0ea05aa

File tree

5 files changed

+31
-11
lines changed

5 files changed

+31
-11
lines changed

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

+11-9
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ object TypeErasure {
326326
val sym = t.symbol
327327
// Only a few classes have both primitives and references as subclasses.
328328
if (sym eq defn.AnyClass) || (sym eq defn.AnyValClass) || (sym eq defn.MatchableClass) || (sym eq defn.SingletonClass)
329-
|| isScala2 && !(t.derivesFrom(defn.ObjectClass) || t.isNullType) then
329+
|| isScala2 && !(t.derivesFrom(defn.ObjectClass) || t.isNullType | t.isNothingType) then
330330
NoSymbol
331331
// We only need to check for primitives because derived value classes in arrays are always boxed.
332332
else if sym.isPrimitiveValueClass then
@@ -596,8 +596,8 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
596596
* will be returned.
597597
*
598598
* In all other situations, |T| will be computed as follow:
599-
* - For a refined type scala.Array+[T]:
600-
* - if T is Nothing or Null, []Object
599+
* - For a refined type scala.Array[T]:
600+
* - {Scala 2} if T is Nothing or Null, []Object
601601
* - otherwise, if T <: Object, []|T|
602602
* - otherwise, if T is a type parameter coming from Java, []Object
603603
* - otherwise, Object
@@ -781,12 +781,14 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
781781

782782
private def eraseArray(tp: Type)(using Context) = {
783783
val defn.ArrayOf(elemtp) = tp: @unchecked
784-
if (isGenericArrayElement(elemtp, isScala2 = sourceLanguage.isScala2)) defn.ObjectType
785-
else
786-
try
787-
val eElem = erasureFn(sourceLanguage, semiEraseVCs = false, isConstructor, isSymbol, inSigName)(elemtp)
788-
if eElem.isInstanceOf[WildcardType] then WildcardType
789-
else JavaArrayType(eElem)
784+
if isGenericArrayElement(elemtp, isScala2 = sourceLanguage.isScala2) then
785+
defn.ObjectType
786+
else if sourceLanguage.isScala2 && (elemtp.hiBound.isNullType || elemtp.hiBound.isNothingType) then
787+
JavaArrayType(defn.ObjectType)
788+
else
789+
try erasureFn(sourceLanguage, semiEraseVCs = false, isConstructor, isSymbol, inSigName)(elemtp) match
790+
case _: WildcardType => WildcardType
791+
case elem => JavaArrayType(elem)
790792
catch case ex: Throwable =>
791793
handleRecursive("erase array type", tp.show, ex)
792794
}

sbt-test/scala2-compat/erasure/build.sbt

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
ThisBuild / fork := true
2+
13
lazy val scala2Lib = project.in(file("scala2Lib"))
24
.settings(
35
scalaVersion := sys.props("plugin.scala2Version")

sbt-test/scala2-compat/erasure/dottyApp/Api.scala

+6
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,10 @@ class Z {
195195
def objectARRAY_88(x: Array[Any]): Unit = {}
196196
def objectARRAY_89(x: Array[AnyRef]): Unit = {}
197197
def objectARRAY_90(x: Array[AnyVal]): Unit = {}
198+
199+
def nothing$ARRAY_91(x: Array[Nothing]): Unit = {}
200+
def null$ARRAY_92(x: Array[Null]): Unit = {}
201+
def nothing$ARRAY_93(x: Array[_ <: Nothing]): Unit = {}
202+
def null$ARRAY_94(x: Array[_ <: Null]): Unit = {}
203+
198204
}

sbt-test/scala2-compat/erasure/dottyApp/Main.scala

+6-2
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ object Main {
5353
z.c_40(dummy)
5454
z.c_41(dummy)
5555
z.c_42(dummy)
56-
z.b_43(dummy)
56+
//z.b_43(dummy)
5757
z.c_44(dummy)
5858
z.c_45(dummy)
59-
z.b_46(dummy)
59+
//z.b_46(dummy)
6060
z.c_47(dummy)
6161
// z.a_48(dummy)
6262
// z.c_49(dummy)
@@ -101,6 +101,10 @@ object Main {
101101
z.objectARRAY_88(dummy)
102102
z.objectARRAY_89(dummy)
103103
z.objectARRAY_90(dummy)
104+
z.objectARRAY_91(dummy)
105+
z.objectARRAY_92(dummy)
106+
z.objectARRAY_93(dummy)
107+
z.objectARRAY_94(dummy)
104108

105109
val methods = classOf[scala2Lib.Z].getDeclaredMethods.toList ++ classOf[dottyApp.Z].getDeclaredMethods.toList
106110
methods.foreach { m =>

sbt-test/scala2-compat/erasure/scala2Lib/Api.scala

+6
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,10 @@ class Z {
186186
def objectARRAY_88(x: Array[Any]): Unit = {}
187187
def objectARRAY_89(x: Array[AnyRef]): Unit = {}
188188
def objectARRAY_90(x: Array[AnyVal]): Unit = {}
189+
190+
def objectARRAY_91(x: Array[Nothing]): Unit = {}
191+
def objectARRAY_92(x: Array[Null]): Unit = {}
192+
def objectARRAY_93(x: Array[_ <: Nothing]): Unit = {}
193+
def objectARRAY_94(x: Array[_ <: Null]): Unit = {}
194+
189195
}

0 commit comments

Comments
 (0)