Skip to content

Commit a24d522

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 72b3852 commit a24d522

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
@@ -338,8 +338,9 @@ object desugar {
338338
case _ => false
339339
}
340340

341-
val isCaseClass = mods.is(Case) && !mods.is(Module)
342-
val isCaseObject = mods.is(Case) && mods.is(Module)
341+
val isObject = mods.is(Module)
342+
val isCaseClass = mods.is(Case) && !isObject
343+
val isCaseObject = mods.is(Case) && isObject
343344
val isImplicit = mods.is(Implicit)
344345
val isEnum = mods.isEnumClass && !mods.is(Module)
345346
def isEnumCase = mods.isEnumCase
@@ -522,6 +523,8 @@ object desugar {
522523
parents1 = enumClassTypeRef :: Nil
523524
if (isCaseClass | isCaseObject)
524525
parents1 = parents1 :+ scalaDot(str.Product.toTypeName) :+ scalaDot(nme.Serializable.toTypeName)
526+
else if (isObject)
527+
parents1 = parents1 :+ scalaDot(nme.Serializable.toTypeName)
525528
if (isEnum)
526529
parents1 = parents1 :+ ref(defn.EnumType)
527530

0 commit comments

Comments
 (0)