Skip to content

Commit 6307e99

Browse files
committed
IArray.toArray: Fix unsound cast
This changes the result type in a binary-compatible way since `Array[T]` and `Array[? <: T]` both erase to `Object` here.
1 parent 1f4d125 commit 6307e99

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

library/src/scala/IArray.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ object IArray:
250250
genericArrayOps(arr).takeWhile(p)
251251

252252
/** Returns a mutable copy of this immutable array. */
253-
extension [T](arr: IArray[T]) def toArray: Array[T] =
254-
arr.clone.asInstanceOf[Array[T]]
253+
extension [T](arr: IArray[T]) def toArray: Array[? <: T] =
254+
arr.clone
255255

256256
extension [T](arr: IArray[T])
257257
def ++[U >: T: ClassTag](suffix: IArray[U]): IArray[U] = genericArrayOps(arr) ++ suffix.toSeq

tests/neg/i12597.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@main def Test =
2+
val a: IArray[Int] = IArray(2)
3+
val b: IArray[Any] = a
4+
val c = b.toArray
5+
c(0) = "" // error: Found: String Required: c.T

tests/run/i12597.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@main def Test =
2+
val a: IArray[Int] = IArray(2)
3+
val b: IArray[Any] = a
4+
b.toArray

0 commit comments

Comments
 (0)