Closed
Description
Compiler version
3.0.0 and 3.0.2-RC1-bin-20210622-a059dbc-NIGHTLY
Minimized code
case class Normal(value: String)
object Normal
case class ClassWithCaseCompanion(value: String)
case object ClassWithCaseCompanion
def instantiate[T](product: Product)(implicit mirror: scala.deriving.Mirror.ProductOf[T]) =
mirror.fromProduct(product)
@main def test() = {
assert(instantiate[Normal](Tuple1("a")) == Normal("a")) // works as expected
assert(instantiate[ClassWithCaseCompanion.type](EmptyTuple) == ClassWithCaseCompanion) // works as expected
val c = instantiate[ClassWithCaseCompanion](Tuple1("b")) // throws java.lang.ClassCastException: class ClassWithCaseCompanion$ cannot be cast to class ClassWithCaseCompanion
assert(c == ClassWithCaseCompanion("b")) // desired behaviour
}
Expectation
The Mirror.Product[ClassWithCaseCompanion]
synthesised by the compiler should correctly instantiate the case class it claims to mirror, not its companion object. It seems the compiler gets confused when the companion object is a case object.