Skip to content

Commit 82196a5

Browse files
committed
Make FirstTransform.reorder semi-tailrec
1 parent 60591f8 commit 82196a5

File tree

3 files changed

+2038
-4016
lines changed

3 files changed

+2038
-4016
lines changed

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,26 +92,27 @@ 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]): List[Tree] = stats match {
95+
def reorder(stats: List[Tree], revPrefix: List[Tree] = Nil): List[Tree] = stats match {
9696
case (stat: TypeDef) :: stats1 if stat.symbol.isClass =>
9797
if (stat.symbol is Flags.Module) {
9898
moduleClassDefs += (stat.name -> stat)
9999
singleClassDefs -= stat.name.stripModuleClassSuffix
100100
val stats1r = reorder(stats1)
101-
if (moduleClassDefs contains stat.name) stat :: stats1r else stats1r
101+
revPrefix.reverse ::: (if (moduleClassDefs contains stat.name) stat :: stats1r else stats1r)
102102
} else {
103-
def stats1r = reorder(stats1)
104-
val normalized = moduleClassDefs remove stat.name.moduleClassName match {
105-
case Some(mcdef) =>
106-
mcdef :: stats1r
107-
case None =>
108-
singleClassDefs += (stat.name -> stat)
109-
stats1r
110-
}
111-
stat :: normalized
103+
reorder(
104+
stats1,
105+
moduleClassDefs remove stat.name.moduleClassName match {
106+
case Some(mcdef) =>
107+
mcdef :: stat :: revPrefix
108+
case None =>
109+
singleClassDefs += (stat.name -> stat)
110+
stat :: revPrefix
111+
}
112+
)
112113
}
113-
case stat :: stats1 => stat :: reorder(stats1)
114-
case Nil => Nil
114+
case stat :: stats1 => reorder(stats1, stat :: revPrefix)
115+
case Nil => revPrefix.reverse
115116
}
116117

117118
def registerCompanion(name: TermName, forClass: Symbol): TermSymbol = {

0 commit comments

Comments
 (0)