Skip to content

Commit

Permalink
Use compiletime.uninitialized in compiler (#18823)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Nov 3, 2023
2 parents dc1e35a + 38b0168 commit 94dceed
Show file tree
Hide file tree
Showing 55 changed files with 216 additions and 141 deletions.
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/backend/jvm/BCodeHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import scala.tools.asm
import scala.tools.asm.AnnotationVisitor
import scala.tools.asm.ClassWriter
import scala.collection.mutable
import scala.compiletime.uninitialized

import dotty.tools.dotc.CompilationUnit
import dotty.tools.dotc.ast.tpd
Expand Down Expand Up @@ -576,7 +577,7 @@ trait BCodeHelpers extends BCodeIdiomatic {
/* builder of mirror classes */
class JMirrorBuilder extends JCommonBuilder {

private var cunit: CompilationUnit = _
private var cunit: CompilationUnit = uninitialized
def getCurrentCUnit(): CompilationUnit = cunit;

/* Generate a mirror class for a top-level module. A mirror class is a class
Expand Down
13 changes: 7 additions & 6 deletions compiler/src/dotty/tools/backend/jvm/GenBCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Contexts._
import Symbols._
import dotty.tools.io._
import scala.collection.mutable
import scala.compiletime.uninitialized

class GenBCode extends Phase { self =>

Expand All @@ -25,7 +26,7 @@ class GenBCode extends Phase { self =>
private val entryPoints = new mutable.HashSet[String]()
def registerEntryPoint(s: String): Unit = entryPoints += s

private var _backendInterface: DottyBackendInterface = _
private var _backendInterface: DottyBackendInterface = uninitialized
def backendInterface(using ctx: Context): DottyBackendInterface = {
if _backendInterface eq null then
// Enforce usage of FreshContext so we would be able to modify compilation unit between runs
Expand All @@ -36,7 +37,7 @@ class GenBCode extends Phase { self =>
_backendInterface
}

private var _codeGen: CodeGen = _
private var _codeGen: CodeGen = uninitialized
def codeGen(using Context): CodeGen = {
if _codeGen eq null then
val int = backendInterface
Expand All @@ -45,28 +46,28 @@ class GenBCode extends Phase { self =>
_codeGen
}

private var _bTypes: BTypesFromSymbols[DottyBackendInterface] = _
private var _bTypes: BTypesFromSymbols[DottyBackendInterface] = uninitialized
def bTypes(using Context): BTypesFromSymbols[DottyBackendInterface] = {
if _bTypes eq null then
_bTypes = BTypesFromSymbols(backendInterface, frontendAccess)
_bTypes
}

private var _frontendAccess: PostProcessorFrontendAccess | Null = _
private var _frontendAccess: PostProcessorFrontendAccess | Null = uninitialized
def frontendAccess(using Context): PostProcessorFrontendAccess = {
if _frontendAccess eq null then
_frontendAccess = PostProcessorFrontendAccess.Impl(backendInterface, entryPoints)
_frontendAccess.nn
}

private var _postProcessor: PostProcessor | Null = _
private var _postProcessor: PostProcessor | Null = uninitialized
def postProcessor(using Context): PostProcessor = {
if _postProcessor eq null then
_postProcessor = new PostProcessor(frontendAccess, bTypes)
_postProcessor.nn
}

private var _generatedClassHandler: GeneratedClassHandler | Null = _
private var _generatedClassHandler: GeneratedClassHandler | Null = uninitialized
def generatedClassHandler(using Context): GeneratedClassHandler = {
if _generatedClassHandler eq null then
_generatedClassHandler = GeneratedClassHandler(postProcessor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import dotty.tools.dotc.core.Phases
import dotty.tools.dotc.core.Decorators.em

import scala.language.unsafeNulls
import scala.compiletime.uninitialized

/**
* Interface to handle post-processing and classfile writing (see [[PostProcessor]]) of generated
Expand Down Expand Up @@ -185,7 +186,7 @@ final private class CompilationUnitInPostProcess(private var classes: List[Gener
}

/** the main async task submitted onto the scheduler */
var task: Future[Unit] = _
var task: Future[Unit] = uninitialized

val bufferedReporting = new PostProcessorFrontendAccess.BufferingBackendReporting()
}
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/backend/sjs/JSDefinitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Symbols._
import StdNames._

import dotty.tools.dotc.config.SJSPlatform
import scala.compiletime.uninitialized

object JSDefinitions {
/** The Scala.js-specific definitions for the current context. */
Expand Down Expand Up @@ -249,7 +250,7 @@ final class JSDefinitions()(using Context) {
@threadUnsafe lazy val Selectable_reflectiveSelectableFromLangReflectiveCallsR = SelectableModule.requiredMethodRef("reflectiveSelectableFromLangReflectiveCalls")
def Selectable_reflectiveSelectableFromLangReflectiveCalls(using Context) = Selectable_reflectiveSelectableFromLangReflectiveCallsR.symbol

private var allRefClassesCache: Set[Symbol] = _
private var allRefClassesCache: Set[Symbol] = uninitialized
def allRefClasses(using Context): Set[Symbol] = {
if (allRefClassesCache == null) {
val baseNames = List("Object", "Boolean", "Character", "Byte", "Short",
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/Bench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import reporting.Reporter
import io.AbstractFile

import scala.annotation.internal.sharable
import scala.compiletime.uninitialized

/** A main class for running compiler benchmarks. Can instantiate a given
* number of compilers and run each (sequentially) a given number of times
Expand All @@ -17,7 +18,7 @@ object Bench extends Driver:
@sharable private var numCompilers = 1
@sharable private var waitAfter = -1
@sharable private var curCompiler = 0
@sharable private var times: Array[Int] = _
@sharable private var times: Array[Int] = uninitialized

override def doCompile(compiler: Compiler, files: List[AbstractFile])(using Context): Reporter =
var reporter: Reporter = emptyReporter
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/Run.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint

private var myUnits: List[CompilationUnit] = Nil
private var myUnitsCached: List[CompilationUnit] = Nil
private var myFiles: Set[AbstractFile] = _
private var myFiles: Set[AbstractFile] = uninitialized

// `@nowarn` annotations by source file, populated during typer
private val mySuppressions: mutable.LinkedHashMap[SourceFile, mutable.ListBuffer[Suppression]] = mutable.LinkedHashMap.empty
Expand Down Expand Up @@ -511,7 +511,7 @@ object Run {
var currentCompletedSubtraversalCount: Int = 0 // completed subphases in the current phase
var seenPhaseCount: Int = 0 // how many phases we've seen so far

private var currPhase: Phase = uninitialized // initialized by enterPhase
private var currPhase: Phase = uninitialized // initialized by enterPhase
private var subPhases: SubPhases = uninitialized // initialized by enterPhase
private var currPhaseName: String = uninitialized // initialized by enterPhase
private var nextPhaseName: String = uninitialized // initialized by enterPhase
Expand Down
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/ast/Positioned.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import ast.Trees.mods
import annotation.constructorOnly
import annotation.internal.sharable

import scala.compiletime.uninitialized

/** A base class for things that have positions (currently: modifiers and trees)
*/
abstract class Positioned(implicit @constructorOnly src: SourceFile) extends SrcPos, Product, Cloneable {
import Positioned.{ids, nextId, debugId}

private var mySpan: Span = _
private var mySpan: Span = uninitialized

private var mySource: SourceFile = src

Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/ast/tpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import typer.ConstFold

import scala.annotation.tailrec
import scala.collection.mutable.ListBuffer
import scala.compiletime.uninitialized

/** Some creators for typed trees */
object tpd extends Trees.Instance[Type] with TypedTreeInfo {
Expand Down Expand Up @@ -1309,7 +1310,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
trait TreeProvider {
protected def computeRootTrees(using Context): List[Tree]

private var myTrees: List[Tree] | Null = _
private var myTrees: List[Tree] | Null = uninitialized

/** Get trees defined by this provider. Cache them if -Yretain-trees is set. */
def rootTrees(using Context): List[Tree] =
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/core/ConstraintRunInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package core

import Contexts._
import config.Printers.{default, typr}
import scala.compiletime.uninitialized

trait ConstraintRunInfo { self: Run =>
private var maxSize = 0
private var maxConstraint: Constraint | Null = _
private var maxConstraint: Constraint | Null = uninitialized
def recordConstraintSize(c: Constraint, size: Int): Unit =
if (size > maxSize) {
maxSize = size
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ object Contexts {
private var _owner: Symbol = uninitialized
final def owner: Symbol = _owner

private var _tree: Tree[?]= _
private var _tree: Tree[?] = uninitialized
final def tree: Tree[?] = _tree

private var _scope: Scope = uninitialized
Expand Down
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import cc.{CaptureSet, RetainingType}
import ast.tpd.ref

import scala.annotation.tailrec
import scala.compiletime.uninitialized

object Definitions {

Expand All @@ -44,7 +45,7 @@ object Definitions {
class Definitions {
import Definitions._

private var initCtx: Context = _
private var initCtx: Context = uninitialized
private given currentContext[Dummy_so_its_a_def]: Context = initCtx

private def newPermanentSymbol[N <: Name](owner: Symbol, name: N, flags: FlagSet, info: Type) =
Expand Down Expand Up @@ -2001,7 +2002,7 @@ class Definitions {

class PerRun[T](generate: Context ?=> T) {
private var current: RunId = NoRunId
private var cached: T = _
private var cached: T = uninitialized
def apply()(using Context): T = {
if (current != ctx.runId) {
cached = generate
Expand Down
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Denotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import util.common._
import typer.ProtoTypes.NoViewsAllowed
import collection.mutable.ListBuffer

import scala.compiletime.uninitialized

/** Denotations represent the meaning of symbols and named types.
* The following diagram shows how the principal types of denotations
* and their denoting entities relate to each other. Lines ending in
Expand Down Expand Up @@ -121,8 +123,8 @@ object Denotations {
/** Map `f` over all single denotations and aggregate the results with `g`. */
def aggregate[T](f: SingleDenotation => T, g: (T, T) => T): T

private var cachedPrefix: Type = _
private var cachedAsSeenFrom: AsSeenFromResult = _
private var cachedPrefix: Type = uninitialized
private var cachedAsSeenFrom: AsSeenFromResult = uninitialized
private var validAsSeenFrom: Period = Nowhere

type AsSeenFromResult <: PreDenotation
Expand Down
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/core/OrderingConstraint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import annotation.tailrec
import annotation.internal.sharable
import cc.{CapturingType, derivedCapturingType}

import scala.compiletime.uninitialized

object OrderingConstraint {

/** If true, use reverse dependencies in `replace` to avoid checking the bounds
Expand Down Expand Up @@ -881,7 +883,7 @@ class OrderingConstraint(private val boundsMap: ParamBounds,
i += 1
}

private var myUninstVars: mutable.ArrayBuffer[TypeVar] | Null = _
private var myUninstVars: mutable.ArrayBuffer[TypeVar] | Null = uninitialized

/** The uninstantiated typevars of this constraint */
def uninstVars: collection.Seq[TypeVar] = {
Expand Down
51 changes: 26 additions & 25 deletions compiler/src/dotty/tools/dotc/core/Phases.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import typer.ImportInfo.withRootImports
import ast.{tpd, untpd}
import scala.annotation.internal.sharable
import scala.util.control.NonFatal
import scala.compiletime.uninitialized

object Phases {

Expand Down Expand Up @@ -205,30 +206,30 @@ object Phases {
if nextDenotTransformerId(i) == phase.id then
nextDenotTransformerId(i) = nextDenotTransformerId(phase.id + 1)

private var myParserPhase: Phase = _
private var myTyperPhase: Phase = _
private var myPostTyperPhase: Phase = _
private var mySbtExtractDependenciesPhase: Phase = _
private var myPicklerPhase: Phase = _
private var myInliningPhase: Phase = _
private var myStagingPhase: Phase = _
private var mySplicingPhase: Phase = _
private var myFirstTransformPhase: Phase = _
private var myCollectNullableFieldsPhase: Phase = _
private var myRefChecksPhase: Phase = _
private var myPatmatPhase: Phase = _
private var myElimRepeatedPhase: Phase = _
private var myElimByNamePhase: Phase = _
private var myExtensionMethodsPhase: Phase = _
private var myExplicitOuterPhase: Phase = _
private var myGettersPhase: Phase = _
private var myErasurePhase: Phase = _
private var myElimErasedValueTypePhase: Phase = _
private var myLambdaLiftPhase: Phase = _
private var myCountOuterAccessesPhase: Phase = _
private var myFlattenPhase: Phase = _
private var myGenBCodePhase: Phase = _
private var myCheckCapturesPhase: Phase = _
private var myParserPhase: Phase = uninitialized
private var myTyperPhase: Phase = uninitialized
private var myPostTyperPhase: Phase = uninitialized
private var mySbtExtractDependenciesPhase: Phase = uninitialized
private var myPicklerPhase: Phase = uninitialized
private var myInliningPhase: Phase = uninitialized
private var myStagingPhase: Phase = uninitialized
private var mySplicingPhase: Phase = uninitialized
private var myFirstTransformPhase: Phase = uninitialized
private var myCollectNullableFieldsPhase: Phase = uninitialized
private var myRefChecksPhase: Phase = uninitialized
private var myPatmatPhase: Phase = uninitialized
private var myElimRepeatedPhase: Phase = uninitialized
private var myElimByNamePhase: Phase = uninitialized
private var myExtensionMethodsPhase: Phase = uninitialized
private var myExplicitOuterPhase: Phase = uninitialized
private var myGettersPhase: Phase = uninitialized
private var myErasurePhase: Phase = uninitialized
private var myElimErasedValueTypePhase: Phase = uninitialized
private var myLambdaLiftPhase: Phase = uninitialized
private var myCountOuterAccessesPhase: Phase = uninitialized
private var myFlattenPhase: Phase = uninitialized
private var myGenBCodePhase: Phase = uninitialized
private var myCheckCapturesPhase: Phase = uninitialized

final def parserPhase: Phase = myParserPhase
final def typerPhase: Phase = myTyperPhase
Expand Down Expand Up @@ -389,7 +390,7 @@ object Phases {
def printingContext(ctx: Context): Context = ctx

private var myPeriod: Period = Periods.InvalidPeriod
private var myBase: ContextBase = _
private var myBase: ContextBase = uninitialized
private var myErasedTypes = false
private var myFlatClasses = false
private var myRefChecked = false
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/core/Scopes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import printing.Printer
import SymDenotations.NoDenotation

import collection.mutable
import scala.compiletime.uninitialized

object Scopes {

Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import transform.TypeUtils._
import cc.{CapturingType, derivedCapturingType}

import scala.annotation.internal.sharable
import scala.compiletime.uninitialized

object SymDenotations {

Expand Down Expand Up @@ -2431,7 +2432,7 @@ object SymDenotations {
initPrivateWithin: Symbol)
extends ClassDenotation(symbol, ownerIfExists, name, initFlags, initInfo, initPrivateWithin) {

private var packageObjsCache: List[ClassDenotation] = _
private var packageObjsCache: List[ClassDenotation] = uninitialized
private var packageObjsRunId: RunId = NoRunId
private var ambiguityWarningIssued: Boolean = false

Expand Down
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Symbols.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import scala.annotation.internal.sharable
import config.Printers.typr
import dotty.tools.dotc.classpath.FileUtils.isScalaBinary

import scala.compiletime.uninitialized

object Symbols {

implicit def eqSymbol: CanEqual[Symbol, Symbol] = CanEqual.derived
Expand Down Expand Up @@ -88,7 +90,7 @@ object Symbols {
ctx.settings.YcheckInitGlobal.value

/** The last denotation of this symbol */
private var lastDenot: SymDenotation = _
private var lastDenot: SymDenotation = uninitialized
private var checkedPeriod: Period = Nowhere

private[core] def invalidateDenotCache(): Unit = { checkedPeriod = Nowhere }
Expand Down
Loading

0 comments on commit 94dceed

Please sign in to comment.