Skip to content

Commit c197afb

Browse files
committed
fix: align erasure of Array[Nothing] and Array[Null] with Scala 2
1 parent 81e057a commit c197afb

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -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
@@ -783,10 +783,12 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
783783
val defn.ArrayOf(elemtp) = tp: @unchecked
784784
if (isGenericArrayElement(elemtp, isScala2 = sourceLanguage.isScala2)) defn.ObjectType
785785
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)
786+
try
787+
if sourceLanguage.isScala2 && (elemtp.isNullType || elemtp.isNothingType) then
788+
JavaArrayType(defn.ObjectType)
789+
else 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/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 objectARRAY_91(x: Array[Nothing]): Unit = {}
200+
def objectARRAY_92(x: Array[Null]): Unit = {}
201+
def objectARRAY_93(x: Array[_ <: Nothing]): Unit = {}
202+
def objectARRAY_94(x: Array[_ <: Null]): Unit = {}
203+
198204
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -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)