@@ -76,13 +76,29 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
7676
7777 override def changesMembers : Boolean = true // the phase adds super accessors and synthetic members
7878
79+ /**
80+ * Serializable and AbstractFunction1 are added for companion objects of case classes in scala2-library
81+ */
82+ override def changesParents : Boolean =
83+ if ! initContextCalled then
84+ throw new Exception (" Calling changesParents before initContext, should call initContext first" )
85+ compilingScala2StdLib
86+
7987 override def transformPhase (using Context ): Phase = thisPhase.next
8088
8189 def newTransformer (using Context ): Transformer =
8290 new PostTyperTransformer
8391
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+
8499 private var compilingScala2StdLib = false
85100 override def initContext (ctx : FreshContext ): Unit =
101+ initContextCalled = true
86102 compilingScala2StdLib = ctx.settings.YcompileScala2Library .value(using ctx)
87103
88104 val superAcc : SuperAccessors = new SuperAccessors (thisPhase)
@@ -576,7 +592,9 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
576592 if ! sym.hasAnnotation(defn.ExperimentalAnnot ) && ctx.settings.experimental.value && isTopLevelDefinitionInSource(sym) then
577593 sym.addAnnotation(ExperimentalAnnotation (" Added by -experimental" , sym.span))
578594
579- private def scala2LibPatch (tree : TypeDef )(using Context ) =
595+ // It needs to run at the phase of the postTyper --- otherwise, the test of the symbols will use
596+ // the transformed denotation with added `Serializable` and `AbstractFunction1`.
597+ private def scala2LibPatch (tree : TypeDef )(using Context ) = atPhase(thisPhase):
580598 val sym = tree.symbol
581599 if compilingScala2StdLib && sym.is(ModuleClass ) then
582600 // Add Serializable to companion objects of serializable classes,
0 commit comments