Skip to content

Commit a05ff76

Browse files
authored
Merge pull request #14032 from dotty-staging/en-migrate-main
Migrating the main compiler to explicit nulls
2 parents 1b25f65 + 48e4697 commit a05ff76

File tree

290 files changed

+1262
-811
lines changed

Some content is hidden

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

290 files changed

+1262
-811
lines changed

compiler/src/dotty/tools/MainGenericRunner.scala

+1
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/backend/jvm/AsmUtils.scala

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package dotty.tools
22
package backend
33
package jvm
44

5+
import scala.language.unsafeNulls
6+
57
import scala.tools.asm.tree.{AbstractInsnNode}
68
import java.io.PrintWriter
79
import scala.tools.asm.util.{TraceClassVisitor, TraceMethodVisitor, Textifier}

compiler/src/dotty/tools/backend/jvm/BCodeAsmCommon.scala

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package dotty.tools
22
package backend
33
package jvm
44

5+
import scala.language.unsafeNulls
6+
57
import dotty.tools.dotc.core.Flags._
68
import dotty.tools.dotc.core.Symbols._
79
import dotty.tools.dotc.report

compiler/src/dotty/tools/backend/jvm/BCodeBodyBuilder.scala

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package dotty.tools
22
package backend
33
package jvm
44

5+
import scala.language.unsafeNulls
6+
57
import scala.annotation.switch
68
import scala.collection.mutable.SortedMap
79

compiler/src/dotty/tools/backend/jvm/BCodeHelpers.scala

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package dotty.tools
22
package backend
33
package jvm
44

5+
import scala.language.unsafeNulls
6+
57
import scala.annotation.threadUnsafe
68
import scala.tools.asm
79
import scala.tools.asm.AnnotationVisitor

compiler/src/dotty/tools/backend/jvm/BCodeIdiomatic.scala

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package dotty.tools
22
package backend
33
package jvm
44

5+
import scala.language.unsafeNulls
6+
57
import scala.tools.asm
68
import scala.annotation.switch
79
import Primitives.{NE, EQ, TestOp, ArithmeticOp}

compiler/src/dotty/tools/backend/jvm/BCodeSkelBuilder.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package dotty.tools
22
package backend
33
package jvm
44

5+
import scala.language.unsafeNulls
6+
57
import scala.collection.{ mutable, immutable }
68

79
import scala.tools.asm
8-
910
import dotty.tools.dotc.ast.tpd
1011
import dotty.tools.dotc.ast.TreeTypeMap
1112
import dotty.tools.dotc.CompilationUnit

compiler/src/dotty/tools/backend/jvm/BCodeSyncAndTry.scala

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package dotty.tools
22
package backend
33
package jvm
44

5+
import scala.language.unsafeNulls
6+
57
import scala.collection.immutable
68
import scala.tools.asm
79

compiler/src/dotty/tools/backend/jvm/BTypes.scala

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package dotty.tools
22
package backend
33
package jvm
44

5+
import scala.language.unsafeNulls
6+
57
import scala.tools.asm
68

79
/**

compiler/src/dotty/tools/backend/jvm/BytecodeWriters.scala

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package dotty.tools
22
package backend
33
package jvm
44

5+
import scala.language.unsafeNulls
6+
57
import java.io.{ DataOutputStream, FileOutputStream, IOException, File as JFile }
68
import java.nio.channels.ClosedByInterruptException
79
import dotty.tools.io._

compiler/src/dotty/tools/backend/jvm/DottyBackendInterface.scala

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dotty.tools.backend.jvm
22

3+
import scala.language.unsafeNulls
4+
35
import dotty.tools.dotc.ast.tpd
46
import dotty.tools.dotc.core.Flags._
57
import dotty.tools.dotc.transform.SymUtils._

compiler/src/dotty/tools/backend/jvm/GenBCode.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dotty.tools.backend.jvm
22

3+
import scala.language.unsafeNulls
4+
35
import dotty.tools.dotc.CompilationUnit
46
import dotty.tools.dotc.ast.Trees.{PackageDef, ValDef}
57
import dotty.tools.dotc.ast.tpd
@@ -73,7 +75,7 @@ class GenBCode extends Phase {
7375
try super.runOn(units)
7476
finally outputDir match {
7577
case jar: JarArchive =>
76-
if (ctx.run.suspendedUnits.nonEmpty)
78+
if (ctx.run.nn.suspendedUnits.nonEmpty)
7779
// If we close the jar the next run will not be able to write on the jar.
7880
// But if we do not close it we cannot use it as part of the macro classpath of the suspended files.
7981
report.error("Can not suspend and output to a jar at the same time. See suspension with -Xprint-suspension.")

compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package dotty.tools.backend.sjs
22

3-
import scala.annotation.switch
3+
import scala.language.unsafeNulls
44

5+
import scala.annotation.switch
56
import scala.collection.mutable
67

78
import dotty.tools.FatalError
8-
99
import dotty.tools.dotc.CompilationUnit
1010
import dotty.tools.dotc.ast.tpd
11-
1211
import dotty.tools.dotc.core._
1312
import Contexts._
1413
import Decorators._

compiler/src/dotty/tools/backend/sjs/JSDefinitions.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package dotty.tools.backend.sjs
22

3-
import dotty.tools.dotc.core._
3+
import scala.language.unsafeNulls
44

55
import scala.annotation.threadUnsafe
6+
7+
import dotty.tools.dotc.core._
68
import Types._
79
import Contexts._
810
import Symbols._

compiler/src/dotty/tools/backend/sjs/JSEncoding.scala

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dotty.tools.backend.sjs
22

3+
import scala.language.unsafeNulls
4+
35
import scala.collection.mutable
46

57
import dotty.tools.dotc.core._

compiler/src/dotty/tools/backend/sjs/JSExportsGen.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package dotty.tools.backend.sjs
22

3-
import scala.annotation.tailrec
3+
import scala.language.unsafeNulls
44

5+
import scala.annotation.tailrec
56
import scala.collection.mutable
67

78
import dotty.tools.dotc.core._

compiler/src/dotty/tools/backend/sjs/JSPositions.scala

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dotty.tools.backend.sjs
22

3+
import scala.language.unsafeNulls
4+
35
import java.net.{URI, URISyntaxException}
46

57
import dotty.tools.dotc.core._

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ object Bench extends Driver:
3030
println(s"time elapsed: ${times(i)}ms")
3131
if ctx.settings.Xprompt.value then
3232
print("hit <return> to continue >")
33-
System.in.read()
33+
System.in.nn.read()
3434
println()
3535
reporter
3636

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

+16-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import typer.Nullables
1313
import transform.SymUtils._
1414
import core.Decorators._
1515
import config.SourceVersion
16+
import scala.annotation.internal.sharable
1617

1718
class CompilationUnit protected (val source: SourceFile) {
1819

@@ -70,20 +71,30 @@ class CompilationUnit protected (val source: SourceFile) {
7071
if (ctx.settings.XprintSuspension.value)
7172
report.echo(i"suspended: $this")
7273
suspended = true
73-
ctx.run.suspendedUnits += this
74+
ctx.run.nn.suspendedUnits += this
7475
if ctx.phase == Phases.inliningPhase then
7576
suspendedAtInliningPhase = true
7677
throw CompilationUnit.SuspendException()
7778

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

8081
/** A map from (name-) offsets of all local variables in this compilation unit
8182
* that can be tracked for being not null to the list of spans of assignments
8283
* to these variables.
8384
*/
8485
def assignmentSpans(using Context): Map[Int, List[Span]] =
8586
if myAssignmentSpans == null then myAssignmentSpans = Nullables.assignmentSpans
86-
myAssignmentSpans
87+
myAssignmentSpans.nn
88+
}
89+
90+
@sharable object NoCompilationUnit extends CompilationUnit(NoSource) {
91+
92+
override def isJava: Boolean = false
93+
94+
override def suspend()(using Context): Nothing =
95+
throw CompilationUnit.SuspendException()
96+
97+
override def assignmentSpans(using Context): Map[Int, List[Span]] = Map.empty
8798
}
8899

