Skip to content

Commit 02f2688

Browse files
committed
Improve FirstTransform reorder.
1 parent 82196a5 commit 02f2688

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,21 @@ class FirstTransform extends MiniPhaseTransform with InfoTransformer with Annota
9292
private def reorderAndComplete(stats: List[Tree])(implicit ctx: Context): List[Tree] = {
9393
val moduleClassDefs, singleClassDefs = mutable.Map[Name, Tree]()
9494

95-
def reorder(stats: List[Tree], revPrefix: List[Tree] = Nil): List[Tree] = stats match {
95+
/* Returns the result of reordering stats and prepending revPrefix in reverse order to it.
96+
* The result of reorder is equivalent to reorder(stats, revPrefix) = revPrefix.reverse ::: reorder(stats, Nil).
97+
* This implementation is tail recursive as long as the element is not a module TypeDef.
98+
*/
99+
def reorder(stats: List[Tree], revPrefix: List[Tree]): List[Tree] = stats match {
96100
case (stat: TypeDef) :: stats1 if stat.symbol.isClass =>
97101
if (stat.symbol is Flags.Module) {
102+
def pushOnTop(xs: List[Tree], ys: List[Tree]): List[Tree] = xs match {
103+
case x :: xs1 => pushOnTop(xs1, x :: ys)
104+
case Nil => ys
105+
}
98106
moduleClassDefs += (stat.name -> stat)
99107
singleClassDefs -= stat.name.stripModuleClassSuffix
100-
val stats1r = reorder(stats1)
101-
revPrefix.reverse ::: (if (moduleClassDefs contains stat.name) stat :: stats1r else stats1r)
108+
val stats1r = reorder(stats1, Nil)
109+
pushOnTop(revPrefix, if (moduleClassDefs contains stat.name) stat :: stats1r else stats1r)
102110
} else {
103111
reorder(
104112
stats1,
@@ -137,7 +145,7 @@ class FirstTransform extends MiniPhaseTransform with InfoTransformer with Annota
137145
case stat => stat
138146
}
139147

140-
addMissingCompanions(reorder(stats))
148+
addMissingCompanions(reorder(stats, Nil))
141149
}
142150

143151
private def newCompanion(name: TermName, forClass: Symbol)(implicit ctx: Context) = {

0 commit comments

Comments
 (0)