@@ -2121,7 +2121,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
2121
2121
}
2122
2122
.asInstanceOf [List [CaseDef ]]
2123
2123
var nni = sel.notNullInfo
2124
- if ( cases1.nonEmpty) nni = nni.seq(cases1.map(_.notNullInfo).reduce(_.alt(_)))
2124
+ if cases1.nonEmpty then nni = nni.seq(cases1.map(_.notNullInfo).reduce(_.alt(_)))
2125
2125
assignType(cpy.Match (tree)(sel, cases1), sel, cases1).cast(pt).withNotNullInfo(nni)
2126
2126
}
2127
2127
@@ -2130,7 +2130,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
2130
2130
val cases1 = harmonic(harmonize, pt)(typedCases(cases, sel, wideSelType, pt.dropIfProto))
2131
2131
.asInstanceOf [List [CaseDef ]]
2132
2132
var nni = sel.notNullInfo
2133
- if ( cases1.nonEmpty) nni = nni.seq(cases1.map(_.notNullInfo).reduce(_.alt(_)))
2133
+ if cases1.nonEmpty then nni = nni.seq(cases1.map(_.notNullInfo).reduce(_.alt(_)))
2134
2134
assignType(cpy.Match (tree)(sel, cases1), sel, cases1).withNotNullInfo(nni)
2135
2135
}
2136
2136
@@ -2203,13 +2203,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
2203
2203
// will end up taking too much memory. If it does, we should just limit
2204
2204
// how much GADT constraints we infer - it's always sound to infer less.
2205
2205
pat1.putAttachment(InferredGadtConstraints , ctx.gadt)
2206
- if ( pt1.isValueType) // insert a cast if body does not conform to expected type if we disregard gadt bounds
2206
+ if pt1.isValueType then // insert a cast if body does not conform to expected type if we disregard gadt bounds
2207
2207
body1 = body1.ensureConforms(pt1)(using originalCtx)
2208
- val nni = pat1.notNullInfo.seq(
2209
- guard1.notNullInfoIf(false ).alt(
2210
- guard1.notNullInfoIf(true ).seq(body1.notNullInfo)
2211
- )
2212
- )
2208
+ val nni = pat1.notNullInfo
2209
+ .seq(guard1.notNullInfoIf(false ).alt(guard1.notNullInfoIf(true )))
2210
+ .seq(body1.notNullInfo)
2213
2211
assignType(cpy.CaseDef (tree)(pat1, guard1, body1), pat1, body1).withNotNullInfo(nni)
2214
2212
}
2215
2213
@@ -2314,16 +2312,25 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
2314
2312
untpd.Block (makeCanThrow(capabilityProof), expr)
2315
2313
2316
2314
def typedTry (tree : untpd.Try , pt : Type )(using Context ): Try = {
2315
+ // We want to type check tree.expr first to comput NotNullInfo, but `addCanThrowCapabilities`
2316
+ // uses the types of patterns in `tree.cases` to determine the capabilities.
2317
+ // Hence, we create a copy of cases with empty body and type check that first, then type check
2318
+ // the rest of the tree in order.
2319
+ val casesEmptyBody1 = tree.cases.mapconserve(cpy.CaseDef (_)(body = EmptyTree ))
2320
+ val casesEmptyBody2 = typedCases(casesEmptyBody1, EmptyTree , defn.ThrowableType , WildcardType )
2321
+
2317
2322
val expr2 :: cases2x = harmonic(harmonize, pt) {
2318
- val cases1 = typedCases(tree.cases, EmptyTree , defn.ThrowableType , pt.dropIfProto)
2319
- val expr1 = typed(addCanThrowCapabilities(tree.expr, cases1), pt.dropIfProto)
2323
+ val expr1 = typed(addCanThrowCapabilities(tree.expr, casesEmptyBody2), pt.dropIfProto)
2324
+ val casesCtx = ctx.addNotNullInfo(expr1.notNullInfo.retractedInfo)
2325
+ val cases1 = typedCases(tree.cases, EmptyTree , defn.ThrowableType , pt.dropIfProto)(using casesCtx)
2320
2326
expr1 :: cases1
2321
2327
}: @ unchecked
2322
- val finalizer1 = typed(tree.finalizer, defn.UnitType )
2323
2328
val cases2 = cases2x.asInstanceOf [List [CaseDef ]]
2324
- val nni = expr2.notNullInfo.retractedInfo.seq(
2325
- cases2.map(_.notNullInfo.retractedInfo).fold(NotNullInfo .empty)(_.alt(_))
2326
- ).seq(finalizer1.notNullInfo)
2329
+
2330
+ var nni = expr2.notNullInfo.retractedInfo
2331
+ if cases2.nonEmpty then nni = nni.seq(cases2.map(_.notNullInfo).reduce(_.alt(_)))
2332
+ val finalizer1 = typed(tree.finalizer, defn.UnitType )(using ctx.addNotNullInfo(nni))
2333
+ nni = nni.seq(finalizer1.notNullInfo)
2327
2334
assignType(cpy.Try (tree)(expr2, cases2, finalizer1), expr2, cases2).withNotNullInfo(nni)
2328
2335
}
2329
2336
0 commit comments