Skip to content

Commit 7f07500

Browse files
committed
Constant fold null comparisons only under -Yexplicit-nulls
1 parent a717f4c commit 7f07500

File tree

5 files changed

+6
-2
lines changed

5 files changed

+6
-2
lines changed

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ class ScalaSettings extends Settings.SettingGroup {
162162

163163
// Extremely experimental language features
164164
val YnoKindPolymorphism: Setting[Boolean] = BooleanSetting("-Yno-kind-polymorphism", "Enable kind polymorphism (see https://dotty.epfl.ch/docs/reference/kind-polymorphism.html). Potentially unsound.")
165+
val YexplicitNulls: Setting[Boolean] = BooleanSetting("-Yexplicit-nulls", "Make reference types non-nullable. Nullable types can be expressed with unions: e.g. String|Null.")
165166

166167
/** Area-specific debug output */
167168
val YexplainLowlevel: Setting[Boolean] = BooleanSetting("-Yexplain-lowlevel", "When explaining type errors, show types at a lower level.")

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ object ConstFold {
2020
/** If tree is a constant operation, replace with result. */
2121
def apply[T <: Tree](tree: T)(implicit ctx: Context): T = finish(tree) {
2222
tree match {
23-
case CompareNull(TrackedRef(ref), testEqual) if ctx.notNullInfos.containsRef(ref) =>
23+
case CompareNull(TrackedRef(ref), testEqual)
24+
if ctx.settings.YexplicitNulls.value && ctx.notNullInfos.containsRef(ref) =>
2425
// TODO maybe drop once we have general Nullability?
2526
Constant(!testEqual)
2627
case Apply(Select(xt, op), yt :: Nil) =>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ object Nullables with
109109
&& sym.owner.isTerm
110110
&& sym.owner.enclosingMethod == curCtx.owner.enclosingMethod
111111
&& sym.span.exists
112+
&& curCtx.compilationUnit != null // could be null under -Ytest-pickler
112113
&& curCtx.compilationUnit.trackedVarSpans.contains(sym.span.start)
113-
}
114+
}
114115

115116
def afterPatternContext(sel: Tree, pat: Tree)(given ctx: Context) = (sel, pat) match
116117
case (TrackedRef(ref), Literal(Constant(null))) => ctx.addNotNullRefs(Set(ref))

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class CompilationTests extends ParallelTesting {
5959
compileFile("tests/pos-special/typeclass-scaling.scala", defaultOptions.and("-Xmax-inlines", "40")),
6060
compileFile("tests/pos-special/indent-colons.scala", defaultOptions.and("-Yindent-colons")),
6161
compileFile("tests/pos-special/i7296.scala", defaultOptions.and("-strict", "-deprecation", "-Xfatal-warnings")),
62+
compileFile("tests/pos-special/nullable.scala", defaultOptions.and("-Yexplicit-nulls")),
6263
compileDir("tests/pos-special/adhoc-extension", defaultOptions.and("-strict", "-feature", "-Xfatal-warnings"))
6364
).checkCompile()
6465
}
File renamed without changes.

0 commit comments

Comments
 (0)