From 0c99742c51706ee4b60a56a8f5babb13682f9b10 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Mon, 27 Jul 2015 15:33:30 +1000 Subject: SI-9365 Don't null out dependencies of transient lazy vals As per Iulian's analysis: > When lazy vals are transient, the optimization in SI-720 is invalid, > leading to NPEs. These NPEs appear when recomputing the lazy val when deserializaing the object. This commit disables the field nulling if the lazy val is marked transient. The post-mixin tree in the enclosed test changes as follows: ``` --- sandbox/old.log 2015-07-27 15:48:03.000000000 +1000 +++ sandbox/new.log 2015-07-27 15:47:56.000000000 +1000 @@ -1,61 +1,54 @@ [[syntax trees at end of mixin]] // t9365.scala package { class Test extends Object with Serializable { @transient @volatile private[this] var bitmap$trans$0: Boolean = false; private def foo$lzycompute(): Object = { { Test.this.synchronized({ if (Test.this.bitmap$trans$0.unary_!()) { Test.this.foo = Test.this.x.apply(); Test.this.bitmap$trans$0 = true; () }; scala.runtime.BoxedUnit.UNIT }); - Test.this.x = null + () }; Test.this.foo }; ``` In addition to the test from the ticket, I've added a reflection based test that directly tests the nulling. This complements the test added in 449f2a7473, the fix for SI-720, which passes by virtue of not exhausting the heap. --- src/compiler/scala/tools/nsc/transform/Mixin.scala | 2 +- test/files/run/t720.scala | 48 ++++++++++++++++++++++ test/files/run/t9365.check | 2 + test/files/run/t9365.scala | 18 ++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 test/files/run/t720.scala create mode 100644 test/files/run/t9365.check create mode 100644 test/files/run/t9365.scala diff --git a/src/compiler/scala/tools/nsc/transform/Mixin.scala b/src/compiler/scala/tools/nsc/transform/Mixin.scala index 25d45cc819..a079a76ce7 100644 --- a/src/compiler/scala/tools/nsc/transform/Mixin.scala +++ b/src/compiler/scala/tools/nsc/transform/Mixin.scala @@ -1122,7 +1122,7 @@ abstract class Mixin extends InfoTransform with ast.TreeDSL { if (scope exists (_.isLazy)) { val map = mutable.Map[Symbol, Set[Symbol]]() withDefaultValue Set() // check what fields can be nulled for - for ((field, users) <- singleUseFields(templ); lazyFld <- users) + for ((field, users) <- singleUseFields(templ); lazyFld <- users if !lazyFld.accessed.hasAnnotation(TransientAttr)) map(lazyFld) += field map.toMap diff --git a/test/files/run/t720.scala b/test/files/run/t720.scala new file mode 100644 index 0000000000..a5cb2495cf --- /dev/null +++ b/test/files/run/t720.scala @@ -0,0 +1,48 @@ +class Lazy(f: => Int) { + lazy val get: Int = f +} + +class UsedLater(f: => Int) { + lazy val get: Int = f + def other = f +} + +class TransientLazy(f: => Int) { + @transient + lazy val get: Int = f +} + +object Test { + def main(args: Array[String]): Unit = { + testLazy() + testUsedLater() + } + + def testLazy() { + val o = new Lazy("".length) + val f = classOf[Lazy].getDeclaredField("f") + f.setAccessible(true) + assert(f.get(o) != null) + o.get + assert(f.get(o) == null) + } + + def testUsedLater() { + val o = new UsedLater("".length) + val f = classOf[UsedLater].getDeclaredField("f") + f.setAccessible(true) + assert(f.get(o) != null) + o.get + assert(f.get(o) != null) + } + + def testTransientLazy() { + val o = new TransientLazy("".length) + val f = classOf[TransientLazy].getDeclaredField("f") + f.setAccessible(true) + assert(f.get(o) != null) + o.get + assert(f.get(o) != null) // SI-9365 + } +} + diff --git a/test/files/run/t9365.check b/test/files/run/t9365.check new file mode 100644 index 0000000000..0d55bed3a3 --- /dev/null +++ b/test/files/run/t9365.check @@ -0,0 +1,2 @@ +foo +foo diff --git a/test/files/run/t9365.scala b/test/files/run/t9365.scala new file mode 100644 index 0000000000..0c4477dda9 --- /dev/null +++ b/test/files/run/t9365.scala @@ -0,0 +1,18 @@ +class Test(x: => Object) extends Serializable { + @transient lazy val foo = x +} + +object Test { + def main(args: Array[String]): Unit = { + import java.io._ + val t = new Test("foo") + println(t.foo) + val baos = new ByteArrayOutputStream + val dos = new ObjectOutputStream(baos) + dos.writeObject(t) + dos.close() + val dis = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())) + val t1 = dis.readObject().asInstanceOf[Test] + println(t1.foo) // was NPE + } +} -- cgit v1.2.3