aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/dotty/tools/backend/jvm/DottyBackendInterface.scala3
-rw-r--r--src/dotty/tools/dotc/Compiler.scala1
-rw-r--r--src/dotty/tools/dotc/ast/tpd.scala4
-rw-r--r--src/dotty/tools/dotc/core/Definitions.scala2
-rw-r--r--src/dotty/tools/dotc/core/SymDenotations.scala4
-rw-r--r--src/dotty/tools/dotc/transform/CheckStatic.scala83
-rw-r--r--src/scala/annotation/static.scala13
7 files changed, 108 insertions, 2 deletions
diff --git a/src/dotty/tools/backend/jvm/DottyBackendInterface.scala b/src/dotty/tools/backend/jvm/DottyBackendInterface.scala
index 5776cc8e2..ef8e4997f 100644
--- a/src/dotty/tools/backend/jvm/DottyBackendInterface.scala
+++ b/src/dotty/tools/backend/jvm/DottyBackendInterface.scala
@@ -601,7 +601,8 @@ class DottyBackendInterface(outputDirectory: AbstractFile)(implicit ctx: Context
isPrivate //|| (sym.isPrimaryConstructor && sym.owner.isTopLevelModuleClass)
def isFinal: Boolean = sym is Flags.Final
- def isStaticMember: Boolean = (sym ne NoSymbol) && ((sym is Flags.JavaStatic) || (owner is Flags.ImplClass))
+ def isStaticMember: Boolean = (sym ne NoSymbol) &&
+ ((sym is Flags.JavaStatic) || (owner is Flags.ImplClass) || toDenot(sym).hasAnnotation(ctx.definitions.ScalaStaticAnnot))
// guard against no sumbol cause this code is executed to select which call type(static\dynamic) to use to call array.clone
def isBottomClass: Boolean = (sym ne defn.NullClass) && (sym ne defn.NothingClass)
diff --git a/src/dotty/tools/dotc/Compiler.scala b/src/dotty/tools/dotc/Compiler.scala
index be4477ee2..5d8010ca2 100644
--- a/src/dotty/tools/dotc/Compiler.scala
+++ b/src/dotty/tools/dotc/Compiler.scala
@@ -44,6 +44,7 @@ class Compiler {
List(new FirstTransform,
new CheckReentrant),
List(new RefChecks,
+ new CheckStatic,
new ElimRepeated,
new NormalizeFlags,
new ExtensionMethods,
diff --git a/src/dotty/tools/dotc/ast/tpd.scala b/src/dotty/tools/dotc/ast/tpd.scala
index 7cd469d7a..81e1f7751 100644
--- a/src/dotty/tools/dotc/ast/tpd.scala
+++ b/src/dotty/tools/dotc/ast/tpd.scala
@@ -320,7 +320,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
case _ =>
false
}
- try test || tp.symbol.is(JavaStatic)
+ try test || tp.symbol.is(JavaStatic) || tp.symbol.hasAnnotation(defn.ScalaStaticAnnot)
catch { // See remark in SymDenotations#accessWithin
case ex: NotDefinedHere => test(ctx.addMode(Mode.FutureDefsOK))
}
@@ -337,6 +337,8 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
else if (prefixIsElidable(tp)) Ident(tp)
else if (tp.symbol.is(Module) && ctx.owner.isContainedIn(tp.symbol.moduleClass))
followOuterLinks(This(tp.symbol.moduleClass.asClass))
+ else if (tp.symbol hasAnnotation defn.ScalaStaticAnnot)
+ Ident(tp)
else tp.prefix match {
case pre: SingletonType => followOuterLinks(singleton(pre)).select(tp)
case pre => SelectFromTypeTree(TypeTree(pre), tp)
diff --git a/src/dotty/tools/dotc/core/Definitions.scala b/src/dotty/tools/dotc/core/Definitions.scala
index f16f25b23..6f8a8f837 100644
--- a/src/dotty/tools/dotc/core/Definitions.scala
+++ b/src/dotty/tools/dotc/core/Definitions.scala
@@ -462,6 +462,8 @@ class Definitions {
def ScalaLongSignatureAnnot(implicit ctx: Context) = ScalaLongSignatureAnnotType.symbol.asClass
lazy val ScalaStrictFPAnnotType = ctx.requiredClassRef("scala.annotation.strictfp")
def ScalaStrictFPAnnot(implicit ctx: Context) = ScalaStrictFPAnnotType.symbol.asClass
+ lazy val ScalaStaticAnnotType = ctx.requiredClassRef("scala.annotation.static")
+ def ScalaStaticAnnot(implicit ctx: Context) = ScalaStaticAnnotType.symbol.asClass
lazy val SerialVersionUIDAnnotType = ctx.requiredClassRef("scala.SerialVersionUID")
def SerialVersionUIDAnnot(implicit ctx: Context) = SerialVersionUIDAnnotType.symbol.asClass
lazy val TASTYSignatureAnnotType = ctx.requiredClassRef("scala.annotation.internal.TASTYSignature")
diff --git a/src/dotty/tools/dotc/core/SymDenotations.scala b/src/dotty/tools/dotc/core/SymDenotations.scala
index bde8cc10a..cc36ddcfa 100644
--- a/src/dotty/tools/dotc/core/SymDenotations.scala
+++ b/src/dotty/tools/dotc/core/SymDenotations.scala
@@ -285,6 +285,10 @@ object SymDenotations {
final def addAnnotation(annot: Annotation): Unit =
annotations = annot :: myAnnotations
+ /** Add given annotation to the annotations of this denotation */
+ final def addAnnotation(annot: ClassSymbol)(implicit ctx: Context): Unit =
+ addAnnotation(ConcreteAnnotation(tpd.ref(annot)))
+
/** Remove annotation with given class from this denotation */
final def removeAnnotation(cls: Symbol)(implicit ctx: Context): Unit =
annotations = myAnnotations.filterNot(_ matches cls)
diff --git a/src/dotty/tools/dotc/transform/CheckStatic.scala b/src/dotty/tools/dotc/transform/CheckStatic.scala
new file mode 100644
index 000000000..264c20eb2
--- /dev/null
+++ b/src/dotty/tools/dotc/transform/CheckStatic.scala
@@ -0,0 +1,83 @@
+package dotty.tools.dotc
+package transform
+
+import core._
+import Names._
+import StdNames.nme
+import Types._
+import dotty.tools.dotc.transform.TreeTransforms.{AnnotationTransformer, TransformerInfo, MiniPhaseTransform, TreeTransformer}
+import ast.Trees._
+import Flags._
+import Contexts.Context
+import Symbols._
+import Constants._
+import Denotations._, SymDenotations._
+import Decorators.StringInterpolators
+import dotty.tools.dotc.ast.tpd
+import dotty.tools.dotc.core.Annotations.ConcreteAnnotation
+import scala.collection.mutable
+import DenotTransformers._
+import Names.Name
+import NameOps._
+import Decorators._
+import TypeUtils._
+
+/** A transformer that check that requirements of Static fields\methods are implemented:
+ * 1. Only objects can have members annotated with `@static`
+ * 2. The fields annotated with `@static` should preceed any non-`@static` fields.
+ * This ensures that we do not introduce surprises for users in initialization order.
+ * 3. If a member `foo` of an `object C` is annotated with `@static`,
+ * the companion class `C` is not allowed to define term members with name `foo`.
+ * 4. If a member `foo` of an `object C` is annotated with `@static`, the companion class `C`
+ * is not allowed to inherit classes that define a term member with name `foo`.
+ * 5. Only `@static` methods and vals are supported in companions of traits.
+ * Java8 supports those, but not vars, and JavaScript does not have interfaces at all.
+ */
+class CheckStatic extends MiniPhaseTransform { thisTransformer =>
+ import ast.tpd._
+
+ override def phaseName = "elimRepeated"
+
+
+ def check(tree: tpd.DefTree)(implicit ctx: Context) = {
+
+ }
+
+ override def transformTemplate(tree: tpd.Template)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = {
+ val defns = tree.body.collect{case t: ValOrDefDef => t}
+ var hadNonStaticField = false
+ for(defn <- defns) {
+ if (defn.symbol.hasAnnotation(ctx.definitions.ScalaStaticAnnot)) {
+ if(!ctx.owner.is(Module)) {
+ ctx.error("@static fields are only allowed inside objects", defn.pos)
+ }
+
+ if (defn.isInstanceOf[ValDef] && hadNonStaticField) {
+ ctx.error("@static fields should preceed non-static ones", defn.pos)
+ }
+
+ val companion = ctx.owner.companionClass
+ if (!companion.exists) {
+ ctx.error("object that conatin @static members should have companion class", defn.pos)
+ }
+
+ val clashes = companion.asClass.membersNamed(defn.name)
+ if (clashes.exists) {
+ ctx.error("companion classes cannot define members with same name as @static member", defn.pos)
+ }
+
+ if (defn.symbol.is(Flags.Mutable) && companion.is(Flags.Trait)) {
+ ctx.error("Companions of traits cannot define mutable @static fields")
+ }
+ } else hadNonStaticField = hadNonStaticField || defn.isInstanceOf[ValDef]
+
+ }
+ tree
+ }
+
+ override def transformSelect(tree: tpd.Select)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = {
+ if (tree.symbol.hasAnnotation(defn.ScalaStaticAnnot))
+ ref(tree.symbol)
+ else tree
+ }
+}
diff --git a/src/scala/annotation/static.scala b/src/scala/annotation/static.scala
new file mode 100644
index 000000000..fd683b467
--- /dev/null
+++ b/src/scala/annotation/static.scala
@@ -0,0 +1,13 @@
+package scala.annotation
+
+import scala.annotation.meta._
+
+/** https://github.com/scala/scala.github.com/pull/491 */
+
+@field
+@getter
+@beanGetter
+@beanGetter
+@param
+@setter
+final class static extends StaticAnnotation