@@ -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
@@ -425,7 +425,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
425425 if sym.isOpaqueAlias then
426426 VarianceChecker .checkLambda(rhs, TypeBounds .upper(sym.opaqueAlias))
427427 case _ =>
428- processMemberDef(super .transform(tree))
428+ processMemberDef(super .transform(scala2LibPatch( tree) ))
429429 case tree : Bind =>
430430 if tree.symbol.isType && ! tree.symbol.name.is(WildcardParamName ) then
431431 Checking .checkGoodBounds(tree.symbol)
@@ -534,5 +534,30 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
534534 sym.addAnnotation(Annotation (defn.ExperimentalAnnot , sym.span))
535535 sym.companionModule.addAnnotation(Annotation (defn.ExperimentalAnnot , sym.span))
536536
537+ private def scala2LibPatch (tree : TypeDef )(using Context ) =
538+ val sym = tree.symbol
539+ if ctx.settings.Yscala2Stdlib .value
540+ && sym.is(ModuleClass ) && ! sym.derivesFrom(defn.SerializableClass )
541+ && sym.companionClass.derivesFrom(defn.SerializableClass )
542+ then
543+ // Add Serializable to companion objects of serializable classes
544+ tree.rhs match
545+ case impl : Template =>
546+ val parents1 = impl.parents :+ TypeTree (defn.SerializableType )
547+ val impl1 = cpy.Template (impl)(parents = parents1)
548+ cpy.TypeDef (tree)(rhs = impl1)
549+ else tree
537550 }
551+
552+ protected override def infoMayChange (sym : Symbol )(using Context ): Boolean =
553+ ctx.settings.Yscala2Stdlib .value && sym.isAllOf(ModuleClass , butNot = Package )
554+
555+ def transformInfo (tp : Type , sym : Symbol )(using Context ): Type = tp match
556+ case info : ClassInfo =>
557+ if ! sym.derivesFrom(defn.SerializableClass )
558+ && sym.companionClass.derivesFrom(defn.SerializableClass )
559+ then
560+ info.derivedClassInfo(declaredParents = info.parents :+ defn.SerializableType )
561+ else tp
562+ case _ => tp
538563}
0 commit comments