@@ -1841,12 +1841,17 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
1841
1841
/** Special typing of Match tree when the expected type is a MatchType,
1842
1842
* and the patterns of the Match tree and the MatchType correspond.
1843
1843
*/
1844
- def typedDependentMatchFinish (tree : untpd.Match , sel : Tree , wideSelType : Type , cases : List [untpd.CaseDef ], pt : MatchType )(using Context ): Tree = {
1844
+ def typedDependentMatchFinish (tree : untpd.Match , sel : Tree , wideSelType0 : Type , cases : List [untpd.CaseDef ], pt : MatchType )(using Context ): Tree = {
1845
1845
var caseCtx = ctx
1846
+ var wideSelType = wideSelType0
1847
+ var alreadyStripped = false
1846
1848
val cases1 = tree.cases.zip(pt.cases)
1847
1849
.map { case (cas, tpe) =>
1848
1850
val case1 = typedCase(cas, sel, wideSelType, tpe)(using caseCtx)
1849
1851
caseCtx = Nullables .afterPatternContext(sel, case1.pat)
1852
+ if ! alreadyStripped && Nullables .matchesNull(case1) then
1853
+ wideSelType = wideSelType.stripNull
1854
+ alreadyStripped = true
1850
1855
case1
1851
1856
}
1852
1857
.asInstanceOf [List [CaseDef ]]
@@ -1860,10 +1865,15 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
1860
1865
assignType(cpy.Match (tree)(sel, cases1), sel, cases1)
1861
1866
}
1862
1867
1863
- def typedCases (cases : List [untpd.CaseDef ], sel : Tree , wideSelType : Type , pt : Type )(using Context ): List [CaseDef ] =
1868
+ def typedCases (cases : List [untpd.CaseDef ], sel : Tree , wideSelType0 : Type , pt : Type )(using Context ): List [CaseDef ] =
1864
1869
var caseCtx = ctx
1870
+ var wideSelType = wideSelType0
1871
+ var alreadyStripped = false
1865
1872
cases.mapconserve { cas =>
1866
1873
val case1 = typedCase(cas, sel, wideSelType, pt)(using caseCtx)
1874
+ if ! alreadyStripped && Nullables .matchesNull(case1) then
1875
+ wideSelType = wideSelType.stripNull
1876
+ alreadyStripped = true
1867
1877
caseCtx = Nullables .afterPatternContext(sel, case1.pat)
1868
1878
case1
1869
1879
}
0 commit comments