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

Allow Init Checker to look within anonymous classes #10549

Closed
bishabosha opened this issue Sep 2, 2020 · 2 comments · Fixed by #12285
Closed

Allow Init Checker to look within anonymous classes #10549

bishabosha opened this issue Sep 2, 2020 · 2 comments · Fixed by #12285

Comments

@bishabosha
Copy link
Member

class Wrap {
  class E
  object E {
    final val A = new E {}
    val $values = Array(A)
  }
}

produces warning with -Ycheck-init

-- 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

when it could look inside to prove it is safe.

One example that is not safe and will lead to NPE in initialisation of object E:

class Wrap {                                                                                                     
  def qux[T](e: E[T]) = e.foo

  abstract class E[+T] { def foo: T }
  object E {
    final val A: E[Nothing] = new E { def foo = ref }
    val ref = qux(A)
  }
}

as pointed out in #9665 (comment)

if ref is made lazy then there is no warning on Array(A) and object E can be constructed correctly.

@propensive
Copy link
Contributor

This issue is referenced in a comment here: https://github.com/lampepfl/dotty/blob/4a6ec60f13be412e11bb81b3c3028cc47dc1c2ec/compiler/src/dotty/tools/dotc/ast/DesugarEnums.scala#L122-L127

Given that this issue is closed, it might be possible to address the comment now.

@liufengyun
Copy link
Contributor

This issue will be fixed by #11533. @natsukagami Could you add it as a test:

  • tests/init/pos/i10549a.scala
  • tests/init/neg/i10549b.scala

liufengyun added a commit to dotty-staging/dotty that referenced this issue Apr 29, 2021
michelou pushed a commit to michelou/dotty that referenced this issue May 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment