Skip to content

Commit b9b1314

Browse files
authored
fix #18282: consider Predef.eq/ne in nullability flow typing (#18299)
Fixes #18282
2 parents 16de9eb + a38f11d commit b9b1314

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ object Nullables:
115115
testSym(tree.symbol, l)
116116
case Apply(Select(Literal(Constant(null)), _), r :: Nil) =>
117117
testSym(tree.symbol, r)
118+
case Apply(Apply(op, l :: Nil), Literal(Constant(null)) :: Nil) =>
119+
testPredefSym(op.symbol, l)
120+
case Apply(Apply(op, Literal(Constant(null)) :: Nil), r :: Nil) =>
121+
testPredefSym(op.symbol, r)
118122
case _ =>
119123
None
120124

@@ -123,6 +127,13 @@ object Nullables:
123127
else if sym == defn.Any_!= || sym == defn.Object_ne then Some((operand, false))
124128
else None
125129

130+
private def testPredefSym(opSym: Symbol, operand: Tree)(using Context) =
131+
if opSym.owner == defn.ScalaPredefModuleClass then
132+
if opSym.name == nme.eq then Some((operand, true))
133+
else if opSym.name == nme.ne then Some((operand, false))
134+
else None
135+
else None
136+
126137
end CompareNull
127138

128139
/** An extractor for null-trackable references */
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def f(s: String|Null): String = {
2+
if(s eq null) "foo" else s
3+
}
4+
5+
def f2(s: String|Null): String = {
6+
if(s ne null) s else "foo"
7+
}

0 commit comments

Comments
 (0)