Skip to content

Commit

Permalink
Make Lazy* classes serializable
Browse files Browse the repository at this point in the history
The instances may be captured by closures, which should
be serializable.

Fixes scala/bug#10522
  • Loading branch information
lrytz committed Sep 27, 2017
1 parent dd162ea commit d831810
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 11 deletions.
24 changes: 23 additions & 1 deletion src/library/mima-filters/2.12.0.forwards.excludes
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,26 @@ ProblemFilters.exclude[MissingClassProblem]("scala.annotation.showAsInfix$")
ProblemFilters.exclude[MissingClassProblem]("scala.annotation.showAsInfix")

ProblemFilters.exclude[DirectMissingMethodProblem]("scala.util.PropertiesTrait.coloredOutputEnabled")
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.util.Properties.coloredOutputEnabled")
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.util.Properties.coloredOutputEnabled")

# https://github.com/scala/scala/pull/6101
ProblemFilters.exclude[MissingTypesProblem]("scala.runtime.LazyRef")
ProblemFilters.exclude[MissingTypesProblem]("scala.runtime.LazyDouble")
ProblemFilters.exclude[MissingTypesProblem]("scala.runtime.LazyChar")
ProblemFilters.exclude[MissingTypesProblem]("scala.runtime.LazyUnit")
ProblemFilters.exclude[MissingTypesProblem]("scala.runtime.LazyShort")
ProblemFilters.exclude[MissingTypesProblem]("scala.runtime.LazyInt")
ProblemFilters.exclude[MissingTypesProblem]("scala.runtime.LazyByte")
ProblemFilters.exclude[MissingTypesProblem]("scala.runtime.LazyLong")
ProblemFilters.exclude[MissingTypesProblem]("scala.runtime.LazyBoolean")
ProblemFilters.exclude[MissingTypesProblem]("scala.runtime.LazyFloat")
ProblemFilters.exclude[MissingFieldProblem]("scala.runtime.LazyRef.serialVersionUID")
ProblemFilters.exclude[MissingFieldProblem]("scala.runtime.LazyDouble.serialVersionUID")
ProblemFilters.exclude[MissingFieldProblem]("scala.runtime.LazyChar.serialVersionUID")
ProblemFilters.exclude[MissingFieldProblem]("scala.runtime.LazyUnit.serialVersionUID")
ProblemFilters.exclude[MissingFieldProblem]("scala.runtime.LazyShort.serialVersionUID")
ProblemFilters.exclude[MissingFieldProblem]("scala.runtime.LazyInt.serialVersionUID")
ProblemFilters.exclude[MissingFieldProblem]("scala.runtime.LazyByte.serialVersionUID")
ProblemFilters.exclude[MissingFieldProblem]("scala.runtime.LazyLong.serialVersionUID")
ProblemFilters.exclude[MissingFieldProblem]("scala.runtime.LazyBoolean.serialVersionUID")
ProblemFilters.exclude[MissingFieldProblem]("scala.runtime.LazyFloat.serialVersionUID")
30 changes: 20 additions & 10 deletions src/library/scala/runtime/LazyRef.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ package scala.runtime

/** Classes used as holders for lazy vals defined in methods. */

class LazyRef[T] {
@SerialVersionUID(1l)
class LazyRef[T] extends Serializable {
@volatile private[this] var _initialized: Boolean = _
def initialized = _initialized

Expand All @@ -25,7 +26,8 @@ class LazyRef[T] {
override def toString = s"LazyRef ${if (_initialized) s"of: ${_value}" else "thunk"}"
}

class LazyBoolean {
@SerialVersionUID(1l)
class LazyBoolean extends Serializable {
@volatile private[this] var _initialized: Boolean = _
def initialized = _initialized

Expand All @@ -40,7 +42,8 @@ class LazyBoolean {
override def toString = s"LazyBoolean ${if (_initialized) s"of: ${_value}" else "thunk"}"
}

class LazyByte {
@SerialVersionUID(1l)
class LazyByte extends Serializable {
@volatile private[this] var _initialized: Boolean = _
def initialized = _initialized

Expand All @@ -57,7 +60,8 @@ class LazyByte {
override def toString = s"LazyByte ${if (_initialized) s"of: ${_value}" else "thunk"}"
}

class LazyChar {
@SerialVersionUID(1l)
class LazyChar extends Serializable {
@volatile private[this] var _initialized: Boolean = _
def initialized = _initialized

Expand All @@ -72,7 +76,8 @@ class LazyChar {
override def toString = s"LazyChar ${if (_initialized) s"of: ${_value}" else "thunk"}"
}

class LazyShort {
@SerialVersionUID(1l)
class LazyShort extends Serializable {
@volatile private[this] var _initialized: Boolean = _
def initialized = _initialized

Expand All @@ -87,7 +92,8 @@ class LazyShort {
override def toString = s"LazyShort ${if (_initialized) s"of: ${_value}" else "thunk"}"
}

class LazyInt {
@SerialVersionUID(1l)
class LazyInt extends Serializable {
@volatile private[this] var _initialized: Boolean = _
def initialized = _initialized

Expand All @@ -102,7 +108,8 @@ class LazyInt {
override def toString = s"LazyInt ${if (_initialized) s"of: ${_value}" else "thunk"}"
}

class LazyLong {
@SerialVersionUID(1l)
class LazyLong extends Serializable {
@volatile private[this] var _initialized: Boolean = _
def initialized = _initialized

Expand All @@ -117,7 +124,8 @@ class LazyLong {
override def toString = s"LazyLong ${if (_initialized) s"of: ${_value}" else "thunk"}"
}

class LazyFloat {
@SerialVersionUID(1l)
class LazyFloat extends Serializable {
@volatile private[this] var _initialized: Boolean = _
def initialized = _initialized

Expand All @@ -132,7 +140,8 @@ class LazyFloat {
override def toString = s"LazyFloat ${if (_initialized) s"of: ${_value}" else "thunk"}"
}

class LazyDouble {
@SerialVersionUID(1l)
class LazyDouble extends Serializable {
@volatile private[this] var _initialized: Boolean = _
def initialized = _initialized

Expand All @@ -147,7 +156,8 @@ class LazyDouble {
override def toString = s"LazyDouble ${if (_initialized) s"of: ${_value}" else "thunk"}"
}

class LazyUnit {
@SerialVersionUID(1l)
class LazyUnit extends Serializable {
@volatile private[this] var _initialized: Boolean = _
def initialized = _initialized

Expand Down
12 changes: 12 additions & 0 deletions test/files/run/t10522.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
gi init x
1
1
gi init x
1
1
gs init x
hi
hi
gs init x
hi
hi
38 changes: 38 additions & 0 deletions test/files/run/t10522.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 gi: () => Int = {
lazy val x = { println("gi init x"); 1 }
serializeDeserialize(() => x)
}

def gs: () => String = {
lazy val x = { println("gs init x"); "hi" }
serializeDeserialize(() => x)
}

def main(args: Array[String]): Unit = {
val fi1 = gi
println(fi1())
println(fi1())

val fi2 = gi
println(fi2())
println(fi2())

val fs1 = gs
println(fs1())
println(fs1())

val fs2 = gs
println(fs2())
println(fs2())
}
}

0 comments on commit d831810

Please sign in to comment.