Skip to content

Array parameterized with match type causes ClassCastException at runtime #15618

Closed
@sbrunk

Description

@sbrunk

It looks like the array is instantiated as Array[Any] / Object[], even though i.e. ScalaType[Float32] resolves to Float.

Compiler version

3.1.3

Minimized code

sealed abstract class DType
sealed class Float16 extends DType
sealed class Float32 extends DType
sealed class Int32 extends DType

object Float16 extends Float16
object Float32 extends Float32
object Int32 extends Int32

type ScalaType[U <: DType] <: Int | Float = U match
  case Float16 => Float
  case Float32 => Float
  case Int32 => Int

class Tensor[T <: DType](dtype: T):
  def toSeq: Seq[ScalaType[T]] = Seq()
  def toArray: Array[ScalaType[T]] = Array()

@main
def main = 
  val t = Tensor(Float32) // Tensor[Float32]
  println(t.toSeq.headOption) // works, Seq[Float]
  println(t.toArray.headOption) // ClassCastException

Output

java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [F ([Ljava.lang.Object; and [F are in module java.base of loader 'bootstrap')

Expectation

toArray should return an Array[Float]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions