# Inlining stack overflow

**URL:** <https://softwaremill.community/t/inlining-stack-overflow/121>\
**Category:** magnolia\
**Created:** [February 7, 2023, 1:46pm UTC](https://softwaremill.community/t/inlining-stack-overflow/121 "2023-02-07T13:46:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sake92](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/sake92/32/85_2.png) [@sake92](https://softwaremill.community/u/sake92)\
**Post date:** [February 7, 2023, 1:46pm UTC](https://softwaremill.community/t/inlining-stack-overflow/121/1 "2023-02-07T13:46:05Z")

</div>

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](https://github.com/softwaremill/magnolia/pull/457))  
I doubt it matters in this case.

This is my typeclass derivation repo: [tupson/JsonRW.scala at main · sake92/tupson · GitHub](https://github.com/sake92/tupson/blob/main/tupson/shared/src/main/scala/ba/sake/tupson/JsonRW.scala)  
Nothing too scary, just about 100 lines of derivation code.

This is a minimized example:

```plaintext
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:

```auto
[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…

---

<div class="post-metadata">

**Author:** ![adamw](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/adamw/32/28_2.png) [@adamw](https://softwaremill.community/u/adamw)\
**Post date:** [February 12, 2023, 1:21pm UTC](https://softwaremill.community/t/inlining-stack-overflow/121/2 "2023-02-12T13:21:34Z")

</div>

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](https://docs.scala-lang.org/scala3/guides/migration/options-new.html))?

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

---

<div class="post-metadata">

**Author:** ![sake92](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/sake92/32/85_2.png) [@sake92](https://softwaremill.community/u/sake92)\
**Post date:** [February 13, 2023, 11:46am UTC](https://softwaremill.community/t/inlining-stack-overflow/121/3 "2023-02-13T11:46:01Z")

</div>

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](https://github.com/sacode387/FlowRun))…  
Maybe I was using an older version of compiler or some weird combination of flags… 😕

Anyway, this works, thank you!
