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

ClassCastException when trying to use Mirror.Of[T] when T contains a Tuple #11961

Closed
VlachJosef opened this issue Mar 31, 2021 · 1 comment · Fixed by #12062
Closed

ClassCastException when trying to use Mirror.Of[T] when T contains a Tuple #11961

VlachJosef opened this issue Mar 31, 2021 · 1 comment · Fixed by #12062
Milestone

Comments

@VlachJosef
Copy link

Compiler version

3.0.0-RC2
sbt 1.4.9 (GraalVM Community Java 1.8.0_282)

Minimized code

import scala.deriving.*
import scala.compiletime.{erasedValue, summonInline}

case class Simple(a: String, b: Boolean) derives Printable
case class SimpleT(a: (String, Boolean)) derives Printable

@main def hello: Unit = {

  summon[Printable[Simple]].print // Prints STRING BOOLEAN as expected

  summon[Printable[SimpleT]].print // java.lang.ClassCastException: SimpleT$$anon$1 cannot be cast to scala.deriving.Mirror$Product

}

trait Printable[T]:
   def print: Unit

object Printable:

   given Printable[String] with
      def print: Unit = println("STRING")

   given Printable[Boolean] with
      def print: Unit = println("BOOLEAN")

   def printProduct[T](p: Mirror.ProductOf[T], elems: => List[Printable[_]]): Printable[T] =
      new Printable[T]:
         def print: Unit =
            elems.foreach(_.print)

   inline given derived[T](using m: Mirror.Of[T]): Printable[T] =
      val elemInstances = summonAllPrintable[m.MirroredElemTypes]
      inline m match
         case p: Mirror.ProductOf[T] => printProduct(p, elemInstances)

end Printable

inline def summonAllPrintable[T <: Tuple]: List[Printable[_]] =
   inline erasedValue[T] match
      case _: EmptyTuple => Nil
      case _: (t *: ts) => summonInline[Printable[t]] :: summonAllPrintable[ts]

Output

> run
[info] running hello 
STRING
BOOLEAN
[error] (run-main-0) java.lang.ClassCastException: SimpleT$$anon$1 cannot be cast to scala.deriving.Mirror$Product
[error] java.lang.ClassCastException: SimpleT$$anon$1 cannot be cast to scala.deriving.Mirror$Product
[error] 	at SimpleT$.derived$Printable(Main.scala:5)
[error] 	at Main$package$.hello(Main.scala:11)
[error] 	at hello.main(Main.scala:7)
[error] 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error] 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error] 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error] 	at java.lang.reflect.Method.invoke(Method.java:498)
[error] stack trace is suppressed; run last Compile / bgRun for the full output
[error] Nonzero exit code: 1
[error] (Compile / run) Nonzero exit code: 1
[error] Total time: 3 s, completed Mar 31, 2021 9:16:29 PM

Expectation

I'd expect this program to produce compilation error about missing Printable[(String, Boolean)] or similar.

Trying to get familiar with type-classes-supporting-automatic-deriving I'm trying to implement typeclass which prints its own type.

Minimized project is available here.

@bishabosha
Copy link
Member

Hi, thank you for opening this issue, I believe that this is the same issue as #11542 so I will close

odersky added a commit to dotty-staging/dotty that referenced this issue Apr 12, 2021
Mirror support runs in PostTyper to add new members to mirrors generated
during Typer. But some anonymous mirrors are generated during inlining.
We need to add the missing methods for them as well.

Fixes scala#11542
Fixes scala#11961
Fixes scala#12052
odersky added a commit to dotty-staging/dotty that referenced this issue Apr 13, 2021
Mirror support runs in PostTyper to add new members to mirrors generated
during Typer. But some anonymous mirrors are generated during inlining.
We need to add the missing methods for them as well.

Fixes scala#11542
Fixes scala#11961
Fixes scala#12052
michelou pushed a commit to michelou/dotty that referenced this issue Apr 14, 2021
Mirror support runs in PostTyper to add new members to mirrors generated
during Typer. But some anonymous mirrors are generated during inlining.
We need to add the missing methods for them as well.

Fixes scala#11542
Fixes scala#11961
Fixes scala#12052
@Kordyjan Kordyjan added this to the 3.0.1 milestone 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