89100
object CompilationUnit {
@@ -92,7 +103,8 @@ object CompilationUnit {
92103

93104
/** Make a compilation unit for top class `clsd` with the contents of the `unpickled` tree */
94105
def apply(clsd: ClassDenotation, unpickled: Tree, forceTrees: Boolean)(using Context): CompilationUnit =
95-
apply(new SourceFile(clsd.symbol.associatedFile, Array.empty[Char]), unpickled, forceTrees)
106+
val file = clsd.symbol.associatedFile.nn
107+
apply(new SourceFile(file, Array.empty[Char]), unpickled, forceTrees)
96108

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

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ class Compiler {
152152

153153
def reset()(using Context): Unit = {
154154
ctx.base.reset()
155-
if (ctx.run != null) ctx.run.reset()
155+
val run = ctx.run
156+
if (run != null) run.reset()
156157
}
157158

158159
def newRun(using Context): Run = {

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ class Driver {
2828

2929
protected def emptyReporter: Reporter = new StoreReporter(null)
3030

31-
protected def doCompile(compiler: Compiler, files: List[AbstractFile])(using Context): Reporter =
31+
protected def doCompile(compiler: Compiler, files: List[AbstractFile])(using Context): Reporter =
3232
if files.nonEmpty then
3333
try
3434
val run = compiler.newRun
3535
run.compile(files)
3636
finish(compiler, run)
3737
catch
3838
case ex: FatalError =>
39-
report.error(ex.getMessage) // signals that we should fail compilation.
39+
report.error(ex.getMessage.nn) // signals that we should fail compilation.
4040
case ex: TypeError =>
4141
println(s"${ex.toMessage} while compiling ${files.map(_.path).mkString(", ")}")
4242
throw ex
@@ -111,7 +111,7 @@ class Driver {
111111
.distinct
112112
val ctx1 = ctx.fresh
113113
val fullClassPath =
114-
(newEntries :+ ctx.settings.classpath.value).mkString(java.io.File.pathSeparator)
114+
(newEntries :+ ctx.settings.classpath.value).mkString(java.io.File.pathSeparator.nn)
115115
ctx1.setSetting(ctx1.settings.classpath, fullClassPath)
116116
else ctx
117117

@@ -134,8 +134,8 @@ class Driver {
134134
* process. No callbacks will be executed if this is `null`.
135135
* @return
136136
*/
137-
final def process(args: Array[String], simple: interfaces.SimpleReporter,
138-
callback: interfaces.CompilerCallback): interfaces.ReporterResult = {
137+
final def process(args: Array[String], simple: interfaces.SimpleReporter | Null,
138+
callback: interfaces.CompilerCallback | Null): interfaces.ReporterResult = {
139139
val reporter = if (simple == null) null else Reporter.fromSimpleReporter(simple)
140140
process(args, reporter, callback)
141141
}
@@ -153,8 +153,8 @@ class Driver {
153153
* @return The `Reporter` used. Use `Reporter#hasErrors` to check
154154
* if compilation succeeded.
155155
*/
156-
final def process(args: Array[String], reporter: Reporter = null,
157-
callback: interfaces.CompilerCallback = null): Reporter = {
156+
final def process(args: Array[String], reporter: Reporter | Null = null,
157+
callback: interfaces.CompilerCallback | Null = null): Reporter = {
158158
val compileCtx = initCtx.fresh
159159
if (reporter != null)
160160
compileCtx.setReporter(reporter)
@@ -172,7 +172,7 @@ class Driver {
172172
* with sbt.
173173
*/
174174
final def process(args: Array[String]): Reporter =
175-
process(args, null: Reporter, null: interfaces.CompilerCallback)
175+
process(args, null: Reporter | Null, null: interfaces.CompilerCallback | Null)
176176

177177
/** Entry point to the compiler using a custom `Context`.
178178
*

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class Resident extends Driver {
5151
nextCtx = rootCtx
5252
line = getLine()
5353
}
54-
if (line.startsWith(quit)) ctx.reporter
55-
else loop(line split "\\s+", nextCtx)
54+
if line.startsWith(quit) then ctx.reporter
55+
else loop((line split "\\s+").asInstanceOf[Array[String]], nextCtx)
5656
case None =>
5757
prevCtx.reporter
5858
}

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
5858

5959
private var compiling = false
6060

61-
private var myUnits: List[CompilationUnit] = _
62-
private var myUnitsCached: List[CompilationUnit] = _
61+
private var myUnits: List[CompilationUnit] = Nil
62+
private var myUnitsCached: List[CompilationUnit] = Nil
6363
private var myFiles: Set[AbstractFile] = _
6464

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

@@ -170,7 +170,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
170170
compileSources(sources)
171171
catch
172172
case NonFatal(ex) =>
173-
if units != null then report.echo(i"exception occurred while compiling $units%, %")
173+
if units.nonEmpty then report.echo(i"exception occurred while compiling $units%, %")
174174
else report.echo(s"exception occurred while compiling ${files.map(_.name).mkString(", ")}")
175175
throw ex
176176

@@ -309,7 +309,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
309309
val uuid = java.util.UUID.randomUUID().toString
310310
val ext = if (isJava) ".java" else ".scala"
311311
val virtualFile = new VirtualFile(s"compileFromString-$uuid.$ext")
312-
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, StandardCharsets.UTF_8.name)) // buffering is still advised by javadoc
312+
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, StandardCharsets.UTF_8.nn.name)) // buffering is still advised by javadoc
313313
writer.write(source)
314314
writer.close()
315315
new SourceFile(virtualFile, Codec.UTF8)
@@ -333,8 +333,8 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
333333
super[ImplicitRunInfo].reset()
334334
super[ConstraintRunInfo].reset()
335335
myCtx = null
336-
myUnits = null
337-
myUnitsCached = null
336+
myUnits = Nil
337+
myUnitsCached = Nil
338338
}
339339

340340
/** Produces the following contexts, from outermost to innermost
@@ -367,9 +367,9 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
367367
start.setRun(this: @unchecked)
368368
}
369369

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

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

0 commit comments

Comments
 (0)