@@ -2136,7 +2136,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
2136
2136
}
2137
2137
.asInstanceOf [List [CaseDef ]]
2138
2138
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(_)))
2140
2140
assignType(cpy.Match (tree)(sel, cases1), sel, cases1).cast(pt).withNotNullInfo(nni)
2141
2141
}
2142
2142
@@ -2145,7 +2145,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
2145
2145
val cases1 = harmonic(harmonize, pt)(typedCases(cases, sel, wideSelType, pt.dropIfProto))
2146
2146
.asInstanceOf [List [CaseDef ]]
2147
2147
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(_)))
2149
2149
assignType(cpy.Match (tree)(sel, cases1), sel, cases1).withNotNullInfo(nni)
2150
2150
}
2151
2151
@@ -2218,13 +2218,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
2218
2218
// will end up taking too much memory. If it does, we should just limit
2219
2219
// how much GADT constraints we infer - it's always sound to infer less.
2220
2220
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
2222
2222
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)
2228
2226
assignType(cpy.CaseDef (tree)(pat1, guard1, body1), pat1, body1).withNotNullInfo(nni)
2229
2227
}
2230
2228
@@ -2329,16 +2327,25 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
2329
2327
untpd.Block (makeCanThrow(capabilityProof), expr)
2330
2328
2331
2329
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
+
2332
2337
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)
2335
2341
expr1 :: cases1
2336
2342
}: @ unchecked
2337
- val finalizer1 = typed(tree.finalizer, defn.UnitType )
2338
2343
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)
2342
2349
assignType(cpy.Try (tree)(expr2, cases2, finalizer1), expr2, cases2).withNotNullInfo(nni)
2343
2350
}
2344
2351
0 commit comments