Skip to content
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
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2216,7 +2216,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
var alreadyStripped = false
val cases1 = tree.cases.zip(pt.cases)
.map { case (cas, tpe) =>
val case1 = typedCase(cas, sel, wideSelType, tpe)(using caseCtx)
given Context = caseCtx
val case1 = typedCase(cas, sel, wideSelType, tpe)
caseCtx = Nullables.afterPatternContext(sel, case1.pat)
Comment on lines +2219 to 2221
Copy link
Preview

Copilot AI Sep 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The caseCtx is being updated after typedCase is called, but afterPatternContext should use the context from before typing the case, not the caseCtx that was passed to typedCase. This could lead to incorrect null flow analysis.

Suggested change
given Context = caseCtx
val case1 = typedCase(cas, sel, wideSelType, tpe)
caseCtx = Nullables.afterPatternContext(sel, case1.pat)
val beforeCaseCtx = caseCtx
given Context = caseCtx
val case1 = typedCase(cas, sel, wideSelType, tpe)
caseCtx = Nullables.afterPatternContext(sel, case1.pat)(using beforeCaseCtx)

Copilot uses AI. Check for mistakes.

if !alreadyStripped && Nullables.matchesNull(case1) then
wideSelType = wideSelType.stripNull()
Expand Down Expand Up @@ -2248,7 +2249,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
var wideSelType = wideSelType0
var alreadyStripped = false
cases.mapconserve { cas =>
val case1 = typedCase(cas, sel, wideSelType, pt)(using caseCtx)
given Context = caseCtx
val case1 = typedCase(cas, sel, wideSelType, pt)
caseCtx = Nullables.afterPatternContext(sel, case1.pat)
Comment on lines +2252 to 2254
Copy link
Preview

Copilot AI Sep 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue as in typedMatchFinish: the caseCtx update should use the original context, not the caseCtx that was used for typing the case.

Suggested change
given Context = caseCtx
val case1 = typedCase(cas, sel, wideSelType, pt)
caseCtx = Nullables.afterPatternContext(sel, case1.pat)
caseCtx = Nullables.afterPatternContext(sel, case1.pat)(using ctx)

Copilot uses AI. Check for mistakes.

if !alreadyStripped && Nullables.matchesNull(case1) then
wideSelType = wideSelType.stripNull()
Expand Down
16 changes: 16 additions & 0 deletions tests/explicit-nulls/pos/flow-match.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,20 @@ object MatchTest {
case s2 => s2.nn
case s3 => s3
}

def f8(a: AnyRef | Null): String = a match {
case null => "null"
case s: String => s
case _ =>
val a2: AnyRef = a
a.toString
}

def f9(a: AnyRef | Null): String = a match {
case null => "null"
case s: String => s
case a1 =>
val a2: AnyRef = a1
a1.toString
}
}
Loading