From c449be69925af28280f064e86165c48ae00db868 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Sat, 16 Mar 2024 16:42:34 -0700 Subject: [PATCH] Prefer ArrayBuffer to List --- src/compiler/scala/tools/nsc/Global.scala | 2 +- .../nsc/typechecker/TypeDiagnostics.scala | 6 ++-- .../scala/reflect/internal/Symbols.scala | 29 ++++++++++--------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/compiler/scala/tools/nsc/Global.scala b/src/compiler/scala/tools/nsc/Global.scala index 25f28d3990dd..62f1557fd69c 100644 --- a/src/compiler/scala/tools/nsc/Global.scala +++ b/src/compiler/scala/tools/nsc/Global.scala @@ -90,7 +90,7 @@ class Global(var currentSettings: Settings, reporter0: Reporter) override def settings = currentSettings - override def isSymbolLockTracingEnabled = settings.cyclic + override def isSymbolLockTracingEnabled = settings.cyclic.value private[this] var currentReporter: FilteringReporter = null locally { reporter = reporter0 } diff --git a/src/compiler/scala/tools/nsc/typechecker/TypeDiagnostics.scala b/src/compiler/scala/tools/nsc/typechecker/TypeDiagnostics.scala index 6dff00e9ee0e..4ded173977fb 100644 --- a/src/compiler/scala/tools/nsc/typechecker/TypeDiagnostics.scala +++ b/src/compiler/scala/tools/nsc/typechecker/TypeDiagnostics.scala @@ -842,13 +842,13 @@ trait TypeDiagnostics extends splain.SplainDiagnostics { /** Returns Some(msg) if the given tree is untyped apparently due * to a cyclic reference, and None otherwise. */ - def cyclicReferenceMessage(sym: Symbol, tree: Tree, trace: List[Symbol], pos: Position) = { + def cyclicReferenceMessage(sym: Symbol, tree: Tree, trace: Array[Symbol], pos: Position) = { def symWasOverloaded(sym: Symbol) = sym.owner.isClass && sym.owner.info.member(sym.name).isOverloaded def cyclicAdjective(sym: Symbol) = if (symWasOverloaded(sym)) "overloaded" else "recursive" val badsym = if (!sym.isSynthetic) sym else trace.filter(!_.isSynthetic) match { - case badsym :: Nil => badsym - case Nil => sym + case CyclicReference.emptyTrace => sym + case Array(badsym) => badsym case baddies => baddies.find(_.pos.focus == pos.focus).getOrElse(baddies.head) } condOpt(tree) { diff --git a/src/reflect/scala/reflect/internal/Symbols.scala b/src/reflect/scala/reflect/internal/Symbols.scala index 3ccd97ec48d9..08ac03efb1f4 100644 --- a/src/reflect/scala/reflect/internal/Symbols.scala +++ b/src/reflect/scala/reflect/internal/Symbols.scala @@ -18,12 +18,12 @@ package scala package reflect package internal -import scala.collection.immutable -import scala.collection.mutable.{ListBuffer, Stack} -import util.{ ReusableInstance, Statistics, shortClassOfInstance } -import Flags._ import scala.annotation.tailrec +import scala.collection.mutable.{ArrayBuffer, ListBuffer} import scala.reflect.io.{AbstractFile, NoAbstractFile} + +import util.{ReusableInstance, Statistics, shortClassOfInstance} +import Flags._ import Variance._ trait Symbols extends api.Symbols { self: SymbolTable => @@ -36,15 +36,15 @@ trait Symbols extends api.Symbols { self: SymbolTable => protected def nextId() = { ids += 1; ids } /** Used to keep track of the recursion depth on locked symbols */ - private[this] var _recursionTable = immutable.Map.empty[Symbol, Int] + private[this] var _recursionTable = Map.empty[Symbol, Int] def recursionTable = _recursionTable - def recursionTable_=(value: immutable.Map[Symbol, Int]) = _recursionTable = value + def recursionTable_=(value: Map[Symbol, Int]) = _recursionTable = value private[this] var _lockedCount = 0 def lockedCount = this._lockedCount def lockedCount_=(i: Int) = _lockedCount = i - private[this] val _lockingTrace = Stack.empty[Symbol] + private[this] val _lockingTrace = ArrayBuffer.empty[Symbol] private[this] val lockTracing: Boolean = self.isSymbolLockTracingEnabled @deprecated("Global existential IDs no longer used", "2.12.1") @@ -568,7 +568,7 @@ trait Symbols extends api.Symbols { self: SymbolTable => // Lock a symbol, using the handler if the recursion depth becomes too great. private[scala] def lock(handler: => Unit): Boolean = { - if (lockTracing) _lockingTrace.push(this) + if (lockTracing) _lockingTrace.addOne(this) if ((_rawflags & LOCKED) != 0L) { if (settings.Yrecursion.value != 0) { recursionTable.get(this) match { @@ -599,7 +599,7 @@ trait Symbols extends api.Symbols { self: SymbolTable => if ((_rawflags & LOCKED) != 0L) { _rawflags &= ~LOCKED if (lockTracing && !_lockingTrace.isEmpty) - _lockingTrace.remove(idx = 0, count = 1) + _lockingTrace.remove(index = _lockingTrace.size - 1, count = 1) // dropRightInPlace(1) if (settings.Yrecursion.value != 0) recursionTable -= this } @@ -1565,14 +1565,14 @@ trait Symbols extends api.Symbols { self: SymbolTable => setInfo(ErrorType) val trace = if (lockTracing) { - val t = _lockingTrace.toList + val t = _lockingTrace.toArray _lockingTrace.clear() t - } else Nil + } else CyclicReference.emptyTrace throw CyclicReference(this, tp, trace) } } else { - if (lockTracing) _lockingTrace.push(this) + if (lockTracing) _lockingTrace.addOne(this) _rawflags |= LOCKED } val current = phase @@ -3849,10 +3849,13 @@ trait Symbols extends api.Symbols { self: SymbolTable => else closestEnclMethod(from.owner) /** An exception for cyclic references of symbol definitions */ - case class CyclicReference(sym: Symbol, info: Type, trace: List[Symbol] = Nil) + case class CyclicReference(sym: Symbol, info: Type, trace: Array[Symbol] = CyclicReference.emptyTrace) extends TypeError(s"illegal cyclic reference involving $sym") { if (settings.isDebug) printStackTrace() } + object CyclicReference { + val emptyTrace: Array[Symbol] = Array.empty[Symbol] + } /** A class for type histories */ private final case class TypeHistory protected (private var _validFrom: Period, private var _info: Type, private var _prev: TypeHistory) {