Skip to content

StackOverflowError when summoning a Mirror.Of within Companion object nested in method #16873

Open
@zarthross

Description

@zarthross

Compiler version

3.x.x, Tested specifically 3.0.0 and 3.2.2 (via Scastie) and saw the same behavior.

Minimized code

Scastie extended reproduction

import scala.deriving.Mirror

object Main extends App {
  def badWrapper() = {
    case class Baz(i: Int)
    object Baz { // It seems to have to do with the companion object
      val forceInit = 3
      summon[Mirror.Of[Baz]] // This causes a stack overflow of the init...
    }
    Baz.forceInit
  }

  badWrapper()
}

Output

 scala bug2.scala
Exception in thread "main" java.lang.StackOverflowError
        at Main$.Main$$$_$Baz$2(bug2.scala:6)
        at Main$Baz$3$.<init>(bug2.scala:8)
        at Main$.Baz$lzyINIT1$1(bug2.scala:6)
        at Main$.Main$$$_$Baz$2(bug2.scala:6)
        at Main$Baz$3$.<init>(bug2.scala:8)
        at Main$.Baz$lzyINIT1$1(bug2.scala:6)
        at Main$.Main$$$_$Baz$2(bug2.scala:6)

Expectation

I would not expect a stack overflow at runtime in this situation. I would expect the summon to occur, and the number 3 to be returned.

The extended example I provided in the scastie link shows that this only seems to occur if the mirror summon happens in a companion object within a method. If you do it in a singleton its fine, or some other case classes' companion object.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions