@@ -4,46 +4,46 @@ class VCString(val x: String) extends AnyVal
44class LazyNullable (a : => Int ) {
55 lazy val l0 = a // null out a
66
7- private val b = " B"
7+ private [ this ] val b = " B"
88 lazy val l1 = b // null out b
99
10- private val c = " C"
10+ private [ this ] val c = " C"
1111 @ volatile lazy val l2 = c // null out c
1212
13- private val d = " D"
13+ private [ this ] val d = " D"
1414 lazy val l3 = d + d // null out d (Scalac require single use?)
1515}
1616
1717object LazyNullable2 {
18- private val a = " A"
18+ private [ this ] val a = " A"
1919 lazy val l0 = a // null out a
2020}
2121
2222class LazyNotNullable {
23- private val a = 'A' .toInt // not nullable type
23+ private [ this ] val a = 'A' .toInt // not nullable type
2424 lazy val l0 = a
2525
26- private val b = new VCInt ('B' .toInt) // not nullable type
26+ private [ this ] val b = new VCInt ('B' .toInt) // not nullable type
2727 lazy val l1 = b
2828
29- private val c = new VCString (" C" ) // should be nullable but is not??
29+ private [ this ] val c = new VCString (" C" ) // should be nullable but is not??
3030 lazy val l2 = c
3131
32- private lazy val d = " D" // not nullable because lazy
32+ private [ this ] lazy val d = " D" // not nullable because lazy
3333 lazy val l3 = d
3434
35- val e = " E" // not nullable because not private
35+ private val e = " E" // not nullable because not private[this]
3636 lazy val l4 = e
3737
38- private val f = " F" // not nullable because used in mutiple lazy vals
38+ private [ this ] val f = " F" // not nullable because used in mutiple lazy vals
3939 lazy val l5 = f
4040 lazy val l6 = f
4141
42- private val g = " G" // not nullable because used outside a lazy val initializer
42+ private [ this ] val g = " G" // not nullable because used outside a lazy val initializer
4343 def foo = g
4444 lazy val l7 = g
4545
46- private val h = " H" // not nullable because field and lazy val not defined in the same class
46+ private [ this ] val h = " H" // not nullable because field and lazy val not defined in the same class
4747 class Inner {
4848 lazy val l8 = h
4949 }
@@ -54,6 +54,13 @@ trait LazyTrait {
5454 lazy val l0 = a
5555}
5656
57+ class Foo (val x : String )
58+
59+ class LazyNotNullable2 (x : String ) extends Foo (x) {
60+ lazy val y = x // not nullable. Here x is super.x
61+ }
62+
63+
5764object Test {
5865 def main (args : Array [String ]): Unit = {
5966 nullableTests()
@@ -115,11 +122,15 @@ object Test {
115122
116123 val inner = new lz.Inner
117124 assert(inner.l8 == " H" )
118- assertNotNull(" h" )
125+ assertNotNull(" LazyNotNullable$$ h" ) // fragile: test will break if compiler generated names change
119126
120127 val fromTrait = new LazyTrait {}
121128 assert(fromTrait.l0 == " A" )
122129 assert(readField(" LazyTrait$$a" , fromTrait) != null ) // fragile: test will break if compiler generated names change
130+
131+ val lz2 = new LazyNotNullable2 (" Hello" )
132+ assert(lz2.y == " Hello" )
133+ assert(lz2.x == " Hello" )
123134 }
124135
125136 def readField (fieldName : String , target : Any ): Any = {
0 commit comments