Skip to content

Commit db9fcbc

Browse files
Merge pull request #3021 from dotty-staging/fix-3018
Fix #3018: use List instead of Map in InlineLocalObjects
2 parents 4366453 + 69a9fdb commit db9fcbc

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

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

Lines changed: 10 additions & 8 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,12 +34,12 @@ 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, Map[Symbol, Symbol]] = null
38-
// | | |
39-
// | | New fields, replacements these getters
40-
// | Usages of getters of these classes
41-
// ValDefs of the classes that are being torn apart; = candidates.intersect(gettersCalled)
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
42+
// ValDefs of the classes that are being torn apart; = candidates.intersect(gettersCalled)
4243

4344
def clear(): Unit = {
4445
candidates.clear()
@@ -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).toMap)
61+
(refVal, LinkedHashMap[Symbol, Symbol](accessors.zip(newLocals): _*))
6162
}.toMap
6263
}
6364

@@ -99,7 +100,7 @@ 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, Map.empty).get(t.symbol) match {
103+
newFieldsMapping.getOrElse(rec.symbol, Map.empty[Symbol, Symbol]).get(t.symbol) match {
103104
case None => t
104105
case Some(newSym) => ref(newSym)
105106
}
@@ -108,3 +109,4 @@ class InlineLocalObjects(val simplifyPhase: Simplify) extends Optimisation {
108109
}
109110
}
110111
}
112+

tests/run/i3018.flags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-optimise

tests/run/i3018.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Type
2+
class Symbol
3+
4+
sealed trait Space
5+
case object Empty extends Space
6+
case class Typ(tp: Type, decomposed: Boolean) extends Space
7+
case class Prod(tp: Type, unappTp: Type, unappSym: Symbol, params: List[Space], full: Boolean) extends Space
8+
case class Or(spaces: List[Space]) extends Space
9+
10+
object Test {
11+
def simplify(space: Space, aggressive: Boolean = false): Space = space match {
12+
case Prod(tp, fun, sym, spaces, full) =>
13+
val sp = Prod(tp, fun, sym, spaces.map(simplify(_)), full)
14+
if (sp.params.contains(Empty)) Empty
15+
else sp
16+
case Or(spaces) => space
17+
case Typ(tp, _) => space
18+
case _ => space
19+
}
20+
21+
def main(args: Array[String]): Unit = {
22+
simplify(Prod(new Type, new Type, new Symbol, Nil, false))
23+
}
24+
}

0 commit comments

Comments
 (0)