Skip to content

Commit a7a4687

Browse files
Backport "add tracking of NotNullInfo for Match, Case, Try trees (fix #21380)" to LTS (#22121)
Backports #21389 to the 3.3.5. PR submitted by the release tooling. [skip ci]
2 parents 010fdad + fdbae42 commit a7a4687

File tree

4 files changed

+117
-9
lines changed

4 files changed

+117
-9
lines changed

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

+32-9
Original file line numberDiff line numberDiff line change
@@ -1856,14 +1856,18 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
18561856
case1
18571857
}
18581858
.asInstanceOf[List[CaseDef]]
1859-
assignType(cpy.Match(tree)(sel, cases1), sel, cases1).cast(pt)
1859+
var nni = sel.notNullInfo
1860+
if cases1.nonEmpty then nni = nni.seq(cases1.map(_.notNullInfo).reduce(_.alt(_)))
1861+
assignType(cpy.Match(tree)(sel, cases1), sel, cases1).cast(pt).withNotNullInfo(nni)
18601862
}
18611863

18621864
// Overridden in InlineTyper for inline matches
18631865
def typedMatchFinish(tree: untpd.Match, sel: Tree, wideSelType: Type, cases: List[untpd.CaseDef], pt: Type)(using Context): Tree = {
18641866
val cases1 = harmonic(harmonize, pt)(typedCases(cases, sel, wideSelType, pt.dropIfProto))
18651867
.asInstanceOf[List[CaseDef]]
1866-
assignType(cpy.Match(tree)(sel, cases1), sel, cases1)
1868+
var nni = sel.notNullInfo
1869+
if cases1.nonEmpty then nni = nni.seq(cases1.map(_.notNullInfo).reduce(_.alt(_)))
1870+
assignType(cpy.Match(tree)(sel, cases1), sel, cases1).withNotNullInfo(nni)
18671871
}
18681872

18691873
def typedCases(cases: List[untpd.CaseDef], sel: Tree, wideSelType: Type, pt: Type)(using Context): List[CaseDef] =
@@ -1925,17 +1929,22 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
19251929
}
19261930
val pat1 = indexPattern(tree).transform(pat)
19271931
val guard1 = typedExpr(tree.guard, defn.BooleanType)
1928-
var body1 = ensureNoLocalRefs(typedExpr(tree.body, pt1), pt1, ctx.scope.toList)
1932+
var body1 = ensureNoLocalRefs(
1933+
typedExpr(tree.body, pt1)(using ctx.addNotNullInfo(guard1.notNullInfoIf(true))),
1934+
pt1, ctx.scope.toList)
19291935
if ctx.gadt.isNarrowing then
19301936
// Store GADT constraint to later retrieve it (in PostTyper, for now).
19311937
// GADT constraints are necessary to correctly check bounds of type app,
19321938
// see tests/pos/i12226 and issue #12226. It might be possible that this
19331939
// will end up taking too much memory. If it does, we should just limit
19341940
// how much GADT constraints we infer - it's always sound to infer less.
19351941
pat1.putAttachment(InferredGadtConstraints, ctx.gadt)
1936-
if (pt1.isValueType) // insert a cast if body does not conform to expected type if we disregard gadt bounds
1942+
if pt1.isValueType then // insert a cast if body does not conform to expected type if we disregard gadt bounds
19371943
body1 = body1.ensureConforms(pt1)(using originalCtx)
1938-
assignType(cpy.CaseDef(tree)(pat1, guard1, body1), pat1, body1)
1944+
val nni = pat1.notNullInfo
1945+
.seq(guard1.notNullInfoIf(true))
1946+
.seq(body1.notNullInfo)
1947+
assignType(cpy.CaseDef(tree)(pat1, guard1, body1), pat1, body1).withNotNullInfo(nni)
19391948
}
19401949

19411950
val pat1 = typedPattern(tree.pat, wideSelType)(using gadtCtx)
@@ -2040,13 +2049,27 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
20402049

