@@ -76,13 +76,29 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
76
76
77
77
override def changesMembers : Boolean = true // the phase adds super accessors and synthetic members
78
78
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
+
79
87
override def transformPhase (using Context ): Phase = thisPhase.next
80
88
81
89
def newTransformer (using Context ): Transformer =
82
90
new PostTyperTransformer
83
91
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
+
84
99
private var compilingScala2StdLib = false
85
100
override def initContext (ctx : FreshContext ): Unit =
101
+ initContextCalled = true
86
102
compilingScala2StdLib = ctx.settings.YcompileScala2Library .value(using ctx)
87
103
88
104
val superAcc : SuperAccessors = new SuperAccessors (thisPhase)
@@ -576,7 +592,9 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
576
592
if ! sym.hasAnnotation(defn.ExperimentalAnnot ) && ctx.settings.experimental.value && isTopLevelDefinitionInSource(sym) then
577
593
sym.addAnnotation(ExperimentalAnnotation (" Added by -experimental" , sym.span))
578
594
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):
580
598
val sym = tree.symbol
581
599
if compilingScala2StdLib && sym.is(ModuleClass ) then
582
600
// Add Serializable to companion objects of serializable classes,
0 commit comments