Skip to content

Commit 1d55a77

Browse files
committed
Hide free
1 parent 097cae6 commit 1d55a77

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ abstract class Dependencies(@constructorOnly rootContext: Context):
3333
type SymSet = TreeSet[Symbol]
3434

3535
/** A map storing free variables of functions and classes */
36-
val free: mutable.LinkedHashMap[Symbol, SymSet] = new LinkedHashMap
36+
private val free: mutable.LinkedHashMap[Symbol, SymSet] = new LinkedHashMap
3737

3838
/** A hashtable storing calls between functions */
3939
private val called = new LinkedHashMap[Symbol, SymSet]
@@ -61,6 +61,8 @@ abstract class Dependencies(@constructorOnly rootContext: Context):
6161

6262
def freeVars(sym: Symbol): collection.Set[Symbol] = free.getOrElse(sym, Set.empty)
6363

64+
def tracked: Iterable[Symbol] = free.keys
65+
6466
/** A symbol is local if it is owned by a term or a local trait,
6567
* or if it is a constructor of a local symbol.
6668
* Note: we count members of local traits as local since their free variables

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ object LambdaLift {
5050
liftedDefs(tree.symbol.owner) = new mutable.ListBuffer
5151
case _ =>
5252
end deps
53-
export deps.{liftedOwner, free}
53+
export deps.{liftedOwner}
5454

5555
/** A map storing the free variable proxies of functions and classes.
5656
* For every function and class, this is a map from the free variables
@@ -71,19 +71,18 @@ object LambdaLift {
7171
else sym.name.freshened
7272

7373
private def generateProxies()(using Context): Unit =
74-
for ((owner, freeValues) <- free.iterator) {
74+
for owner <- deps.tracked do
75+
val fvs = deps.freeVars(owner).toList
7576
val newFlags = Synthetic | (if (owner.isClass) ParamAccessor | Private else Param)
76-
report.debuglog(i"free var proxy of ${owner.showLocated}: ${freeValues.toList}%, %")
77-
proxyMap(owner) = {
78-
for (fv <- freeValues.toList) yield {
77+
report.debuglog(i"free var proxy of ${owner.showLocated}: $fvs%, %")
78+
val freeProxyPairs =
79+
for fv <- fvs yield
7980
val proxyName = newName(fv)
8081
val proxy =
8182
newSymbol(owner, proxyName.asTermName, newFlags, fv.info, coord = fv.coord)
8283
.enteredAfter(thisPhase)
8384
(fv, proxy)
84-
}
85-
}.toMap
86-
}
85+
proxyMap(owner) = freeProxyPairs.toMap
8786

8887
private def liftedInfo(local: Symbol)(using Context): Type = local.info match {
8988
case MethodTpe(pnames, ptypes, restpe) =>
@@ -131,7 +130,7 @@ object LambdaLift {
131130
initFlags = initFlags,
132131
info = liftedInfo(local)).installAfter(thisPhase)
133132
}
134-
for (local <- free.keys)
133+
for (local <- deps.tracked)
135134
if (!liftedOwner.contains(local))
136135
local.copySymDenotation(info = liftedInfo(local)).installAfter(thisPhase)
137136
}
@@ -190,10 +189,8 @@ object LambdaLift {
190189
}
191190

192191
def addFreeArgs(sym: Symbol, args: List[Tree])(using Context): List[Tree] =
193-
free get sym match {
194-
case Some(fvs) => fvs.toList.map(proxyRef(_)) ++ args
195-
case _ => args
196-
}
192+
val fvs = deps.freeVars(sym)
193+
if fvs.nonEmpty then fvs.toList.map(proxyRef(_)) ++ args else args
197194

198195
def addFreeParams(tree: Tree, proxies: List[Symbol])(using Context): Tree = proxies match {
199196
case Nil => tree
@@ -319,7 +316,7 @@ class LambdaLift extends MiniPhase with IdentityDenotTransformer { thisPhase =>
319316
// reload them manually here.
320317
// Note: If you tweak this code, make sure to test your changes with
321318
// `Config.reuseSymDenotations` set to false to exercise this path more.
322-
if denot.isInstanceOf[NonSymSingleDenotation] && lifter.free.contains(sym) then
319+
if denot.isInstanceOf[NonSymSingleDenotation] && lifter.deps.freeVars(sym).nonEmpty then
323320
tree.qualifier.select(sym).withSpan(tree.span)
324321
else tree
325322

@@ -333,7 +330,7 @@ class LambdaLift extends MiniPhase with IdentityDenotTransformer { thisPhase =>
333330
val sym = tree.symbol
334331
val lft = lifter
335332
val paramsAdded =
336-
if (lft.free.contains(sym)) lft.addFreeParams(tree, lft.proxies(sym)).asInstanceOf[DefDef]
333+
if lft.deps.freeVars(sym).nonEmpty then lft.addFreeParams(tree, lft.proxies(sym)).asInstanceOf[DefDef]
337334
else tree
338335
if (lft.needsLifting(sym)) lft.liftDef(paramsAdded)
339336
else paramsAdded

0 commit comments

Comments
 (0)