@@ -2135,14 +2135,18 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
2135
2135
case1
2136
2136
}
2137
2137
.asInstanceOf [List [CaseDef ]]
2138
- assignType(cpy.Match (tree)(sel, cases1), sel, cases1).cast(pt)
2138
+ var nni = sel.notNullInfo
2139
+ if cases1.nonEmpty then nni = nni.seq(cases1.map(_.notNullInfo).reduce(_.alt(_)))
2140
+ assignType(cpy.Match (tree)(sel, cases1), sel, cases1).cast(pt).withNotNullInfo(nni)
2139
2141
}
2140
2142
2141
2143
// Overridden in InlineTyper for inline matches
2142
2144
def typedMatchFinish (tree : untpd.Match , sel : Tree , wideSelType : Type , cases : List [untpd.CaseDef ], pt : Type )(using Context ): Tree = {
2143
2145
val cases1 = harmonic(harmonize, pt)(typedCases(cases, sel, wideSelType, pt.dropIfProto))
2144
2146
.asInstanceOf [List [CaseDef ]]
2145
- assignType(cpy.Match (tree)(sel, cases1), sel, cases1)
2147
+ var nni = sel.notNullInfo
2148
+ if cases1.nonEmpty then nni = nni.seq(cases1.map(_.notNullInfo).reduce(_.alt(_)))
2149
+ assignType(cpy.Match (tree)(sel, cases1), sel, cases1).withNotNullInfo(nni)
2146
2150
}
2147
2151
2148
2152
def typedCases (cases : List [untpd.CaseDef ], sel : Tree , wideSelType0 : Type , pt : Type )(using Context ): List [CaseDef ] =
@@ -2206,17 +2210,22 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
2206
2210
}
2207
2211
val pat1 = indexPattern(tree).transform(pat)
2208
2212
val guard1 = typedExpr(tree.guard, defn.BooleanType )
2209
- var body1 = ensureNoLocalRefs(typedExpr(tree.body, pt1), pt1, ctx.scope.toList)
2213
+ var body1 = ensureNoLocalRefs(
2214
+ typedExpr(tree.body, pt1)(using ctx.addNotNullInfo(guard1.notNullInfoIf(true ))),
2215
+ pt1, ctx.scope.toList)
2210
2216
if ctx.gadt.isNarrowing then
2211
2217
// Store GADT constraint to later retrieve it (in PostTyper, for now).
2212
2218
// GADT constraints are necessary to correctly check bounds of type app,
2213
2219
// see tests/pos/i12226 and issue #12226. It might be possible that this
2214
2220
// will end up taking too much memory. If it does, we should just limit
2215
2221
// how much GADT constraints we infer - it's always sound to infer less.
2216
2222
pat1.putAttachment(InferredGadtConstraints , ctx.gadt)
2217
- if ( pt1.isValueType) // insert a cast if body does not conform to expected type if we disregard gadt bounds
2223
+ if pt1.isValueType then // insert a cast if body does not conform to expected type if we disregard gadt bounds
2218
2224
body1 = body1.ensureConforms(pt1)(using originalCtx)
2219
- assignType(cpy.CaseDef (tree)(pat1, guard1, body1), pat1, body1)
2225
+ val nni = pat1.notNullInfo
2226
+ .seq(guard1.notNullInfoIf(true ))
2227
+ .seq(body1.notNullInfo)
2228
+ assignType(cpy.CaseDef (tree)(pat1, guard1, body1), pat1, body1).withNotNullInfo(nni)
2220
2229
}
2221
2230
2222
2231
val pat1 = typedPattern(tree.pat, wideSelType)(using gadtCtx)
@@ -2321,13 +2330,27 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
2321
2330
2322
2331
def typedTry (tree : untpd.Try , pt : Type )(using Context ): Try = {
2323
2332
val expr2 :: cases2x = harmonic(harmonize, pt) {
2324
- val cases1 = typedCases(tree.cases, EmptyTree , defn.ThrowableType , pt.dropIfProto)
2325
- val expr1 = typed(addCanThrowCapabilities(tree.expr, cases1), pt.dropIfProto)
2333
+ // We want to type check tree.expr first to comput NotNullInfo, but `addCanThrowCapabilities`
2334
+ // uses the types of patterns in `tree.cases` to determine the capabilities.
2335
+ // Hence, we create a copy of cases with empty body and type check that first, then type check
2336
+ // the rest of the tree in order.
2337
+ // It may seem that invalid references can be created if the type of the pattern contains
2338
+ // type binds, but this is not a valid `CanThrow` capability (checked by `addCanThrowCapabilities`),
2339
+ // so it is not a problem.
2340
+ val casesEmptyBody1 = tree.cases.mapconserve(cpy.CaseDef (_)(body = EmptyTree ))
2341
+ val casesEmptyBody2 = typedCases(casesEmptyBody1, EmptyTree , defn.ThrowableType , WildcardType )
2342
+ val expr1 = typed(addCanThrowCapabilities(tree.expr, casesEmptyBody2), pt.dropIfProto)
2343
+ val casesCtx = ctx.addNotNullInfo(expr1.notNullInfo.retractedInfo)
2344
+ val cases1 = typedCases(tree.cases, EmptyTree , defn.ThrowableType , pt.dropIfProto)(using casesCtx)
2326
2345
expr1 :: cases1
2327
2346
}: @ unchecked
2328
- val finalizer1 = typed(tree.finalizer, defn.UnitType )
2329
2347
val cases2 = cases2x.asInstanceOf [List [CaseDef ]]
2330
- assignType(cpy.Try (tree)(expr2, cases2, finalizer1), expr2, cases2)
2348
+
2349
+ var nni = expr2.notNullInfo.retractedInfo
2350
+ if cases2.nonEmpty then nni = nni.seq(cases2.map(_.notNullInfo.retractedInfo).reduce(_.alt(_)))
2351
+ val finalizer1 = typed(tree.finalizer, defn.UnitType )(using ctx.addNotNullInfo(nni))
2352
+ nni = nni.seq(finalizer1.notNullInfo)
2353
+ assignType(cpy.Try (tree)(expr2, cases2, finalizer1), expr2, cases2).withNotNullInfo(nni)
2331
2354
}
2332
2355
2333
2356
def typedTry (tree : untpd.ParsedTry , pt : Type )(using Context ): Try =
0 commit comments