20412050
def typedTry(tree: untpd.Try, pt: Type)(using Context): Try = {
20422051
val expr2 :: cases2x = harmonic(harmonize, pt) {
2043-
val cases1 = typedCases(tree.cases, EmptyTree, defn.ThrowableType, pt.dropIfProto)
2044-
val expr1 = typed(addCanThrowCapabilities(tree.expr, cases1), pt.dropIfProto)
2052+
// We want to type check tree.expr first to comput NotNullInfo, but `addCanThrowCapabilities`
2053+
// uses the types of patterns in `tree.cases` to determine the capabilities.
2054+
// Hence, we create a copy of cases with empty body and type check that first, then type check
2055+
// the rest of the tree in order.
2056+
// It may seem that invalid references can be created if the type of the pattern contains
2057+
// type binds, but this is not a valid `CanThrow` capability (checked by `addCanThrowCapabilities`),
2058+
// so it is not a problem.
2059+
val casesEmptyBody1 = tree.cases.mapconserve(cpy.CaseDef(_)(body = EmptyTree))
2060+
val casesEmptyBody2 = typedCases(casesEmptyBody1, EmptyTree, defn.ThrowableType, WildcardType)
2061+
val expr1 = typed(addCanThrowCapabilities(tree.expr, casesEmptyBody2), pt.dropIfProto)
2062+
val casesCtx = ctx.addNotNullInfo(expr1.notNullInfo.retractedInfo)
2063+
val cases1 = typedCases(tree.cases, EmptyTree, defn.ThrowableType, pt.dropIfProto)(using casesCtx)
20452064
expr1 :: cases1
20462065
}: @unchecked
2047-
val finalizer1 = typed(tree.finalizer, defn.UnitType)
20482066
val cases2 = cases2x.asInstanceOf[List[CaseDef]]
2049-
assignType(cpy.Try(tree)(expr2, cases2, finalizer1), expr2, cases2)
2067+
2068+
var nni = expr2.notNullInfo.retractedInfo
2069+
if cases2.nonEmpty then nni = nni.seq(cases2.map(_.notNullInfo.retractedInfo).reduce(_.alt(_)))
2070+
val finalizer1 = typed(tree.finalizer, defn.UnitType)(using ctx.addNotNullInfo(nni))
2071+
nni = nni.seq(finalizer1.notNullInfo)
2072+
assignType(cpy.Try(tree)(expr2, cases2, finalizer1), expr2, cases2).withNotNullInfo(nni)
20502073
}
20512074

20522075
def typedTry(tree: untpd.ParsedTry, pt: Type)(using Context): Try =

tests/explicit-nulls/neg/i21380.scala

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@main def test() = {
2+
var x: String | Null = null
3+
if (false) {
4+
x = ""
5+
6+
} else {
7+
x = ""
8+
}
9+
try {
10+
x = ""
11+
throw new Exception()
12+
}
13+
catch {
14+
case e: Exception => {
15+
x = null
16+
}
17+
}
18+
x.replace("", "") // error
19+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def test1 =
2+
var x: String | Null = null
3+
x = ""
4+
1 match
5+
case 1 => x = null
6+
case _ => x = x.trim() // error // LTS specific
7+
x.replace("", "") // error
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("", "") // error // LTS specific
15+
16+
def test3(i: Int) =
17+
var x: String | Null = null
18+
i match
19+
case 1 if x != null => ()
20+
case _ => x = " "
21+
x.trim() // error // LTS specific
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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(" ", "") // error // LTS specific
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 // error // LTS specific
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 // error
35+
// Although the catch block here is exhaustive,
36+
// it is possible that the exception is thrown and not caught.
37+
// Therefore, the code after the try block can only rely on the retracted info.
38+
39+
def test5: Int =
40+
var x: String | Null = null
41+
try
42+
x = ""
43+
throw new Exception()
44+
catch
45+
case npe: NullPointerException => val i: Int = x.length // error

0 commit comments

Comments
 (0)