Skip to content

Incorrect erasure of intersection involving Array leads to ClassCastException #12381

Open
@smarter

Description

@smarter

reproduction steps

using Scala 2.13.5,

object A {
  def foo(x: Array[_ <: Any] with Any): Unit = {}
  def id[T](x: T): T = x

  def main(args: Array[String]): Unit = {
    val ai: Array[Int] = Array(0)

    foo(id(ai))
  }
}

problem

$ scalac A.scala
$ scala A
java.lang.ClassCastException: [I cannot be cast to [Ljava.lang.Object;
        at A$.main(A.scala:8)
        at A.main(A.scala)

The issue is that Array[_ <: Any] with Any gets erased to Object[] instead of Object. This happens because intersectionDominator will return Array[Any] instead of Array[_ <: Any] for this intersection: https://github.com/scala/scala/blob/4ed5e37e1a7f43106bca83746ff292b3a545fcaf/src/reflect/scala/reflect/internal/transform/Erasure.scala#L372-L376

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions