Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Members of enums are uninitialized at the time of val evaluation #22254

Open
t9dupuy opened this issue Dec 20, 2024 · 2 comments
Open

Members of enums are uninitialized at the time of val evaluation #22254

t9dupuy opened this issue Dec 20, 2024 · 2 comments

Comments

@t9dupuy
Copy link

t9dupuy commented Dec 20, 2024

Compiler version

3.5.2

Minimized code

enum Color:
  case Red, Green, Blue

  val red = Red

println(Color.Red.red)

Output

null

Expectation

Value should be initialized and print Color.Red

@t9dupuy t9dupuy added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 20, 2024
@Gedochao Gedochao added area:enums and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 20, 2024
@bishabosha
Copy link
Member

bishabosha commented Dec 20, 2024

it does at least warn with -Ysafe-init-global

-- Warning: --------------------------------------------------------------------
4 |  val red = Red
  |            ^^^
  |            Access uninitialized field value Red. Calling trace:
  |            ├── enum Color:	[ rs$line$1:1 ]
  |               ^
  |            ├── case Red, Green, Blue	[ rs$line$1:2 ]
  |                                ^
  |            ├── enum Color:	[ rs$line$1:1 ]
  |               ^
  |            └── val red = Red	[ rs$line$1:4 ]
  |                          ^^^

this isn't really possible to fix, it is exactly like doing this cyclic initialisation:

class Foo:
  val red = Foo.Red // constructor sets to value of uninitialised field

object Foo:
  val Red = Foo() // can't store field until constructor is finished

println(Foo.Red.red)

@Gedochao
Copy link
Contributor

this isn't really possible to fix, it is exactly like doing this cyclic initialisation:

class Foo:
  val red = Foo.Red // constructor sets to value of uninitialised field

object Foo:
  val Red = Foo() // can't store field until constructor is finished

println(Foo.Red.red)

@bishabosha Hmm... If it is indeed a won'tfix, then we should have something better than a warning with a private flag such as -Ysafe-init-global, I think...
Could we just always warn, or even throw an error in this case?
cc @dwijnand @odersky @kasiaMarek

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants