Skip to content

Commit 83ee246

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. For some reason, this commit causes the issue described in scala#3383 to reappear, we add a workaround for that in Trees.scala.
1 parent 45a51d8 commit 83ee246

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

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

Lines changed: 5 additions & 2 deletions
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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,8 @@ object Trees {
887887

888888
// ----- Generic Tree Instances, inherited from `tpt` and `untpd`.
889889

890-
abstract class Instance[T >: Untyped <: Type] { inst =>
890+
// FIXME: Work around #3383 by writing `Types.Type` instead of `Type`
891+
abstract class Instance[T >: Untyped <: Types.Type] { inst =>
891892

892893
type Tree = Trees.Tree[T]
893894
type TypTree = Trees.TypTree[T]

0 commit comments

Comments
 (0)