Skip to content

Commit 54d5466

Browse files
committed
Convert more sets to util.HashSet
1 parent 09d0cb0 commit 54d5466

File tree

13 files changed

+62
-43
lines changed

13 files changed

+62
-43
lines changed

compiler/src/dotty/tools/dotc/classpath/AggregateClassPath.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package dotc.classpath
77
import java.net.URL
88
import scala.collection.mutable.ArrayBuffer
99
import scala.collection.immutable.ArraySeq
10+
import dotc.util
1011

1112
import dotty.tools.io.{ AbstractFile, ClassPath, ClassRepresentation, EfficientClassPath }
1213

@@ -132,7 +133,7 @@ case class AggregateClassPath(aggregates: Seq[ClassPath]) extends ClassPath {
132133
}
133134

134135
private def getDistinctEntries[EntryType <: ClassRepresentation](getEntries: ClassPath => Seq[EntryType]): Seq[EntryType] = {
135-
val seenNames = collection.mutable.HashSet[String]()
136+
val seenNames = util.HashSet[String]()
136137
val entriesBuffer = new ArrayBuffer[EntryType](1024)
137138
for {
138139
cp <- aggregates

compiler/src/dotty/tools/dotc/core/Contexts.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Uniques._
1414
import ast.Trees._
1515
import ast.untpd
1616
import Flags.GivenOrImplicit
17-
import util.{NoSource, SimpleIdentityMap, SourceFile}
17+
import util.{NoSource, SimpleIdentityMap, SourceFile, HashSet}
1818
import typer.{Implicits, ImportInfo, Inliner, SearchHistory, SearchRoot, TypeAssigner, Typer, Nullables}
1919
import Nullables.{NotNullInfo, given _}
2020
import Implicits.ContextualImplicits
@@ -534,7 +534,7 @@ object Contexts {
534534
def settings: ScalaSettings = base.settings
535535
def definitions: Definitions = base.definitions
536536
def platform: Platform = base.platform
537-
def pendingUnderlying: mutable.HashSet[Type] = base.pendingUnderlying
537+
def pendingUnderlying: util.HashSet[Type] = base.pendingUnderlying
538538
def uniqueNamedTypes: Uniques.NamedTypeUniques = base.uniqueNamedTypes
539539
def uniques: util.HashSet[Type] = base.uniques
540540

@@ -838,8 +838,8 @@ object Contexts {
838838
def nextSymId: Int = { _nextSymId += 1; _nextSymId }
839839

840840
/** Sources that were loaded */
841-
val sources: mutable.HashMap[AbstractFile, SourceFile] = new mutable.HashMap[AbstractFile, SourceFile]
842-
val sourceNamed: mutable.HashMap[TermName, SourceFile] = new mutable.HashMap[TermName, SourceFile]
841+
val sources: util.HashMap[AbstractFile, SourceFile] = util.HashMap[AbstractFile, SourceFile]()
842+
val sourceNamed: util.HashMap[TermName, SourceFile] = util.HashMap[TermName, SourceFile]()
843843

844844
// Types state
845845
/** A table for hash consing unique types */
@@ -869,7 +869,7 @@ object Contexts {
869869

870870
/** The set of named types on which a currently active invocation
871871
* of underlying during a controlled operation exists. */
872-
private[core] val pendingUnderlying: mutable.HashSet[Type] = new mutable.HashSet[Type]
872+
private[core] val pendingUnderlying: util.HashSet[Type] = util.HashSet[Type]()
873873

874874
/** A map from ErrorType to associated message. We use this map
875875
* instead of storing messages directly in ErrorTypes in order

compiler/src/dotty/tools/dotc/core/NameKinds.scala

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import NameOps._
77
import StdNames._
88
import NameTags._
99
import Contexts._
10-
import collection.mutable
1110

1211
import scala.annotation.internal.sharable
1312

@@ -17,10 +16,10 @@ object NameKinds {
1716
// These are sharable since all NameKinds are created eagerly at the start of the program
1817
// before any concurrent threads are forked. for this to work, NameKinds should never
1918
// be created lazily or in modules that start running after compilers are forked.
20-
@sharable private val simpleNameKinds = new mutable.HashMap[Int, ClassifiedNameKind]
21-
@sharable private val qualifiedNameKinds = new mutable.HashMap[Int, QualifiedNameKind]
22-
@sharable private val numberedNameKinds = new mutable.HashMap[Int, NumberedNameKind]
23-
@sharable private val uniqueNameKinds = new mutable.HashMap[String, UniqueNameKind]
19+
@sharable private val simpleNameKinds = util.HashMap[Int, ClassifiedNameKind]()
20+
@sharable private val qualifiedNameKinds = util.HashMap[Int, QualifiedNameKind]()
21+
@sharable private val numberedNameKinds = util.HashMap[Int, NumberedNameKind]()
22+
@sharable private val uniqueNameKinds = util.HashMap[String, UniqueNameKind]()
2423

2524
/** A class for the info stored in a derived name */
2625
abstract class NameInfo {
@@ -390,8 +389,8 @@ object NameKinds {
390389
val Scala2MethodNameKinds: List[NameKind] =
391390
List(DefaultGetterName, ExtMethName, UniqueExtMethName)
392391

393-
def simpleNameKindOfTag : collection.Map[Int, ClassifiedNameKind] = simpleNameKinds
394-
def qualifiedNameKindOfTag : collection.Map[Int, QualifiedNameKind] = qualifiedNameKinds
395-
def numberedNameKindOfTag : collection.Map[Int, NumberedNameKind] = numberedNameKinds
396-
def uniqueNameKindOfSeparator: collection.Map[String, UniqueNameKind] = uniqueNameKinds
392+
def simpleNameKindOfTag : util.ReadOnlyMap[Int, ClassifiedNameKind] = simpleNameKinds
393+
def qualifiedNameKindOfTag : util.ReadOnlyMap[Int, QualifiedNameKind] = qualifiedNameKinds
394+
def numberedNameKindOfTag : util.ReadOnlyMap[Int, NumberedNameKind] = numberedNameKinds
395+
def uniqueNameKindOfSeparator: util.ReadOnlyMap[String, UniqueNameKind] = uniqueNameKinds
397396
}

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
4949
needsGc = false
5050
if Config.checkTypeComparerReset then checkReset()
5151

52-
private var pendingSubTypes: mutable.Set[(Type, Type)] = null
52+
private var pendingSubTypes: util.MutableSet[(Type, Type)] = null
5353
private var recCount = 0
5454
private var monitored = false
5555

@@ -202,7 +202,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
202202

203203
def monitoredIsSubType = {
204204
if (pendingSubTypes == null) {
205-
pendingSubTypes = new mutable.HashSet[(Type, Type)]
205+
pendingSubTypes = util.HashSet[(Type, Type)]()
206206
report.log(s"!!! deep subtype recursion involving ${tp1.show} <:< ${tp2.show}, constraint = ${state.constraint.show}")
207207
report.log(s"!!! constraint = ${constraint.show}")
208208
//if (ctx.settings.YnoDeepSubtypes.value) {
@@ -231,7 +231,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
231231
}
232232
}
233233
val p = (normalize(tp1), normalize(tp2))
234-
!pendingSubTypes(p) && {
234+
!pendingSubTypes.contains(p) && {
235235
try {
236236
pendingSubTypes += p
237237
firstTry

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3405,7 +3405,7 @@ object Types {
34053405
abstract class LambdaTypeCompanion[N <: Name, PInfo <: Type, LT <: LambdaType] {
34063406
def syntheticParamName(n: Int): N
34073407

3408-
@sharable private val memoizedNames = new mutable.HashMap[Int, List[N]]
3408+
@sharable private val memoizedNames = util.HashMap[Int, List[N]]()
34093409
def syntheticParamNames(n: Int): List[N] = synchronized {
34103410
memoizedNames.getOrElseUpdate(n, (0 until n).map(syntheticParamName).toList)
34113411
}

compiler/src/dotty/tools/dotc/plugins/Plugin.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,12 @@ object Plugin {
162162
case Failure(e) => Failure(e)
163163
})
164164

165-
val seen = mutable.HashSet[String]()
165+
val seen = util.HashSet[String]()
166166
val enabled = (fromPaths ::: fromDirs) map(_.flatMap {
167167
case (classname, loader) =>
168168
Plugin.load(classname, loader).flatMap { clazz =>
169169
val plugin = instantiate(clazz)
170-
if (seen(classname)) // a nod to scala/bug#7494, take the plugin classes distinctly
170+
if (seen.contains(classname)) // a nod to scala/bug#7494, take the plugin classes distinctly
171171
Failure(new PluginLoadException(plugin.name, s"Ignoring duplicate plugin ${plugin.name} (${classname})"))
172172
else if (ignoring contains plugin.name)
173173
Failure(new PluginLoadException(plugin.name, s"Disabling plugin ${plugin.name}"))

compiler/src/dotty/tools/dotc/transform/CapturedVars.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ class CapturedVars extends MiniPhase with IdentityDenotTransformer { thisPhase =
2626
override def runsAfterGroupsOf: Set[String] = Set(LiftTry.name)
2727
// lifting tries changes what variables are considered to be captured
2828

29-
private[this] var Captured: Store.Location[collection.Set[Symbol]] = _
29+
private[this] var Captured: Store.Location[util.ReadOnlySet[Symbol]] = _
3030
private def captured(using Context) = ctx.store(Captured)
3131

3232
override def initContext(ctx: FreshContext): Unit =
33-
Captured = ctx.addLocation(Set.empty)
33+
Captured = ctx.addLocation(util.ReadOnlySet.empty)
3434

3535
private class RefInfo(using Context) {
3636
/** The classes for which a Ref type exists. */
@@ -54,7 +54,7 @@ class CapturedVars extends MiniPhase with IdentityDenotTransformer { thisPhase =
5454
}
5555

5656
private class CollectCaptured extends TreeTraverser {
57-
private val captured = mutable.HashSet[Symbol]()
57+
private val captured = util.HashSet[Symbol]()
5858
def traverse(tree: Tree)(using Context) = tree match {
5959
case id: Ident =>
6060
val sym = id.symbol
@@ -68,7 +68,7 @@ class CapturedVars extends MiniPhase with IdentityDenotTransformer { thisPhase =
6868
case _ =>
6969
traverseChildren(tree)
7070
}
71-
def runOver(tree: Tree)(using Context): collection.Set[Symbol] = {
71+
def runOver(tree: Tree)(using Context): util.ReadOnlySet[Symbol] = {
7272
traverse(tree)
7373
captured
7474
}

compiler/src/dotty/tools/dotc/transform/Getters.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class Getters extends MiniPhase with SymTransformer { thisPhase =>
9191
}
9292
private val NoGetterNeededFlags = Method | Param | JavaDefined | JavaStatic
9393

94-
val newSetters = mutable.HashSet[Symbol]()
94+
val newSetters = util.HashSet[Symbol]()
9595

9696
def ensureSetter(sym: TermSymbol)(using Context) =
9797
if !sym.setter.exists then

compiler/src/dotty/tools/dotc/transform/OverridingPairs.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ object OverridingPairs {
7777
* (maybe excluded because of hasCommonParentAsSubclass).
7878
* These will not appear as overriding
7979
*/
80-
private val visited = new mutable.HashSet[Symbol]
80+
private val visited = util.HashSet[Symbol]()
8181

8282
/** The current entry candidate for overriding
8383
*/

compiler/src/dotty/tools/dotc/transform/TreeChecker.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ class TreeChecker extends Phase with SymTransformer {
160160

161161
class Checker(phasesToCheck: Seq[Phase]) extends ReTyper with Checking {
162162

163-
private val nowDefinedSyms = new mutable.HashSet[Symbol]
164-
private val patBoundSyms = new mutable.HashSet[Symbol]
163+
private val nowDefinedSyms = util.HashSet[Symbol]()
164+
private val patBoundSyms = util.HashSet[Symbol]()
165165
private val everDefinedSyms = MutableSymbolMap[untpd.Tree]()
166166

167167
// don't check value classes after typer, as the constraint about constructors doesn't hold after transform

compiler/src/dotty/tools/dotc/util/MutableSet.scala

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ package dotty.tools.dotc.util
22

33
/** A common class for lightweight mutable sets.
44
*/
5-
abstract class MutableSet[T] {
6-
7-
/** The entry in the set such that `isEqual(x, entry)`, or else `null`. */
8-
def lookup(x: T): T | Null
5+
abstract class MutableSet[T] extends ReadOnlySet[T]:
96

107
/** Add element `x` to the set */
118
def +=(x: T): Unit
@@ -15,16 +12,14 @@ abstract class MutableSet[T] {
1512
*/
1613
def put(x: T): T
1714

18-
def clear(): Unit
19-
20-
def size: Int
15+
/** Remove element `x` from the set */
16+
def -=(x: T): Unit
2117

22-
def iterator: Iterator[T]
23-
24-
def contains(x: T): Boolean = lookup(x) != null
18+
def clear(): Unit
2519

26-
def foreach[U](f: T => U): Unit = iterator foreach f
20+
def ++= (xs: IterableOnce[T]): Unit =
21+
xs.iterator.foreach(this += _)
2722

28-
def toList: List[T] = iterator.toList
23+
def --= (xs: IterableOnce[T]): Unit =
24+
xs.iterator.foreach(this -= _)
2925

30-
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package dotty.tools.dotc.util
2+
3+
/** A class for the readonly part of mutable sets.
4+
*/
5+
abstract class ReadOnlySet[T]:
6+
7+
/** The entry in the set such that `isEqual(x, entry)`, or else `null`. */
8+
def lookup(x: T): T | Null
9+
10+
def size: Int
11+
12+
def iterator: Iterator[T]
13+
14+
def contains(x: T): Boolean = lookup(x) != null
15+
16+
def foreach[U](f: T => U): Unit = iterator.foreach(f)
17+
18+
def toList: List[T] = iterator.toList
19+
20+
def isEmpty = size == 0
21+
22+
object ReadOnlySet:
23+
def empty[T]: ReadOnlySet[T] = HashSet[T](4)
24+

tests/neg-custom-args/i1650.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
object Test {
2-
test4(test4$default$1) // error: recursion limit exceeded
2+
test4(test4$default$1)
33
def test4[T[P]](x: T[T[List[T[X forSome { type X }]]]]) = ??? // error // error
44
def test4$default$1[T[P]]: T[Int] = ???
55
}

0 commit comments

Comments
 (0)