Skip to content

Commit 7674fef

Browse files
committed
Fix NotNullInfo for Case and Try; add more tests
1 parent 10497c9 commit 7674fef

File tree

3 files changed

+65
-18
lines changed

3 files changed

+65
-18
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

+21-14
Original file line numberDiff line numberDiff line change
@@ -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

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
@main def test() = {
1+
def test1 =
22
var x: String | Null = null
33
x = ""
4-
1 match {
4+
1 match
55
case 1 => x = null
6-
}
6+
case _ => x = x.trim() // ok
77
x.replace("", "") // error
8-
}
8+
9+
def test2(i: Int) =
10+
var x: String | Null = null
11+
i match
12+
case 1 => x = "1"
13+
case _ => x = " "
14+
x.replace("", "") // ok
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def test1(i: Int): Int =
2+
var x: String | Null = null
3+
if i == 0 then x = ""
4+
else x = ""
5+
try
6+
x = x.replace(" ", "") // ok
7+
throw new Exception()
8+
catch
9+
case e: Exception =>
10+
x = x.replaceAll(" ", "") // error
11+
x = null
12+
x.length // error
13+
14+
def test2: Int =
15+
var x: String | Null = null
16+
try throw new Exception()
17+
finally x = ""
18+
x.length // ok
19+
20+
def test3 =
21+
var x: String | Null = ""
22+
try throw new Exception()
23+
catch case e: Exception =>
24+
x = (??? : String | Null)
25+
finally
26+
val l = x.length // error
27+
28+
def test4: Int =
29+
var x: String | Null = null
30+
try throw new Exception()
31+
catch
32+
case npe: NullPointerException => x = ""
33+
case _ => x = ""
34+
x.length // ok

0 commit comments

Comments
 (0)