From b0d71fd942a84a8e7a87e2b7619f4ab6fcc741ca Mon Sep 17 00:00:00 2001 From: Allan Renucci Date: Wed, 2 May 2018 16:51:27 +0200 Subject: [PATCH] Make partial function literals serializable --- .../dotty/tools/dotc/transform/ExpandSAMs.scala | 4 ++-- tests/run/serialize.scala | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 tests/run/serialize.scala diff --git a/compiler/src/dotty/tools/dotc/transform/ExpandSAMs.scala b/compiler/src/dotty/tools/dotc/transform/ExpandSAMs.scala index e2c04b920250..ca652e157cd5 100644 --- a/compiler/src/dotty/tools/dotc/transform/ExpandSAMs.scala +++ b/compiler/src/dotty/tools/dotc/transform/ExpandSAMs.scala @@ -111,8 +111,8 @@ class ExpandSAMs extends MiniPhase { val isDefinedAtDef = transformFollowingDeep(DefDef(isDefinedAtFn, isDefinedAtRhs(_))) val applyOrElseDef = transformFollowingDeep(DefDef(applyOrElseFn, applyOrElseRhs(_))) - val parent = defn.AbstractPartialFunctionType.appliedTo(tpe.argInfos) - val anonCls = AnonClass(parent :: Nil, List(isDefinedAtFn, applyOrElseFn), List(nme.isDefinedAt, nme.applyOrElse)) + val parents = List(defn.AbstractPartialFunctionType.appliedTo(tpe.argInfos), defn.SerializableType) + val anonCls = AnonClass(parents, List(isDefinedAtFn, applyOrElseFn), List(nme.isDefinedAt, nme.applyOrElse)) cpy.Block(tree)(List(isDefinedAtDef, applyOrElseDef), anonCls) case _ => diff --git a/tests/run/serialize.scala b/tests/run/serialize.scala new file mode 100644 index 000000000000..3c97892ae5ec --- /dev/null +++ b/tests/run/serialize.scala @@ -0,0 +1,16 @@ +object Test { + def serializeDeserialize[T <: AnyRef](obj: T): T = { + import java.io._ + val buffer = new ByteArrayOutputStream + val out = new ObjectOutputStream(buffer) + out.writeObject(obj) + val in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray)) + in.readObject.asInstanceOf[T] + } + + def main(args: Array[String]): Unit = { + val x: PartialFunction[Int, Int] = { case x => x + 1 } + val adder = serializeDeserialize(x) + assert(adder(1) == 2) + } +}