@@ -1857,7 +1857,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
1857
1857
}
1858
1858
.asInstanceOf [List [CaseDef ]]
1859
1859
var nni = sel.notNullInfo
1860
- if ( cases1.nonEmpty) nni = nni.seq(cases1.map(_.notNullInfo).reduce(_.alt(_)))
1860
+ if cases1.nonEmpty then nni = nni.seq(cases1.map(_.notNullInfo).reduce(_.alt(_)))
1861
1861
assignType(cpy.Match (tree)(sel, cases1), sel, cases1).cast(pt).withNotNullInfo(nni)
1862
1862
}
1863
1863
@@ -1866,7 +1866,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
1866
1866
val cases1 = harmonic(harmonize, pt)(typedCases(cases, sel, wideSelType, pt.dropIfProto))
1867
1867
.asInstanceOf [List [CaseDef ]]
1868
1868
var nni = sel.notNullInfo
1869
- if ( cases1.nonEmpty) nni = nni.seq(cases1.map(_.notNullInfo).reduce(_.alt(_)))
1869
+ if cases1.nonEmpty then nni = nni.seq(cases1.map(_.notNullInfo).reduce(_.alt(_)))
1870
1870
assignType(cpy.Match (tree)(sel, cases1), sel, cases1).withNotNullInfo(nni)
1871
1871
}
1872
1872
@@ -1937,13 +1937,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
1937
1937
// will end up taking too much memory. If it does, we should just limit
1938
1938
// how much GADT constraints we infer - it's always sound to infer less.
1939
1939
pat1.putAttachment(InferredGadtConstraints , ctx.gadt)
1940
- if ( pt1.isValueType) // insert a cast if body does not conform to expected type if we disregard gadt bounds
1940
+ if pt1.isValueType then // insert a cast if body does not conform to expected type if we disregard gadt bounds
1941
1941
body1 = body1.ensureConforms(pt1)(using originalCtx)
1942
- val nni = pat1.notNullInfo.seq(
1943
- guard1.notNullInfoIf(false ).alt(
1944
- guard1.notNullInfoIf(true ).seq(body1.notNullInfo)
1945
- )
1946
- )
1942
+ val nni = pat1.notNullInfo
1943
+ .seq(guard1.notNullInfoIf(false ).alt(guard1.notNullInfoIf(true )))
1944
+ .seq(body1.notNullInfo)
1947
1945
assignType(cpy.CaseDef (tree)(pat1, guard1, body1), pat1, body1).withNotNullInfo(nni)
1948
1946
}
1949
1947
@@ -2048,16 +2046,25 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
2048
2046
untpd.Block (makeCanThrow(capabilityProof), expr)
2049
2047
2050
2048
def typedTry (tree : untpd.Try , pt : Type )(using Context ): Try = {
2049
+ // We want to type check tree.expr first to comput NotNullInfo, but `addCanThrowCapabilities`
2050
+ // uses the types of patterns in `tree.cases` to determine the capabilities.
2051
+ // Hence, we create a copy of cases with empty body and type check that first, then type check
2052
+ // the rest of the tree in order.
2053
+ val casesEmptyBody1 = tree.cases.mapconserve(cpy.CaseDef (_)(body = EmptyTree ))
2054
+ val casesEmptyBody2 = typedCases(casesEmptyBody1, EmptyTree , defn.ThrowableType , WildcardType )
2055
+
2051
2056
val expr2 :: cases2x = harmonic(harmonize, pt) {
2052
- val cases1 = typedCases(tree.cases, EmptyTree , defn.ThrowableType , pt.dropIfProto)
2053
- val expr1 = typed(addCanThrowCapabilities(tree.expr, cases1), pt.dropIfProto)
2057
+ val expr1 = typed(addCanThrowCapabilities(tree.expr, casesEmptyBody2), pt.dropIfProto)
2058
+ val casesCtx = ctx.addNotNullInfo(expr1.notNullInfo.retractedInfo)
2059
+ val cases1 = typedCases(tree.cases, EmptyTree , defn.ThrowableType , pt.dropIfProto)(using casesCtx)
2054
2060
expr1 :: cases1
2055
2061
}: @ unchecked
2056
- val finalizer1 = typed(tree.finalizer, defn.UnitType )
2057
2062
val cases2 = cases2x.asInstanceOf [List [CaseDef ]]
2058
- val nni = expr2.notNullInfo.retractedInfo.seq(
2059
- cases2.map(_.notNullInfo.retractedInfo).fold(NotNullInfo .empty)(_.alt(_))
2060
- ).seq(finalizer1.notNullInfo)
2063
+
2064
+ var nni = expr2.notNullInfo.retractedInfo
2065
+ if cases2.nonEmpty then nni = nni.seq(cases2.map(_.notNullInfo).reduce(_.alt(_)))
2066
+ val finalizer1 = typed(tree.finalizer, defn.UnitType )(using ctx.addNotNullInfo(nni))
2067
+ nni = nni.seq(finalizer1.notNullInfo)
2061
2068
assignType(cpy.Try (tree)(expr2, cases2, finalizer1), expr2, cases2).withNotNullInfo(nni)
2062
2069
}
2063
2070
0 commit comments