Skip to content

Commit dd8caef

Browse files
hamzaremmaltgodzik
authored andcommitted
fix: align erasure of Array[Nothing] and Array[Null] with Scala 2
[Cherry-picked c197afb]
1 parent cfaba1b commit dd8caef

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,8 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
597597
* will be returned.
598598
*
599599
* In all other situations, |T| will be computed as follow:
600-
* - For a refined type scala.Array+[T]:
601-
* - if T is Nothing or Null, []Object
600+
* - For a refined type scala.Array[T]:
601+
* - {Scala 2} if T is Nothing or Null, []Object
602602
* - otherwise, if T <: Object, []|T|
603603
* - otherwise, if T is a type parameter coming from Java, []Object
604604
* - otherwise, Object
@@ -781,10 +781,12 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
781781
val defn.ArrayOf(elemtp) = tp: @unchecked
782782
if (isGenericArrayElement(elemtp, isScala2 = sourceLanguage.isScala2)) defn.ObjectType
783783
else
784-
try
785-
val eElem = erasureFn(sourceLanguage, semiEraseVCs = false, isConstructor, isSymbol, inSigName)(elemtp)
786-
if eElem.isInstanceOf[WildcardType] then WildcardType
787-
else JavaArrayType(eElem)
784+
try
785+
if sourceLanguage.isScala2 && (elemtp.isNullType || elemtp.isNothingType) then
786+
JavaArrayType(defn.ObjectType)
787+
else erasureFn(sourceLanguage, semiEraseVCs = false, isConstructor, isSymbol, inSigName)(elemtp) match
788+
case _: WildcardType => WildcardType
789+
case elem => JavaArrayType(elem)
788790
catch case ex: Throwable =>
789791
handleRecursive("erase array type", tp.show, ex)
790792
}

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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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)