Skip to content

IArray.toArray is potentially unsafe (ArrayStoreException, ClassCastException) #12597

Closed
@xuwei-k

Description

@xuwei-k

Compiler version

3.0.0

Minimized code

scala> val a: IArray[Some[Int]] = IArray(Some(1))
val a: IArray[Some[Int]] = Array(Some(1))

scala> val b: IArray[Option[Int]] = a
val b: IArray[Option[Int]] = Array(Some(1))

scala> val c = b.toArray
val c: Array[Option[Int]] = Array(Some(1))

scala> c.getClass                                                                                                                 
val res0: Class[? <: Array[Option[Int]]] = class [Lscala.Some;

scala> c(0) = None
java.lang.ArrayStoreException: scala.None$
  ... 28 elided

Output

Expectation

b.toArray should return Array[Option[?]] instead of Array[Some[?]] ? 🤔
Is this by design or bug 🤔

https://github.com/lampepfl/dotty/blob/release-3.0.0/library/src/scala/IArray.scala#L252-L254

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions