Skip to content

Commit

Permalink
allow nullability flow typing even in presence of pattern match (#18206)
Browse files Browse the repository at this point in the history
Nullability flow typing is conservatively disabled for mutable variables
to which a write occurs nested inside a Tree other than some known ones,
such as If and WhileDo. This is to prevent flow-sensitive reasoning for
variables that are captured and written to in a closure. Pattern matches
do not create a closure. This change enables nullability flow typing
even for mutable variables that are written to inside a pattern match.
  • Loading branch information
odersky authored Oct 1, 2023
2 parents 2be96cf + 74f6851 commit 5c47c5e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Nullables.scala
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ object Nullables:
else candidates -= name
case None =>
traverseChildren(tree)
case _: (If | WhileDo | Typed) =>
case _: (If | WhileDo | Typed | Match | CaseDef | untpd.ParsedTry) =>
traverseChildren(tree) // assignments to candidate variables are OK here ...
case _ =>
reachable = Set.empty // ... but not here
Expand Down
21 changes: 21 additions & 0 deletions tests/explicit-nulls/pos/match-flow-typing.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def m(): String = {
var x: String|Null = "foo"
1 match {
case 1 => x = x
}
if(x == null) "foo"
else x
}

def m2(): String = {
var x: String|Null = "foo"
try {
x = x
} catch {
case e => x = x
} finally {
x = x
}
if(x == null) "foo"
else x
}

0 comments on commit 5c47c5e

Please sign in to comment.