File tree 2 files changed +15
-2
lines changed
compiler/src/dotty/tools/dotc/transform
2 files changed +15
-2
lines changed Original file line number Diff line number Diff line change @@ -33,12 +33,12 @@ object CollectNullableFields {
33
33
* initialised.
34
34
*
35
35
* A field is nullable if all the conditions below hold:
36
+ * - belongs to a non trait-class
36
37
* - is private
37
38
* - is not lazy
38
39
* - its type is nullable, or is an expression type (e.g. => Int)
39
40
* - is on used in a lazy val initializer
40
41
* - defined in the same class as the lazy val
41
- * - TODO from Scalac? from a non-trait class
42
42
*/
43
43
class CollectNullableFields extends MiniPhase {
44
44
import tpd ._
@@ -63,7 +63,11 @@ class CollectNullableFields extends MiniPhase {
63
63
def isNullableType (tpe : Type ) =
64
64
tpe.isInstanceOf [ExprType ] ||
65
65
tpe.widenDealias.typeSymbol.isNullableClass
66
- val isNullablePrivateField = sym.isField && sym.is(Private , butNot = Lazy ) && isNullableType(sym.info)
66
+ val isNullablePrivateField =
67
+ sym.isField &&
68
+ sym.is(Private , butNot = Lazy ) &&
69
+ ! sym.owner.is(Trait ) &&
70
+ isNullableType(sym.info)
67
71
68
72
if (isNullablePrivateField)
69
73
nullability.get(sym) match {
Original file line number Diff line number Diff line change @@ -49,6 +49,11 @@ class LazyNotNullable {
49
49
}
50
50
}
51
51
52
+ trait LazyTrait {
53
+ private val a = " A"
54
+ lazy val l0 = a
55
+ }
56
+
52
57
object Test {
53
58
def main (args : Array [String ]): Unit = {
54
59
nullableTests()
@@ -111,6 +116,10 @@ object Test {
111
116
val inner = new lz.Inner
112
117
assert(inner.l8 == " H" )
113
118
assertNotNull(" h" )
119
+
120
+ val fromTrait = new LazyTrait {}
121
+ assert(fromTrait.l0 == " A" )
122
+ assert(readField(" LazyTrait$$a" , fromTrait) != null ) // fragile: test will break if compiler generated name change
114
123
}
115
124
116
125
def readField (fieldName : String , target : Any ): Any = {
You can’t perform that action at this time.
0 commit comments