From faa114e2fb6003031efa2cdd56a32a3c44aa71fb Mon Sep 17 00:00:00 2001 From: Aleksandar Prokopec Date: Fri, 17 Aug 2012 10:55:14 +0200 Subject: Fixes SI-6236. In separate compilation runs, the static field symbol in the companion class of an object was not being recreated. Given that the singleton object was compiled separately, the static field symbol will be recreated on demand. --- src/compiler/scala/tools/nsc/backend/icode/GenICode.scala | 15 +++++++++++++-- src/compiler/scala/tools/nsc/transform/CleanUp.scala | 2 +- test/files/run/static-annot/field.scala | 13 +++++++++++-- test/files/run/t6236.check | 2 ++ test/files/run/t6236/file_1.scala | 9 +++++++++ test/files/run/t6236/file_2.scala | 10 ++++++++++ 6 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 test/files/run/t6236.check create mode 100644 test/files/run/t6236/file_1.scala create mode 100644 test/files/run/t6236/file_2.scala diff --git a/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala b/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala index 1b89aa5560..a40d8c1a06 100644 --- a/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala +++ b/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala @@ -890,8 +890,19 @@ abstract class GenICode extends SubComponent { generatedType = toTypeKind(sym.accessed.info) val hostOwner = qual.tpe.typeSymbol.orElse(sym.owner) val hostClass = hostOwner.companionClass - val staticfield = hostClass.info.findMember(sym.accessed.name, NoFlags, NoFlags, false) - + val staticfield = hostClass.info.findMember(sym.accessed.name, NoFlags, NoFlags, false) orElse { + if (!currentRun.compiles(hostOwner)) { + // hostOwner was separately compiled -- the static field symbol needs to be recreated in hostClass + import Flags._ + debuglog("recreating sym.accessed.name: " + sym.accessed.name) + val objectfield = hostOwner.info.findMember(sym.accessed.name, NoFlags, NoFlags, false) + val staticfield = hostClass.newVariable(newTermName(sym.accessed.name.toString), tree.pos, STATIC | SYNTHETIC | FINAL) setInfo objectfield.tpe + staticfield.addAnnotation(definitions.StaticClass) + hostClass.info.decls enter staticfield + staticfield + } else NoSymbol + } + if (sym.isGetter) { ctx.bb.emit(LOAD_FIELD(staticfield, true) setHostClass hostClass, tree.pos) ctx diff --git a/src/compiler/scala/tools/nsc/transform/CleanUp.scala b/src/compiler/scala/tools/nsc/transform/CleanUp.scala index 4258ee8a88..6f402e9cd1 100644 --- a/src/compiler/scala/tools/nsc/transform/CleanUp.scala +++ b/src/compiler/scala/tools/nsc/transform/CleanUp.scala @@ -586,7 +586,7 @@ abstract class CleanUp extends Transform with ast.TreeDSL { // create a static field in the companion class for this @static field val stfieldSym = linkedClass.newVariable(newTermName(name), tree.pos, STATIC | SYNTHETIC | FINAL) setInfo sym.tpe stfieldSym.addAnnotation(StaticClass) - + val names = classNames.getOrElseUpdate(linkedClass, linkedClass.info.decls.collect { case sym if sym.name.isTermName => sym.name } toSet) diff --git a/test/files/run/static-annot/field.scala b/test/files/run/static-annot/field.scala index a7d8158321..8408a51800 100644 --- a/test/files/run/static-annot/field.scala +++ b/test/files/run/static-annot/field.scala @@ -23,8 +23,10 @@ class Foo trait Check { def checkStatic(cls: Class[_]) { cls.getDeclaredFields.find(_.getName == "bar") match { - case Some(f) => assert(Modifier.isStatic(f.getModifiers), "no static modifier") - case None => assert(false, "no static field bar in class") + case Some(f) => + assert(Modifier.isStatic(f.getModifiers), "no static modifier") + case None => + assert(false, "no static field bar in class") } } @@ -170,6 +172,10 @@ object Foo7 { @static val bar = "string" } class AndHisFriend + + object AndHisLonelyFriend { + @static val bar = "another" + } } @@ -177,6 +183,9 @@ object Test7 extends Check { def test() { checkStatic(classOf[Foo7.AndHisFriend]) assert(Foo7.AndHisFriend.bar == "string") + + checkStatic(Class.forName("Foo7$AndHisLonelyFriend")) + assert(Foo7.AndHisLonelyFriend.bar == "another") } } diff --git a/test/files/run/t6236.check b/test/files/run/t6236.check new file mode 100644 index 0000000000..a0a2e88d0a --- /dev/null +++ b/test/files/run/t6236.check @@ -0,0 +1,2 @@ +353 +353 \ No newline at end of file diff --git a/test/files/run/t6236/file_1.scala b/test/files/run/t6236/file_1.scala new file mode 100644 index 0000000000..92d22799fc --- /dev/null +++ b/test/files/run/t6236/file_1.scala @@ -0,0 +1,9 @@ + + +package p { + object y { + object x { + @scala.annotation.static val foo: Int = 353 + } + } +} diff --git a/test/files/run/t6236/file_2.scala b/test/files/run/t6236/file_2.scala new file mode 100644 index 0000000000..51823004ca --- /dev/null +++ b/test/files/run/t6236/file_2.scala @@ -0,0 +1,10 @@ + + + +object Test { + def main(args: Array[String]): Unit = { + println(p.y.x.foo) + println(p.y.x.foo) + } +} + -- cgit v1.2.3