diff options
-rw-r--r-- | src/compiler/scala/tools/nsc/backend/icode/GenICode.scala | 10 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/transform/CleanUp.scala | 1 | ||||
-rw-r--r-- | test/files/neg/static-annot.scala | 6 | ||||
-rw-r--r-- | test/files/run/static-annot-repl.check | 32 | ||||
-rw-r--r-- | test/files/run/static-annot-repl.scala | 22 |
5 files changed, 68 insertions, 3 deletions
diff --git a/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala b/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala index a480429026..1b89aa5560 100644 --- a/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala +++ b/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala @@ -880,12 +880,16 @@ abstract class GenICode extends SubComponent { case app @ Apply(fun @ Select(qual, _), args) if !ctx.method.symbol.isStaticConstructor - && fun.symbol.isAccessor && fun.symbol.accessed.hasStaticAnnotation => + && fun.symbol.isAccessor && fun.symbol.accessed.hasStaticAnnotation + && qual.tpe.typeSymbol.orElse(fun.symbol.owner).companionClass != NoSymbol => // bypass the accessor to the companion object and load the static field directly - // the only place were this bypass is not done, is the static intializer for the static field itself + // this bypass is not done: + // - if the static intializer for the static field itself + // - if there is no companion class of the object owner - this happens in the REPL val sym = fun.symbol generatedType = toTypeKind(sym.accessed.info) - val hostClass = qual.tpe.typeSymbol.orElse(sym.owner).companionClass + val hostOwner = qual.tpe.typeSymbol.orElse(sym.owner) + val hostClass = hostOwner.companionClass val staticfield = hostClass.info.findMember(sym.accessed.name, NoFlags, NoFlags, false) if (sym.isGetter) { diff --git a/src/compiler/scala/tools/nsc/transform/CleanUp.scala b/src/compiler/scala/tools/nsc/transform/CleanUp.scala index 436867257a..4258ee8a88 100644 --- a/src/compiler/scala/tools/nsc/transform/CleanUp.scala +++ b/src/compiler/scala/tools/nsc/transform/CleanUp.scala @@ -768,6 +768,7 @@ abstract class CleanUp extends Transform with ast.TreeDSL { // add field definition to new defs newStaticMembers append stfieldDef newStaticInits append stfieldInit + case _ => // ignore @static on other members } } diff --git a/test/files/neg/static-annot.scala b/test/files/neg/static-annot.scala index c6c626d42b..c0b5ed30d8 100644 --- a/test/files/neg/static-annot.scala +++ b/test/files/neg/static-annot.scala @@ -45,3 +45,9 @@ class PrivateProtectedLazy { println(PrivateProtectedLazy.baz) println(PrivateProtectedLazy.bam) } + + +class StaticDef { + // this should not crash the compiler + @static def x = 42 +} diff --git a/test/files/run/static-annot-repl.check b/test/files/run/static-annot-repl.check new file mode 100644 index 0000000000..d1029a9809 --- /dev/null +++ b/test/files/run/static-annot-repl.check @@ -0,0 +1,32 @@ +Type in expressions to have them evaluated. +Type :help for more information. + +scala> + +scala> import annotation.static +import annotation.static + +scala> @static var x1 = 42 +x1: Int = 42 + +scala> @static val x2 = 43 +x2: Int = 43 + +scala> @static def x3 = 44 +x3: Int + +scala> x1 +res0: Int = 42 + +scala> x2 +res1: Int = 43 + +scala> x3 +res2: Int = 44 + +scala> class Test { + @static def x = 42 +} +defined class Test + +scala>
\ No newline at end of file diff --git a/test/files/run/static-annot-repl.scala b/test/files/run/static-annot-repl.scala new file mode 100644 index 0000000000..1d2e9b2d7e --- /dev/null +++ b/test/files/run/static-annot-repl.scala @@ -0,0 +1,22 @@ + + + +import scala.tools.partest.ReplTest + + + +object Test extends ReplTest { + def code = """ +import annotation.static +@static var x1 = 42 +@static val x2 = 43 +@static def x3 = 44 +x1 +x2 +x3 +class Test { + @static def x = 42 +} +""" + +} |