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
}

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: 4 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 util.Spans._, Types._, Contexts._, Constants._, Names._, NameOps._, Flags._
79
import Symbols._, StdNames._, Trees._, Phases._, ContextOps._
@@ -1298,7 +1300,7 @@ object desugar {
12981300
val ts = tree.trees
12991301
val arity = ts.length
13001302
assert(arity <= Definitions.MaxTupleArity)
1301-
def tupleTypeRef = defn.TupleType(arity)
1303+
def tupleTypeRef = defn.TupleType(arity).nn
13021304
if (arity == 0)
13031305
if (ctx.mode is Mode.Type) TypeTree(defn.UnitType) else unitLiteral
13041306
else if (ctx.mode is Mode.Type) AppliedTypeTree(ref(tupleTypeRef), ts)
@@ -1361,7 +1363,7 @@ object desugar {
13611363
* def $anonfun(params) = body
13621364
* Closure($anonfun)
13631365
*/
1364-
def makeClosure(params: List[ValDef], body: Tree, tpt: Tree = null, isContextual: Boolean, span: Span)(using Context): Block =
1366+
def makeClosure(params: List[ValDef], body: Tree, tpt: Tree | Null = null, isContextual: Boolean, span: Span)(using Context): Block =
13651367
Block(
13661368
DefDef(nme.ANON_FUN, params :: Nil, if (tpt == null) TypeTree() else tpt, body)
13671369
.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 =

0 commit comments

Comments
 (0)