Skip to content

Commit ad6ec5e

Browse files
authored
Merge pull request #235 from scala/backport-lts-3.3-22517
Backport "Align erasure of `Array[Nothing]` and `Array[Null]` with Scala 2" to 3.3 LTS
2 parents 6b87b26 + bae72d8 commit ad6ec5e

File tree

6 files changed

+32
-12
lines changed

6 files changed

+32
-12
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ object TypeErasure {
327327
val sym = t.symbol
328328
// Only a few classes have both primitives and references as subclasses.
329329
if (sym eq defn.AnyClass) || (sym eq defn.AnyValClass) || (sym eq defn.MatchableClass) || (sym eq defn.SingletonClass)
330-
|| isScala2 && !(t.derivesFrom(defn.ObjectClass) || t.isNullType) then
330+
|| isScala2 && !(t.derivesFrom(defn.ObjectClass) || t.isNullType | t.isNothingType) then
331331
NoSymbol
332332
// We only need to check for primitives because derived value classes in arrays are always boxed.
333333
else if sym.isPrimitiveValueClass then
@@ -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
@@ -779,12 +779,14 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
779779

780780
private def eraseArray(tp: Type)(using Context) = {
781781
val defn.ArrayOf(elemtp) = tp: @unchecked
782-
if (isGenericArrayElement(elemtp, isScala2 = sourceLanguage.isScala2)) defn.ObjectType
783-
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)
782+
if isGenericArrayElement(elemtp, isScala2 = sourceLanguage.isScala2) then
783+
defn.ObjectType
784+
else if sourceLanguage.isScala2 && (elemtp.hiBound.isNullType || elemtp.hiBound.isNothingType) then
785+
JavaArrayType(defn.ObjectType)
786+
else
787+
try 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
}

presentation-compiler/src/main/dotty/tools/pc/PcInlayHintsProvider.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class PcInlayHintsProvider(
5757
.headOption
5858
.getOrElse(unit.tpdTree)
5959
.enclosedChildren(pos.span)
60-
.flatMap(tpdTree => deepFolder(InlayHints.empty(params.uri()), tpdTree).result())
60+
.flatMap(tpdTree => deepFolder(InlayHints.empty(params.uri().nn), tpdTree).result())
6161

6262
private def adjustPos(pos: SourcePosition): SourcePosition =
6363
pos.adjust(text)._1

sbt-test/scala2-compat/erasure/build.sbt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
ThisBuild / fork := true
2+
13
lazy val scala2Lib = project.in(file("scala2Lib"))
24
.settings(
35
scalaVersion := sys.props("plugin.scala2Version")

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 nothing$ARRAY_91(x: Array[Nothing]): Unit = {}
200+
def null$ARRAY_92(x: Array[Null]): Unit = {}
201+
def nothing$ARRAY_93(x: Array[_ <: Nothing]): Unit = {}
202+
def null$ARRAY_94(x: Array[_ <: Null]): Unit = {}
203+
198204
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ object Main {
5353
z.c_40(dummy)
5454
z.c_41(dummy)
5555
z.c_42(dummy)
56-
z.b_43(dummy)
56+
//z.b_43(dummy)
5757
z.c_44(dummy)
5858
z.c_45(dummy)
59-
z.b_46(dummy)
59+
//z.b_46(dummy)
6060
z.c_47(dummy)
6161
// z.a_48(dummy)
6262
// z.c_49(dummy)
@@ -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)