Skip to content

Commit 3f85d02

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

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-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
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
val scala3Version = sys.props("plugin.scalaVersion")
2+
val scala2Version = sys.props("plugin.scala2Version")
3+
4+
lazy val s2 = project.in(file("scala-2"))
5+
.settings(
6+
scalaVersion := scala2Version
7+
)
8+
9+
lazy val s3 = project.in(file("scala-3"))
10+
.dependsOn(s2)
11+
.settings(
12+
scalaVersion := scala3Version
13+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object Foo {
2+
def nothing: Array[scala.Nothing] = println("Array[scala.Nothing]")
3+
def `null`: Array[scala.Null] = println("Array[scala.Null]")
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@main def Test =
2+
Foo.nothing
3+
Foo.`null`

sbt-test/scala2-compat/i22515/test

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
> s3/run

0 commit comments

Comments
 (0)