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

method is declared as erased, but is in fact used #374

Open
adamw opened this issue Jan 24, 2022 · 2 comments
Open

method is declared as erased, but is in fact used #374

adamw opened this issue Jan 24, 2022 · 2 comments
Labels

Comments

@adamw
Copy link
Member

adamw commented Jan 24, 2022

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
Copy link
Member Author

adamw commented Jan 25, 2022

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 ?

@KacperFKorban
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants