question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

given instance is declared as erased, but is in fact used

See original GitHub issue

Compiler version

3.0.1

Minimized code

import scala.deriving.Mirror
import scala.compiletime._

trait Schema[T] {
  def build: T
}

object Schema extends SchemaDerivation {
  implicit lazy val int: Schema[Int]   = ???
  implicit def option[A](implicit ev: Schema[A]): Schema[Option[A]] = ???
}

trait SchemaDerivation {
  inline def recurse[A <: Tuple]: List[Schema[Any]] =
    inline erasedValue[A] match {
      case _: (t *: ts) =>
        val builder = summonInline[Schema[t]].asInstanceOf[Schema[Any]]
        builder :: recurse[ts]
      case _: EmptyTuple => Nil
    }

  inline def derived[A]: Schema[A] =
    inline summonInline[Mirror.Of[A]] match {
      case m: Mirror.SumOf[A] =>
        lazy val subTypes = recurse[m.MirroredElemTypes]
        new Schema[A] {
          def build: A = ???
        }

      case m: Mirror.ProductOf[A] =>
        lazy val fields = recurse[m.MirroredElemTypes]
        new Schema[A] {
          def build: A = ???
        }
    }

  inline given gen[A]: Schema[A] = derived
}

case class H(i: Int)
case class G(h: H)
case class F(g: G)
case class E(f: Option[F])
case class D(e: E)
case class C(d: D)
case class B(c: C)
case class A(a: Option[A], b: B)

object TestApp {
  @main def test = {
    implicit lazy val typeSchema: Schema[A] = Schema.gen
  }
}

Output

given instance gen is declared as erased, but is in fact used
method recurse is declared as erased, but is in fact used

Expectation

I got this error while upgrading caliban from 3.0.0 to 3.0.1 on some code that was previously compiling. It seems that it fails to derive a typeclass instance in the presence of recursion and nesting. If I reduce the nesting, it works. I managed to reproduce it with the smaller example above, but this code fails with 3.0.0 with a different error. Any idea what’s going on?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
adamwcommented, Jan 25, 2022

We’ve hit this problem in tapir (https://github.com/softwaremill/tapir/issues/1731), and minimized a bit in magnolia (https://github.com/softwaremill/magnolia/issues/374). While bumping the max-inline does solve the problem, I think it’s not really resolved - we either have to come up with a better solution in magnolia (suggestions welcome 😃 - one idea we have is to replace depending on Mirrors with macros, but if at all, this might only reduce the inlining height, not eliminate it completely) or maybe bump the limit in Scala?

The case classes involved aren’t that complicated, so I suspect this might surface once and again when using typeclass derivation (esp using Magnolia) in real-world scenarios, which get much more complex.

Finally, the error is quite misleading. Maybe it would make sense to open another issue to make this more actionable (sth like “Max-inline limit reach. Increase using max-inlines to continue”? Would that be technically possible to detect?

1reaction
dwijnandcommented, Aug 27, 2021

Also a bit annoying for build.sbt that we can’t use -Xmax-inlines:33 or -Xmax-inlines=33

Coming in 3.1.0: https://github.com/lampepfl/dotty/pull/13267

Read more comments on GitHub >

github_iconTop Results From Across the Web

given instance is declared as erased, but is in fact used #13785
Compiler version 3.1.0 Problem This is a follow-up to #13044. There are few occurrences where I observe the error given instance is declared...
Read more >
Compilation error when using an inline method inside a non ...
method refineValue is declared as erased, but is in fact used. How can I work around this error ? Note: it works without...
Read more >
Erased Definitions - Scala 3 - EPFL
How to define erased terms? Parameters of methods and functions can be declared as erased, placing erased in front of a parameter list...
Read more >
Scala 3: “Erased” Definitions - Medium
The defined given instances constrain what's allowed by the compiler. This has been a common idiom, to limit what's allowed to compile, implemented...
Read more >
Why is generic of a return type erased when there is an ...
15.12.2.6. Method Result and Throws Types · If the chosen method is declared with a return type of void, then the result is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found