Skip to content

-Ysafe-init-global false postive with nested private object #24350

@eejbyfeldt

Description

@eejbyfeldt

Compiler version

3.7.3, 3.8.0-RC1-bin-20251106-b6d9a6d-NIGHTLY

Minimized code

//> using options -Ysafe-init-global

object Outer {
  val y = 1
  private object Inner {
    val x = y + 42
  }
  val z = Inner.x
}

Output

[warn] ./circular.scala:7:11
[warn] Access uninitialized field value x. Calling trace:
[warn] ├── object Outer {	[ circular.scala:2 ]
[warn]    ^
[warn] └── val z = Inner.x	[ circular.scala:7 ]
[warn]             ^^^^^^^
[warn]   val z = Inner.x
[warn]           ^^^^^^^
[warn] ./circular.scala:4:18
[warn] Cyclic initialization: object Inner -> object Outer -> object Inner. Calling trace:
[warn] ├── private object Inner {	[ circular.scala:4 ]
[warn]    ^
[warn] ├── val x = y + 42	[ circular.scala:5 ]
[warn]            ^
[warn] ├── object Outer {	[ circular.scala:2 ]
[warn]    ^
[warn] └── val z = Inner.x	[ circular.scala:7 ]
[warn]             ^^^^^
[warn]   private object Inner {
[warn]  

Expectation

No warning. My understanding is that because Inner is private it should not be possible to trigger the cyclic initialization since it only happens if Inner is initialized first.

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