Skip to content

Commit

Permalink
Prefer ArrayBuffer to Stack, Array to List
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Mar 17, 2024
1 parent 5bb27fd commit 4a80252
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/compiler/scala/tools/nsc/Global.scala
Expand Up @@ -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 }
Expand Down
13 changes: 7 additions & 6 deletions src/compiler/scala/tools/nsc/typechecker/TypeDiagnostics.scala
Expand Up @@ -842,19 +842,20 @@ 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 baddies => baddies.find(_.pos.focus == pos.focus).getOrElse(baddies.head)
val badsym = if (!sym.isSynthetic) sym else {
val organics = trace.filter(!_.isSynthetic)
if (organics.length == 0) sym
else if (organics.length == 1) organics(0)
else organics.find(_.pos.focus == pos.focus).getOrElse(organics(0))
}
condOpt(tree) {
case ValDef(_, _, TypeTree(), _) => s"recursive $badsym needs type"
case DefDef(_, _, _, _, TypeTree(), _) => s"${cyclicAdjective(badsym)} $badsym needs result type"
case Import(expr, selectors) =>
case Import(_, _) =>
sm"""encountered unrecoverable cycle resolving import.
|Note: this is often due in part to a class depending on a definition nested within its companion.
|If applicable, you may wish to try moving some members into another object."""
Expand Down
29 changes: 16 additions & 13 deletions src/reflect/scala/reflect/internal/Symbols.scala
Expand Up @@ -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 =>
Expand All @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4a80252

Please sign in to comment.