Skip to content

Commit 96aa562

Browse files
committed
Simplify UsedName
1 parent 31a93a1 commit 96aa562

File tree

1 file changed

+31
-45
lines changed

1 file changed

+31
-45
lines changed

compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class ExtractDependencies extends Phase {
6969
extractDeps.traverse(unit.tpdTree)
7070

7171
if (dumpInc) {
72-
val names = extractDeps.usedNames.map(_.toString).toArray[Object]
72+
val names = extractDeps.usedNames.map { case (clazz, names) => s"$clazz: $names" }.toArray[Object]
7373
val deps = extractDeps.dependencies.map(_.toString).toArray[Object]
7474
Arrays.sort(names)
7575
Arrays.sort(deps)
@@ -82,20 +82,12 @@ class ExtractDependencies extends Phase {
8282
}
8383

8484
if (ctx.sbtCallback != null) {
85-
extractDeps.usedNames.foreach{
86-
case (rawClassName, usedNames) =>
87-
val className = rawClassName
88-
usedNames.defaultNames.foreach { rawUsedName =>
89-
val useName = rawUsedName.toString
90-
val useScopes =
91-
usedNames.scopedNames.get(rawUsedName) match {
92-
case None => EnumSet.of(UseScope.Default)
93-
case Some(existingScopes) =>
94-
existingScopes.add(UseScope.Default)
95-
existingScopes
96-
}
97-
98-
ctx.sbtCallback.usedName(className, useName, useScopes)
85+
extractDeps.usedNames.foreach {
86+
case (clazz, usedNames) =>
87+
val className = clazz
88+
usedNames.names.foreach {
89+
case (usedName, scopes) =>
90+
ctx.sbtCallback.usedName(className, usedName.toString, scopes)
9991
}
10092
}
10193

@@ -184,29 +176,25 @@ object ExtractDependencies {
184176

185177
private case class ClassDependency(from: Symbol, to: Symbol, context: DependencyContext)
186178

187-
private final class NameUsedInClass {
188-
// Default names and other scopes are separated for performance reasons
189-
val defaultNames: mutable.Set[Name] = new mutable.HashSet[Name]
190-
val scopedNames: mutable.Map[Name, EnumSet[UseScope]] = new mutable.HashMap[Name, EnumSet[UseScope]].withDefault(_ => EnumSet.noneOf(classOf[UseScope]))
179+
private final class UsedNamesInClass {
180+
private val _names = new mutable.HashMap[Name, EnumSet[UseScope]]
181+
def names: collection.Map[Name, EnumSet[UseScope]] = _names
182+
183+
def update(name: Name, scope: UseScope): Unit = {
184+
val scopes = _names.getOrElseUpdate(name, EnumSet.noneOf(classOf[UseScope]))
185+
scopes.add(scope)
186+
}
191187

192-
// We have to leave with commas on ends
193188
override def toString(): String = {
194-
val builder = new StringBuilder(": ")
195-
defaultNames.foreach { name =>
196-
builder.append(name.toString.trim)
197-
val otherScopes = scopedNames.get(name)
198-
scopedNames.get(name) match {
199-
case None =>
200-
case Some(otherScopes) =>
201-
// Pickling tests fail when this is turned in an anonymous class
202-
class Consumer extends java.util.function.Consumer[UseScope]() {
203-
override def accept(scope: UseScope): Unit =
204-
builder.append(scope.name()).append(", ")
205-
}
206-
builder.append(" in [")
207-
otherScopes.forEach(new Consumer)
208-
builder.append("]")
209-
}
189+
val builder = new StringBuilder
190+
names.foreach { case (name, scopes) =>
191+
builder.append(name.mangledString)
192+
builder.append(" in [")
193+
scopes.forEach(new java.util.function.Consumer[UseScope]() { // TODO: Adapt to SAM type when #2732 is fixed
194+
override def accept(scope: UseScope): Unit =
195+
builder.append(scope.toString)
196+
})
197+
builder.append("]")
210198
builder.append(", ")
211199
}
212200
builder.toString()
@@ -225,13 +213,13 @@ private class ExtractDependenciesCollector(responsibleForImports: Symbol)(implic
225213
import tpd._
226214
import ExtractDependencies._
227215

228-
private[this] val _usedNames = new mutable.HashMap[String, NameUsedInClass]
216+
private[this] val _usedNames = new mutable.HashMap[String, UsedNamesInClass]
229217
private[this] val _dependencies = new mutable.HashSet[ClassDependency]
230218

231219
/** The names used in this class, this does not include names which are only
232220
* defined and not referenced.
233221
*/
234-
def usedNames: collection.Map[String, NameUsedInClass] = _usedNames
222+
def usedNames: collection.Map[String, UsedNamesInClass] = _usedNames
235223

236224
/** The set of class dependencies from this compilation unit.
237225
*/
@@ -241,10 +229,8 @@ private class ExtractDependenciesCollector(responsibleForImports: Symbol)(implic
241229
val enclosingName =
242230
if (enclosingSym == defn.RootClass) classNameAsString(responsibleForImports)
243231
else classNameAsString(enclosingSym)
244-
val nameUsed = _usedNames.getOrElseUpdate(enclosingName, new NameUsedInClass)
245-
nameUsed.defaultNames += name
246-
// TODO: Set correct scope
247-
nameUsed.scopedNames(name).add(UseScope.Default)
232+
val nameUsed = _usedNames.getOrElseUpdate(enclosingName, new UsedNamesInClass)
233+
nameUsed.update(name, UseScope.Default)
248234
}
249235

250236
private def addDependency(sym: Symbol)(implicit ctx: Context): Unit =
@@ -284,10 +270,10 @@ private class ExtractDependenciesCollector(responsibleForImports: Symbol)(implic
284270
override protected def addDependency(symbol: Symbol)(implicit ctx: Context): Unit = {
285271
if (!ignoreDependency(symbol) && symbol.is(Sealed)) {
286272
val encName = nonLocalEnclosingClass(ctx.owner).fullName.stripModuleClassSuffix.toString
287-
val nameUsed = _usedNames.getOrElseUpdate(encName, new NameUsedInClass)
273+
val nameUsed = _usedNames.getOrElseUpdate(encName, new UsedNamesInClass)
288274

289-
nameUsed.defaultNames += symbol.name
290-
nameUsed.scopedNames(symbol.name).add(UseScope.PatMatTarget)
275+
nameUsed.update(symbol.name, UseScope.Default)
276+
nameUsed.update(symbol.name, UseScope.PatMatTarget)
291277
}
292278
}
293279
}

0 commit comments

Comments
 (0)