Skip to content

Commit

Permalink
Merge pull request #10780 from som-snytt/backport/12774-lambda-serial
Browse files Browse the repository at this point in the history
[backport] Lambda is serializable when SAM defined in nonserializable parent
  • Loading branch information
lrytz committed May 13, 2024
2 parents 735198f + 1cfb11a commit 1cb2e87
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/compiler/scala/tools/nsc/transform/Delambdafy.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ abstract class Delambdafy extends Transform with TypingTransformers with ast.Tre

// no need for adaptation when the implemented sam is of a specialized built-in function type
val lambdaTarget = if (isSpecialized) target else createBoxingBridgeMethodIfNeeded(fun, target, functionalInterface, sam)
val isSerializable = samUserDefined == NoSymbol || samUserDefined.owner.isNonBottomSubClass(definitions.JavaSerializableClass)
val isSerializable = samUserDefined == NoSymbol || functionalInterface.isNonBottomSubClass(definitions.JavaSerializableClass)
val addScalaSerializableMarker = samUserDefined == NoSymbol

val samBridges = logResultIf[List[Symbol]](s"will add SAM bridges for $fun", _.nonEmpty) {
Expand Down
16 changes: 16 additions & 0 deletions test/files/run/t12774.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
trait SerializableBiFunction[T, U, R] extends java.util.function.BiFunction[T, U, R] with Serializable {
// def apply(t: T, u: U): R
}
object Test {
def main(args: Array[String]): Unit = {
import java.io._
val fn: SerializableBiFunction[String, Int, Boolean] = (str, expected) => str.length == expected

val buffer = new ByteArrayOutputStream
val out = new ObjectOutputStream(buffer)
out.writeObject(fn)
val in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray))
val res = in.readObject.asInstanceOf[SerializableBiFunction[String, Int, Boolean]]
assert(res("success", 7))
}
}

0 comments on commit 1cb2e87

Please sign in to comment.