@@ -1857,7 +1857,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
18571857 }
18581858 .asInstanceOf [List [CaseDef ]]
18591859 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(_)))
18611861 assignType(cpy.Match (tree)(sel, cases1), sel, cases1).cast(pt).withNotNullInfo(nni)
18621862 }
18631863
@@ -1866,7 +1866,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
18661866 val cases1 = harmonic(harmonize, pt)(typedCases(cases, sel, wideSelType, pt.dropIfProto))
18671867 .asInstanceOf [List [CaseDef ]]
18681868 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(_)))
18701870 assignType(cpy.Match (tree)(sel, cases1), sel, cases1).withNotNullInfo(nni)
18711871 }
18721872
@@ -1937,13 +1937,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
19371937 // will end up taking too much memory. If it does, we should just limit
19381938 // how much GADT constraints we infer - it's always sound to infer less.
19391939 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
19411941 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)
19471945 assignType(cpy.CaseDef (tree)(pat1, guard1, body1), pat1, body1).withNotNullInfo(nni)
19481946 }
19491947
@@ -2048,16 +2046,25 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
20482046 untpd.Block (makeCanThrow(capabilityProof), expr)
20492047
20502048 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+
20512056 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)
20542060 expr1 :: cases1
20552061 }: @ unchecked
2056- val finalizer1 = typed(tree.finalizer, defn.UnitType )
20572062 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)
20612068 assignType(cpy.Try (tree)(expr2, cases2, finalizer1), expr2, cases2).withNotNullInfo(nni)
20622069 }
20632070
0 commit comments