@@ -1843,12 +1843,17 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
18431843 /** Special typing of Match tree when the expected type is a MatchType,
18441844 * and the patterns of the Match tree and the MatchType correspond.
18451845 */
1846- def typedDependentMatchFinish (tree : untpd.Match , sel : Tree , wideSelType : Type , cases : List [untpd.CaseDef ], pt : MatchType )(using Context ): Tree = {
1846+ def typedDependentMatchFinish (tree : untpd.Match , sel : Tree , wideSelType0 : Type , cases : List [untpd.CaseDef ], pt : MatchType )(using Context ): Tree = {
18471847 var caseCtx = ctx
1848+ var wideSelType = wideSelType0
1849+ var alreadyStripped = false
18481850 val cases1 = tree.cases.zip(pt.cases)
18491851 .map { case (cas, tpe) =>
18501852 val case1 = typedCase(cas, sel, wideSelType, tpe)(using caseCtx)
18511853 caseCtx = Nullables .afterPatternContext(sel, case1.pat)
1854+ if ! alreadyStripped && Nullables .matchesNull(case1) then
1855+ wideSelType = wideSelType.stripNull
1856+ alreadyStripped = true
18521857 case1
18531858 }
18541859 .asInstanceOf [List [CaseDef ]]
@@ -1862,10 +1867,15 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
18621867 assignType(cpy.Match (tree)(sel, cases1), sel, cases1)
18631868 }
18641869
1865- def typedCases (cases : List [untpd.CaseDef ], sel : Tree , wideSelType : Type , pt : Type )(using Context ): List [CaseDef ] =
1870+ def typedCases (cases : List [untpd.CaseDef ], sel : Tree , wideSelType0 : Type , pt : Type )(using Context ): List [CaseDef ] =
18661871 var caseCtx = ctx
1872+ var wideSelType = wideSelType0
1873+ var alreadyStripped = false
18671874 cases.mapconserve { cas =>
18681875 val case1 = typedCase(cas, sel, wideSelType, pt)(using caseCtx)
1876+ if ! alreadyStripped && Nullables .matchesNull(case1) then
1877+ wideSelType = wideSelType.stripNull
1878+ alreadyStripped = true
18691879 caseCtx = Nullables .afterPatternContext(sel, case1.pat)
18701880 case1
18711881 }
0 commit comments