8000 Fixes SI-6236. · gkossakowski/scala@faa114e · GitHub
[go: up one dir, main page]

Skip to content

Commit faa114e

Browse files
Aleksandar Prokopecpaulp
authored andcommitted
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.
1 parent 5a8dfad commit faa114e

File tree

6 files changed

+46
-5
lines changed

6 files changed

+46
-5
lines changed

src/compiler/scala/tools/nsc/backend/icode/GenICode.scala

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -890,8 +890,19 @@ abstract class GenICode extends SubComponent {
890890
generatedType = toTypeKind(sym.accessed.info)
891891
val hostOwner = qual.tpe.typeSymbol.orElse(sym.owner)
892892
val hostClass = hostOwner.companionClass
893-
val staticfield = hostClass.info.findMember(sym.accessed.name, NoFlags, NoFlags, false)
894-
893+
val staticfield = hostClass.info.findMember(sym.accessed.name, NoFlags, NoFlags, false) orElse {
894+
if (!currentRun.compiles(hostOwner)) {
895+
// hostOwner was separately compiled -- the static field symbol needs to be recreated in hostClass
896+
import Flags._
897+
debuglog("recreating sym.accessed.name: " + sym.accessed.name)
898+
val objectfield = hostOwner.info.findMember(sym.accessed.name, NoFlags, NoFlags, false)
899+
val staticfield = hostClass.newVariable(newTermName(sym.accessed.name.toString), tree.pos, STATIC | SYNTHETIC | FINAL) setInfo objectfield.tpe
900+
staticfield.addAnnotation(definitions.StaticClass)
901+
hostClass.info.decls enter staticfield
902+
staticfield
903+
} else NoSymbol
904+
}
905+
895906
if (sym.isGetter) {
896907
ctx.bb.emit(LOAD_FIELD(staticfield, true) setHostClass hostClass, tree.pos)
897908
ctx

src/compiler/scala/tools/nsc/transform/CleanUp.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ abstract class CleanUp extends Transform with ast.TreeDSL {
586586
// create a static field in the companion class for this @static field
587587
val stfieldSym = linkedClass.newVariable(newTermName(name), tree.pos, STATIC | SYNTHETIC | FINAL) setInfo sym.tpe
588588
stfieldSym.addAnnotation(StaticClass)
589-
589+
590590
val names = classNames.getOrElseUpdate(linkedClass, linkedClass.info.decls.collect {
591591
case sym if sym.name.isTermName => sym.name
592592
} toSet)

test/files/run/static-annot/field.scala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ class Foo
2323
trait Check {
2424
def checkStatic(cls: Class[_]) {
2525
cls.getDeclaredFields.find(_.getName == "bar") match {
26-
case Some(f) => assert(Modifier.isStatic(f.getModifiers), "no static modifier")
27-
case None => assert(false, "no static field bar in class")
26+
case Some(f) =>
27+
assert(Modifier.isStatic(f.getModifiers), "no static modifier")
28+
case None =>
29+
assert(false, "no static field bar in class")
2830
}
2931
}
3032

@@ -170,13 +172,20 @@ object Foo7 {
170172
@static val bar = "string"
171173
}
172174
class AndHisFriend
175+
176+
object AndHisLonelyFriend {
177+
@static val bar = "another"
178+
}
173179
}
174180

175181

176182
object Test7 extends Check {
177183
def test() {
178184
checkStatic(classOf[Foo7.AndHisFriend])
179185
assert(Foo7.AndHisFriend.bar == "string")
186+
187+
checkStatic(Class.forName("Foo7$AndHisLonelyFriend"))
188+
assert(Foo7.AndHisLonelyFriend.bar == "another")
180189
}
181190
}
182191

test/files/run/t6236.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
353
2+
353

test/files/run/t6236/file_1.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
package p {
4+
object y {
5+
object x {
6+
@scala.annotation.static val foo: Int = 353
7+
}
8+
}
9+
}

test/files/run/t6236/file_2.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
3+
4+
object Test {
5+
def main(args: Array[String]): Unit = {
6+
println(p.y.x.foo)
7+
println(p.y.x.foo)
8+
}
9+
}
10+

0 commit comments

Comments
 (0)
0