Skip to content

Commit 1bfb0f1

Browse files
committed
Only set PostTyper.changesParents to true for scala2-lib
1 parent 07f6949 commit 1bfb0f1

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,13 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
292292
if (ctx.settings.YtestPickler.value) List("pickler")
293293
else ctx.settings.YstopAfter.value
294294

295+
val runCtx = ctx.fresh
296+
runCtx.setProfiler(Profiler())
297+
295298
val pluginPlan = ctx.base.addPluginPhases(ctx.base.phasePlan)
296299
val phases = ctx.base.fusePhases(pluginPlan,
297300
ctx.settings.Yskip.value, ctx.settings.YstopBefore.value, stopAfter, ctx.settings.Ycheck.value)
298-
ctx.base.usePhases(phases)
301+
ctx.base.usePhases(phases, runCtx)
299302

300303
if ctx.settings.YnoDoubleBindings.value then
301304
ctx.base.checkNoDoubleBindings = true
@@ -339,9 +342,6 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
339342
profiler.finished()
340343
}
341344

342-
val runCtx = ctx.fresh
343-
runCtx.setProfiler(Profiler())
344-
unfusedPhases.foreach(_.initContext(runCtx))
345345
val fusedPhases = runCtx.base.allPhases
346346
if ctx.settings.explainCyclic.value then
347347
runCtx.setProperty(CyclicReference.Trace, new CyclicReference.Trace())

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ object Contexts {
889889
val definitions: Definitions = new Definitions
890890

891891
// Set up some phases to get started */
892-
usePhases(List(SomePhase))
892+
usePhases(List(SomePhase), FreshContext(this))
893893

894894
/** Initializes the `ContextBase` with a starting context.
895895
* This initializes the `platform` and the `definitions`.

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ object Phases {
126126
* The list should never contain NoPhase.
127127
* if fusion is enabled, phases in same subgroup will be fused to single phase.
128128
*/
129-
final def usePhases(phasess: List[Phase], fuse: Boolean = true): Unit = {
129+
final def usePhases(phasess: List[Phase], runCtx: FreshContext, fuse: Boolean = true): Unit = {
130130

131131
val flatPhases = collection.mutable.ListBuffer[Phase]()
132132

@@ -161,11 +161,21 @@ object Phases {
161161
phase match {
162162
case p: MegaPhase =>
163163
val miniPhases = p.miniPhases
164-
miniPhases.foreach{ phase =>
164+
for phase <- miniPhases do
165165
checkRequirements(phase)
166-
phase.init(this, nextPhaseId)}
166+
// Given phases a chance to initialize state based on the run context.
167+
//
168+
// `phase.initContext` should be called before `phase.init` as the later calls abstract methods
169+
// `changesMembers` and `changeParents` which may depend on the run context.
170+
//
171+
// See `PostTyper.changeParents`
172+
phase.initContext(runCtx)
173+
phase.init(this, nextPhaseId)
174+
end for
167175
p.init(this, miniPhases.head.id, miniPhases.last.id)
168176
case _ =>
177+
// See comment above about the ordering of the two calls.
178+
phase.initContext(runCtx)
169179
phase.init(this, nextPhaseId)
170180
checkRequirements(phase)
171181
}

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,28 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
7676
override def changesMembers: Boolean = true // the phase adds super accessors and synthetic members
7777

7878
/**
79-
* Serializable and AbstractFunction are added for scala2-library companion object of case class
80-
*
81-
* Ideally `compilingScala2StdLib` should be used, but it is initialized too late to be effective.
79+
* Serializable and AbstractFunction1 are added for companion objects of case classes in scala2-library
8280
*/
83-
override def changesParents: Boolean = true
81+
override def changesParents: Boolean =
82+
if !initContextCalled then
83+
throw new Exception("Calling changesParents before initContext, should call initContext first")
84+
compilingScala2StdLib
8485

8586
override def transformPhase(using Context): Phase = thisPhase.next
8687

8788
def newTransformer(using Context): Transformer =
8889
new PostTyperTransformer
8990

91+
/**
92+
* Used to check that `changesParents` is called after `initContext`.
93+
*
94+
* This contract is easy to break and results in subtle bugs.
95+
*/
96+
private var initContextCalled = false
97+
9098
private var compilingScala2StdLib = false
9199
override def initContext(ctx: FreshContext): Unit =
100+
initContextCalled = true
92101
compilingScala2StdLib = ctx.settings.YcompileScala2Library.value(using ctx)
93102

94103
val superAcc: SuperAccessors = new SuperAccessors(thisPhase)
@@ -562,7 +571,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
562571
sym.addAnnotation(Annotation(defn.ExperimentalAnnot, sym.span))
563572

564573
// It needs to run at the phase of the postTyper --- otherwise, the test of the symbols will use
565-
// the transformed denotation with added `Serializable` and `AbstractFunction`.
574+
// the transformed denotation with added `Serializable` and `AbstractFunction1`.
566575
private def scala2LibPatch(tree: TypeDef)(using Context) = atPhase(thisPhase):
567576
val sym = tree.symbol
568577
if compilingScala2StdLib && sym.is(ModuleClass) then

0 commit comments

Comments
 (0)