Skip to content

Commit 8f3fdf5

Browse files
authored
Merge pull request #12598 from dotty-staging/fix-toArray
IArray.toArray: Deprecate broken method
2 parents 17a6ab0 + 4cfb637 commit 8f3fdf5

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

library/src/scala/IArray.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,11 @@ object IArray:
249249
extension [T](arr: IArray[T]) def takeWhile(p: T => Boolean): IArray[T] =
250250
genericArrayOps(arr).takeWhile(p)
251251

252-
/** Returns a mutable copy of this immutable array. */
253-
extension [T](arr: IArray[T]) def toArray: Array[T] =
254-
arr.clone.asInstanceOf[Array[T]]
252+
extension [T](arr: IArray[T])
253+
/** Returns a mutable copy of this immutable array. */
254+
@deprecated("This method implementation is incorrect and calling it can crash your program, please use `IArray.genericWrapArray(myIArray).toArray` instead.", "3.0.1")
255+
def toArray: Array[T] =
256+
arr.clone.asInstanceOf[Array[T]]
255257

256258
extension [T](arr: IArray[T])
257259
def ++[U >: T: ClassTag](suffix: IArray[U]): IArray[U] = genericArrayOps(arr) ++ suffix.toSeq
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 // error: deprecated
5+
c(0) = ""

tests/run/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 = IArray.genericWrapArray(b).toArray
5+
c(0) = ""

0 commit comments

Comments
 (0)