Skip to content

Commit 45b8dc0

Browse files
Merge pull request #12108 from dotty-staging/backport-fix-isInstanceOf-array
Backport: Fix isInstanceOf[Array[?]] returning true on non-Array
2 parents 6dff34c + 4a384e7 commit 45b8dc0

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,21 +1029,22 @@ class Definitions {
10291029

10301030
/** An extractor for multi-dimensional arrays.
10311031
* Note that this will also extract the high bound if an
1032-
* element type is a wildcard. E.g.
1032+
* element type is a wildcard upper-bounded by an array. E.g.
10331033
*
10341034
* Array[? <: Array[? <: Number]]
10351035
*
10361036
* would match
10371037
*
1038-
* MultiArrayOf(<Number>, 2)
1038+
* MultiArrayOf(<? <: Number>, 2)
10391039
*/
10401040
object MultiArrayOf {
10411041
def apply(elem: Type, ndims: Int)(using Context): Type =
10421042
if (ndims == 0) elem else ArrayOf(apply(elem, ndims - 1))
10431043
def unapply(tp: Type)(using Context): Option[(Type, Int)] = tp match {
10441044
case ArrayOf(elemtp) =>
10451045
def recur(elemtp: Type): Option[(Type, Int)] = elemtp.dealias match {
1046-
case TypeBounds(lo, hi) => recur(hi)
1046+
case tp @ TypeBounds(lo, hi @ MultiArrayOf(finalElemTp, n)) =>
1047+
Some(finalElemTp, n)
10471048
case MultiArrayOf(finalElemTp, n) => Some(finalElemTp, n + 1)
10481049
case _ => Some(elemtp, 1)
10491050
}

tests/run/array-erasure.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,11 @@ object Test {
6565
arr4(x)
6666
arr5(x)
6767
arr6(x)
68+
69+
70+
val str: Any = ""
71+
assert(!str.isInstanceOf[Array[?]])
72+
assert(!str.isInstanceOf[Array[Array[?]]])
73+
assert(!str.isInstanceOf[Array[? <: Array[?]]])
6874
}
6975
}

0 commit comments

Comments
 (0)