Skip to content

Commit 5c47c5e

Browse files
authored
allow nullability flow typing even in presence of pattern match (#18206)
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.
2 parents 2be96cf + 74f6851 commit 5c47c5e

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ object Nullables:
456456
else candidates -= name
457457
case None =>
458458
traverseChildren(tree)
459-
case _: (If | WhileDo | Typed) =>
459+
case _: (If | WhileDo | Typed | Match | CaseDef | untpd.ParsedTry) =>
460460
traverseChildren(tree) // assignments to candidate variables are OK here ...
461461
case _ =>
462462
reachable = Set.empty // ... but not here
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def m(): String = {
2+
var x: String|Null = "foo"
3+
1 match {
4+
case 1 => x = x
5+
}
6+
if(x == null) "foo"
7+
else x
8+
}
9+
10+
def m2(): String = {
11+
var x: String|Null = "foo"
12+
try {
13+
x = x
14+
} catch {
15+
case e => x = x
16+
} finally {
17+
x = x
18+
}
19+
if(x == null) "foo"
20+
else x
21+
}

0 commit comments

Comments
 (0)