Skip to content

Commit

Permalink
Passing NotNullInfos to a mutable field of a Completer
Browse files Browse the repository at this point in the history
  • Loading branch information
noti0na1 committed Jan 16, 2024
1 parent 2945fd1 commit b4a293e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import parsing.JavaParsers.JavaParser
import parsing.Parsers.Parser
import Annotations.*
import Inferencing.*
import Nullables.*
import transform.ValueClasses.*
import TypeErasure.erasure
import reporting.*
Expand Down Expand Up @@ -784,8 +785,11 @@ class Namer { typer: Typer =>

protected def localContext(owner: Symbol): FreshContext = ctx.fresh.setOwner(owner).setTree(original)

var myNotNullInfos: List[NotNullInfo] | Null = null

/** The context with which this completer was created */
given creationContext: Context = ictx
given creationContext[T]: Context =
if myNotNullInfos == null then ictx else ictx.withNotNullInfos(myNotNullInfos.nn)

// make sure testing contexts are not captured by completers
assert(!ictx.reporter.isInstanceOf[ExploringReporter])
Expand Down
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3366,8 +3366,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
// The RHS of a val def should know about not null facts established
// in preceding statements (unless the DefTree is completed ahead of time,
// then it is impossible).
sym.info = Completer(completer.original)(
completer.creationContext.withNotNullInfos(ctx.notNullInfos))
completer.myNotNullInfos = ctx.notNullInfos
true
case _ =>
// If it has been completed, then it must be because there is a forward reference
Expand Down
27 changes: 27 additions & 0 deletions tests/explicit-nulls/pos/i19202.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Test {
def test1(s: String | Null): Unit = {
if s == null then return

case class XXX()
}

def test2(s: String | Null): Unit = {
if s == "" then return

case class XXX()
}

def test3(s: String | Null): Unit = {
if s == null then return

case class XXX()
()
}

def test4(s: String | Null): String | Null = {
if s == null then return ""

case class XXX()
"xxx"
}
}
27 changes: 27 additions & 0 deletions tests/pos/i19202.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Test {
def test1(s: String): Unit = {
if s == null then return

case class XXX()
}

def test2(s: String): Unit = {
if s == "" then return

case class XXX()
}

def test3(s: String): Unit = {
if s == null then return

case class XXX()
()
}

def test4(s: String): String = {
if s == null then return ""

case class XXX()
"xxx"
}
}

0 comments on commit b4a293e

Please sign in to comment.