Skip to content

Commit 59ea58e

Browse files
authored
Merge pull request #12103 from dotty-staging/fix-isInstanceOf-array
Fix isInstanceOf[Array[?]] returning true on non-Array
2 parents 26e3795 + 46b8d67 commit 59ea58e

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
@@ -1031,21 +1031,22 @@ class Definitions {
10311031

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

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)