@@ -60,7 +60,7 @@ object PostTyper {
60
60
* mini-phase or subfunction of a macro phase equally well. But taken by themselves
61
61
* they do not warrant their own group of miniphases before pickling.
62
62
*/
63
- class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase =>
63
+ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
64
64
import tpd ._
65
65
66
66
override def phaseName : String = PostTyper .name
@@ -80,6 +80,10 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
80
80
def newTransformer (using Context ): Transformer =
81
81
new PostTyperTransformer
82
82
83
+ private var compilingScala2StdLib = false
84
+ override def initContext (ctx : FreshContext ): Unit =
85
+ compilingScala2StdLib = ctx.settings.Yscala2Stdlib .value(using ctx)
86
+
83
87
val superAcc : SuperAccessors = new SuperAccessors (thisPhase)
84
88
val synthMbr : SyntheticMembers = new SyntheticMembers (thisPhase)
85
89
val beanProps : BeanProperties = new BeanProperties (thisPhase)
@@ -425,7 +429,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
425
429
if sym.isOpaqueAlias then
426
430
VarianceChecker .checkLambda(rhs, TypeBounds .upper(sym.opaqueAlias))
427
431
case _ =>
428
- processMemberDef(super .transform(tree))
432
+ processMemberDef(super .transform(scala2LibPatch( tree) ))
429
433
case tree : Bind =>
430
434
if tree.symbol.isType && ! tree.symbol.name.is(WildcardParamName ) then
431
435
Checking .checkGoodBounds(tree.symbol)
@@ -543,5 +547,30 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
543
547
sym.addAnnotation(Annotation (defn.ExperimentalAnnot , sym.span))
544
548
sym.companionModule.addAnnotation(Annotation (defn.ExperimentalAnnot , sym.span))
545
549
550
+ private def scala2LibPatch (tree : TypeDef )(using Context ) =
551
+ val sym = tree.symbol
552
+ if compilingScala2StdLib
553
+ && sym.is(ModuleClass ) && ! sym.derivesFrom(defn.SerializableClass )
554
+ && sym.companionClass.derivesFrom(defn.SerializableClass )
555
+ then
556
+ // Add Serializable to companion objects of serializable classes
557
+ tree.rhs match
558
+ case impl : Template =>
559
+ val parents1 = impl.parents :+ TypeTree (defn.SerializableType )
560
+ val impl1 = cpy.Template (impl)(parents = parents1)
561
+ cpy.TypeDef (tree)(rhs = impl1)
562
+ else tree
546
563
}
564
+
565
+ protected override def infoMayChange (sym : Symbol )(using Context ): Boolean =
566
+ compilingScala2StdLib && sym.isAllOf(ModuleClass , butNot = Package )
567
+
568
+ def transformInfo (tp : Type , sym : Symbol )(using Context ): Type = tp match
569
+ case info : ClassInfo =>
570
+ if ! sym.derivesFrom(defn.SerializableClass )
571
+ && sym.companionClass.derivesFrom(defn.SerializableClass )
572
+ then
573
+ info.derivedClassInfo(declaredParents = info.parents :+ defn.SerializableType )
574
+ else tp
575
+ case _ => tp
547
576
}
0 commit comments