Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
xuwei-k opened this issue May 25, 2021 · 3 comments · Fixed by #12598
Closed

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

xuwei-k opened this issue May 25, 2021 · 3 comments · Fixed by #12598
Milestone

Comments

@xuwei-k
Copy link
Contributor

xuwei-k commented May 25, 2021

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

@xuwei-k
Copy link
Contributor Author

xuwei-k commented May 25, 2021

Another example

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

scala> val b: IArray[Any] = a
val b: IArray[Any] = Array(2)

scala> b.toArray                                                                                                                  
java.lang.ClassCastException: [I cannot be cast to [Ljava.lang.Object;

@xuwei-k xuwei-k changed the title IArray.toArray is potentially unsafe (ArrayStoreException) IArray.toArray is potentially unsafe (ArrayStoreException, ClassCastException) May 25, 2021
@smarter
Copy link
Member

smarter commented May 25, 2021

We need to return Array[? <: T], thankfully that should be binary-compatible since they both erase to Object here.

@smarter smarter added this to the 3.0.1-RC1 milestone May 25, 2021
@xuwei-k
Copy link
Contributor Author

xuwei-k commented May 26, 2021

#12598

@smarter smarter linked a pull request May 26, 2021 that will close this issue
@smarter smarter closed this as completed May 26, 2021
@Kordyjan Kordyjan modified the milestones: 3.0.1-RC1, 3.0.1 Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants