Skip to content

Commit ab9bb2d

Browse files
committed
Fix NotNullInfo for Case and Try; add more tests
1 parent 3d8ccc3 commit ab9bb2d

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
@@ -2121,7 +2121,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
21212121
}
21222122
.asInstanceOf[List[CaseDef]]
21232123
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(_)))
21252125
assignType(cpy.Match(tree)(sel, cases1), sel, cases1).cast(pt).withNotNullInfo(nni)
21262126
}
21272127

@@ -2130,7 +2130,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
21302130
val cases1 = harmonic(harmonize, pt)(typedCases(cases, sel, wideSelType, pt.dropIfProto))
21312131
.asInstanceOf[List[CaseDef]]
21322132
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(_)))
21342134
assignType(cpy.Match(tree)(sel, cases1), sel, cases1).withNotNullInfo(nni)
21352135
}
21362136

@@ -2203,13 +2203,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
22032203
// will end up taking too much memory. If it does, we should just limit
22042204
// how much GADT constraints we infer - it's always sound to infer less.
22052205
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
22072207
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)
22132211
assignType(cpy.CaseDef(tree)(pat1, guard1, body1), pat1, body1).withNotNullInfo(nni)
22142212
}
22152213

@@ -2314,16 +2312,25 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
23142312
untpd.Block(makeCanThrow(capabilityProof), expr)
23152313

23162314
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+
23172322
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)
23202326
expr1 :: cases1
23212327
}: @unchecked
2322-
val finalizer1 = typed(tree.finalizer, defn.UnitType)
23232328
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)
23272334
assignType(cpy.Try(tree)(expr2, cases2, finalizer1), expr2, cases2).withNotNullInfo(nni)
23282335
}
23292336

+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)