Skip to content

Commit 35d4492

Browse files
committed
Initial migration to explicit nulls
1 parent 6b1a662 commit 35d4492

File tree

81 files changed

+569
-338
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+569
-338
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
package dotty.tools
22

3+
import scala.language.{unsafeNulls => _}
4+
35
case class FatalError(msg: String) extends Exception(msg)

compiler/src/dotty/tools/MainGenericRunner.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dotty.tools
22

3+
import scala.language.unsafeNulls
34

45
import scala.annotation.tailrec
56
import scala.io.Source

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dotty.tools
22
package dotc
33

4+
import scala.language.{unsafeNulls => _}
5+
46
import core.Contexts._
57
import reporting.Reporter
68
import io.AbstractFile
@@ -30,7 +32,7 @@ object Bench extends Driver:
3032
println(s"time elapsed: ${times(i)}ms")
3133
if ctx.settings.Xprompt.value then
3234
print("hit <return> to continue >")
33-
System.in.read()
35+
System.in.nn.read()
3436
println()
3537
reporter
3638

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dotty.tools
22
package dotc
33

4+
import scala.language.{unsafeNulls => _}
5+
46
import core._
57
import Contexts._
68
import SymDenotations.ClassDenotation
@@ -76,15 +78,15 @@ class CompilationUnit protected (val source: SourceFile) {
7678
suspendedAtInliningPhase = true
7779
throw CompilationUnit.SuspendException()
7880

79-
private var myAssignmentSpans: Map[Int, List[Span]] = null
81+
private var myAssignmentSpans: Map[Int, List[Span]] | Null = null
8082

8183
/** A map from (name-) offsets of all local variables in this compilation unit
8284
* that can be tracked for being not null to the list of spans of assignments
8385
* to these variables.
8486
*/
8587
def assignmentSpans(using Context): Map[Int, List[Span]] =
8688
if myAssignmentSpans == null then myAssignmentSpans = Nullables.assignmentSpans
87-
myAssignmentSpans
89+
myAssignmentSpans.nn
8890
}
8991

