Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #18282: consider Predef.eq/ne in nullability flow typing #18299

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}