Skip to content

-Ycheck-init promoting value to fully initialised when passed to array #9664

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

Closed
bishabosha opened this issue Aug 28, 2020 · 1 comment · Fixed by #9665
Closed

-Ycheck-init promoting value to fully initialised when passed to array #9664

bishabosha opened this issue Aug 28, 2020 · 1 comment · Fixed by #9665

Comments

@bishabosha
Copy link
Member

bishabosha commented Aug 28, 2020

Minimized code

// compile with -Ycheck-init
object Wrap1 {                                                                              
  class E
  object E {
    final val A = E()
    val $values = Array(A)
  }
}
object Wrap2 {                                                                              
  class E
  object E {
    final val A = E()
    val ref = A
  }
}

Output

-- Warning: initcheck.scala:5:23 ----------------------------------
5 |    val $values = Array(A)
  |                        ^
  |Promoting the value under initialization to fully-initialized. Calling trace:
  | -> val $values = Array(A)	[ initcheck.scala:5 ]
1 warning found

Expectation

I thought that directly referencing the value should have the same behaviour as passing it to an array constructor

liufengyun added a commit to dotty-staging/dotty that referenced this issue Aug 28, 2020
This agrees with theory and is more accurate.
@liufengyun
Copy link
Contributor

This is a bug, because E() returns a fully constructed object in the context of initializing object E. Now it is fixed in #9665 .

I thought that directly referencing the value should have the same behaviour as passing it to an array constructor

There is a subtle difference here. Use a partially constructed value to initialize another field is OK, the fields of the object are not used. In contrast, using a partially constructed object as method argument is dangerous (as non-initialized fields might be accessed elsewhere). As it's not a good initialization practice, the checker issue a warning for such cases.

liufengyun added a commit that referenced this issue Aug 28, 2020
@Kordyjan Kordyjan added this to the 3.0.0 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants