Skip to content

Allow Init Checker to look within anonymous classes #10549

Closed
@bishabosha

Description

@bishabosha
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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions