Skip to content

Commit

Permalink
fix #18282: consider Predef.eq/ne in nullability flow typing (#18299)
Browse files Browse the repository at this point in the history
Fixes #18282
  • Loading branch information
odersky authored Jul 28, 2023
2 parents 16de9eb + a38f11d commit b9b1314
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
11 changes: 11 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Nullables.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ object Nullables:
testSym(tree.symbol, l)
case Apply(Select(Literal(Constant(null)), _), r :: Nil) =>
testSym(tree.symbol, r)
case Apply(Apply(op, l :: Nil), Literal(Constant(null)) :: Nil) =>
testPredefSym(op.symbol, l)
case Apply(Apply(op, Literal(Constant(null)) :: Nil), r :: Nil) =>
testPredefSym(op.symbol, r)
case _ =>
None

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

private def testPredefSym(opSym: Symbol, operand: Tree)(using Context) =
if opSym.owner == defn.ScalaPredefModuleClass then
if opSym.name == nme.eq then Some((operand, true))
else if opSym.name == nme.ne then Some((operand, false))
else None
else None

end CompareNull

/** An extractor for null-trackable references */
Expand Down
7 changes: 7 additions & 0 deletions tests/explicit-nulls/pos/flow-predef-eq.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def f(s: String|Null): String = {
if(s eq null) "foo" else s
}

def f2(s: String|Null): String = {
if(s ne null) s else "foo"
}

0 comments on commit b9b1314

Please sign in to comment.