Skip to content

Commit 1ee4d1e

Browse files
authored
Merge pull request #3350 from dotty-staging/change-transform-phases-3
Run all miniphase transforms at group end
2 parents 3c1bc32 + fdf17d0 commit 1ee4d1e

18 files changed

+169
-180
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ class Compiler {
5353
new ElimJavaPackages), // Eliminate syntactic references to Java packages
5454
List(new CheckStatic, // Check restrictions that apply to @static members
5555
new ElimRepeated, // Rewrite vararg parameters and arguments
56-
new RefChecks, // Various checks mostly related to abstract members and overriding
5756
new NormalizeFlags, // Rewrite some definition flags
5857
new ExtensionMethods, // Expand methods of value classes with extension methods
5958
new ExpandSAMs, // Expand single abstract method closures to anonymous classes
6059
new TailRec, // Rewrite tail recursion to loops
6160
new ByNameClosures, // Expand arguments to by-name parameters to closures
6261
new LiftTry, // Put try expressions that might execute on non-empty stacks into their own methods
6362
new HoistSuperArgs, // Hoist complex arguments of supercalls to enclosing scope
64-
new ClassOf), // Expand `Predef.classOf` calls.
63+
new ClassOf, // Expand `Predef.classOf` calls.
64+
new RefChecks), // Various checks mostly related to abstract members and overriding
6565
List(new TryCatchPatterns, // Compile cases in try/catch
6666
new PatternMatcher, // Compile pattern matches
6767
new ExplicitOuter, // Add accessors to outer classes from nested ones.
@@ -98,10 +98,10 @@ class Compiler {
9898
List(new LinkScala2Impls, // Redirect calls to trait methods defined by Scala 2.x, so that they now go to their implementations
9999
new LambdaLift, // Lifts out nested functions to class scope, storing free variables in environments
100100
// Note: in this mini-phase block scopes are incorrect. No phases that rely on scopes should be here
101-
new ElimStaticThis, // Replace `this` references to static objects by global identifiers
102-
new Flatten, // Lift all inner classes to package scope
103-
new RestoreScopes), // Repair scopes rendered invalid by moving definitions in prior phases of the group
104-
List(new RenameLifted, // Renames lifted classes to local numbering scheme
101+
new ElimStaticThis), // Replace `this` references to static objects by global identifiers
102+
List(new Flatten, // Lift all inner classes to package scope
103+
new RestoreScopes, // Repair scopes rendered invalid by moving definitions in prior phases of the group
104+
new RenameLifted, // Renames lifted classes to local numbering scheme
105105
new TransformWildcards, // Replace wildcards with default values
106106
new MoveStatics, // Move static methods to companion classes
107107
new ExpandPrivate, // Widen private definitions accessed from nested classes

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -656,26 +656,27 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
656656
/** After phase `trans`, set the owner of every definition in this tree that was formerly
657657
* owner by `from` to `to`.
658658
*/
659-
def changeOwnerAfter(from: Symbol, to: Symbol, trans: DenotTransformer)(implicit ctx: Context): ThisTree = {
660-
assert(ctx.phase == trans.next)
661-
val traverser = new TreeTraverser {
662-
def traverse(tree: Tree)(implicit ctx: Context) = tree match {
663-
case tree: DefTree =>
664-
val sym = tree.symbol
665-
val prevDenot = sym.denot(ctx.withPhase(trans))
666-
if (prevDenot.effectiveOwner == from.skipWeakOwner) {
667-
val d = sym.copySymDenotation(owner = to)
668-
d.installAfter(trans)
669-
d.transformAfter(trans, d => if (d.owner eq from) d.copySymDenotation(owner = to) else d)
670-
}
671-
if (sym.isWeakOwner) traverseChildren(tree)
672-
case _ =>
673-
traverseChildren(tree)
659+
def changeOwnerAfter(from: Symbol, to: Symbol, trans: DenotTransformer)(implicit ctx: Context): ThisTree =
660+
if (ctx.phase == trans.next) {
661+
val traverser = new TreeTraverser {
662+
def traverse(tree: Tree)(implicit ctx: Context) = tree match {
663+
case tree: DefTree =>
664+
val sym = tree.symbol
665+
val prevDenot = sym.denot(ctx.withPhase(trans))
666+
if (prevDenot.effectiveOwner == from.skipWeakOwner) {
667+
val d = sym.copySymDenotation(owner = to)
668+
d.installAfter(trans)
669+
d.transformAfter(trans, d => if (d.owner eq from) d.copySymDenotation(owner = to) else d)
670+
}
671+
if (sym.isWeakOwner) traverseChildren(tree)
672+
case _ =>
673+
traverseChildren(tree)
674+
}
674675
}
676+
traverser.traverse(tree)
677+
tree
675678
}
676-
traverser.traverse(tree)
677-
tree
678-
}
679+
else changeOwnerAfter(from, to, trans)(ctx.withPhase(trans.next))
679680

680681
/** A select node with the given selector name and a computed type */
681682
def select(name: Name)(implicit ctx: Context): Select =

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,6 @@ object Phases {
298298
*/
299299
def checkPostCondition(tree: tpd.Tree)(implicit ctx: Context): Unit = ()
300300

301-
/** If set, allow missing or superfluous arguments in applications
302-
* and type applications.
303-
*/
304-
def relaxedTyping: Boolean = false
305-
306301
/** Is this phase the standard typerphase? True for FrontEnd, but
307302
* not for other first phases (such as FromTasty). The predicate
308303
* is tested in some places that perform checks and corrections. It's

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class CapturedVars extends MiniPhase with IdentityDenotTransformer { thisTransfo
9191
}
9292

9393
override def prepareForValDef(vdef: ValDef)(implicit ctx: Context) = {
94-
val sym = vdef.symbol
94+
val sym = vdef.symbol(ctx.withPhase(thisTransform))
9595
if (captured contains sym) {
9696
val newd = sym.denot(ctx.withPhase(thisTransform)).copySymDenotation(
9797
info = refClass(sym.info.classSymbol, sym.hasAnnotation(defn.VolatileAnnot)).typeRef,
@@ -119,7 +119,7 @@ class CapturedVars extends MiniPhase with IdentityDenotTransformer { thisTransfo
119119
override def transformIdent(id: Ident)(implicit ctx: Context, info: TransformerInfo): Tree = {
120120
val vble = id.symbol
121121
if (captured(vble))
122-
(id select nme.elem).ensureConforms(vble.denot(ctx.withPhase(thisTransform)).info)
122+
id.select(nme.elem).ensureConforms(vble.denot(ctx.withPhase(thisTransform)).info)
123123
else id
124124
}
125125

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class ElimByName extends TransformByNameApply with InfoTransformer { thisTransfo
4747

4848
/** Map `tree` to `tree.apply()` is `ftree` was of ExprType and becomes now a function */
4949
private def applyIfFunction(tree: Tree, ftree: Tree)(implicit ctx: Context) =
50-
if (isByNameRef(ftree)) tree.select(defn.Function0_apply).appliedToNone
50+
if (isByNameRef(ftree))
51+
ctx.atPhase(next) { implicit ctx => tree.select(defn.Function0_apply).appliedToNone }
5152
else tree
5253

5354
override def transformIdent(tree: Ident)(implicit ctx: Context, info: TransformerInfo): Tree =
@@ -65,9 +66,11 @@ class ElimByName extends TransformByNameApply with InfoTransformer { thisTransfo
6566
}
6667

6768
override def transformValDef(tree: ValDef)(implicit ctx: Context, info: TransformerInfo): Tree =
68-
if (exprBecomesFunction(tree.symbol))
69-
cpy.ValDef(tree)(tpt = tree.tpt.withType(tree.symbol.info))
70-
else tree
69+
ctx.atPhase(next) { implicit ctx =>
70+
if (exprBecomesFunction(tree.symbol))
71+
cpy.ValDef(tree)(tpt = tree.tpt.withType(tree.symbol.info))
72+
else tree
73+
}
7174

7275
def transformInfo(tp: Type, sym: Symbol)(implicit ctx: Context): Type = tp match {
7376
case ExprType(rt) if exprBecomesFunction(sym) => defn.FunctionOf(Nil, rt)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import dotty.tools.dotc.core.Phases.Phase
2525
/** Make private term members that are accessed from another class
2626
* non-private by resetting the Private flag and expanding their name.
2727
*
28-
* Make private accessor in value class not-private. Ihis is necessary to unbox
28+
* Make private accessor in value class not-private. This is necessary to unbox
2929
* the value class when accessing it from separate compilation units
3030
*
3131
* Also, make non-private any private parameter forwarders that forward to an inherited

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,11 @@ object ExplicitOuter {
132132
import ast.tpd._
133133

134134
/** Ensure that class `cls` has outer accessors */
135-
def ensureOuterAccessors(cls: ClassSymbol)(implicit ctx: Context): Unit = {
136-
//todo: implementing #165 would simplify this logic
137-
val prevPhase = ctx.phase.prev
138-
assert(prevPhase.id <= ctx.explicitOuterPhase.id, "can add $outer symbols only before ExplicitOuter")
139-
assert(prevPhase.isInstanceOf[DenotTransformer], "adding outerAccessors requires being DenotTransformer")
140-
if (!hasOuter(cls)) {
141-
newOuterAccessors(cls).foreach(_.enteredAfter(prevPhase.asInstanceOf[DenotTransformer]))
135+
def ensureOuterAccessors(cls: ClassSymbol)(implicit ctx: Context): Unit =
136+
ctx.atPhase(ctx.explicitOuterPhase.next) { implicit ctx =>
137+
if (!hasOuter(cls))
138+
newOuterAccessors(cls).foreach(_.enteredAfter(ctx.explicitOuterPhase.asInstanceOf[DenotTransformer]))
142139
}
143-
}
144140

145141
/** The outer accessor and potentially outer param accessor needed for class `cls` */
146142
private def newOuterAccessors(cls: ClassSymbol)(implicit ctx: Context) =

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class ExtensionMethods extends MiniPhaseTransform with DenotTransformer with Ful
131131
private def createExtensionMethod(imeth: Symbol, staticClass: Symbol)(implicit ctx: Context): TermSymbol = {
132132
val extensionName = extensionNames(imeth).head.toTermName
133133
val extensionMeth = ctx.newSymbol(staticClass, extensionName,
134-
imeth.flags | Final &~ (Override | Protected | AbsOverride),
134+
(imeth.flags | Final) &~ (Override | Protected | AbsOverride),
135135
fullyParameterizedType(imeth.info, imeth.owner.asClass),
136136
privateWithin = imeth.privateWithin, coord = imeth.coord)
137137
extensionMeth.addAnnotations(imeth.annotations)(ctx.withPhase(thisTransformer))
@@ -175,8 +175,8 @@ class ExtensionMethods extends MiniPhaseTransform with DenotTransformer with Ful
175175
extensionDefs(staticClass) = newC
176176
newC
177177
}
178-
store += atGroupEnd(fullyParameterizedDef(extensionMeth, tree)(_))
179-
cpy.DefDef(tree)(rhs = atGroupEnd(forwarder(extensionMeth, tree)(_)))
178+
store += fullyParameterizedDef(extensionMeth, tree)
179+
cpy.DefDef(tree)(rhs = forwarder(extensionMeth, tree))
180180
} else tree
181181
}
182182
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ class LambdaLift extends MiniPhase with IdentityDenotTransformer { thisTransform
6464
val phaseName: String = "lambdaLift"
6565
val treeTransform = new LambdaLifter
6666

67-
override def relaxedTyping = true
68-
6967
override def runsAfterGroupsOf: Set[Class[_ <: Phase]] = Set(classOf[Constructors], classOf[HoistSuperArgs])
7068
// Constructors has to happen before LambdaLift because the lambda lift logic
7169
// becomes simpler if it can assume that parameter accessors have already been

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,10 @@ class LazyVals extends MiniPhaseTransform with IdentityDenotTransformer {
166166
// backend requires field usage to be after field definition
167167
// need to bring containers to start of method
168168
val (holders, stats) =
169-
atGroupEnd { implicit ctx: Context =>
170-
trees.partition {
171-
_.symbol.flags.&~(Flags.Touched) == containerFlags
172-
// Filtering out Flags.Touched is not required currently, as there are no LazyTypes involved here
173-
// but just to be more safe
174-
}
169+
trees.partition {
170+
_.symbol.flags.&~(Flags.Touched) == containerFlags
171+
// Filtering out Flags.Touched is not required currently, as there are no LazyTypes involved here
172+
// but just to be more safe
175173
}
176174
holders:::stats
177175
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import DenotTransformers.SymTransformer
66
import Phases.Phase
77
import Contexts.Context
88
import SymDenotations.SymDenotation
9-
import TreeTransforms.MiniPhaseTransform
9+
import TreeTransforms.{MiniPhaseTransform, TransformerInfo}
1010
import Flags._, Symbols._
1111

1212
/** 1. Widens all private[this] and protected[this] qualifiers to just private/protected

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ object PatternMatcher {
768768
def outerTest: Tree = trans.transformFollowingDeep {
769769
val expectedOuter = singleton(expectedTp.normalizedPrefix)
770770
val expectedClass = expectedTp.dealias.classSymbol.asClass
771-
ExplicitOuter.ensureOuterAccessors(expectedClass)(ctx.withPhase(ctx.explicitOuterPhase.next))
771+
ExplicitOuter.ensureOuterAccessors(expectedClass)
772772
scrutinee.ensureConforms(expectedTp)
773773
.outerSelect(1, expectedOuter.tpe.widen)
774774
.select(defn.Object_eq)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import dotty.tools.dotc.core.Names._
99
import dotty.tools.dotc.core.Phases
1010
import dotty.tools.dotc.core.SymDenotations.SymDenotation
1111
import dotty.tools.dotc.core.Symbols._
12-
import dotty.tools.dotc.transform.TreeTransforms.MiniPhaseTransform
12+
import dotty.tools.dotc.transform.TreeTransforms.{MiniPhaseTransform, TransformerInfo}
1313

1414
/** Renames lifted classes to local numbering scheme */
1515
class RenameLifted extends MiniPhaseTransform with SymTransformer { thisTransformer =>
1616

1717
override def phaseName = "renameLifted"
1818

19-
override def runsAfterGroupsOf: Set[Class[_ <: Phases.Phase]] = Set(classOf[RestoreScopes])
19+
// Not clear why this should run after restoreScopes
20+
// override def runsAfterGroupsOf: Set[Class[_ <: Phases.Phase]] = Set(classOf[RestoreScopes])
2021

2122
def transformSym(ref: SymDenotation)(implicit ctx: Context): SymDenotation =
2223
if (needsRefresh(ref.symbol)) ref.copySymDenotation(name = refreshedName(ref.symbol))

0 commit comments

Comments
 (0)