9092
object CompilationUnit {
@@ -93,7 +95,9 @@ object CompilationUnit {
9395

9496
/** Make a compilation unit for top class `clsd` with the contents of the `unpickled` tree */
9597
def apply(clsd: ClassDenotation, unpickled: Tree, forceTrees: Boolean)(using Context): CompilationUnit =
96-
apply(new SourceFile(clsd.symbol.associatedFile, Array.empty[Char]), unpickled, forceTrees)
98+
val file = clsd.symbol.associatedFile
99+
// TODO: could file be null?
100+
apply(new SourceFile(file.nn, Array.empty[Char]), unpickled, forceTrees)
97101

98102
/** Make a compilation unit, given picked bytes and unpickled tree */
99103
def apply(source: SourceFile, unpickled: Tree, forceTrees: Boolean)(using Context): CompilationUnit = {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dotty.tools
22
package dotc
33

4+
import scala.language.{unsafeNulls => _}
5+
46
import core._
57
import Contexts._
68
import typer.{TyperPhase, RefChecks}
@@ -150,7 +152,8 @@ class Compiler {
150152

151153
def reset()(using Context): Unit = {
152154
ctx.base.reset()
153-
if (ctx.run != null) ctx.run.reset()
155+
val run: Run | Null = ctx.run
156+
if (run != null) run.reset()
154157
}
155158

156159
def newRun(using Context): Run = {

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dotty.tools.dotc
22

3+
import scala.language.{unsafeNulls => _}
4+
35
import java.nio.file.{Files, Paths}
46

57
import dotty.tools.FatalError
@@ -32,15 +34,18 @@ class Driver {
3234

3335
protected def emptyReporter: Reporter = new StoreReporter(null)
3436

35-
protected def doCompile(compiler: Compiler, files: List[AbstractFile])(using Context): Reporter =
37+
protected def doCompile(compiler: Compiler, files: List[AbstractFile])(using Context): Reporter =
3638
if files.nonEmpty then
3739
try
3840
val run = compiler.newRun
3941
run.compile(files)
4042
finish(compiler, run)
4143
catch
4244
case ex: FatalError =>
43-
report.error(ex.getMessage) // signals that we should fail compilation.
45+
val msg = ex.getMessage
46+
if msg != null then
47+
report.error(msg) // signals that we should fail compilation.
48+
else report.error("null")
4449
case ex: TypeError =>
4550
println(s"${ex.toMessage} while compiling ${files.map(_.path).mkString(", ")}")
4651
throw ex
@@ -115,7 +120,7 @@ class Driver {
115120
.distinct
116121
val ctx1 = ctx.fresh
117122
val fullClassPath =
118-
(newEntries :+ ctx.settings.classpath.value).mkString(java.io.File.pathSeparator)
123+
(newEntries :+ ctx.settings.classpath.value).mkString(java.io.File.pathSeparator.nn)
119124
ctx1.setSetting(ctx1.settings.classpath, fullClassPath)
120125
else ctx
121126

@@ -138,8 +143,8 @@ class Driver {
138143
* process. No callbacks will be executed if this is `null`.
139144
* @return
140145
*/
141-
final def process(args: Array[String], simple: interfaces.SimpleReporter,
142-
callback: interfaces.CompilerCallback): interfaces.ReporterResult = {
146+
final def process(args: Array[String], simple: interfaces.SimpleReporter | Null,
147+
callback: interfaces.CompilerCallback | Null): interfaces.ReporterResult = {
143148
val reporter = if (simple == null) null else Reporter.fromSimpleReporter(simple)
144149
process(args, reporter, callback)
145150
}
@@ -157,8 +162,8 @@ class Driver {
157162
* @return The `Reporter` used. Use `Reporter#hasErrors` to check
158163
* if compilation succeeded.
159164
*/
160-
final def process(args: Array[String], reporter: Reporter = null,
161-
callback: interfaces.CompilerCallback = null): Reporter = {
165+
final def process(args: Array[String], reporter: Reporter | Null = null,
166+
callback: interfaces.CompilerCallback | Null = null): Reporter = {
162167
val compileCtx = initCtx.fresh
163168
if (reporter != null)
164169
compileCtx.setReporter(reporter)
@@ -176,7 +181,7 @@ class Driver {
176181
* with sbt.
177182
*/
178183
final def process(args: Array[String]): Reporter =
179-
process(args, null: Reporter, null: interfaces.CompilerCallback)
184+
process(args, null: Reporter | Null, null: interfaces.CompilerCallback | Null)
180185

181186
/** Entry point to the compiler using a custom `Context`.
182187
*
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dotty.tools
22
package dotc
33

4+
import scala.language.{unsafeNulls => _}
5+
46
/** Main class of the `dotc` batch compiler. */
57
object Main extends Driver

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dotty.tools.dotc
22

3+
import scala.language.{unsafeNulls => _}
4+
35
import dotty.tools.FatalError
46

57
class MissingCoreLibraryException(rootPackage: String) extends FatalError(

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dotty.tools
22
package dotc
33

4+
import scala.language.{unsafeNulls => _}
5+
46
import core.Contexts._
57
import reporting.Reporter
68
import java.io.EOFException
@@ -52,7 +54,9 @@ class Resident extends Driver {
5254
line = getLine()
5355
}
5456
if (line.startsWith(quit)) ctx.reporter
55-
else loop(line split "\\s+", nextCtx)
57+
else
58+
// assuming split returns non-nullable values
59+
loop((line split "\\s+").asInstanceOf, nextCtx)
5660
case None =>
5761
prevCtx.reporter
5862
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dotty.tools
22
package dotc
33

4+
import scala.language.{unsafeNulls => _}
5+
46
import core._
57
import Contexts._
68
import Periods._
@@ -109,7 +111,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
109111
// When the REPL creates a new run (ReplDriver.compile), parsing is already done in the old context, with the
110112
// previous Run. Parser warnings were suspended in the old run and need to be copied over so they are not lost.
111113
// Same as scala/scala/commit/79ca1408c7.
112-
def initSuspendedMessages(oldRun: Run) = if oldRun != null then
114+
def initSuspendedMessages(oldRun: Run | Null) = if oldRun != null then
113115
mySuspendedMessages.clear()
114116
mySuspendedMessages ++= oldRun.mySuspendedMessages
115117

@@ -206,7 +208,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
206208
compileSources(sources)
207209
catch
208210
case NonFatal(ex) =>
209-
if units != null then report.echo(i"exception occurred while compiling $units%, %")
211+
if !units.isInitialized then report.echo(i"exception occurred while compiling $units%, %")
210212
else report.echo(s"exception occurred while compiling ${files.map(_.name).mkString(", ")}")
211213
throw ex
212214

@@ -351,7 +353,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
351353
val uuid = java.util.UUID.randomUUID().toString
352354
val ext = if (isJava) ".java" else ".scala"
353355
val virtualFile = new VirtualFile(s"compileFromString-$uuid.$ext")
354-
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, StandardCharsets.UTF_8.name)) // buffering is still advised by javadoc
356+
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, StandardCharsets.UTF_8.nn.name)) // buffering is still advised by javadoc
355357
writer.write(source)
356358
writer.close()
357359
new SourceFile(virtualFile, Codec.UTF8)
@@ -373,8 +375,8 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
373375
override def reset(): Unit = {
374376
super[ImplicitRunInfo].reset()
375377
super[ConstraintRunInfo].reset()
376-
myCtx = null
377-
myUnits = null
378-
myUnitsCached = null
378+
myCtx = null.asInstanceOf
379+
myUnits = null.asInstanceOf
380+
myUnitsCached = null.asInstanceOf
379381
}
380382
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dotty.tools.dotc
22

3+
import scala.language.{unsafeNulls => _}
4+
35
import config.Properties._
46
import config.CompilerCommand
57

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package dotty.tools
22
package dotc
33
package ast
44

5+
import scala.language.{unsafeNulls => _}
6+
57
import core._
68
import util.Spans._, Types._, Contexts._, Constants._, Names._, NameOps._, Flags._
79
import Symbols._, StdNames._, Trees._, Phases._, ContextOps._
@@ -1344,7 +1346,7 @@ object desugar {
13441346
* def $anonfun(params) = body
13451347
* Closure($anonfun)
13461348
*/
1347-
def makeClosure(params: List[ValDef], body: Tree, tpt: Tree = null, isContextual: Boolean, span: Span)(using Context): Block =
1349+
def makeClosure(params: List[ValDef], body: Tree, tpt: Tree | Null = null, isContextual: Boolean, span: Span)(using Context): Block =
13481350
Block(
13491351
DefDef(nme.ANON_FUN, params :: Nil, if (tpt == null) TypeTree() else tpt, body)
13501352
.withSpan(span)

compiler/src/dotty/tools/dotc/ast/DesugarEnums.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package dotty.tools
22
package dotc
33
package ast
44

5+
import scala.language.{unsafeNulls => _}
6+
57
import core._
68
import util.Spans._, Types._, Contexts._, Constants._, Names._, NameOps._, Flags._
79
import Symbols._, StdNames._, Trees._

compiler/src/dotty/tools/dotc/ast/MainProxies.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dotty.tools.dotc
22
package ast
33

4+
import scala.language.{unsafeNulls => _}
5+
46
import core._
57
import Symbols._, Types._, Contexts._, Decorators._, util.Spans._, Flags._, Constants._
68
import StdNames.nme

compiler/src/dotty/tools/dotc/ast/NavigateAST.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dotty.tools.dotc
22
package ast
33

4+
import scala.language.{unsafeNulls => _}
5+
46
import core.Contexts._
57
import core.Decorators._
68
import util.Spans._

compiler/src/dotty/tools/dotc/ast/Positioned.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package dotty.tools
22
package dotc
33
package ast
44

5+
import scala.language.{unsafeNulls => _}
6+
57
import util.Spans._
68
import util.{SourceFile, NoSource, SourcePosition, SrcPos}
79
import core.Contexts._
@@ -29,13 +31,13 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Src
2931
* is set, -1 otherwise.
3032
*/
3133
def uniqueId: Int =
32-
if ids != null && ids.containsKey(this) then ids.get(this) else -1
34+
if ids != null && ids.nn.containsKey(this) then ids.nn.get(this).nn else -1
3335

3436
private def allocateId() =
3537
if ids != null then
3638
val ownId = nextId
3739
nextId += 1
38-
ids.put(this, ownId)
40+
ids.nn.put(this, ownId)
3941
if ownId == debugId then
4042
println(s"Debug tree (id=$debugId) creation \n$this\n")
4143
Thread.dumpStack()
@@ -163,7 +165,7 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Src
163165
*/
164166
def checkPos(nonOverlapping: Boolean)(using Context): Unit = try {
165167
import untpd._
166-
var lastPositioned: Positioned = null
168+
var lastPositioned: Positioned | Null = null
167169
var lastSpan = NoSpan
168170
def check(p: Any): Unit = p match {
169171
case p: Positioned =>
@@ -184,7 +186,7 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Src
184186
case _: XMLBlock =>
185187
// FIXME: Trees generated by the XML parser do not satisfy `checkPos`
186188
case _: WildcardFunction
187-
if lastPositioned.isInstanceOf[ValDef] && !p.isInstanceOf[ValDef] =>
189+
if lastPositioned != null && lastPositioned.isInstanceOf[ValDef] && !p.isInstanceOf[ValDef] =>
188190
// ignore transition from last wildcard parameter to body
189191
case _ =>
190192
assert(!lastSpan.exists || !p.span.exists || lastSpan.end <= p.span.start,
@@ -237,7 +239,7 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Src
237239

238240
object Positioned {
239241
@sharable private var debugId = Int.MinValue
240-
@sharable private var ids: java.util.WeakHashMap[Positioned, Int] = null
242+
@sharable private var ids: java.util.WeakHashMap[Positioned, Int] | Null = null
241243
@sharable private var nextId: Int = 0
242244

243245
def init(using Context): Unit =

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package dotty.tools
22
package dotc
33
package ast
44

5+
import scala.language.{unsafeNulls => _}
6+
57
import core._
68
import Flags._, Trees._, Types._, Contexts._
79
import Names._, StdNames._, NameOps._, Symbols._
@@ -823,8 +825,9 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
823825
* tree must be reachable from come tree stored in an enclosing context.
824826
*/
825827
def definingStats(sym: Symbol)(using Context): List[Tree] =
826-
if (!sym.span.exists || (ctx eq NoContext) || ctx.compilationUnit == null) Nil
827-
else defPath(sym, ctx.compilationUnit.tpdTree) match {
828+
val unit: CompilationUnit | Null = ctx.compilationUnit
829+
if (!sym.span.exists || (ctx eq NoContext) || unit == null) Nil
830+
else defPath(sym, unit.tpdTree) match {
828831
case defn :: encl :: _ =>
829832
def verify(stats: List[Tree]) =
830833
if (stats exists (definedSym(_) == sym)) stats else Nil

compiler/src/dotty/tools/dotc/ast/TreeMapWithImplicits.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dotty.tools.dotc
22
package ast
33

4+
import scala.language.{unsafeNulls => _}
5+
46
import Trees._
57
import core.Contexts._
68
import core.ContextOps.enter

compiler/src/dotty/tools/dotc/ast/TreeTypeMap.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package dotty.tools
22
package dotc
33
package ast
44

5+
import scala.language.{unsafeNulls => _}
6+
57
import core._
68
import Types._, Contexts._, Flags._
79
import Symbols._, Annotations._, Trees._, Symbols._, Constants.Constant

0 commit comments

Comments
 (0)