Skip to content

Commit

Permalink
Make all objects Serializable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
smarter committed Feb 3, 2019
1 parent 45a51d8 commit 83ee246
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
7 changes: 5 additions & 2 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,9 @@ object desugar {
case _ => false
}

val isCaseClass = mods.is(Case) && !mods.is(Module)
val isCaseObject = mods.is(Case) && mods.is(Module)
val isObject = mods.is(Module)
val isCaseClass = mods.is(Case) && !isObject
val isCaseObject = mods.is(Case) && isObject
val isImplicit = mods.is(Implicit)
val isInstance = isImplicit && mods.mods.exists(_.isInstanceOf[Mod.Instance])
val isEnum = mods.isEnumClass && !mods.is(Module)
Expand Down Expand Up @@ -533,6 +534,8 @@ object desugar {
parents1 = enumClassTypeRef :: Nil
if (isCaseClass | isCaseObject)
parents1 = parents1 :+ scalaDot(str.Product.toTypeName) :+ scalaDot(nme.Serializable.toTypeName)
else if (isObject)
parents1 = parents1 :+ scalaDot(nme.Serializable.toTypeName)
if (isEnum)
parents1 = parents1 :+ ref(defn.EnumType)

Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/ast/Trees.scala
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,8 @@ object Trees {

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

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

type Tree = Trees.Tree[T]
type TypTree = Trees.TypTree[T]
Expand Down

0 comments on commit 83ee246

Please sign in to comment.