Skip to content

Commit

Permalink
fix: align erasure of Array[Nothing] and Array[Null] with Scala 2
Browse files Browse the repository at this point in the history
  • Loading branch information
hamzaremmal committed Feb 4, 2025
1 parent 81e057a commit c197afb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
14 changes: 8 additions & 6 deletions compiler/src/dotty/tools/dotc/core/TypeErasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,8 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
* will be returned.
*
* In all other situations, |T| will be computed as follow:
* - For a refined type scala.Array+[T]:
* - if T is Nothing or Null, []Object
* - For a refined type scala.Array[T]:
* - {Scala 2} if T is Nothing or Null, []Object
* - otherwise, if T <: Object, []|T|
* - otherwise, if T is a type parameter coming from Java, []Object
* - otherwise, Object
Expand Down Expand Up @@ -783,10 +783,12 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
val defn.ArrayOf(elemtp) = tp: @unchecked
if (isGenericArrayElement(elemtp, isScala2 = sourceLanguage.isScala2)) defn.ObjectType
else
try
val eElem = erasureFn(sourceLanguage, semiEraseVCs = false, isConstructor, isSymbol, inSigName)(elemtp)
if eElem.isInstanceOf[WildcardType] then WildcardType
else JavaArrayType(eElem)
try
if sourceLanguage.isScala2 && (elemtp.isNullType || elemtp.isNothingType) then
JavaArrayType(defn.ObjectType)
else erasureFn(sourceLanguage, semiEraseVCs = false, isConstructor, isSymbol, inSigName)(elemtp) match
case _: WildcardType => WildcardType
case elem => JavaArrayType(elem)
catch case ex: Throwable =>
handleRecursive("erase array type", tp.show, ex)
}
Expand Down
6 changes: 6 additions & 0 deletions sbt-test/scala2-compat/erasure/dottyApp/Api.scala
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,10 @@ class Z {
def objectARRAY_88(x: Array[Any]): Unit = {}
def objectARRAY_89(x: Array[AnyRef]): Unit = {}
def objectARRAY_90(x: Array[AnyVal]): Unit = {}

def objectARRAY_91(x: Array[Nothing]): Unit = {}
def objectARRAY_92(x: Array[Null]): Unit = {}
def objectARRAY_93(x: Array[_ <: Nothing]): Unit = {}
def objectARRAY_94(x: Array[_ <: Null]): Unit = {}

}
4 changes: 4 additions & 0 deletions sbt-test/scala2-compat/erasure/dottyApp/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ object Main {
z.objectARRAY_88(dummy)
z.objectARRAY_89(dummy)
z.objectARRAY_90(dummy)
z.objectARRAY_91(dummy)
z.objectARRAY_92(dummy)
z.objectARRAY_93(dummy)
z.objectARRAY_94(dummy)

val methods = classOf[scala2Lib.Z].getDeclaredMethods.toList ++ classOf[dottyApp.Z].getDeclaredMethods.toList
methods.foreach { m =>
Expand Down
6 changes: 6 additions & 0 deletions sbt-test/scala2-compat/erasure/scala2Lib/Api.scala
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,10 @@ class Z {
def objectARRAY_88(x: Array[Any]): Unit = {}
def objectARRAY_89(x: Array[AnyRef]): Unit = {}
def objectARRAY_90(x: Array[AnyVal]): Unit = {}

def objectARRAY_91(x: Array[Nothing]): Unit = {}
def objectARRAY_92(x: Array[Null]): Unit = {}
def objectARRAY_93(x: Array[_ <: Nothing]): Unit = {}
def objectARRAY_94(x: Array[_ <: Null]): Unit = {}

}

0 comments on commit c197afb

Please sign in to comment.