Skip to content

Commit a40039e

Browse files
authored
Merge pull request #5112 from dotty-staging/tailrec-after-erasure
Move TailRec after Erasure.
2 parents 561c06a + f102203 commit a40039e

File tree

13 files changed

+143
-208
lines changed

13 files changed

+143
-208
lines changed

compiler/src/dotty/tools/dotc/Compiler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ class Compiler {
6767
new ProtectedAccessors, // Add accessors for protected members
6868
new ExtensionMethods, // Expand methods of value classes with extension methods
6969
new ShortcutImplicits, // Allow implicit functions without creating closures
70-
new TailRec, // Rewrite tail recursion to loops
7170
new ByNameClosures, // Expand arguments to by-name parameters to closures
7271
new LiftTry, // Put try expressions that might execute on non-empty stacks into their own methods
7372
new HoistSuperArgs, // Hoist complex arguments of supercalls to enclosing scope
@@ -96,6 +95,7 @@ class Compiler {
9695
List(new Erasure) :: // Rewrite types to JVM model, erasing all type parameters, abstract types and refinements.
9796
List(new ElimErasedValueType, // Expand erased value types to their underlying implmementation types
9897
new VCElideAllocations, // Peep-hole optimization to eliminate unnecessary value class allocations
98+
new TailRec, // Rewrite tail recursion to loops
9999
new Mixin, // Expand trait fields and trait initializers
100100
new LazyVals, // Expand lazy vals
101101
new Memoize, // Add private fields to getters and setters

compiler/src/dotty/tools/dotc/ast/TreeTypeMap.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ class TreeTypeMap(
116116
val guard1 = tmap.transform(guard)
117117
val rhs1 = tmap.transform(rhs)
118118
cpy.CaseDef(cdef)(pat1, guard1, rhs1)
119+
case labeled @ Labeled(bind, expr) =>
120+
val tmap = withMappedSyms(bind.symbol :: Nil)
121+
val bind1 = tmap.transformSub(bind)
122+
val expr1 = tmap.transform(expr)
123+
cpy.Labeled(labeled)(bind1, expr1)
119124
case Hole(n, args) =>
120125
Hole(n, args.mapConserve(transform)).withPos(tree.pos).withType(mapType(tree.tpe))
121126
case tree1 =>

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,17 @@ object Types {
208208
def loop(tp: Type): Boolean = tp match {
209209
case tp: TypeRef =>
210210
val sym = tp.symbol
211-
if (sym.isClass) sym.derivesFrom(cls) else loop(tp.superType): @tailrec
211+
if (sym.isClass) sym.derivesFrom(cls) else loop(tp.superType)
212212
case tp: AppliedType =>
213213
tp.superType.derivesFrom(cls)
214214
case tp: MatchType =>
215215
tp.bound.derivesFrom(cls) || tp.reduced.derivesFrom(cls)
216216
case tp: TypeProxy =>
217-
loop(tp.underlying): @tailrec
217+
loop(tp.underlying)
218218
case tp: AndType =>
219-
loop(tp.tp1) || loop(tp.tp2): @tailrec
219+
loop(tp.tp1) || loop(tp.tp2)
220220
case tp: OrType =>
221-
loop(tp.tp1) && loop(tp.tp2): @tailrec
221+
loop(tp.tp1) && loop(tp.tp2)
222222
case tp: JavaArrayType =>
223223
cls == defn.ObjectClass
224224
case _ =>
@@ -403,16 +403,16 @@ object Types {
403403
*/
404404
final def classSymbol(implicit ctx: Context): Symbol = this match {
405405
case ConstantType(constant) =>
406-
constant.tpe.classSymbol: @tailrec
406+
constant.tpe.classSymbol
407407
case tp: TypeRef =>
408408
val sym = tp.symbol
409-
if (sym.isClass) sym else tp.superType.classSymbol: @tailrec
409+
if (sym.isClass) sym else tp.superType.classSymbol
410410
case tp: ClassInfo =>
411411
tp.cls
412412
case tp: SingletonType =>
413413
NoSymbol
414414
case tp: TypeProxy =>
415-
tp.underlying.classSymbol: @tailrec
415+
tp.underlying.classSymbol
416416
case AndType(l, r) =>
417417
val lsym = l.classSymbol
418418
val rsym = r.classSymbol
@@ -436,9 +436,9 @@ object Types {
436436
tp.cls :: Nil
437437
case tp: TypeRef =>
438438
val sym = tp.symbol
439-
if (sym.isClass) sym.asClass :: Nil else tp.superType.classSymbols: @tailrec
439+
if (sym.isClass) sym.asClass :: Nil else tp.superType.classSymbols
440440
case tp: TypeProxy =>
441-
tp.underlying.classSymbols: @tailrec
441+
tp.underlying.classSymbols
442442
case AndType(l, r) =>
443443
l.classSymbols union r.classSymbols
444444
case OrType(l, r) =>
@@ -479,7 +479,7 @@ object Types {
479479
case tp: ClassInfo =>
480480
tp.decls
481481
case tp: TypeProxy =>
482-
tp.underlying.decls: @tailrec
482+
tp.underlying.decls
483483
case _ =>
484484
EmptyScope
485485
}
@@ -725,7 +725,7 @@ object Types {
725725
val ns = tp.parent.memberNames(keepOnly, pre)
726726
if (keepOnly(pre, tp.refinedName)) ns + tp.refinedName else ns
727727
case tp: TypeProxy =>
728-
tp.underlying.memberNames(keepOnly, pre): @tailrec
728+
tp.underlying.memberNames(keepOnly, pre)
729729
case tp: AndType =>
730730
tp.tp1.memberNames(keepOnly, pre) | tp.tp2.memberNames(keepOnly, pre)
731731
case tp: OrType =>
@@ -1042,21 +1042,21 @@ object Types {
10421042
case tp: TypeRef =>
10431043
if (tp.symbol.isClass) tp
10441044
else tp.info match {
1045-
case TypeAlias(alias) => alias.dealias1(keep): @tailrec
1045+
case TypeAlias(alias) => alias.dealias1(keep)
10461046
case _ => tp
10471047
}
10481048
case app @ AppliedType(tycon, args) =>
10491049
val tycon1 = tycon.dealias1(keep)
1050-
if (tycon1 ne tycon) app.superType.dealias1(keep): @tailrec
1050+
if (tycon1 ne tycon) app.superType.dealias1(keep)
10511051
else this
10521052
case tp: TypeVar =>
10531053
val tp1 = tp.instanceOpt
1054-
if (tp1.exists) tp1.dealias1(keep): @tailrec else tp
1054+
if (tp1.exists) tp1.dealias1(keep) else tp
10551055
case tp: AnnotatedType =>
10561056
val tp1 = tp.parent.dealias1(keep)
10571057
if (keep(tp)(ctx)) tp.derivedAnnotatedType(tp1, tp.annot) else tp1
10581058
case tp: LazyRef =>
1059-
tp.ref.dealias1(keep): @tailrec
1059+
tp.ref.dealias1(keep)
10601060
case _ => this
10611061
}
10621062

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class MixinOps(cls: ClassSymbol, thisPhase: DenotTransformer)(implicit ctx: Cont
2424
name = member.name.stripScala2LocalSuffix,
2525
flags = member.flags &~ Deferred,
2626
info = cls.thisType.memberInfo(member)).enteredAfter(thisPhase).asTerm
27-
res.addAnnotations(member.annotations)
27+
res.addAnnotations(member.annotations.filter(_.symbol != defn.TailrecAnnot))
2828
res
2929
}
3030

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class PatternMatcher extends MiniPhase {
2727

2828
override def phaseName = PatternMatcher.name
2929
override def runsAfter = Set(ElimRepeated.name)
30-
override def runsAfterGroupsOf = Set(TailRec.name) // tailrec is not capable of reversing the patmat tranformation made for tree
3130

3231
override def transformMatch(tree: Match)(implicit ctx: Context): Tree = {
3332
val translated = new Translator(tree.tpe, this).translateMatch(tree)

0 commit comments

Comments
 (0)