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
}

0 commit comments

Comments
 (0)