magnolia icon indicating copy to clipboard operation
magnolia copied to clipboard

method is declared as erased, but is in fact used

Open adamw opened this issue 3 years ago • 2 comments

Given the following derivation:

import SchemaType.*
import magnolia1.*
import scala.deriving.Mirror

sealed trait SchemaType[T]

object SchemaType extends Derivation[SchemaType] {
  case class SString[T]() extends SchemaType[T]
  case class SOption[T, E](element: SchemaType[E]) extends SchemaType[T]
  case class SArray[T, E](element: SchemaType[E]) extends SchemaType[T]

  case class SProductField(schema: SchemaType[_])
  case class SProduct[T](fields: List[SProductField]) extends SchemaType[T]

  //

  implicit val schemaForString: SchemaType[String] = SString()

  implicit def schemaForOption[T: SchemaType]: SchemaType[Option[T]] = SOption(implicitly[SchemaType[T]])
  implicit def schemaForArray[T: SchemaType]: SchemaType[Array[T]] = SArray(implicitly[SchemaType[T]])
  implicit def schemaForIterable[T: SchemaType, C[X] <: Iterable[X]]: SchemaType[C[T]] = SArray(implicitly[SchemaType[T]])

  inline implicit def schemaForCaseClass[T](implicit m: Mirror.Of[T]): SchemaType[T] = derived[T]

  //
  type Typeclass[T] = SchemaType[T]

  override def join[T](ctx: CaseClass[SchemaType, T]): SchemaType[T] =
    SProduct(ctx.params.map { p => SProductField(p.typeclass) }.toList)
  override def split[T](ctx: SealedTrait[SchemaType, T]): SchemaType[T] = ???
}

and the following test code:

case class StandardAreaInfo(
    country: String,
    city: String,
    district: String
)

case class TwinNode(
    id: String,
    realSceneID: String,
    parentID: String,
    name: Option[String],
    coverImage: Option[String],
    areaInfo: Option[StandardAreaInfo]
)

case class Anchor(twin: TwinNode)

case class AnchorsResponse(anchors: Option[Anchor])

object Test {
  SchemaType.derived[Anchor]
}

I'm getting:

[error] -- Error: /Users/adamw/projects/magnolia/src/examples/test2.scala:25:2 ---------
[error] 25 |  SchemaType.derived[Anchor]
[error]    |  ^^^^^^^^^^^^^^^^^^^^^^^^^^
[error]    |  method schemaForCaseClass is declared as erased, but is in fact used
[error]    | This location contains code that was inlined from magnolia.scala:83
[error]    | This location contains code that was inlined from magnolia.scala:94
[error]    | This location contains code that was inlined from magnolia.scala:94
[error]    | This location contains code that was inlined from magnolia.scala:94
[error]    | This location contains code that was inlined from magnolia.scala:94
[error]    | This location contains code that was inlined from magnolia.scala:94
[error]    | This location contains code that was inlined from magnolia.scala:16
[error]    | This location contains code that was inlined from magnolia.scala:149
[error]    | This location contains code that was inlined from magnolia.scala:151
[error]    | This location contains code that was inlined from test.scala:31
[error]    | This location contains code that was inlined from magnolia.scala:83
[error]    | This location contains code that was inlined from magnolia.scala:16
[error]    | This location contains code that was inlined from magnolia.scala:149
[error]    | This location contains code that was inlined from magnolia.scala:151

Removing any fields, or e.g. the schemaForArray implicit (which is even more weird) causes this code to compile

adamw avatar Jan 24 '22 17:01 adamw

Adding scalacOptions ++= Seq("-Xmax-inlines", "50") solves the problem, however I'll keep this open as the case classes involved aren't that complicated so this can come up pretty often. Maybe rewriting derivation using macros would help in reducing the inlining depth - what do you think @KacperFKorban ?

adamw avatar Jan 25 '22 11:01 adamw

It might help, though I haven't worked on macrolia for a while. The current implementation works for some cases, but I had problems with implementing isType and asType for objects.

KacperFKorban avatar Jan 26 '22 09:01 KacperFKorban