Skip to content

Commit ac0f851

Browse files
committed
Fix serialization of partial function literals
1 parent a3c4ec7 commit ac0f851

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

compiler/src/dotty/tools/dotc/transform/ExpandSAMs.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ class ExpandSAMs extends MiniPhase {
111111
val isDefinedAtDef = transformFollowingDeep(DefDef(isDefinedAtFn, isDefinedAtRhs(_)))
112112
val applyOrElseDef = transformFollowingDeep(DefDef(applyOrElseFn, applyOrElseRhs(_)))
113113

114-
val parent = defn.AbstractPartialFunctionType.appliedTo(tpe.argInfos)
115-
val anonCls = AnonClass(parent :: Nil, List(isDefinedAtFn, applyOrElseFn), List(nme.isDefinedAt, nme.applyOrElse))
114+
val parents = List(defn.AbstractPartialFunctionType.appliedTo(tpe.argInfos), defn.SerializableType)
115+
val anonCls = AnonClass(parents, List(isDefinedAtFn, applyOrElseFn), List(nme.isDefinedAt, nme.applyOrElse))
116116
cpy.Block(tree)(List(isDefinedAtDef, applyOrElseDef), anonCls)
117117

118118
case _ =>

tests/run/serialize.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
object Test {
2+
def serializeDeserialize[T <: AnyRef](obj: T): T = {
3+
import java.io._
4+
val buffer = new ByteArrayOutputStream
5+
val out = new ObjectOutputStream(buffer)
6+
out.writeObject(obj)
7+
val in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray))
8+
in.readObject.asInstanceOf[T]
9+
}
10+
11+
def main(args: Array[String]): Unit = {
12+
val x: PartialFunction[Int, Int] = { case x => x + 1 }
13+
val adder = serializeDeserialize(x)
14+
assert(adder(1) == 2)
15+
}
16+
}

0 commit comments

Comments
 (0)