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 6dd2f80
Show file tree
Hide file tree
Showing 5 changed files with 25 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
13 changes: 13 additions & 0 deletions sbt-test/scala2-compat/i22515/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
val scala3Version = sys.props("plugin.scalaVersion")
val scala2Version = sys.props("plugin.scala2Version")

lazy val s2 = project.in(file("scala-2"))
.settings(
scalaVersion := scala2Version
)

lazy val s3 = project.in(file("scala-3"))
.dependsOn(s2)
.settings(
scalaVersion := scala3Version
)
Empty file.
3 changes: 3 additions & 0 deletions sbt-test/scala2-compat/i22515/scala-3/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@main def Test =
Foo.nothing
Foo.`null`
1 change: 1 addition & 0 deletions sbt-test/scala2-compat/i22515/test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
> s3/run

0 comments on commit 6dd2f80

Please sign in to comment.