Skip to content

Commit

Permalink
Only set PostTyper.changesParents to true for scala2-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
liufengyun committed Apr 2, 2024
1 parent 07f6949 commit 1bfb0f1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/Run.scala
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,13 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
if (ctx.settings.YtestPickler.value) List("pickler")
else ctx.settings.YstopAfter.value

val runCtx = ctx.fresh
runCtx.setProfiler(Profiler())

val pluginPlan = ctx.base.addPluginPhases(ctx.base.phasePlan)
val phases = ctx.base.fusePhases(pluginPlan,
ctx.settings.Yskip.value, ctx.settings.YstopBefore.value, stopAfter, ctx.settings.Ycheck.value)
ctx.base.usePhases(phases)
ctx.base.usePhases(phases, runCtx)

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

val runCtx = ctx.fresh
runCtx.setProfiler(Profiler())
unfusedPhases.foreach(_.initContext(runCtx))
val fusedPhases = runCtx.base.allPhases
if ctx.settings.explainCyclic.value then
runCtx.setProperty(CyclicReference.Trace, new CyclicReference.Trace())
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ object Contexts {
val definitions: Definitions = new Definitions

// Set up some phases to get started */
usePhases(List(SomePhase))
usePhases(List(SomePhase), FreshContext(this))

/** Initializes the `ContextBase` with a starting context.
* This initializes the `platform` and the `definitions`.
Expand Down
16 changes: 13 additions & 3 deletions compiler/src/dotty/tools/dotc/core/Phases.scala
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ object Phases {
* The list should never contain NoPhase.
* if fusion is enabled, phases in same subgroup will be fused to single phase.
*/
final def usePhases(phasess: List[Phase], fuse: Boolean = true): Unit = {
final def usePhases(phasess: List[Phase], runCtx: FreshContext, fuse: Boolean = true): Unit = {

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

Expand Down Expand Up @@ -161,11 +161,21 @@ object Phases {
phase match {
case p: MegaPhase =>
val miniPhases = p.miniPhases
miniPhases.foreach{ phase =>
for phase <- miniPhases do
checkRequirements(phase)
phase.init(this, nextPhaseId)}
// Given phases a chance to initialize state based on the run context.
//
// `phase.initContext` should be called before `phase.init` as the later calls abstract methods
// `changesMembers` and `changeParents` which may depend on the run context.
//
// See `PostTyper.changeParents`
phase.initContext(runCtx)
phase.init(this, nextPhaseId)
end for
p.init(this, miniPhases.head.id, miniPhases.last.id)
case _ =>
// See comment above about the ordering of the two calls.
phase.initContext(runCtx)
phase.init(this, nextPhaseId)
checkRequirements(phase)
}
Expand Down
19 changes: 14 additions & 5 deletions compiler/src/dotty/tools/dotc/transform/PostTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,28 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
override def changesMembers: Boolean = true // the phase adds super accessors and synthetic members

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

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

def newTransformer(using Context): Transformer =
new PostTyperTransformer

/**
* Used to check that `changesParents` is called after `initContext`.
*
* This contract is easy to break and results in subtle bugs.
*/
private var initContextCalled = false

private var compilingScala2StdLib = false
override def initContext(ctx: FreshContext): Unit =
initContextCalled = true
compilingScala2StdLib = ctx.settings.YcompileScala2Library.value(using ctx)

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

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

0 comments on commit 1bfb0f1

Please sign in to comment.