Inlining stack overflow

Not sure if this is a bug in scalac compiler or in Magnolia, but wanted to share.

I am using a slightly modified version of Magnolia 2, (commit from my PR here)
I doubt it matters in this case.

This is my typeclass derivation repo: tupson/JsonRW.scala at main · sake92/tupson · GitHub
Nothing too scary, just about 100 lines of derivation code.

This is a minimized example:

import ba.sake.tupson.*

sealed trait Statement derives JsonRW

object Statement:

  case class Begin() extends Statement
  case class Return() extends Statement

  case class Block(
      id: String,
      statements: List[Statement]
  ) extends Statement
  // derives JsonRW
  // COMPILES if uncommented

  case class If(
      id: String,
      condition: String,
      trueBlock: Block,
      falseBlock: Block
  ) extends Statement

@main def hepek: Unit = {
  val s: Statement = Statement.Block("", List(Statement.Begin()))
  println(s.toJson)
}

Here is stacktrace snippet of error I get:

[info] compiling 1 Scala source to C:\projects\sake\tupson\out\examples\compile.dest\classes ...
[error] -- Error: C:\projects\sake\tupson\examples\src\main\scala\hepek.scala:3:31 -----
[error]   3 |sealed trait Statement derives JsonRW
[error]     |                               ^
[error]     |    method paramsFromMaps is declared as `inline`, but was not inlined
[error]     |
[error]     |    Try increasing `-Xmax-inlines` above 32
[error]     |---------------------------------------------------------------------------
[error]     |Inline stack trace
[error]     |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[error]     |This location contains code that was inlined from impl.scala:145
[error]     |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[error]     |This location contains code that was inlined from impl.scala:145
[error]     |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[error]     |This location contains code that was inlined from impl.scala:145
[error]     |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[error]     |This location contains code that was inlined from impl.scala:145
[error]     |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[error]     |This location contains code that was inlined from impl.scala:145
[error]     |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[error]     |This location contains code that was inlined from impl.scala:145
[error]     |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[error]     |This location contains code that was inlined from impl.scala:145
[error]     |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[error]     |This location contains code that was inlined from impl.scala:145
[error]     |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[error]     |This location contains code that was inlined from impl.scala:145

I am guessing that this is some recursive inline limitation of scala compiler.
But even if I increase the stack size, it still fails. So it might be something else happening here.

It is interesting that if I comment out id: String, in any of these case classes then it compiles…

Did you try increasing the number of inlines that can be done using -Xmax-inlines (see New Compiler Options | Scala 3 Migration Guide | Scala Documentation)?

This often needs to be bumped when doing a lot of derivations. I’d try with 64 and see what happens.

1 Like

Thanks Adam, that works.
But I could swear that I tried with even 128 and it didnt work in my project (GitHub - sacode387/FlowRun: Executable flow diagrams)…
Maybe I was using an older version of compiler or some weird combination of flags… :confused:

Anyway, this works, thank you!

1 Like