summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIulian Dragos <jaguarul@gmail.com>2008-06-03 10:13:48 +0000
committerIulian Dragos <jaguarul@gmail.com>2008-06-03 10:13:48 +0000
commita7f12d2e14eabf0a915e2b306f5a624012c239fe (patch)
tree006964110166b9ed3e7eac86b1dea8b91900ae6a
parentba33786e9b0247bc8c796dddbbed1c69905c667e (diff)
downloadscala-a7f12d2e14eabf0a915e2b306f5a624012c239fe.tar.gz
scala-a7f12d2e14eabf0a915e2b306f5a624012c239fe.tar.bz2
scala-a7f12d2e14eabf0a915e2b306f5a624012c239fe.zip
Fixed #948.
-rw-r--r--src/compiler/scala/tools/nsc/transform/LazyVals.scala28
-rw-r--r--test/files/run/lazy-locals.check1
-rw-r--r--test/files/run/lazy-locals.scala17
3 files changed, 39 insertions, 7 deletions
diff --git a/src/compiler/scala/tools/nsc/transform/LazyVals.scala b/src/compiler/scala/tools/nsc/transform/LazyVals.scala
index 43565eb087..d5d96927ca 100644
--- a/src/compiler/scala/tools/nsc/transform/LazyVals.scala
+++ b/src/compiler/scala/tools/nsc/transform/LazyVals.scala
@@ -42,6 +42,8 @@ abstract class LazyVals extends Transform {
/** Perform the following transformations:
* - for a lazy accessor inside a method, make it check the initialization bitmap
* - for all methods, add enough int vars to allow one flag per lazy local value
+ * - blocks in template bodies behave almost like methods. A single bitmaps section is
+ * added in the first block, for all lazy values defined in such blocks.
* - remove ACCESSOR flags: accessors in traits are not statically implemented,
* but moved to the host class. local lazy values should be statically implemented.
*/
@@ -50,17 +52,30 @@ abstract class LazyVals extends Transform {
tree match {
case DefDef(mods, name, tparams, vparams, tpt, rhs) =>
val res = if (!sym.owner.isClass && sym.hasFlag(LAZY)) {
- val idx = lazyVals(sym.enclMethod)
- val rhs1 = mkLazyDef(sym.enclMethod, super.transform(rhs), idx)
+ val enclosingDummyOrMethod = if (sym.owner.isLocalDummy) sym.owner else sym.enclMethod
+ val idx = lazyVals(enclosingDummyOrMethod)
+ val rhs1 = mkLazyDef(enclosingDummyOrMethod, super.transform(rhs), idx)
lazyVals(sym.owner) = idx + 1
sym.resetFlag(LAZY | ACCESSOR)
rhs1
} else
super.transform(rhs)
- val bmps = bitmaps(sym) map { b => ValDef(b, Literal(Constant(0))) }
- val tmp = addBitmapDefs(sym, res, bmps)
copy.DefDef(tree, mods, name, tparams, vparams, tpt,
- typed(addBitmapDefs(sym, res, bmps)))
+ typed(addBitmapDefs(sym, res)))
+
+ case Template(parents, self, body) =>
+ val body1 = super.transformTrees(body)
+ var added = false
+ val stats =
+ for (stat <- body1) yield stat match {
+ case Block(_, _) if !added =>
+ added = true
+ typed(addBitmapDefs(sym, stat))
+ case _ =>
+ stat
+ }
+ copy.Template(tree, parents, self, stats)
+
case _ => super.transform(tree)
}
@@ -72,7 +87,8 @@ abstract class LazyVals extends Transform {
* iteration has the lazy values un-initialized. Otherwise add them
* at the very beginning of the method.
*/
- private def addBitmapDefs(methSym: Symbol, rhs: Tree, bmps: List[Tree]): Tree = {
+ private def addBitmapDefs(methSym: Symbol, rhs: Tree): Tree = {
+ val bmps = bitmaps(methSym) map { b => ValDef(b, Literal(Constant(0))) }
if (bmps.isEmpty) rhs else rhs match {
case Block(assign, l @ LabelDef(name, params, rhs1))
if (name.toString.equals("_" + methSym.name)
diff --git a/test/files/run/lazy-locals.check b/test/files/run/lazy-locals.check
index 7f65cd3db2..08d99562c2 100644
--- a/test/files/run/lazy-locals.check
+++ b/test/files/run/lazy-locals.check
@@ -80,3 +80,4 @@ forced lazy val t at n = 4
forced lazy val t at n = 5
1764
First 5 elements of ones: List(1, 1, 1, 1, 1)
+I am initialized when the constructor is run
diff --git a/test/files/run/lazy-locals.scala b/test/files/run/lazy-locals.scala
index 937b5a80c0..324f7c00e4 100644
--- a/test/files/run/lazy-locals.scala
+++ b/test/files/run/lazy-locals.scala
@@ -133,11 +133,26 @@ object Test extends Application {
()
}
+ {
+ lazy val inCtor = "I am initialized when the constructor is run"
+ inCtor
+ }
+
+ class CtorBlock {
+ {
+ lazy val inCtor = {
+ println("I am initialized when the constructor is run")
+ 42
+ }
+ inCtor
+ }
+ }
+
println(testLazy)
testLazy32
testLazy33
println(testLazyRec(5))
println(testLazyRecMany(5))
testRecVal
-
+ new CtorBlock
}