Skip to content

Commit 44fca94

Browse files
committed
Delete LazyVar usage
1 parent 2e7bce6 commit 44fca94

12 files changed

+150
-325
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
12551255
.toList
12561256

12571257
// `StringConcatFactory` only got added in JDK 9, so use `StringBuilder` for lower
1258-
if (backendUtils.classfileVersion.get < asm.Opcodes.V9) {
1258+
if (backendUtils.classfileVersion < asm.Opcodes.V9) {
12591259

12601260
// Estimate capacity needed for the string builder
12611261
val approxBuilderSize = concatArguments.view.map {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ trait BCodeHelpers extends BCodeIdiomatic {
599599

600600
val mirrorClass = new asm.tree.ClassNode
601601
mirrorClass.visit(
602-
backendUtils.classfileVersion.get,
602+
backendUtils.classfileVersion,
603603
bType.info.flags,
604604
mirrorName,
605605
null /* no java-generic-signature */,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ trait BCodeSkelBuilder extends BCodeHelpers {
271271
val flags = javaFlags(claszSymbol)
272272

273273
val thisSignature = getGenericSignature(claszSymbol, claszSymbol.owner)
274-
cnode.visit(backendUtils.classfileVersion.get, flags,
274+
cnode.visit(backendUtils.classfileVersion, flags,
275275
thisName, thisSignature,
276276
superClass, interfaceNames.toArray)
277277

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

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -861,55 +861,6 @@ abstract class BTypes { self =>
861861
*/
862862
/*final*/ case class MethodNameAndType(name: String, methodType: MethodBType)
863863

864-
865-
import dotty.tools.dotc.core.Contexts.Context
866-
867-
// The [[LazyVar]] class would conceptually be better placed within
868-
// PostProcessorFrontendAccess (they access the `frontendLock` defined in that class). However,
869-
// for every component in which we define nested classes, we need to make sure that the compiler
870-
// knows that all component instances (val frontendAccess) in various classes are all the same,
871-
// otherwise the prefixes don't match and we get type mismatch errors.
872-
// Since we already do this dance (val bTypes: GenBCode.this.bTypes.type = GenBCode.this.bTypes)
873-
// for BTypes, it's easier to add those nested classes to BTypes.
874-
875-
/**
876-
* Create state that lazily evaluated (to work around / not worry about initialization ordering
877-
* issues). The state is re-initialized in each compiler run when the component is initialized.
878-
*/
879-
type Contextual[T] = Context ?=> T
880-
def perRunLazy[T](component: PerRunInit)(init: => Contextual[T]): LazyVar[T] = {
881-
val r = new LazyVar(() => init)
882-
component.perRunInit(r.reInitialize())
883-
r
884-
}
885-
886-
/**
887-
* This implements a lazy value that can be reset and re-initialized.
888-
* It synchronizes on `frontendLock` so that lazy state created through this utility can
889-
* be safely initialized in the post-processor.
890-
*
891-
* Note that values defined as `LazyVar`s are usually `lazy val`s themselves (created through the
892-
* `perRunLazy` method). This ensures that re-initializing a component only re-initializes those
893-
* `LazyVar`s that have actually been used in the previous compiler run.
894-
*/
895-
class LazyVar[T](init: Contextual[() => T]) {
896-
@volatile private var isInit: Boolean = false
897-
private var v: T = _
898-
899-
def get(using Context): T = {
900-
if (isInit) v
901-
else frontendSynch {
902-
if (!isInit) v = init()
903-
isInit = true
904-
v
905-
}
906-
}
907-
908-
def reInitialize(): Unit = frontendSynch{
909-
isInit = false
910-
}
911-
}
912-
913864
}
914865

915866
object BTypes {

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ class BTypesFromSymbols[I <: DottyBackendInterface](val int: I, val frontendAcce
3636
}
3737
import coreBTypes._
3838

39-
final def initialize()(using Context): Unit = {
40-
coreBTypes.initialize()
41-
}
42-
4339
@threadUnsafe protected lazy val classBTypeFromInternalNameMap =
4440
collection.concurrent.TrieMap.empty[String, ClassBType]
4541

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

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,31 @@ import scala.language.unsafeNulls
1414
* This component hosts tools and utilities used in the backend that require access to a `BTypes`
1515
* instance.
1616
*/
17-
class BackendUtils(val postProcessor: PostProcessor) extends PerRunInit {
17+
class BackendUtils(val postProcessor: PostProcessor) {
1818
import postProcessor.{bTypes, frontendAccess}
1919
import frontendAccess.{compilerSettings}
2020
import bTypes.*
2121
import coreBTypes.jliLambdaMetaFactoryAltMetafactoryHandle
2222

2323
// Keep synchronized with `minTargetVersion` and `maxTargetVersion` in ScalaSettings
24-
lazy val classfileVersion: LazyVar[Int] = perRunLazy(this){
25-
compilerSettings.target match {
26-
case "8" => asm.Opcodes.V1_8
27-
case "9" => asm.Opcodes.V9
28-
case "10" => asm.Opcodes.V10
29-
case "11" => asm.Opcodes.V11
30-
case "12" => asm.Opcodes.V12
31-
case "13" => asm.Opcodes.V13
32-
case "14" => asm.Opcodes.V14
33-
case "15" => asm.Opcodes.V15
34-
case "16" => asm.Opcodes.V16
35-
case "17" => asm.Opcodes.V17
36-
case "18" => asm.Opcodes.V18
37-
case "19" => asm.Opcodes.V19
38-
}
24+
lazy val classfileVersion: Int = compilerSettings.target match {
25+
case "8" => asm.Opcodes.V1_8
26+
case "9" => asm.Opcodes.V9
27+
case "10" => asm.Opcodes.V10
28+
case "11" => asm.Opcodes.V11
29+
case "12" => asm.Opcodes.V12
30+
case "13" => asm.Opcodes.V13
31+
case "14" => asm.Opcodes.V14
32+
case "15" => asm.Opcodes.V15
33+
case "16" => asm.Opcodes.V16
34+
case "17" => asm.Opcodes.V17
35+
case "18" => asm.Opcodes.V18
36+
case "19" => asm.Opcodes.V19
3937
}
4038

41-
lazy val extraProc: LazyVar[Int] = perRunLazy(this){
39+
lazy val extraProc: Int = {
4240
import GenBCodeOps.addFlagIf
43-
val majorVersion: Int = (classfileVersion.get & 0xFF)
41+
val majorVersion: Int = (classfileVersion & 0xFF)
4442
val emitStackMapFrame = (majorVersion >= 50)
4543
asm.ClassWriter.COMPUTE_MAXS
4644
.addFlagIf(emitStackMapFrame, asm.ClassWriter.COMPUTE_FRAMES)

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ import scala.tools.asm.ClassTooLargeException
3838
import dotty.tools.dotc.util.NoSourcePosition
3939

4040

41-
class CodeGen(val int: DottyBackendInterface, val primitives: DottyPrimitives)( val bTypes: BTypesFromSymbols[int.type]) extends PerRunInit { self =>
41+
class CodeGen(val int: DottyBackendInterface, val primitives: DottyPrimitives)( val bTypes: BTypesFromSymbols[int.type]) { self =>
4242
import DottyBackendInterface.symExtensions
4343
import bTypes._
44+
import int.given
4445

45-
private lazy val mirrorCodeGen = perRunLazy(this)(Impl.JMirrorBuilder())
46+
private lazy val mirrorCodeGen = Impl.JMirrorBuilder()
4647

4748
def apply()(using Context): Unit = {
4849
val unit = ctx.compilationUnit
@@ -90,7 +91,6 @@ class CodeGen(val int: DottyBackendInterface, val primitives: DottyPrimitives)(
9091
genClassDefs(unit.tpdTree)
9192
}
9293

93-
import int.given
9494
private def onFileCreated(cls: ClassNode, claszSymbol: Symbol, sourceFile: interfaces.SourceFile): AbstractFile => Unit = clsFile => {
9595
val (fullClassName, isLocal) = atPhase(sbtExtractDependenciesPhase) {
9696
(ExtractDependencies.classNameAsString(claszSymbol), claszSymbol.isLocal)
@@ -99,13 +99,12 @@ class CodeGen(val int: DottyBackendInterface, val primitives: DottyPrimitives)(
9999
val className = cls.name.replace('/', '.')
100100
if (ctx.compilerCallback != null)
101101
ctx.compilerCallback.onClassGenerated(sourceFile, convertAbstractFile(clsFile), className)
102+
102103
if (ctx.sbtCallback != null) {
103-
if (isLocal)
104-
ctx.sbtCallback.generatedLocalClass(sourceFile.jfile.orElse(null), clsFile.file)
105-
else {
106-
ctx.sbtCallback.generatedNonLocalClass(sourceFile.jfile.orElse(null), clsFile.file,
107-
className, fullClassName)
108-
}
104+
val jSourceFile = sourceFile.jfile.orElse(null)
105+
val cb = ctx.sbtCallback
106+
if (isLocal) cb.generatedLocalClass(jSourceFile, clsFile.file)
107+
else cb.generatedNonLocalClass(jSourceFile, clsFile.file, className, fullClassName)
109108
}
110109
}
111110

@@ -140,23 +139,25 @@ class CodeGen(val int: DottyBackendInterface, val primitives: DottyPrimitives)(
140139
}
141140

142141
private def genClass(cd: TypeDef, unit: CompilationUnit): ClassNode = {
143-
checkForCaseConflict(cd.symbol.javaClassName, cd.symbol)
144142
val b = new Impl.SyncAndTryBuilder(unit) {}
145143
b.genPlainClass(cd)
146-
b.cnode
144+
val cls = b.cnode
145+
checkForCaseConflict(cls.name, cd.symbol)
146+
cls
147147
}
148148

149149
private def genMirrorClass(classSym: Symbol, unit: CompilationUnit): ClassNode = {
150-
// checkForCaseConflict(classSym.javaClassName, classSym)
151-
mirrorCodeGen.get.genMirrorClass(classSym, unit)
150+
val cls = mirrorCodeGen.genMirrorClass(classSym, unit)
151+
checkForCaseConflict(cls.name, classSym)
152+
cls
152153
}
153154

154-
private val lowerCaseNames = perRunLazy(this)(mutable.HashMap.empty[String, Symbol])
155+
private val lowerCaseNames = mutable.HashMap.empty[String, Symbol]
155156
private def checkForCaseConflict(javaClassName: String, classSymbol: Symbol) = {
156157
val lowerCaseName = javaClassName.toLowerCase
157-
lowerCaseNames.get.get(lowerCaseName) match {
158+
lowerCaseNames.get(lowerCaseName) match {
158159
case None =>
159-
lowerCaseNames.get.put(lowerCaseName, classSymbol)
160+
lowerCaseNames.put(lowerCaseName, classSymbol)
160161
case Some(dupClassSym) =>
161162
// Order is not deterministic so we enforce lexicographic order between the duplicates for error-reporting
162163
val (cl1, cl2) =

0 commit comments

Comments
 (0)