From 227239018b38ab7218ee6b30493c9c8e1836c8c9 Mon Sep 17 00:00:00 2001 From: Aleksandar Date: Thu, 19 Jul 2012 14:33:07 +0200 Subject: WIP add private/lazy checks and a few tests. --- .../scala/tools/nsc/transform/CleanUp.scala | 11 +++++++ test/files/neg/static-annot.check | 11 ++++++- test/files/neg/static-annot.scala | 14 ++++++++ test/files/run/static-annot/field.scala | 38 ++++++++++++++++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/compiler/scala/tools/nsc/transform/CleanUp.scala b/src/compiler/scala/tools/nsc/transform/CleanUp.scala index 1d8d58ccf7..e672f1914a 100644 --- a/src/compiler/scala/tools/nsc/transform/CleanUp.scala +++ b/src/compiler/scala/tools/nsc/transform/CleanUp.scala @@ -566,12 +566,23 @@ abstract class CleanUp extends Transform with ast.TreeDSL { val owner = sym.owner val staticBeforeLifting = atPhase(currentRun.erasurePhase) { owner.isStatic } + val isPrivate = atPhase(currentRun.typerPhase) { sym.getter(owner).hasFlag(PRIVATE) } + val isProtected = atPhase(currentRun.typerPhase) { sym.getter(owner).hasFlag(PROTECTED) } + val isLazy = atPhase(currentRun.typerPhase) { sym.getter(owner).hasFlag(LAZY) } if (!owner.isModuleClass || !staticBeforeLifting) { if (!sym.isSynthetic) { reporter.error(tree.pos, "Only members of top-level objects and their nested objects can be annotated with @static.") tree.symbol.removeAnnotation(StaticClass) } super.transform(tree) + } else if (isPrivate || isProtected) { + reporter.error(tree.pos, "The @static annotation is only allowed on public members.") + tree.symbol.removeAnnotation(StaticClass) + super.transform(tree) + } else if (isLazy) { + reporter.error(tree.pos, "The @static annotation is not allowed on lazy members.") + tree.symbol.removeAnnotation(StaticClass) + super.transform(tree) } else if (owner.isModuleClass) { val linkedClass = owner.companionClass match { case NoSymbol => diff --git a/test/files/neg/static-annot.check b/test/files/neg/static-annot.check index 80aebcd406..66efebdcee 100644 --- a/test/files/neg/static-annot.check +++ b/test/files/neg/static-annot.check @@ -4,7 +4,16 @@ static-annot.scala:8: error: Only members of top-level objects and their nested static-annot.scala:27: error: @static annotated field bar has the same name as a member of class Conflicting @static val bar = 1 ^ +static-annot.scala:37: error: The @static annotation is only allowed on public members. + @static private val bar = 1 + ^ +static-annot.scala:38: error: The @static annotation is only allowed on public members. + @static private val baz = 2 + ^ +static-annot.scala:39: error: The @static annotation is not allowed on lazy members. + @static lazy val bam = 3 + ^ static-annot.scala:14: error: Only members of top-level objects and their nested objects can be annotated with @static. @static val blah = 2 ^ -three errors found \ No newline at end of file +6 errors found \ No newline at end of file diff --git a/test/files/neg/static-annot.scala b/test/files/neg/static-annot.scala index b8c4651076..c6c626d42b 100644 --- a/test/files/neg/static-annot.scala +++ b/test/files/neg/static-annot.scala @@ -31,3 +31,17 @@ object Conflicting { class Conflicting { val bar = 45 } + + +object PrivateProtectedLazy { + @static private val bar = 1 + @static private val baz = 2 + @static lazy val bam = 3 +} + + +class PrivateProtectedLazy { + println(PrivateProtectedLazy.bar) + println(PrivateProtectedLazy.baz) + println(PrivateProtectedLazy.bam) +} diff --git a/test/files/run/static-annot/field.scala b/test/files/run/static-annot/field.scala index 0677082cf6..a7d8158321 100644 --- a/test/files/run/static-annot/field.scala +++ b/test/files/run/static-annot/field.scala @@ -182,6 +182,43 @@ object Test7 extends Check { +/* TEST 8 */ + +object Foo8 { + @static val field = 7 + + val function: () => Int = () => { + field + 1 + } + + val anon = new Runnable { + def run() { + assert(field == 7, "runnable asserting field is 7") + } + } + + @static var mutable = 10 + + val mutation: () => Unit = () => { + mutable += 1 + } +} + +object Test8 { + def test() { + assert(Foo8.function() == 8, "function must return 8") + Foo8.anon.run() + assert(Foo8.mutable == 10, "mutable is 10") + Foo8.mutation() + assert(Foo8.mutable == 11, "mutable is 11") + Foo8.mutation() + assert(Foo8.mutable == 12, "mutable is 12") + } +} + + + + /* main */ object Test { @@ -194,6 +231,7 @@ object Test { Test5.test() Test6.test() Test7.test() + Test8.test() } } -- cgit v1.2.3