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