Skip to content

Commit 4216400

Browse files
committed
Nullables refactorings
1 parent 575df47 commit 4216400

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

compiler/src/dotty/tools/dotc/typer/Nullables.scala

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package typer
55
import core._
66
import Types._, Contexts._, Symbols._, Decorators._, Constants._
77
import annotation.{tailrec, infix}
8+
import StdNames.nme
89
import util.Property
910

1011
/** Operations for implementing a flow analysis for nullability */
@@ -17,7 +18,7 @@ object Nullables with
1718
case class NotNullInfo(asserted: Set[TermRef], retracted: Set[TermRef])
1819
assert((asserted & retracted).isEmpty)
1920

20-
def isEmpty = this eq noNotNulls
21+
def isEmpty = this eq NotNullInfo.empty
2122

2223
/** The sequential combination with another not-null info */
2324
@infix def seq(that: NotNullInfo): NotNullInfo =
@@ -28,25 +29,23 @@ object Nullables with
2829
this.retracted.union(that.retracted).diff(that.asserted))
2930

3031
object NotNullInfo with
32+
val empty = new NotNullInfo(Set(), Set())
3133
def apply(asserted: Set[TermRef], retracted: Set[TermRef]): NotNullInfo =
32-
if asserted.isEmpty && retracted.isEmpty then noNotNulls
34+
if asserted.isEmpty && retracted.isEmpty then empty
3335
else new NotNullInfo(asserted, retracted)
3436
end NotNullInfo
3537

36-
val noNotNulls = new NotNullInfo(Set(), Set())
37-
3838
/** A pair of not-null sets, depending on whether a condition is `true` or `false` */
3939
case class NotNullConditional(ifTrue: Set[TermRef], ifFalse: Set[TermRef]) with
40-
def isEmpty = this eq neitherNotNull
40+
def isEmpty = this eq NotNullConditional.empty
4141

4242
object NotNullConditional with
43+
val empty = new NotNullConditional(Set(), Set())
4344
def apply(ifTrue: Set[TermRef], ifFalse: Set[TermRef]): NotNullConditional =
44-
if ifTrue.isEmpty && ifFalse.isEmpty then neitherNotNull
45+
if ifTrue.isEmpty && ifFalse.isEmpty then empty
4546
else new NotNullConditional(ifTrue, ifFalse)
4647
end NotNullConditional
4748

48-
val neitherNotNull = new NotNullConditional(Set(), Set())
49-
5049
/** An attachment that represents conditional flow facts established
5150
* by this tree, which represents a condition.
5251
*/
@@ -117,8 +116,9 @@ object Nullables with
117116
false
118117

119118
def extendWith(info: NotNullInfo) =
120-
if info.asserted.forall(infos.containsRef(_))
121-
&& !info.retracted.exists(infos.containsRef(_))
119+
if info.isEmpty
120+
|| info.asserted.forall(infos.containsRef(_))
121+
&& !info.retracted.exists(infos.containsRef(_))
122122
then infos
123123
else info :: infos
124124

@@ -135,7 +135,7 @@ object Nullables with
135135
def notNullInfo(given Context): NotNullInfo =
136136
stripInlined(tree).getAttachment(NNInfo) match
137137
case Some(info) if !curCtx.erasedTypes => info
138-
case _ => noNotNulls
138+
case _ => NotNullInfo.empty
139139

140140
/** The paths that are known to be not null if the condition represented
141141
* by `tree` yields `true` or `false`. Two empty sets if `tree` is not
@@ -144,7 +144,7 @@ object Nullables with
144144
def notNullConditional(given Context): NotNullConditional =
145145
stripBlock(tree).getAttachment(NNConditional) match
146146
case Some(cond) if !curCtx.erasedTypes => cond
147-
case _ => neitherNotNull
147+
case _ => NotNullConditional.empty
148148

149149
/** The current context augmented with nullability information of `tree` */
150150
def nullableContext(given Context): Context =
@@ -181,7 +181,7 @@ object Nullables with
181181
def computeNullable()(given Context): tree.type =
182182
def setConditional(ifTrue: Set[TermRef], ifFalse: Set[TermRef]) =
183183
tree.putAttachment(NNConditional, NotNullConditional(ifTrue, ifFalse))
184-
if !curCtx.erasedTypes then
184+
if !curCtx.erasedTypes && analyzedOps.contains(tree.symbol.name.toTermName) then
185185
tree match
186186
case CompareNull(TrackedRef(ref), testEqual) =>
187187
if testEqual then setConditional(Set(), Set(ref))
@@ -209,4 +209,6 @@ object Nullables with
209209
tree.computeNullable()
210210
}.traverse(tree)
211211

212+
private val analyzedOps = Set(nme.EQ, nme.NE, nme.eq, nme.ne, nme.ZAND, nme.ZOR, nme.UNARY_!)
213+
212214
end Nullables

0 commit comments

Comments
 (0)