Skip to content
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

Empty WrappedArray.ofRef.array throws ClassCastException #9941

Open
scabug opened this issue Sep 29, 2016 · 4 comments
Open

Empty WrappedArray.ofRef.array throws ClassCastException #9941

scabug opened this issue Sep 29, 2016 · 4 comments

Comments

@scabug
Copy link

scabug commented Sep 29, 2016

.array on an empty WrappedArray$ofRef throws a ClassCastException.

Here is a simple example where calling .array on an Array hits it via an implicit conversion from Array to WrappedArray$ofRef:

scala> case class A(n: Int)
defined class A

scala> val arr: Array[A] = Array()
arr: Array[A] = Array()

scala> arr.array
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [LA;
  ... 32 elided

Here's the above implicit unrolled:

scala> val wrapped: scala.collection.mutable.WrappedArray[A] = arr
wrapped: scala.collection.mutable.WrappedArray[A] = WrappedArray()

scala> wrapped.array
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [LA;
  ... 32 elided

Other, primitive WrappedArray subclasses seem to not have the issue:

scala> val ints: Array[Int] = Array()
ints: Array[Int] = Array()

scala> ints.array
res6: Array[Int] = Array()

I haven't fully pieced together exactly where/why the exception occurs, but it seems unexpected/buggy for Array to have an always-available (via an implicit) .array method that will throw such an inscrutable exception on empty Arrays.

@scabug
Copy link
Author

scabug commented Sep 29, 2016

Imported From: https://issues.scala-lang.org/browse/SI-9941?orig=1
Reporter: Ryan Williams (rdub)
Affected Versions: 2.11.8, 2.12.0-RC1

@scabug scabug added the quickfix label Apr 7, 2017
@scabug scabug added this to the Backlog milestone Apr 7, 2017
@eugengarkusha
Copy link

Additional example(just hit this problem on my project):

the following code

object ArrayIssue {
  def main(args: Array[String]): Unit ={
    // inferred type of k is Array[_ <: String]
    val k = if (true )Array.empty else Array.empty[String]
    println(k)
  }
}

throws
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

the main method decompiles to:

    {
        String k[] = (String[])Array$.MODULE$.empty(ClassTag$.MODULE$.Nothing());
        Predef$.MODULE$.println(k);
    }

reproducible on 2.13: https://scastie.scala-lang.org/wvg8VYr2QNSa11r8WmIDFQ

@SethTisue
Copy link
Member

not fixed in Scala 3 (as of 3.3.2-RC1-bin-20230613-d83aa49-NIGHTLY)

@som-snytt
Copy link

The improvement

Welcome to Scala 2.13.11 (OpenJDK 64-Bit Server VM, Java 20.0.1).
Type in expressions for evaluation. Or try :help.

scala> def f(b: Boolean) = if (b) Array.empty else Array("hello, world")
def f(b: Boolean): Array[_ <: String]

scala> f(true)
java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.String; ([Ljava.lang.Object; and [Ljava.lang.String; are in module java.base of loader 'bootstrap')
  at f(<console>:1)
  ... 30 elided

but

Welcome to Scala 3.3.0 (20.0.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> List(Array(42), Array("hello, world"))
val res0: List[Array[? >: Int & String <: Int | String]] = List(Array(42), Array(hello, world))

scala> def f(b: Boolean) = if (b) Array(42) else Array("hello, world")
def f(b: Boolean): Array[? >: Int & String <: Int | String]

scala> f(true)
val res5: Array[? >: Int & String <: Int | String] = Array(42)

scala> f(true).array
val res6: Array[?] = Array(42)

scala> val xs: Array[String] = Array.empty
val xs: Array[String] = Array()

scala> xs.array
java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.String; ([Ljava.lang.Object; and [Ljava.lang.String; are in module java.base of loader 'bootstrap')
  ... 33 elided

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants