Skip to content

Commit de9ca64

Browse files
smarterSimon Ochsenreither
and
Simon Ochsenreither
committed
Replace Cloneable/Serializable traits with type aliases
Fixes scala/bug#9080. This commit is adapted from scala#5520 and is thus: Co-authored-by: Simon Ochsenreither <[email protected]>
1 parent 2b49d71 commit de9ca64

19 files changed

+68
-70
lines changed

src/library/scala/package.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import scala.annotation.migration
1414
* @contentDiagram hideNodes "scala.Serializable"
1515
*/
1616
package object scala {
17+
type Cloneable = java.lang.Cloneable
18+
type Serializable = java.io.Serializable
19+
1720
type Throwable = java.lang.Throwable
1821
type Exception = java.lang.Exception
1922
type Error = java.lang.Error

src/library/scala/runtime/LambdaDeserializer.scala

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ object LambdaDeserializer {
1919
* concurrent deserialization of the same lambda expression may spin up more than one class.
2020
*
2121
* Assumptions:
22-
* - No additional marker interfaces are required beyond `{java.io,scala.}Serializable`. These are
22+
* - No additional marker interfaces are required beyond `java.io.Serializable`. These are
2323
* not stored in `SerializedLambda`, so we can't reconstitute them.
2424
* - No additional bridge methods are passed to `altMetafactory`. Again, these are not stored.
2525
*
2626
* @param lookup The factory for method handles. Must have access to the implementation method, the
27-
* functional interface class, and `java.io.Serializable` or `scala.Serializable` as
28-
* required.
27+
* functional interface class, and `java.io.Serializable`.
2928
* @param cache A cache used to avoid spinning up a class for each deserialization of a given lambda. May be `null`
3029
* @param serialized The lambda to deserialize. Note that this is typically created by the `readResolve`
3130
* member of the anonymous class created by `LambdaMetaFactory`.
@@ -77,20 +76,15 @@ object LambdaDeserializer {
7776
throw new IllegalArgumentException("Illegal lambda deserialization")
7877
}
7978

80-
val flags: Int = LambdaMetafactory.FLAG_SERIALIZABLE | LambdaMetafactory.FLAG_MARKERS
81-
val isScalaFunction = functionalInterfaceClass.getName.startsWith("scala.Function")
82-
val markerInterface: Class[_] = loader.loadClass(if (isScalaFunction) ScalaSerializable else JavaIOSerializable)
79+
val flags: Int = LambdaMetafactory.FLAG_SERIALIZABLE
8380

8481
LambdaMetafactory.altMetafactory(
8582
lookup, getFunctionalInterfaceMethodName, invokedType,
8683

8784
/* samMethodType = */ funcInterfaceSignature,
8885
/* implMethod = */ implMethod,
8986
/* instantiatedMethodType = */ instantiated,
90-
/* flags = */ flags.asInstanceOf[AnyRef],
91-
/* markerInterfaceCount = */ 1.asInstanceOf[AnyRef],
92-
/* markerInterfaces[0] = */ markerInterface,
93-
/* bridgeCount = */ 0.asInstanceOf[AnyRef]
87+
/* flags = */ flags.asInstanceOf[AnyRef]
9488
)
9589
}
9690

@@ -111,8 +105,6 @@ object LambdaDeserializer {
111105
factory.invokeWithArguments(captures: _*)
112106
}
113107

114-
private[this] val ScalaSerializable = "scala.Serializable"
115-
116108
private[this] val JavaIOSerializable = {
117109
// We could actually omit this marker interface as LambdaMetaFactory will add it if
118110
// the FLAG_SERIALIZABLE is set and of the provided markers extend it. But the code

src/reflect/scala/reflect/internal/Definitions.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,10 @@ trait Definitions extends api.StandardDefinitions {
372372

373373
lazy val TypeConstraintClass = requiredClass[scala.annotation.TypeConstraint]
374374
lazy val SingletonClass = enterNewClass(ScalaPackageClass, tpnme.Singleton, AnyTpe :: Nil, ABSTRACT | TRAIT | FINAL) markAllCompleted
375-
lazy val SerializableClass = requiredClass[scala.Serializable]
375+
lazy val SerializableClass = JavaSerializableClass
376376
lazy val JavaSerializableClass = requiredClass[java.io.Serializable] modifyInfo fixupAsAnyTrait
377377
lazy val ComparableClass = requiredClass[java.lang.Comparable[_]] modifyInfo fixupAsAnyTrait
378-
lazy val JavaCloneableClass = requiredClass[java.lang.Cloneable]
378+
lazy val JavaCloneableClass = requiredClass[java.lang.Cloneable] modifyInfo fixupAsAnyTrait
379379
lazy val JavaNumberClass = requiredClass[java.lang.Number]
380380
lazy val JavaEnumClass = requiredClass[java.lang.Enum[_]]
381381
lazy val JavaUtilMap = requiredClass[java.util.Map[_, _]]
@@ -1368,6 +1368,7 @@ trait Definitions extends api.StandardDefinitions {
13681368
/** Lists core classes that do have underlying bytecode, but are adjusted on-the-fly in every reflection universe */
13691369
lazy val hijackedCoreClasses = List(
13701370
ComparableClass,
1371+
JavaCloneableClass,
13711372
JavaSerializableClass
13721373
)
13731374
/** Lists symbols that are synthesized or hijacked by the compiler.

src/reflect/scala/reflect/internal/Printers.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,9 @@ trait Printers extends api.Printers { self: SymbolTable =>
623623
trees match {
624624
case Nil => trees
625625
case init :+ last => last match {
626-
case Select(Ident(sc), name) if traitsToRemove.contains(name) && sc == nme.scala_ =>
626+
case Select(Select(Ident(nme.scala_), nme.PACKAGE), name) if traitsToRemove.contains(name) =>
627+
removeDefaultTraitsFromList(init, traitsToRemove)
628+
case Select(Ident(nme.scala_), name) if traitsToRemove.contains(name) =>
627629
removeDefaultTraitsFromList(init, traitsToRemove)
628630
case _ => trees
629631
}
@@ -848,7 +850,7 @@ trait Printers extends api.Printers { self: SymbolTable =>
848850
val printedParents =
849851
currentParent map {
850852
case _: CompoundTypeTree => parents
851-
case ClassDef(mods, name, _, _) if mods.isCase => removeDefaultTypesFromList(parents)()(List(tpnme.Product, tpnme.Serializable))
853+
case ClassDef(mods, name, _, _) if mods.isCase => removeDefaultTypesFromList(parents)()(defaultTraitsForCase)
852854
case _ => removeDefaultClassesFromList(parents)
853855
} getOrElse (parents)
854856

test/files/presentation/callcc-interpreter.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ case class Var extends callccInterpreter.Term with Product with Serializable
1818
case object Wrong
1919
def +(other: String): String
2020
def ->[B](y: B): (callccInterpreter.type, B)
21-
def add(a: callccInterpreter.Value, b: callccInterpreter.Value): callccInterpreter.M[_ >: callccInterpreter.Num with callccInterpreter.Wrong.type <: Product with Serializable with callccInterpreter.Value]
21+
def add(a: callccInterpreter.Value, b: callccInterpreter.Value): callccInterpreter.M[_ >: callccInterpreter.Num with callccInterpreter.Wrong.type <: Product with callccInterpreter.Value with java.io.Serializable]
2222
def apply(a: callccInterpreter.Value, b: callccInterpreter.Value): callccInterpreter.M[callccInterpreter.Value]
2323
def callCC[A](h: (A => callccInterpreter.M[A]) => callccInterpreter.M[A]): callccInterpreter.M[A]
2424
def ensuring(cond: Boolean): callccInterpreter.type
@@ -90,7 +90,7 @@ def showM(m: callccInterpreter.M[callccInterpreter.Value]): String = m.in.apply(
9090
askType at CallccInterpreter.scala(50,30)
9191
================================================================================
9292
[response] askTypeAt (50,30)
93-
def add(a: callccInterpreter.Value, b: callccInterpreter.Value): callccInterpreter.M[_ >: callccInterpreter.Num with callccInterpreter.Wrong.type <: Product with Serializable with callccInterpreter.Value] = scala.Tuple2.apply[callccInterpreter.Value, callccInterpreter.Value](a, b) match {
93+
def add(a: callccInterpreter.Value, b: callccInterpreter.Value): callccInterpreter.M[_ >: callccInterpreter.Num with callccInterpreter.Wrong.type <: Product with callccInterpreter.Value with java.io.Serializable] = scala.Tuple2.apply[callccInterpreter.Value, callccInterpreter.Value](a, b) match {
9494
case (_1: callccInterpreter.Value, _2: callccInterpreter.Value)(callccInterpreter.Value, callccInterpreter.Value)((n: Int)callccInterpreter.Num((m @ _)), (n: Int)callccInterpreter.Num((n @ _))) => this.unitM[callccInterpreter.Num](callccInterpreter.this.Num.apply(m.+(n)))
9595
case _ => callccInterpreter.this.unitM[callccInterpreter.Wrong.type](callccInterpreter.this.Wrong)
9696
}

test/files/run/delambdafy_uncurry_byname_inline.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package <empty> {
77
};
88
def bar(x: () => Int): Int = x.apply();
99
def foo(): Int = Foo.this.bar({
10-
@SerialVersionUID(value = 0) final <synthetic> class $anonfun extends scala.runtime.AbstractFunction0[1] with Serializable {
10+
@SerialVersionUID(value = 0) final <synthetic> class $anonfun extends scala.runtime.AbstractFunction0[1] with java.io.Serializable {
1111
def <init>(): <$anon: () => 1> = {
1212
$anonfun.super.<init>();
1313
()

test/files/run/delambdafy_uncurry_inline.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package <empty> {
77
};
88
def bar(): Unit = {
99
val f: Int => Int = {
10-
@SerialVersionUID(value = 0) final <synthetic> class $anonfun extends scala.runtime.AbstractFunction1[Int,Int] with Serializable {
10+
@SerialVersionUID(value = 0) final <synthetic> class $anonfun extends scala.runtime.AbstractFunction1[Int,Int] with java.io.Serializable {
1111
def <init>(): <$anon: Int => Int> = {
1212
$anonfun.super.<init>();
1313
()

test/files/run/existentials3-old.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
_ <: scala.runtime.AbstractFunction0[_ <: Object with Test$ToS with scala.Product with scala.Serializable] with scala.Serializable with java.lang.Object
2-
_ <: Object with Test$ToS with scala.Product with scala.Serializable
1+
_ <: scala.runtime.AbstractFunction0[_ <: Object with Test$ToS with scala.Product with java.io.Serializable] with java.io.Serializable with java.lang.Object
2+
_ <: Object with Test$ToS with scala.Product with java.io.Serializable
33
Object with Test$ToS
44
Object with Test$ToS
55
Object with Test$ToS
@@ -9,8 +9,8 @@ _ <: Object with _ <: Object with Object with Test$ToS
99
_ <: Object with _ <: Object with _ <: Object with Test$ToS
1010
scala.collection.immutable.List[Object with scala.collection.immutable.Seq[Int]]
1111
scala.collection.immutable.List[Object with scala.collection.immutable.Seq[_ <: Int]]
12-
_ <: scala.runtime.AbstractFunction0[_ <: Object with Test$ToS with scala.Product with scala.Serializable] with scala.Serializable with java.lang.Object
13-
_ <: Object with Test$ToS with scala.Product with scala.Serializable
12+
_ <: scala.runtime.AbstractFunction0[_ <: Object with Test$ToS with scala.Product with java.io.Serializable] with java.io.Serializable with java.lang.Object
13+
_ <: Object with Test$ToS with scala.Product with java.io.Serializable
1414
Object with Test$ToS
1515
Object with Test$ToS
1616
Object with Test$ToS

test/files/run/idempotency-case-classes.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ C(2,3)
3737
C.this.x.==(C$1.x).&&(C.this.y.==(C$1.y)).&&(C$1.canEqual(C.this))
3838
}))
3939
};
40-
<synthetic> object C extends scala.runtime.AbstractFunction2[Int,Int,C] with Serializable {
40+
<synthetic> object C extends scala.runtime.AbstractFunction2[Int,Int,C] with java.io.Serializable {
4141
def <init>(): C.type = {
4242
C.super.<init>();
4343
()

test/files/run/t1195-new.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Bar.type, underlying = <: scala.runtime.AbstractFunction1[Int,Bar] with Serializable{case def unapply(x$0: Bar): Option[Int]} with Singleton
1+
Bar.type, underlying = <: scala.runtime.AbstractFunction1[Int,Bar] with java.io.Serializable{case def unapply(x$0: Bar): Option[Int]} with Singleton
22
Bar, underlying = <: Product with Serializable{val x: Int; def copy(x: Int): Bar; def copy$default$1: Int}
33
Product with Serializable, underlying = Product with Serializable
4-
Bar.type, underlying = <: scala.runtime.AbstractFunction1[Int,Bar] with Serializable{case def unapply(x$0: Bar): Option[Int]} with Singleton
4+
Bar.type, underlying = <: scala.runtime.AbstractFunction1[Int,Bar] with java.io.Serializable{case def unapply(x$0: Bar): Option[Int]} with Singleton
55
Bar, underlying = <: Product with Serializable{val x: Int; def copy(x: Int): Bar; def copy$default$1: Int}
66
Product with Serializable, underlying = Product with Serializable

test/files/run/t1195-old.check

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
_ <: scala.runtime.AbstractFunction1[Int, _ <: Object with scala.Product with scala.Serializable] with scala.Serializable with java.lang.Object
2-
_ <: Object with scala.Product with scala.Serializable
3-
Object with scala.Product with scala.Serializable
4-
_ <: scala.runtime.AbstractFunction1[Int, _ <: Object with scala.Product with scala.Serializable] with scala.Serializable with java.lang.Object
5-
_ <: Object with scala.Product with scala.Serializable
6-
Object with scala.Product with scala.Serializable
1+
_ <: scala.runtime.AbstractFunction1[Int, _ <: Object with scala.Product with java.io.Serializable] with java.io.Serializable with java.lang.Object
2+
_ <: Object with scala.Product with java.io.Serializable
3+
Object with scala.Product with java.io.Serializable
4+
_ <: scala.runtime.AbstractFunction1[Int, _ <: Object with scala.Product with java.io.Serializable] with java.io.Serializable with java.lang.Object
5+
_ <: Object with scala.Product with java.io.Serializable
6+
Object with scala.Product with java.io.Serializable

test/files/run/t5271_1.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
case class C extends Product with Serializable {
2+
case class C extends Product with `package`.Serializable {
33
<caseaccessor> <paramaccessor> val foo: Int = _;
44
<caseaccessor> <paramaccessor> val bar: Int = _;
55
def <init>(foo: Int, bar: Int) = {

test/files/run/t5271_2.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
case class C extends Product with Serializable {
2+
case class C extends Product with `package`.Serializable {
33
<caseaccessor> <paramaccessor> val foo: Int = _;
44
<caseaccessor> <paramaccessor> val bar: Int = _;
55
def <init>(foo: Int, bar: Int) = {

test/files/run/t5271_3.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
};
77
def qwe = 4
88
};
9-
case class C extends Product with Serializable {
9+
case class C extends Product with `package`.Serializable {
1010
<caseaccessor> <paramaccessor> val foo: Int = _;
1111
<caseaccessor> <paramaccessor> val bar: Int = _;
1212
def <init>(foo: Int, bar: Int) = {

test/files/run/t6028.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ package <empty> {
2424
(new <$anon: Function0>(T.this, tryyLocal, tryyParam): Function0)
2525
}
2626
};
27-
@SerialVersionUID(value = 0) final <synthetic> class $anonfun$foo$1 extends scala.runtime.AbstractFunction0$mcI$sp with Serializable {
27+
@SerialVersionUID(value = 0) final <synthetic> class $anonfun$foo$1 extends scala.runtime.AbstractFunction0$mcI$sp with java.io.Serializable {
2828
def <init>($outer: T, methodParam$1: Int, methodLocal$1: Int): <$anon: Function0> = {
2929
$anonfun$foo$1.super.<init>();
3030
()
@@ -62,7 +62,7 @@ package <empty> {
6262
MethodLocalObject$module$1.value().$asInstanceOf[T#MethodLocalObject$1.type]()
6363
else
6464
T.this.MethodLocalObject$lzycompute$1(MethodLocalObject$module$1, barParam$1);
65-
@SerialVersionUID(value = 0) final <synthetic> class $anonfun$tryy$1 extends scala.runtime.AbstractFunction0$mcV$sp with Serializable {
65+
@SerialVersionUID(value = 0) final <synthetic> class $anonfun$tryy$1 extends scala.runtime.AbstractFunction0$mcV$sp with java.io.Serializable {
6666
def <init>($outer: T, tryyLocal$1: runtime.IntRef, tryyParam$1: Int): <$anon: Function0> = {
6767
$anonfun$tryy$1.super.<init>();
6868
()

test/files/run/t6555.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package <empty> {
66
()
77
};
88
private[this] val f: Int => Int = {
9-
@SerialVersionUID(value = 0) final <synthetic> class $anonfun extends scala.runtime.AbstractFunction1$mcII$sp with Serializable {
9+
@SerialVersionUID(value = 0) final <synthetic> class $anonfun extends scala.runtime.AbstractFunction1$mcII$sp with java.io.Serializable {
1010
def <init>(): <$anon: Int => Int> = {
1111
$anonfun.super.<init>();
1212
()

test/files/run/t7747-repl.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ object $eval {
282282
}
283283
}
284284

285-
res3: List[Product with Serializable] = List(BippyBups(), PuppyPups(), Bingo())
285+
res3: List[Product with java.io.Serializable] = List(BippyBups(), PuppyPups(), Bingo())
286286

287287
scala> case class Sum(exp: String, exp2: String)
288288
defined class Sum

test/files/scalap/caseClass.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ case class CaseClass[A <: scala.Seq[scala.Int]](i: A, s: scala.Predef.String) ex
1212
override def toString(): java.lang.String = { /* compiled code */ }
1313
override def equals(x$1: scala.Any): scala.Boolean = { /* compiled code */ }
1414
}
15-
object CaseClass extends scala.AnyRef with scala.Serializable {
15+
object CaseClass extends scala.AnyRef with java.io.Serializable {
1616
def this() = { /* compiled code */ }
1717
final override def toString(): java.lang.String = { /* compiled code */ }
1818
def apply[A <: scala.Seq[scala.Int]](i: A, s: scala.Predef.String): CaseClass[A] = { /* compiled code */ }

0 commit comments

Comments
 (0)