Skip to content

Commit 4a9edc5

Browse files
committed
Add mode bit to track whether we are in safe nulls mode
The bit is set if - -Yexplicit-nulls is set - we are not in an `import.unsafeNulls` scope
1 parent 1369a14 commit 4a9edc5

File tree

5 files changed

+18
-2
lines changed

5 files changed

+18
-2
lines changed

compiler/src/dotty/tools/dotc/Run.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,13 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
7171
.setPeriod(Period(comp.nextRunId, FirstPhaseId))
7272
.setScope(rootScope)
7373
rootScope.enter(ctx.definitions.RootPackage)(using bootstrap)
74-
val start = bootstrap.fresh
74+
var start = bootstrap.fresh
7575
.setOwner(defn.RootClass)
7676
.setTyper(new Typer)
7777
.addMode(Mode.ImplicitsEnabled)
7878
.setTyperState(ctx.typerState.fresh(ctx.reporter))
79+
if ctx.settings.YexplicitNulls.value then
80+
start = start.addMode(Mode.SafeNulls)
7981
ctx.initialize()(using start) // re-initialize the base context with start
8082
start.setRun(this)
8183
}

compiler/src/dotty/tools/dotc/core/Contexts.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import collection.mutable
2727
import printing._
2828
import config.{JavaPlatform, SJSPlatform, Platform, ScalaSettings}
2929
import classfile.ReusableDataReader
30+
import StdNames.nme
3031

3132
import scala.annotation.internal.sharable
3233

@@ -627,7 +628,14 @@ object Contexts {
627628
def setRun(run: Run): this.type = updateStore(runLoc, run)
628629
def setProfiler(profiler: Profiler): this.type = updateStore(profilerLoc, profiler)
629630
def setNotNullInfos(notNullInfos: List[NotNullInfo]): this.type = updateStore(notNullInfosLoc, notNullInfos)
630-
def setImportInfo(importInfo: ImportInfo): this.type = updateStore(importInfoLoc, importInfo)
631+
def setImportInfo(importInfo: ImportInfo): this.type =
632+
importInfo.mentionsFeature(nme.unsafeNulls) match
633+
case Some(true) =>
634+
setMode(this.mode &~ Mode.SafeNulls)
635+
case Some(false) if ctx.settings.YexplicitNulls.value =>
636+
setMode(this.mode | Mode.SafeNulls)
637+
case _ =>
638+
updateStore(importInfoLoc, importInfo)
631639
def setTypeAssigner(typeAssigner: TypeAssigner): this.type = updateStore(typeAssignerLoc, typeAssigner)
632640

633641
def setProperty[T](key: Key[T], value: T): this.type =

compiler/src/dotty/tools/dotc/core/Mode.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,7 @@ object Mode {
119119

120120
/** Are we resolving a TypeTest node? */
121121
val InTypeTest: Mode = newMode(27, "InTypeTest")
122+
123+
/** Are we enforcing null safety */
124+
val SafeNulls = newMode(28, "SafeNulls")
122125
}

compiler/src/dotty/tools/dotc/core/StdNames.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ object StdNames {
613613
val unapplySeq: N = "unapplySeq"
614614
val unbox: N = "unbox"
615615
val universe: N = "universe"
616+
val unsafeNulls: N = "unsafeNulls"
616617
val update: N = "update"
617618
val updateDynamic: N = "updateDynamic"
618619
val using: N = "using"

library/src/scala/runtime/stdLibPatches/language.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ object language:
7777
*/
7878
object adhocExtensions
7979

80+
object unsafeNulls
81+
8082
/** Set source version to 3.0-migration.
8183
*
8284
* @see [[https://scalacenter.github.io/scala-3-migration-guide/docs/scala-3-migration-mode]]

0 commit comments

Comments
 (0)