Skip to content

Commit 69a9fdb

Browse files
Address review
1 parent 96dccb9 commit 69a9fdb

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

compiler/src/dotty/tools/dotc/transform/localopt/InlineLocalObjects.scala

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import core.Symbols._
1212
import core.Flags._
1313
import ast.Trees._
1414
import scala.collection.mutable
15+
import scala.collection.mutable.LinkedHashMap
1516
import transform.SymUtils._
1617
import config.Printers.simplify
1718
import Simplify._
@@ -33,11 +34,11 @@ class InlineLocalObjects(val simplifyPhase: Simplify) extends Optimisation {
3334
// ValDefs whose lhs is used with `._1` (or any getter call).
3435
val gettersCalled = mutable.HashSet[Symbol]()
3536

36-
// Map from class to new fields, initialised between visitor and transformer.
37-
var newFieldsMapping: Map[Symbol, List[(Symbol, Symbol)]] = null
38-
// | | |
39-
// | | New fields, replacements these getters
40-
// | Usages of getters of these classes
37+
// Immutable sorted map from class to new fields, initialized between visitor and transformer.
38+
var newFieldsMapping: Map[Symbol, LinkedHashMap[Symbol, Symbol]] = null
39+
// | | |
40+
// | | New fields, replacements these getters
41+
// | Usages of getters of these classes
4142
// ValDefs of the classes that are being torn apart; = candidates.intersect(gettersCalled)
4243

4344
def clear(): Unit = {
@@ -57,7 +58,7 @@ class InlineLocalObjects(val simplifyPhase: Simplify) extends Optimisation {
5758
val info: Type = x.asSeenFrom(refVal.info).info.finalResultType.widenDealias
5859
ctx.newSymbol(owner, name, flags, info)
5960
}
60-
(refVal, accessors.zip(newLocals))
61+
(refVal, LinkedHashMap[Symbol, Symbol](accessors.zip(newLocals): _*))
6162
}.toMap
6263
}
6364

@@ -89,7 +90,7 @@ class InlineLocalObjects(val simplifyPhase: Simplify) extends Optimisation {
8990
initNewFieldsMapping();
9091
{
9192
case t @ NewCaseClassValDef(fun, args) if newFieldsMapping.contains(t.symbol) =>
92-
val newFields = newFieldsMapping(t.symbol).map(_._2)
93+
val newFields = newFieldsMapping(t.symbol).values.toList
9394
val newFieldsDefs = newFields.zip(args).map { case (nf, arg) =>
9495
val rhs = arg.changeOwnerAfter(t.symbol, nf.symbol, simplifyPhase)
9596
ValDef(nf.asTerm, rhs)
@@ -99,11 +100,13 @@ class InlineLocalObjects(val simplifyPhase: Simplify) extends Optimisation {
99100
Thicket(newFieldsDefs :+ recreate)
100101

101102
case t @ Select(rec, _) if isImmutableAccessor(t) =>
102-
newFieldsMapping.getOrElse(rec.symbol, Nil).collect {
103-
case ((oldSym, newSym)) if oldSym == t.symbol => ref(newSym)
104-
}.headOption.getOrElse(t)
103+
newFieldsMapping.getOrElse(rec.symbol, Map.empty[Symbol, Symbol]).get(t.symbol) match {
104+
case None => t
105+
case Some(newSym) => ref(newSym)
106+
}
105107

106108
case t => t
107109
}
108110
}
109111
}
112+

tests/run/i3018.flags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-optimise
File renamed without changes.

0 commit comments

Comments
 (0)