Skip to content

Commit 2cd66da

Browse files
committed
Add Serialize to companions of serializable classes in stdlib-bootsrapped
1 parent bbb70cc commit 2cd66da

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

project/MiMaFilters.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ object MiMaFilters {
4242

4343
// Companion module class
4444
ProblemFilters.exclude[FinalClassProblem]("scala.*$"),
45-
ProblemFilters.exclude[MissingTypesProblem]("scala.*$"),
45+
46+
// Missing types {scala.runtime.AbstractFunction1}
47+
ProblemFilters.exclude[MissingTypesProblem]("scala.ScalaReflectionException$"),
48+
ProblemFilters.exclude[MissingTypesProblem]("scala.UninitializedFieldError$"),
49+
ProblemFilters.exclude[MissingTypesProblem]("scala.collection.StringView$"),
4650

4751
// Tuples
4852
ProblemFilters.exclude[FinalClassProblem]("scala.Tuple1"),

project/TastyMiMaFilters.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ object TastyMiMaFilters {
99
// Probably OK: Case class with varargs
1010
ProblemMatcher.make(ProblemKind.IncompatibleTypeChange, "scala.StringContext.parts"), // before: scala.<repeated>[Predef.String]; after: scala.collection.immutable.Seq[Predef.String] @scala.annotation.internal.Repeated
1111

12-
// Problem: Missing Serializable in companions of serializable classes
13-
ProblemMatcher.make(ProblemKind.MissingParent, "scala.*$"),
12+
// Problem: Missing type {scala.runtime.AbstractFunction1}
13+
ProblemMatcher.make(ProblemKind.MissingParent, "scala.collection.Searching.Found$"),
14+
ProblemMatcher.make(ProblemKind.MissingParent, "scala.collection.Searching.InsertionPoint$"),
15+
ProblemMatcher.make(ProblemKind.MissingParent, "scala.collection.StringView$"),
16+
ProblemMatcher.make(ProblemKind.MissingParent, "scala.jdk.FunctionWrappers.AsJava*$"),
17+
ProblemMatcher.make(ProblemKind.MissingParent, "scala.jdk.FunctionWrappers.FromJava*$"),
18+
ProblemMatcher.make(ProblemKind.MissingParent, "scala.ScalaReflectionException$"),
19+
ProblemMatcher.make(ProblemKind.MissingParent, "scala.UninitializedFieldError$"),
1420

1521
// Problem: Class[T] or ClassTag[T] with `T` equal to wildcard `_ >: Nothing <: AnyVal` instead of a specific primitive type `T`
1622
ProblemMatcher.make(ProblemKind.IncompatibleTypeChange, "scala.reflect.ManifestFactory.*.runtimeClass"),

0 commit comments

Comments
 (0)