Skip to content

Commit

Permalink
Add NoCompilationUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
noti0na1 committed Jan 21, 2022
1 parent c076704 commit 1d886e3
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 31 deletions.
16 changes: 13 additions & 3 deletions compiler/src/dotty/tools/dotc/CompilationUnit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import typer.Nullables
import transform.SymUtils._
import core.Decorators._
import config.SourceVersion
import scala.annotation.internal.sharable

class CompilationUnit protected (val source: SourceFile) {

Expand Down Expand Up @@ -89,15 +90,24 @@ class CompilationUnit protected (val source: SourceFile) {
myAssignmentSpans.nn
}

@sharable object NoCompilationUnit extends CompilationUnit(NoSource) {

override def isJava: Boolean = false

override def suspend()(using Context): Nothing =
throw CompilationUnit.SuspendException()

override def assignmentSpans(using Context): Map[Int, List[Span]] = Map.empty
}

object CompilationUnit {

class SuspendException extends Exception

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

/** Make a compilation unit, given picked bytes and unpickled tree */
def apply(source: SourceFile, unpickled: Tree, forceTrees: Boolean)(using Context): CompilationUnit = {
Expand Down
5 changes: 1 addition & 4 deletions compiler/src/dotty/tools/dotc/Driver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ class Driver {
finish(compiler, run)
catch
case ex: FatalError =>
val msg = ex.getMessage
if msg != null then
report.error(msg) // signals that we should fail compilation.
else report.error("null")
report.error(ex.getMessage.nn) // signals that we should fail compilation.
case ex: TypeError =>
println(s"${ex.toMessage} while compiling ${files.map(_.path).mkString(", ")}")
throw ex
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/Resident.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class Resident extends Driver {
}
if (line.startsWith(quit)) ctx.reporter
else
// assuming split returns non-nullable values
loop((line split "\\s+").asInstanceOf, nextCtx)
// split returns non-nullable values
loop((line split "\\s+").asInstanceOf[Array[String]], nextCtx)
case None =>
prevCtx.reporter
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/ast/TreeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,8 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
* tree must be reachable from come tree stored in an enclosing context.
*/
def definingStats(sym: Symbol)(using Context): List[Tree] =
val unit: CompilationUnit | Null = ctx.compilationUnit
if (!sym.span.exists || (ctx eq NoContext) || unit == null) Nil
val unit = ctx.compilationUnit
if (!sym.span.exists || (ctx eq NoContext) || (unit eq NoCompilationUnit)) Nil
else defPath(sym, unit.tpdTree) match {
case defn :: encl :: _ =>
def verify(stats: List[Tree]) =
Expand Down
4 changes: 1 addition & 3 deletions compiler/src/dotty/tools/dotc/config/Feature.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ object Feature:
SourceVersion.valueOf(ctx.settings.source.value)

def sourceVersion(using Context): SourceVersion =
val unit: CompilationUnit | Null = ctx.compilationUnit
if unit == null then sourceVersionSetting
else unit.sourceVersion match
ctx.compilationUnit.sourceVersion match
case Some(v) => v
case none => sourceVersionSetting

Expand Down
11 changes: 3 additions & 8 deletions compiler/src/dotty/tools/dotc/core/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,7 @@ object Contexts {
if ctx1 == null then
util.Stats.record("Context.withSource.new")
val ctx2 = fresh.setSource(source)
val ctx2unit: CompilationUnit | Null = ctx2.compilationUnit
if ctx2unit == null then
if ctx2.compilationUnit eq NoCompilationUnit then
// `source` might correspond to a file not necessarily
// in the current project (e.g. when inlining library code),
// so set `mustExist` to false.
Expand Down Expand Up @@ -394,12 +393,7 @@ object Contexts {
final def erasedTypes = phase.erasedTypes

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

/** Is current phase after TyperPhase? */
final def isAfterTyper = base.isAfterTyper(phase)
Expand Down Expand Up @@ -837,6 +831,7 @@ object Contexts {
store = initialStore
.updated(settingsStateLoc, settingsGroup.defaultState)
.updated(notNullInfosLoc, Nil)
.updated(compilationUnitLoc, NoCompilationUnit)
searchHistory = new SearchRoot
gadt = EmptyGadtConstraint
}
Expand Down
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1431,8 +1431,7 @@ class Definitions {

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

private def unqualifiedTypes(refs: List[TermRef]) =
val types = refs.toSet[NamedType]
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1230,10 +1230,10 @@ object SymDenotations {
* lookup its companion in the same scope.
*/
private def companionNamed(name: TypeName)(using Context): Symbol =
val unit: CompilationUnit | Null = ctx.compilationUnit
val unit = ctx.compilationUnit
if (owner.isClass)
owner.unforcedDecls.lookup(name).suchThat(_.isCoDefinedWith(symbol)).symbol
else if (!owner.exists || unit == null)
else if (!owner.exists || (unit eq NoCompilationUnit))
NoSymbol
else if (!unit.tpdTree.isEmpty)
tpd.definingStats(symbol).iterator
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Nullables.scala
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ object Nullables:
def isTracked(ref: TermRef)(using Context) =
ref.isStable
|| { val sym = ref.symbol
val unit: CompilationUnit | Null = ctx.compilationUnit
val unit = ctx.compilationUnit
!ref.usedOutOfOrder
&& sym.span.exists
&& unit != null // could be null under -Ytest-pickler
&& (unit ne NoCompilationUnit) // could be null under -Ytest-pickler
&& unit.assignmentSpans.contains(sym.span.start)
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
*/
def isDefinedInCurrentUnit(denot: Denotation)(using Context): Boolean = denot match {
case MultiDenotation(d1, d2) => isDefinedInCurrentUnit(d1) || isDefinedInCurrentUnit(d2)
case denot: SingleDenotation => (ctx.compilationUnit: CompilationUnit | Null) != null && denot.symbol.source == ctx.compilationUnit.source
case denot: SingleDenotation => (ctx.compilationUnit ne NoCompilationUnit) && denot.symbol.source == ctx.compilationUnit.source
}

/** Is `denot` the denotation of a self symbol? */
Expand Down
5 changes: 3 additions & 2 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import dotty.tools.dotc.quoted.reflect._
import dotty.tools.dotc.quoted.QuoteUtils._
import dotty.tools.dotc.core.Decorators._
import dotty.tools.dotc.CompilationUnit
import dotty.tools.dotc.NoCompilationUnit

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

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

object SourceFile extends SourceFileModule {
def current: SourceFile =
val unit: CompilationUnit | Null = ctx.compilationUnit
if unit == null then
val unit = ctx.compilationUnit
if unit == NoCompilationUnit then
throw new java.lang.UnsupportedOperationException(
"`reflect.SourceFile.current` cannot be called within the TASTy ispector")
else unit.source
Expand Down

0 comments on commit 1d886e3

Please sign in to comment.