Skip to content

Rewrite typer.TermRefSet #5593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions compiler/src/dotty/tools/dotc/typer/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ trait ImplicitRunInfo { self: Run =>

private val implicitScopeCache = mutable.AnyRefMap[Type, OfTypeImplicits]()

private val EmptyTermRefSet = new TermRefSet()(NoContext)

/** The implicit scope of a type `tp`
* @param liftingCtx A context to be used when computing the class symbols of
* a type. Types may contain type variables with their instances
Expand Down Expand Up @@ -1533,28 +1535,32 @@ final class SearchRoot extends SearchHistory {
}

/** A set of term references where equality is =:= */
class TermRefSet(implicit ctx: Context) extends mutable.Traversable[TermRef] {
import collection.JavaConverters._
private val elems = (new java.util.LinkedHashMap[TermSymbol, List[Type]]).asScala
final class TermRefSet(implicit ctx: Context) {
private[this] val elems = new java.util.LinkedHashMap[TermSymbol, List[Type]]

def += (ref: TermRef): Unit = {
val pre = ref.prefix
val sym = ref.symbol.asTerm
elems get sym match {
case Some(prefixes) =>
if (!(prefixes exists (_ =:= pre))) elems(sym) = pre :: prefixes
case None =>
elems(sym) = pre :: Nil
elems.get(sym) match {
case null =>
elems.put(sym, pre :: Nil)
case prefixes =>
if (!prefixes.exists(_ =:= pre))
elems.put(sym, pre :: prefixes)
}
}

def ++= (refs: TraversableOnce[TermRef]): Unit =
refs foreach +=
def ++= (that: TermRefSet): Unit =
that.foreach(+=)

override def foreach[U](f: TermRef => U): Unit =
for (sym <- elems.keysIterator)
for (pre <- elems(sym))
f(TermRef(pre, sym))
}
def foreach[U](f: TermRef => U): Unit =
elems.forEach((sym: TermSymbol, prefixes: List[Type]) =>
prefixes.foreach(pre => f(TermRef(pre, sym))))

@sharable object EmptyTermRefSet extends TermRefSet()(NoContext)
// used only for debugging
def toList: List[TermRef] = {
val buffer = new mutable.ListBuffer[TermRef]
foreach(tr => buffer += tr)
buffer.toList
}
}