Skip to content

Inaccurate warning with -Ysafe-init with enum and nested object in companion #12544

Closed
@TVerse

Description

@TVerse

Compiler version

Scala 3.0.0 with compiler flag -Ysafe-init.

Minimized code

enum Enum:
  case Case

object Enum:
  object nested:
    val a: Enum = Case

  val b: Enum = f(nested.a)

  def f(e: Enum): Enum = e

@main def main(): Unit = println(Enum.b)

Output

> sbt run
<SNIP>
[warn] -- Warning: <PATH>
[warn] 8 |  val b: Enum = f(nested.a)
[warn]   |                  ^^^^^^^^
[warn]   |Promoting the value under initialization to fully-initialized. Calling trace:
[warn]   | -> val b: Enum = f(nested.a) [ Enum.scala:8 ]
[warn] one warning found
[warn] one warning found
[info] running main
Case

Expectation

No warning. Given that it prints correctly, the value was correctly initialized. I believe there's no real initialization order issue here: when b initializes, nested gets initialized and nested.a with it.

Things that do not cause a warning:

  • Using a case class, Int, or String instead of the enum
  • Renaming the outer object so it's not a companion anymore
  • val a: Enum = ??? (this breaks when printing of course)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions