Skip to content

Backport "Fix #21619: Refactor NotNullInfo to record every reference which is retracted once." to 3.3 LTS #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 13, 2025
Prev Previous commit
Next Next commit
Consider cases with Nothing type
  • Loading branch information
noti0na1 authored and tgodzik committed Mar 12, 2025
commit aa14cd8a321bd21d4f88ef744f8d9c82e677de89
24 changes: 16 additions & 8 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1334,9 +1334,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
def thenPathInfo = cond1.notNullInfoIf(true).seq(result.thenp.notNullInfo)
def elsePathInfo = cond1.notNullInfoIf(false).seq(result.elsep.notNullInfo)
result.withNotNullInfo(
if result.thenp.tpe.isRef(defn.NothingClass) then
if result.thenp.tpe.isNothingType then
elsePathInfo.withRetracted(thenPathInfo)
else if result.elsep.tpe.isRef(defn.NothingClass) then
else if result.elsep.tpe.isNothingType then
thenPathInfo.withRetracted(elsePathInfo)
else thenPathInfo.alt(elsePathInfo)
)
Expand Down Expand Up @@ -1862,20 +1862,28 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
case1
}
.asInstanceOf[List[CaseDef]]
var nni = sel.notNullInfo
if cases1.nonEmpty then nni = nni.seq(cases1.map(_.notNullInfo).reduce(_.alt(_)))
assignType(cpy.Match(tree)(sel, cases1), sel, cases1).cast(pt).withNotNullInfo(nni)
assignType(cpy.Match(tree)(sel, cases1), sel, cases1).cast(pt)
.withNotNullInfo(notNullInfoFromCases(sel.notNullInfo, cases1))
}

// Overridden in InlineTyper for inline matches
def typedMatchFinish(tree: untpd.Match, sel: Tree, wideSelType: Type, cases: List[untpd.CaseDef], pt: Type)(using Context): Tree = {
val cases1 = harmonic(harmonize, pt)(typedCases(cases, sel, wideSelType, pt.dropIfProto))
.asInstanceOf[List[CaseDef]]
var nnInfo = sel.notNullInfo
if cases1.nonEmpty then nnInfo = nnInfo.seq(cases1.map(_.notNullInfo).reduce(_.alt(_)))
assignType(cpy.Match(tree)(sel, cases1), sel, cases1).withNotNullInfo(nnInfo)
assignType(cpy.Match(tree)(sel, cases1), sel, cases1)
.withNotNullInfo(notNullInfoFromCases(sel.notNullInfo, cases1))
}

private def notNullInfoFromCases(initInfo: NotNullInfo, cases: List[CaseDef])(using Context): NotNullInfo =
var nnInfo = initInfo
if cases.nonEmpty then
val (nothingCases, normalCases) = cases.partition(_.body.tpe.isNothingType)
nnInfo = nothingCases.foldLeft(nnInfo):
(nni, c) => nni.withRetracted(c.notNullInfo)
if normalCases.nonEmpty then
nnInfo = nnInfo.seq(normalCases.map(_.notNullInfo).reduce(_.alt(_)))
nnInfo

def typedCases(cases: List[untpd.CaseDef], sel: Tree, wideSelType: Type, pt: Type)(using Context): List[CaseDef] =
var caseCtx = ctx
cases.mapconserve { cas =>
Expand Down
20 changes: 19 additions & 1 deletion tests/explicit-nulls/neg/i21380b.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,22 @@ def test3(i: Int) =
i match
case 1 if x != null => ()
case _ => x = " "
x.trim() // error // LTS specific
x.trim() // error // LTS specific

def test4(i: Int) =
var x: String | Null = null
var y: String | Null = null
i match
case 1 => x = "1"
case _ => y = " "
x.trim() // error

def test5(i: Int): String =
var x: String | Null = null
var y: String | Null = null
i match
case 1 => x = "1"
case _ =>
y = " "
return y // error // LTS specific
x.trim() // error // LTS specific
4 changes: 2 additions & 2 deletions tests/explicit-nulls/neg/i21619.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test3: String =
case e: Exception =>
finally
x = "f"
x.replace("", "") // ok
x.replace("", "") // error // LTS specific

def test4: String =
var x: String | Null = null
Expand Down Expand Up @@ -76,4 +76,4 @@ def test5: Unit =
catch
case _ =>
val z1: String = x.replace("", "") // error
val z2: String = y.replace("", "")
val z2: String = y.replace("", "") // error // LTS specific