Skip to content

Commit 9f41889

Browse files
committed
Only set PostTyper.changesParents to true for scala2-lib
1 parent ae1a9e5 commit 9f41889

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
@@ -293,10 +293,13 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
293293
if (ctx.settings.YtestPickler.value) List("pickler")
294294
else ctx.settings.YstopAfter.value
295295

296+
val runCtx = ctx.fresh
297+
runCtx.setProfiler(Profiler())
298+
296299
val pluginPlan = ctx.base.addPluginPhases(ctx.base.phasePlan)
297300
val phases = ctx.base.fusePhases(pluginPlan,
298301
ctx.settings.Yskip.value, ctx.settings.YstopBefore.value, stopAfter, ctx.settings.Ycheck.value)
299-
ctx.base.usePhases(phases)
302+
ctx.base.usePhases(phases, runCtx)
300303

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

343-
val runCtx = ctx.fresh
344-
runCtx.setProfiler(Profiler())
345-
unfusedPhases.foreach(_.initContext(runCtx))
346346
val fusedPhases = runCtx.base.allPhases
347347
if ctx.settings.explainCyclic.value then
348348
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
@@ -892,7 +892,7 @@ object Contexts {
892892
val definitions: Definitions = new Definitions
893893

894894
// Set up some phases to get started */
895-
usePhases(List(SomePhase))
895+
usePhases(List(SomePhase), FreshContext(this))
896896

897897
/** Initializes the `ContextBase` with a starting context.
898898
* 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
@@ -77,19 +77,28 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
7777
override def changesMembers: Boolean = true // the phase adds super accessors and synthetic members
7878

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

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

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

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

95104
val superAcc: SuperAccessors = new SuperAccessors(thisPhase)
@@ -584,7 +593,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
584593
sym.addAnnotation(ExperimentalAnnotation("Added by -experimental", sym.span))
585594

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

0 commit comments

Comments
 (0)