Skip to content

Commit 19b5a4e

Browse files
committed
Make all objects Serializable
To avoid deadlocks when combining objects, lambdas and multi-threading, lambdas in objects are compiled to instance methods of the module class instead of static methods (see tests/run/deadlock.scala and scala/scala-dev#195 for details). This has worked well for us so far but this is problematic for serialization: serializing a lambda requires serializing all the values it captures, if this lambda is in an object, this means serializing the enclosing object, which fails if the object does not extend Serializable. Because serializing objects is basically free since scala#5775, it seems like the simplest solution is to simply make all objects Serializable, this certainly seems preferable to deadlocks.
1 parent 45a51d8 commit 19b5a4e

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

+5-2
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,9 @@ object desugar {
340340
case _ => false
341341
}
342342

343-
val isCaseClass = mods.is(Case) && !mods.is(Module)
344-
val isCaseObject = mods.is(Case) && mods.is(Module)
343+
val isObject = mods.is(Module)
344+
val isCaseClass = mods.is(Case) && !isObject
345+
val isCaseObject = mods.is(Case) && isObject
345346
val isImplicit = mods.is(Implicit)
346347
val isInstance = isImplicit && mods.mods.exists(_.isInstanceOf[Mod.Instance])
347348
val isEnum = mods.isEnumClass && !mods.is(Module)
@@ -533,6 +534,8 @@ object desugar {
533534
parents1 = enumClassTypeRef :: Nil
534535
if (isCaseClass | isCaseObject)
535536
parents1 = parents1 :+ scalaDot(str.Product.toTypeName) :+ scalaDot(nme.Serializable.toTypeName)
537+
else if (isObject)
538+
parents1 = parents1 :+ scalaDot(nme.Serializable.toTypeName)
536539
if (isEnum)
537540
parents1 = parents1 :+ ref(defn.EnumType)
538541

0 commit comments

Comments
 (0)