Skip to content

Commit e227128

Browse files
authored
fix: correctly require a ClassTag when building a multidimensional Array (#23902)
Closes #23901
2 parents 4c8e490 + 9da1fcb commit e227128

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,12 @@ object TypeErasure {
386386
case _ => false
387387
}
388388

389+
/** Is `tp` of the form `Array^N[T]` where T is generic? */
390+
def isGenericArrayArg(tp: Type)(using Context): Boolean = tp.dealias match
391+
case defn.ArrayOf(elem) => isGenericArrayArg(elem)
392+
case _ => isGeneric(tp)
393+
end isGenericArrayArg
394+
389395
/** The erased least upper bound of two erased types is computed as follows
390396
* - if both argument are arrays of objects, an array of the erased lub of the element types
391397
* - if both arguments are arrays of same primitives, an array of this primitive

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,14 +1427,11 @@ trait Applications extends Compatibility {
14271427
def convertNewGenericArray(tree: Tree)(using Context): Tree = tree match {
14281428
case Apply(TypeApply(tycon, targs@(targ :: Nil)), args) if tycon.symbol == defn.ArrayConstructor =>
14291429
fullyDefinedType(tree.tpe, "array", tree.srcPos)
1430-
1431-
def newGenericArrayCall =
1430+
if TypeErasure.isGenericArrayArg(targ.tpe) then
14321431
ref(defn.DottyArraysModule)
1433-
.select(defn.newGenericArrayMethod).withSpan(tree.span)
1434-
.appliedToTypeTrees(targs).appliedToTermArgs(args)
1435-
1436-
if (TypeErasure.isGeneric(targ.tpe))
1437-
newGenericArrayCall
1432+
.select(defn.newGenericArrayMethod).withSpan(tree.span)
1433+
.appliedToTypeTrees(targs)
1434+
.appliedToTermArgs(args)
14381435
else tree
14391436
case _ =>
14401437
tree

tests/run/i23901.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import scala.reflect.ClassTag
2+
3+
object MyArray:
4+
def empty[T: ClassTag]: Array[Array[T]] = new Array[Array[T]](0)
5+
6+
@main def Test =
7+
val arr: Array[Array[String]] = MyArray.empty[String]
8+
assert(arr.length == 0)

0 commit comments

Comments
 (0)