circe-derivation icon indicating copy to clipboard operation
circe-derivation copied to clipboard

Derivation for generic case classes fails with additional context bounds

Open vpavkin opened this issue 7 years ago • 5 comments

Hi! Found this issue while trying out circe-derivation 0.8.0-M2:

import io.circe.{Decoder, Encoder}
import io.circe.derivation._

trait Typeclass[T]

// notice additional context bound here
case class Test1[T: Typeclass](v: T)
case class Test2[T](v: T)

object CirceDerivation {
  // we have to pass the bound here.
  implicit def encT1[T: Typeclass : Encoder]: Encoder[Test1[T]] = deriveEncoder[Test1[T]]
  implicit def decT1[T: Typeclass : Decoder]: Decoder[Test1[T]] = deriveDecoder[Test1[T]]

  implicit def encT2[T : Encoder]: Encoder[Test2[T]] = deriveEncoder[Test2[T]]
  implicit def decT2[T : Decoder]: Decoder[Test2[T]] = deriveDecoder[Test2[T]]
}

For Test2 all good. For Test1 derivation fails with this (for both encoder and decoder):

exception during macro expansion:
[error] scala.ScalaReflectionException: value evidence$1 is not a method
[error] 	at scala.reflect.api.Symbols$SymbolApi.asMethod(Symbols.scala:228)
[error] 	at scala.reflect.api.Symbols$SymbolApi.asMethod$(Symbols.scala:222)
[error] 	at scala.reflect.internal.Symbols$SymbolContextApiImpl.asMethod(Symbols.scala:88)
[error] 	at io.circe.derivation.DerivationMacros$$anonfun$membersFromPrimaryConstr$1.$anonfun$applyOrElse$1(DerivationMacros.scala:45)
[error] 	at scala.collection.immutable.List.map(List.scala:287)
[error] 	at io.circe.derivation.DerivationMacros$$anonfun$membersFromPrimaryConstr$1.applyOrElse(DerivationMacros.scala:44)
[error] 	at io.circe.derivation.DerivationMacros$$anonfun$membersFromPrimaryConstr$1.applyOrElse(DerivationMacros.scala:43)
[error] 	at scala.collection.TraversableOnce.collectFirst(TraversableOnce.scala:145)
[error] 	at scala.collection.TraversableOnce.collectFirst$(TraversableOnce.scala:132)
[error] 	at scala.reflect.internal.Scopes$Scope.collectFirst(Scopes.scala:51)
[error] 	at io.circe.derivation.DerivationMacros.membersFromPrimaryConstr(DerivationMacros.scala:43)
[error] 	at io.circe.derivation.DerivationMacros.productRepr(DerivationMacros.scala:52)
[error] 	at io.circe.derivation.DerivationMacros.materializeEncoderImpl(DerivationMacros.scala:221)
[error] 	at io.circe.derivation.DerivationMacros.materializeEncoder(DerivationMacros.scala:87)
[error]   implicit def encT1[T: Typeclass : Encoder]: Encoder[Test1[T]] = deriveEncoder[Test1[T]]

vpavkin avatar Jun 08 '17 07:06 vpavkin

ordering of context bounds does not matter: macro fails anyway.

vpavkin avatar Jun 08 '17 07:06 vpavkin

@vpavkin Good catch, thanks for reporting! I'll take a look.

travisbrown avatar Jun 08 '17 11:06 travisbrown

@travisbrown This issue is still alive in 0.9.0-M3, right? For the time being I have to keep a semiauto usage because the class in question has a [T : ClassTag] bound.

hibikir avatar Apr 25 '18 04:04 hibikir

ordering of context bounds does not matter: macro fails anyway.

Will you please kindly elaborate this a bit more for understanding purposes?

Note: I am new to scala.

aukgit avatar May 06 '20 23:05 aukgit

Is there a hope that it can be fixed someday?

mikla avatar Feb 13 '21 22:02 mikla