Skip to content

Commit 1d886e3

Browse files
committed
Add NoCompilationUnit
1 parent c076704 commit 1d886e3

File tree

11 files changed

+31
-31
lines changed

11 files changed

+31
-31
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import typer.Nullables
1616
import transform.SymUtils._
1717
import core.Decorators._
1818
import config.SourceVersion
19+
import scala.annotation.internal.sharable
1920

2021
class CompilationUnit protected (val source: SourceFile) {
2122

@@ -89,15 +90,24 @@ class CompilationUnit protected (val source: SourceFile) {
8990
myAssignmentSpans.nn
9091
}
9192

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
101+
}
102+
92103
object CompilationUnit {
93104

94105
class SuspendException extends Exception
95106

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

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

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ class Driver {
4242
finish(compiler, run)
4343
catch
4444
case ex: FatalError =>
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")
45+
report.error(ex.getMessage.nn) // signals that we should fail compilation.
4946
case ex: TypeError =>
5047
println(s"${ex.toMessage} while compiling ${files.map(_.path).mkString(", ")}")
5148
throw ex

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ class Resident extends Driver {
5555
}
5656
if (line.startsWith(quit)) ctx.reporter
5757
else
58-
// assuming split returns non-nullable values
59-
loop((line split "\\s+").asInstanceOf, nextCtx)
58+
// split returns non-nullable values
59+
loop((line split "\\s+").asInstanceOf[Array[String]], nextCtx)
6060
case None =>
6161
prevCtx.reporter
6262
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -825,8 +825,8 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
825825
* tree must be reachable from come tree stored in an enclosing context.
826826
*/
827827
def definingStats(sym: Symbol)(using Context): List[Tree] =
828-
val unit: CompilationUnit | Null = ctx.compilationUnit
829-
if (!sym.span.exists || (ctx eq NoContext) || unit == null) Nil
828+
val unit = ctx.compilationUnit
829+
if (!sym.span.exists || (ctx eq NoContext) || (unit eq NoCompilationUnit)) Nil
830830
else defPath(sym, unit.tpdTree) match {
831831
case defn :: encl :: _ =>
832832
def verify(stats: List[Tree]) =

compiler/src/dotty/tools/dotc/config/Feature.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ object Feature:
7878
SourceVersion.valueOf(ctx.settings.source.value)
7979

8080
def sourceVersion(using Context): SourceVersion =
81-
val unit: CompilationUnit | Null = ctx.compilationUnit
82-
if unit == null then sourceVersionSetting
83-
else unit.sourceVersion match
81+
ctx.compilationUnit.sourceVersion match
8482
case Some(v) => v
8583
case none => sourceVersionSetting
8684

compiler/src/dotty/tools/dotc/core/Contexts.scala

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,7 @@ object Contexts {
349349
if ctx1 == null then
350350
util.Stats.record("Context.withSource.new")
351351
val ctx2 = fresh.setSource(source)
352-
val ctx2unit: CompilationUnit | Null = ctx2.compilationUnit
353-
if ctx2unit == null then
352+
if ctx2.compilationUnit eq NoCompilationUnit then
354353
// `source` might correspond to a file not necessarily
355354
// in the current project (e.g. when inlining library code),
356355
// so set `mustExist` to false.
@@ -394,12 +393,7 @@ object Contexts {
394393
final def erasedTypes = phase.erasedTypes
395394

396395
/** Are we in a Java compilation unit? */
397-
final def isJava: Boolean =
398-
// FIXME: It would be much nicer if compilationUnit was non-nullable,
399-
// perhaps we need to introduce a `NoCompilationUnit` compilation unit
400-
// to be used as a default value.
401-
val unit: CompilationUnit | Null = compilationUnit
402-
unit != null && unit.isJava
396+
final def isJava: Boolean = compilationUnit.isJava
403397

404398
/** Is current phase after TyperPhase? */
405399
final def isAfterTyper = base.isAfterTyper(phase)
@@ -837,6 +831,7 @@ object Contexts {
837831
store = initialStore
838832
.updated(settingsStateLoc, settingsGroup.defaultState)
839833
.updated(notNullInfosLoc, Nil)
834+
.updated(compilationUnitLoc, NoCompilationUnit)
840835
searchHistory = new SearchRoot
841836
gadt = EmptyGadtConstraint
842837
}

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,8 +1431,7 @@ class Definitions {
14311431

14321432
/** Are we compiling a java source file? */
14331433
private def isJavaContext(using Context): Boolean =
1434-
val unit: CompilationUnit | Null = ctx.compilationUnit
1435-
unit != null && unit.isJava
1434+
ctx.compilationUnit.isJava
14361435

14371436
private def unqualifiedTypes(refs: List[TermRef]) =
14381437
val types = refs.toSet[NamedType]

compiler/src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,10 +1230,10 @@ object SymDenotations {
12301230
* lookup its companion in the same scope.
12311231
*/
12321232
private def companionNamed(name: TypeName)(using Context): Symbol =
1233-
val unit: CompilationUnit | Null = ctx.compilationUnit
1233+
val unit = ctx.compilationUnit
12341234
if (owner.isClass)
12351235
owner.unforcedDecls.lookup(name).suchThat(_.isCoDefinedWith(symbol)).symbol
1236-
else if (!owner.exists || unit == null)
1236+
else if (!owner.exists || (unit eq NoCompilationUnit))
12371237
NoSymbol
12381238
else if (!unit.tpdTree.isEmpty)
12391239
tpd.definingStats(symbol).iterator

compiler/src/dotty/tools/dotc/typer/Nullables.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ object Nullables:
167167
def isTracked(ref: TermRef)(using Context) =
168168
ref.isStable
169169
|| { val sym = ref.symbol
170-
val unit: CompilationUnit | Null = ctx.compilationUnit
170+
val unit = ctx.compilationUnit
171171
!ref.usedOutOfOrder
172172
&& sym.span.exists
173-
&& unit != null // could be null under -Ytest-pickler
173+
&& (unit ne NoCompilationUnit) // could be null under -Ytest-pickler
174174
&& unit.assignmentSpans.contains(sym.span.start)
175175
}
176176

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
313313
*/
314314
def isDefinedInCurrentUnit(denot: Denotation)(using Context): Boolean = denot match {
315315
case MultiDenotation(d1, d2) => isDefinedInCurrentUnit(d1) || isDefinedInCurrentUnit(d2)
316-
case denot: SingleDenotation => (ctx.compilationUnit: CompilationUnit | Null) != null && denot.symbol.source == ctx.compilationUnit.source
316+
case denot: SingleDenotation => (ctx.compilationUnit ne NoCompilationUnit) && denot.symbol.source == ctx.compilationUnit.source
317317
}
318318

319319
/** Is `denot` the denotation of a self symbol? */

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import dotty.tools.dotc.quoted.reflect._
1414
import dotty.tools.dotc.quoted.QuoteUtils._
1515
import dotty.tools.dotc.core.Decorators._
1616
import dotty.tools.dotc.CompilationUnit
17+
import dotty.tools.dotc.NoCompilationUnit
1718

1819
import dotty.tools.dotc.quoted.{MacroExpansion, PickledQuotes, QuoteUtils}
1920

@@ -2797,8 +2798,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
27972798

27982799
object SourceFile extends SourceFileModule {
27992800
def current: SourceFile =
2800-
val unit: CompilationUnit | Null = ctx.compilationUnit
2801-
if unit == null then
2801+
val unit = ctx.compilationUnit
2802+
if unit == NoCompilationUnit then
28022803
throw new java.lang.UnsupportedOperationException(
28032804
"`reflect.SourceFile.current` cannot be called within the TASTy ispector")
28042805
else unit.source

0 commit comments

Comments
 (0)