@@ -2136,7 +2136,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
21362136 }
21372137 .asInstanceOf [List [CaseDef ]]
21382138 var nni = sel.notNullInfo
2139- if ( cases1.nonEmpty) nni = nni.seq(cases1.map(_.notNullInfo).reduce(_.alt(_)))
2139+ if cases1.nonEmpty then nni = nni.seq(cases1.map(_.notNullInfo).reduce(_.alt(_)))
21402140 assignType(cpy.Match (tree)(sel, cases1), sel, cases1).cast(pt).withNotNullInfo(nni)
21412141 }
21422142
@@ -2145,7 +2145,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
21452145 val cases1 = harmonic(harmonize, pt)(typedCases(cases, sel, wideSelType, pt.dropIfProto))
21462146 .asInstanceOf [List [CaseDef ]]
21472147 var nni = sel.notNullInfo
2148- if ( cases1.nonEmpty) nni = nni.seq(cases1.map(_.notNullInfo).reduce(_.alt(_)))
2148+ if cases1.nonEmpty then nni = nni.seq(cases1.map(_.notNullInfo).reduce(_.alt(_)))
21492149 assignType(cpy.Match (tree)(sel, cases1), sel, cases1).withNotNullInfo(nni)
21502150 }
21512151
@@ -2218,13 +2218,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
22182218 // will end up taking too much memory. If it does, we should just limit
22192219 // how much GADT constraints we infer - it's always sound to infer less.
22202220 pat1.putAttachment(InferredGadtConstraints , ctx.gadt)
2221- if ( pt1.isValueType) // insert a cast if body does not conform to expected type if we disregard gadt bounds
2221+ if pt1.isValueType then // insert a cast if body does not conform to expected type if we disregard gadt bounds
22222222 body1 = body1.ensureConforms(pt1)(using originalCtx)
2223- val nni = pat1.notNullInfo.seq(
2224- guard1.notNullInfoIf(false ).alt(
2225- guard1.notNullInfoIf(true ).seq(body1.notNullInfo)
2226- )
2227- )
2223+ val nni = pat1.notNullInfo
2224+ .seq(guard1.notNullInfoIf(false ).alt(guard1.notNullInfoIf(true )))
2225+ .seq(body1.notNullInfo)
22282226 assignType(cpy.CaseDef (tree)(pat1, guard1, body1), pat1, body1).withNotNullInfo(nni)
22292227 }
22302228
@@ -2329,16 +2327,25 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
23292327 untpd.Block (makeCanThrow(capabilityProof), expr)
23302328
23312329 def typedTry (tree : untpd.Try , pt : Type )(using Context ): Try = {
2330+ // We want to type check tree.expr first to comput NotNullInfo, but `addCanThrowCapabilities`
2331+ // uses the types of patterns in `tree.cases` to determine the capabilities.
2332+ // Hence, we create a copy of cases with empty body and type check that first, then type check
2333+ // the rest of the tree in order.
2334+ val casesEmptyBody1 = tree.cases.mapconserve(cpy.CaseDef (_)(body = EmptyTree ))
2335+ val casesEmptyBody2 = typedCases(casesEmptyBody1, EmptyTree , defn.ThrowableType , WildcardType )
2336+
23322337 val expr2 :: cases2x = harmonic(harmonize, pt) {
2333- val cases1 = typedCases(tree.cases, EmptyTree , defn.ThrowableType , pt.dropIfProto)
2334- val expr1 = typed(addCanThrowCapabilities(tree.expr, cases1), pt.dropIfProto)
2338+ val expr1 = typed(addCanThrowCapabilities(tree.expr, casesEmptyBody2), pt.dropIfProto)
2339+ val casesCtx = ctx.addNotNullInfo(expr1.notNullInfo.retractedInfo)
2340+ val cases1 = typedCases(tree.cases, EmptyTree , defn.ThrowableType , pt.dropIfProto)(using casesCtx)
23352341 expr1 :: cases1
23362342 }: @ unchecked
2337- val finalizer1 = typed(tree.finalizer, defn.UnitType )
23382343 val cases2 = cases2x.asInstanceOf [List [CaseDef ]]
2339- val nni = expr2.notNullInfo.retractedInfo.seq(
2340- cases2.map(_.notNullInfo.retractedInfo).fold(NotNullInfo .empty)(_.alt(_))
2341- ).seq(finalizer1.notNullInfo)
2344+
2345+ var nni = expr2.notNullInfo.retractedInfo
2346+ if cases2.nonEmpty then nni = nni.seq(cases2.map(_.notNullInfo).reduce(_.alt(_)))
2347+ val finalizer1 = typed(tree.finalizer, defn.UnitType )(using ctx.addNotNullInfo(nni))
2348+ nni = nni.seq(finalizer1.notNullInfo)
23422349 assignType(cpy.Try (tree)(expr2, cases2, finalizer1), expr2, cases2).withNotNullInfo(nni)
23432350 }
23442351
0 commit comments