@@ -60,7 +60,7 @@ object PostTyper {
6060 * mini-phase or subfunction of a macro phase equally well. But taken by themselves
6161 * they do not warrant their own group of miniphases before pickling.
6262 */
63- class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase =>
63+ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
6464 import tpd ._
6565
6666 override def phaseName : String = PostTyper .name
@@ -80,6 +80,10 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
8080 def newTransformer (using Context ): Transformer =
8181 new PostTyperTransformer
8282
83+ private var compilingScala2StdLib = false
84+ override def initContext (ctx : FreshContext ): Unit =
85+ compilingScala2StdLib = ctx.settings.Yscala2Stdlib .value(using ctx)
86+
8387 val superAcc : SuperAccessors = new SuperAccessors (thisPhase)
8488 val synthMbr : SyntheticMembers = new SyntheticMembers (thisPhase)
8589 val beanProps : BeanProperties = new BeanProperties (thisPhase)
@@ -425,7 +429,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
425429 if sym.isOpaqueAlias then
426430 VarianceChecker .checkLambda(rhs, TypeBounds .upper(sym.opaqueAlias))
427431 case _ =>
428- processMemberDef(super .transform(tree))
432+ processMemberDef(super .transform(scala2LibPatch( tree) ))
429433 case tree : Bind =>
430434 if tree.symbol.isType && ! tree.symbol.name.is(WildcardParamName ) then
431435 Checking .checkGoodBounds(tree.symbol)
@@ -543,5 +547,30 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
543547 sym.addAnnotation(Annotation (defn.ExperimentalAnnot , sym.span))
544548 sym.companionModule.addAnnotation(Annotation (defn.ExperimentalAnnot , sym.span))
545549
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
546563 }
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
547576}
0 commit comments