Skip to content

derivation: capturing mirror and nested compile-time summoning leads to ClassCastException at runtime #11542

Closed
@jodersky

Description

@jodersky

Compiler version

3.0.0-RC1

Minimized code

object demo {

  trait Reader[A]

  given Reader[Int] with {}

  inline def summonReader[T <: Tuple]: List[Reader[_]] = inline compiletime.erasedValue[T] match {
    case _: EmptyTuple => Nil
    case _: (t *: ts) => compiletime.summonInline[Reader[t]] :: summonReader[ts]
  }

  class CombinedReader[A](
    m: deriving.Mirror.ProductOf[A],
    childReaders: List[Reader[_]]
  ) extends Reader[A]

  inline given [A <: Tuple](using m: deriving.Mirror.ProductOf[A]): Reader[A] = {
    new CombinedReader(m, summonReader[m.MirroredElemTypes])
  }

}

@main def run() = {
  // OK
  //summon[demo.Reader[(Int, Int, Int)]]

  // Exception in thread "main" java.lang.ClassCastException: class main$package$$anon$2 cannot be cast to class scala.deriving.Mirror$Product (main$package$$anon$2 and scala.deriving.Mirror$Product are in unnamed module of loader 'app')
  //      at main$package$.run(main.scala:25)
  //      at run.main(main.scala:23)
  summon[demo.Reader[(Int, (Int, Int))]]
}

Output

The program compiles, but encounters a ClassCastException at runtime, as shown in the comment above.

Expectation

There should be no exception.

Notes

The class cast exception only happens if the mirror is captured and there is a nested summoning. If, for example, CombinedReader would not use take m or childReader arguments, the program works as expected.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions