Skip to content

Commit 48eba1c

Browse files
committed
First step of migration to explicit nulls
1 parent 29f9d33 commit 48eba1c

File tree

226 files changed

+1358
-828
lines changed

Some content is hidden

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

226 files changed

+1358
-828
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: 17 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
@@ -14,6 +16,7 @@ import typer.Nullables
1416
import transform.SymUtils._
1517
import core.Decorators._
1618
import config.SourceVersion
19+
import scala.annotation.internal.sharable
1720

1821
class CompilationUnit protected (val source: SourceFile) {
1922

@@ -76,15 +79,25 @@ class CompilationUnit protected (val source: SourceFile) {
7679
suspendedAtInliningPhase = true
7780
throw CompilationUnit.SuspendException()
7881

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

8184
/** A map from (name-) offsets of all local variables in this compilation unit
8285
* that can be tracked for being not null to the list of spans of assignments
8386
* to these variables.
8487
*/
8588
def assignmentSpans(using Context): Map[Int, List[Span]] =
8689
if myAssignmentSpans == null then myAssignmentSpans = Nullables.assignmentSpans
87-
myAssignmentSpans
90+
myAssignmentSpans.nn
91+
}
92+
93+
@sharable object NoCompilationUnit extends CompilationUnit(NoSource) {
94+
95+
override def isJava: Boolean = false
96+
97+
override def suspend()(using Context): Nothing =
98+
throw CompilationUnit.SuspendException()
99+
100+
override def assignmentSpans(using Context): Map[Int, List[Span]] = Map.empty
88101
}
89102

90103
object CompilationUnit {
@@ -93,7 +106,8 @@ object CompilationUnit {
93106

94107
/** Make a compilation unit for top class `clsd` with the contents of the `unpickled` tree */
95108
def apply(clsd: ClassDenotation, unpickled: Tree, forceTrees: Boolean)(using Context): CompilationUnit =
96-
apply(new SourceFile(clsd.symbol.associatedFile, Array.empty[Char]), unpickled, forceTrees)
109+
val file = clsd.symbol.associatedFile.nn
110+
apply(new SourceFile(file, Array.empty[Char]), unpickled, forceTrees)
97111

98112
/** Make a compilation unit, given picked bytes and unpickled tree */
99113
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}
@@ -152,7 +154,8 @@ class Compiler {
152154

153155
def reset()(using Context): Unit = {
154156
ctx.base.reset()
155-
if (ctx.run != null) ctx.run.reset()
157+
val run: Run | Null = ctx.run
158+
if (run != null) run.reset()
156159
}
157160

158161
def newRun(using Context): Run = {

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

Lines changed: 10 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,15 @@ 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+
report.error(ex.getMessage.nn) // signals that we should fail compilation.
4446
case ex: TypeError =>
4547
println(s"${ex.toMessage} while compiling ${files.map(_.path).mkString(", ")}")
4648
throw ex
@@ -115,7 +117,7 @@ class Driver {
115117
.distinct
116118
val ctx1 = ctx.fresh
117119
val fullClassPath =
118-
(newEntries :+ ctx.settings.classpath.value).mkString(java.io.File.pathSeparator)
120+
(newEntries :+ ctx.settings.classpath.value).mkString(java.io.File.pathSeparator.nn)
119121
ctx1.setSetting(ctx1.settings.classpath, fullClassPath)
120122
else ctx
121123

@@ -138,8 +140,8 @@ class Driver {
138140
* process. No callbacks will be executed if this is `null`.
139141
* @return
140142
*/
141-
final def process(args: Array[String], simple: interfaces.SimpleReporter,
142-
callback: interfaces.CompilerCallback): interfaces.ReporterResult = {
143+
final def process(args: Array[String], simple: interfaces.SimpleReporter | Null,
144+
callback: interfaces.CompilerCallback | Null): interfaces.ReporterResult = {
143145
val reporter = if (simple == null) null else Reporter.fromSimpleReporter(simple)
144146
process(args, reporter, callback)
145147
}
@@ -157,8 +159,8 @@ class Driver {
157159
* @return The `Reporter` used. Use `Reporter#hasErrors` to check
158160
* if compilation succeeded.
159161
*/
160-
final def process(args: Array[String], reporter: Reporter = null,
161-
callback: interfaces.CompilerCallback = null): Reporter = {
162+
final def process(args: Array[String], reporter: Reporter | Null = null,
163+
callback: interfaces.CompilerCallback | Null = null): Reporter = {
162164
val compileCtx = initCtx.fresh
163165
if (reporter != null)
164166
compileCtx.setReporter(reporter)
@@ -176,7 +178,7 @@ class Driver {
176178
* with sbt.
177179
*/
178180
final def process(args: Array[String]): Reporter =
179-
process(args, null: Reporter, null: interfaces.CompilerCallback)
181+
process(args, null: Reporter | Null, null: interfaces.CompilerCallback | Null)
180182

181183
/** Entry point to the compiler using a custom `Context`.
182184
*
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+
// split returns non-nullable values
59+
loop((line split "\\s+").asInstanceOf[Array[String]], nextCtx)
5660
case None =>
5761
prevCtx.reporter
5862
}

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

Lines changed: 11 additions & 9 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._
@@ -59,8 +61,8 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
5961

6062
private var compiling = false
6163

62-
private var myUnits: List[CompilationUnit] = _
63-
private var myUnitsCached: List[CompilationUnit] = _
64+
private var myUnits: List[CompilationUnit] = Nil
65+
private var myUnitsCached: List[CompilationUnit] = Nil
6466
private var myFiles: Set[AbstractFile] = _
6567

6668
// `@nowarn` annotations by source file, populated during typer
@@ -74,7 +76,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
7476
// When the REPL creates a new run (ReplDriver.compile), parsing is already done in the old context, with the
7577
// previous Run. Parser warnings were suspended in the old run and need to be copied over so they are not lost.
7678
// Same as scala/scala/commit/79ca1408c7.
77-
def initSuspendedMessages(oldRun: Run) = if oldRun != null then
79+
def initSuspendedMessages(oldRun: Run | Null) = if oldRun != null then
7880
mySuspendedMessages.clear()
7981
mySuspendedMessages ++= oldRun.mySuspendedMessages
8082

@@ -171,7 +173,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
171173
compileSources(sources)
172174
catch
173175
case NonFatal(ex) =>
174-
if units != null then report.echo(i"exception occurred while compiling $units%, %")
176+
if units != Nil then report.echo(i"exception occurred while compiling $units%, %")
175177
else report.echo(s"exception occurred while compiling ${files.map(_.name).mkString(", ")}")
176178
throw ex
177179

@@ -310,7 +312,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
310312
val uuid = java.util.UUID.randomUUID().toString
311313
val ext = if (isJava) ".java" else ".scala"
312314
val virtualFile = new VirtualFile(s"compileFromString-$uuid.$ext")
313-
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, StandardCharsets.UTF_8.name)) // buffering is still advised by javadoc
315+
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, StandardCharsets.UTF_8.nn.name)) // buffering is still advised by javadoc
314316
writer.write(source)
315317
writer.close()
316318
new SourceFile(virtualFile, Codec.UTF8)
@@ -333,8 +335,8 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
333335
super[ImplicitRunInfo].reset()
334336
super[ConstraintRunInfo].reset()
335337
myCtx = null
336-
myUnits = null
337-
myUnitsCached = null
338+
myUnits = Nil
339+
myUnitsCached = Nil
338340
}
339341

340342
/** Produces the following contexts, from outermost to innermost
@@ -367,9 +369,9 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
367369
start.setRun(this: @unchecked)
368370
}
369371

370-
private var myCtx = rootContext(using ictx)
372+
private var myCtx: Context | Null = rootContext(using ictx)
371373

372374
/** The context created for this run */
373-
given runContext[Dummy_so_its_a_def]: Context = myCtx
375+
given runContext[Dummy_so_its_a_def]: Context = myCtx.nn
374376
assert(runContext.runId <= Periods.MaxPossibleRunId)
375377
}

0 commit comments

Comments
 (0)