Skip to content

Commit d84dc5c

Browse files
authored
Fix NotNullInfo in multi-case match (#23864)
When we compute `afterPatternContext`, the wrong `ctx` is passed to the function. ```scala def f(s: AnyRef | Null) = s match case null => 0 case s: String => s.length case _ => val a: AnyRef = s a.toString.length ``` Will be useful for #23566
2 parents ca687b2 + ddf3f42 commit d84dc5c

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,7 +2216,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
22162216
var alreadyStripped = false
22172217
val cases1 = tree.cases.zip(pt.cases)
22182218
.map { case (cas, tpe) =>
2219-
val case1 = typedCase(cas, sel, wideSelType, tpe)(using caseCtx)
2219+
given Context = caseCtx
2220+
val case1 = typedCase(cas, sel, wideSelType, tpe)
22202221
caseCtx = Nullables.afterPatternContext(sel, case1.pat)
22212222
if !alreadyStripped && Nullables.matchesNull(case1) then
22222223
wideSelType = wideSelType.stripNull()
@@ -2248,7 +2249,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
22482249
var wideSelType = wideSelType0
22492250
var alreadyStripped = false
22502251
cases.mapconserve { cas =>
2251-
val case1 = typedCase(cas, sel, wideSelType, pt)(using caseCtx)
2252+
given Context = caseCtx
2253+
val case1 = typedCase(cas, sel, wideSelType, pt)
22522254
caseCtx = Nullables.afterPatternContext(sel, case1.pat)
22532255
if !alreadyStripped && Nullables.matchesNull(case1) then
22542256
wideSelType = wideSelType.stripNull()

tests/explicit-nulls/pos/flow-match.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,20 @@ object MatchTest {
5555
case s2 => s2.nn
5656
case s3 => s3
5757
}
58+
59+
def f8(a: AnyRef | Null): String = a match {
60+
case null => "null"
61+
case s: String => s
62+
case _ =>
63+
val a2: AnyRef = a
64+
a.toString
65+
}
66+
67+
def f9(a: AnyRef | Null): String = a match {
68+
case null => "null"
69+
case s: String => s
70+
case a1 =>
71+
val a2: AnyRef = a1
72+
a1.toString
73+
}
5874
}

0 commit comments

Comments
 (0)