From 01be1b1c201d6908522d7254075fd1cdf633809a Mon Sep 17 00:00:00 2001 From: Iulian Dragos Date: Tue, 17 Jul 2012 10:50:59 +0200 Subject: Fixed SI-6092. Fixed leaky annotations, and relaxed the conditions under which a try-catch is lifted out to an inner method. Less known fact: lazy values null-out their dependent values is their accessed only from their initializer. The analysis is not context-dependent (meaning the owner where a reference happens needs to be exactly that lazy value). * Removed no-op code around positions in `LazyAnnotationInfo` * Don't lift expressions that have no `catch` clause The two changes combined fix a memory leak that's been plaguing the IDE: an annotation (even when forced) would hang on to a namer, through the outer field of its call-by-name parameter. The test for the memory leak is in the IDE project (couldn't find a simple way to reproduce it outside the IDE), but there's a test checking that the field is null after initialization. --- .../scala/tools/nsc/transform/UnCurry.scala | 15 +++++++++ .../scala/reflect/internal/AnnotationInfos.scala | 7 +---- test/files/run/nullable-lazyvals.check | 3 ++ test/files/run/nullable-lazyvals.scala | 36 ++++++++++++++++++++++ 4 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 test/files/run/nullable-lazyvals.check create mode 100644 test/files/run/nullable-lazyvals.scala diff --git a/src/compiler/scala/tools/nsc/transform/UnCurry.scala b/src/compiler/scala/tools/nsc/transform/UnCurry.scala index de0650b2ea..efc3d25ed0 100644 --- a/src/compiler/scala/tools/nsc/transform/UnCurry.scala +++ b/src/compiler/scala/tools/nsc/transform/UnCurry.scala @@ -33,6 +33,14 @@ import language.postfixOps * - convert implicit method types to method types * - convert non-trivial catches in try statements to matches * - convert non-local returns to throws with enclosing try statements. + * - convert try-catch expressions in contexts where there might be values on the stack to + * a local method and a call to it (since an exception empties the evaluation stack): + * + * meth(x_1,..., try { x_i } catch { ..}, .. x_b0) ==> + * { + * def liftedTry$1 = try { x_i } catch { .. } + * meth(x_1, .., liftedTry$1(), .. ) + * } */ /* */ abstract class UnCurry extends InfoTransform @@ -634,6 +642,13 @@ abstract class UnCurry extends InfoTransform case ret @ Return(_) if (isNonLocalReturn(ret)) => withNeedLift(true) { super.transform(ret) } + case Try(_, Nil, _) => + // try-finally does not need lifting: lifting is needed only for try-catch + // expressions that are evaluated in a context where the stack might not be empty. + // `finally` does not attempt to continue evaluation after an exception, so the fact + // that values on the stack are 'lost' does not matter + super.transform(tree) + case Try(block, catches, finalizer) => if (needTryLift || shouldBeLiftedAnyway(tree)) transform(liftTree(tree)) else super.transform(tree) diff --git a/src/reflect/scala/reflect/internal/AnnotationInfos.scala b/src/reflect/scala/reflect/internal/AnnotationInfos.scala index c283ae408e..2bcc95b9a8 100644 --- a/src/reflect/scala/reflect/internal/AnnotationInfos.scala +++ b/src/reflect/scala/reflect/internal/AnnotationInfos.scala @@ -160,12 +160,7 @@ trait AnnotationInfos extends api.AnnotationInfos { self: SymbolTable => */ final class LazyAnnotationInfo(lazyInfo: => AnnotationInfo) extends AnnotationInfo { private var forced = false - private lazy val forcedInfo = - try { - val result = lazyInfo - if (result.pos == NoPosition) result setPos pos - result - } finally forced = true + private lazy val forcedInfo = try lazyInfo finally forced = true def atp: Type = forcedInfo.atp def args: List[Tree] = forcedInfo.args diff --git a/test/files/run/nullable-lazyvals.check b/test/files/run/nullable-lazyvals.check new file mode 100644 index 0000000000..4db5783257 --- /dev/null +++ b/test/files/run/nullable-lazyvals.check @@ -0,0 +1,3 @@ + +param1: null +param2: null diff --git a/test/files/run/nullable-lazyvals.scala b/test/files/run/nullable-lazyvals.scala new file mode 100644 index 0000000000..c201e74e75 --- /dev/null +++ b/test/files/run/nullable-lazyvals.scala @@ -0,0 +1,36 @@ + +/** Test that call-by-name parameters are set to null if + * they are used only to initialize a lazy value, after the + * value has been initialized. + */ + +class Foo(param1: => Object, param2: => String) { + lazy val field1 = param1 + lazy val field2 = try param2 finally println("") +} + +object Test extends App { + val foo = new Foo(new Object, "abc") + + foo.field1 + foo.field2 + + for (f <- foo.getClass.getDeclaredFields) { + f.setAccessible(true) + if (f.getName.startsWith("param")) { + println("%s: %s".format(f.getName, f.get(foo))) + } + } + + // test that try-finally does not generated a liftedTry + // helper. This would already fail the first part of the test, + // but this check will help diganose it (if the single access to a + // private field does not happen directly in the lazy val, it won't + // be nulled). + for (f <- foo.getClass.getDeclaredMethods) { + f.setAccessible(true) + if (f.getName.startsWith("lifted")) { + println("not expected: %s".format(f)) + } + } +} -- cgit v1.2.3