summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLukas Rytz <lukas.rytz@epfl.ch>2010-08-03 20:03:08 +0000
committerLukas Rytz <lukas.rytz@epfl.ch>2010-08-03 20:03:08 +0000
commitdb99926628a2b636a548049c1204fee78c77b9bb (patch)
tree88122a98135c3c2165ad6364819a66854d61ec16 /src
parent958fb1c6f48aeecf5fc56ad9debbfad8d6d0fc66 (diff)
downloadscala-db99926628a2b636a548049c1204fee78c77b9bb.tar.gz
scala-db99926628a2b636a548049c1204fee78c77b9bb.tar.bz2
scala-db99926628a2b636a548049c1204fee78c77b9bb.zip
added @deprecatedName annotation, allowing to d...
added @deprecatedName annotation, allowing to deprecate parameter names. review by prokopec.
Diffstat (limited to 'src')
-rw-r--r--src/compiler/scala/tools/nsc/symtab/Definitions.scala2
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/Infer.scala2
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala31
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/Typers.scala10
-rw-r--r--src/library/scala/deprecatedName.scala13
5 files changed, 56 insertions, 2 deletions
diff --git a/src/compiler/scala/tools/nsc/symtab/Definitions.scala b/src/compiler/scala/tools/nsc/symtab/Definitions.scala
index 1684b5f071..667bdd5386 100644
--- a/src/compiler/scala/tools/nsc/symtab/Definitions.scala
+++ b/src/compiler/scala/tools/nsc/symtab/Definitions.scala
@@ -136,6 +136,7 @@ trait Definitions extends reflect.generic.StandardDefinitions {
lazy val ScalaObjectClass = getClass("scala.ScalaObject")
lazy val PartialFunctionClass = getClass("scala.PartialFunction")
lazy val SymbolClass = getClass("scala.Symbol")
+ lazy val Symbol_apply = getMember(SymbolClass.companionModule, nme.apply)
lazy val StringClass = getClass(sn.String)
lazy val ClassClass = getClass(sn.Class)
def Class_getMethod = getMember(ClassClass, nme.getMethod_)
@@ -445,6 +446,7 @@ trait Definitions extends reflect.generic.StandardDefinitions {
// special attributes
lazy val SerializableAttr: Symbol = getClass("scala.serializable")
lazy val DeprecatedAttr: Symbol = getClass("scala.deprecated")
+ lazy val DeprecatedNameAttr: Symbol = getClass("scala.deprecatedName")
lazy val MigrationAnnotationClass: Symbol = getClass("scala.annotation.migration")
lazy val BeanPropertyAttr: Symbol = getClass(sn.BeanProperty)
lazy val BooleanBeanPropertyAttr: Symbol = getClass(sn.BooleanBeanProperty)
diff --git a/src/compiler/scala/tools/nsc/typechecker/Infer.scala b/src/compiler/scala/tools/nsc/typechecker/Infer.scala
index ed5f3b0e9a..71c4353834 100644
--- a/src/compiler/scala/tools/nsc/typechecker/Infer.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/Infer.scala
@@ -694,7 +694,7 @@ trait Infer {
val argtpes1 = argtpes map {
case NamedType(name, tp) => // a named argument
var res = tp
- val pos = params.indexWhere(p => p.name == name && !p.hasFlag(SYNTHETIC))
+ val pos = params.indexWhere(p => (p.name == name || deprecatedName(p) == Some(name)) && !p.hasFlag(SYNTHETIC))
if (pos == -1) {
if (positionalAllowed) { // treat assignment as positional argument
argPos(index) = index
diff --git a/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala b/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala
index 91fe113019..d783cf9c9f 100644
--- a/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala
@@ -410,7 +410,10 @@ trait NamesDefaults { self: Analyzer =>
var positionalAllowed = true
val namelessArgs = for ((arg, index) <- (args.zipWithIndex)) yield arg match {
case a @ AssignOrNamedArg(Ident(name), rhs) =>
- val pos = params.indexWhere(p => p.name == name && !p.hasFlag(SYNTHETIC))
+ val (pos, newName) = paramPos(params, name)
+ newName.foreach(n => {
+ typer.context.unit.deprecationWarning(arg.pos, "the parameter name "+ name +" has been deprecated. Use "+ n +" instead.")
+ })
if (pos == -1) {
if (positionalAllowed) {
argPos(index) = index
@@ -482,4 +485,30 @@ trait NamesDefaults { self: Analyzer =>
res = context.lookup(clazz.name.toTermName, clazz.owner)
res
}
+
+ /**
+ * Returns
+ * - the position of the parameter named `name`
+ * - optionally, if `name` is @deprecatedName, the new name
+ */
+ def paramPos(params: List[Symbol], name: Name): (Int, Option[Name]) = {
+ var i = 0
+ var rest = params
+ while (!rest.isEmpty) {
+ val p = rest.head
+ if (!p.hasFlag(SYNTHETIC)) {
+ if (p.name == name) return (i, None)
+ if (deprecatedName(p) == Some(name)) return (i, Some(p.name))
+ }
+ i += 1
+ rest = rest.tail
+ }
+ (-1, None)
+ }
+
+ def deprecatedName(sym: Symbol): Option[Name] =
+ sym.getAnnotation(DeprecatedNameAttr).map(ann => (ann.args(0): @unchecked) match {
+ case Apply(fun, Literal(str) :: Nil) if (fun.symbol == Symbol_apply) =>
+ newTermName(str.stringValue)
+ })
}
diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala
index 043c41fc10..770daccef0 100644
--- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala
@@ -1758,6 +1758,16 @@ trait Typers { self: Analyzer =>
meth.paramss.exists(ps => ps.exists(_.hasFlag(DEFAULTPARAM)) && isRepeatedParamType(ps.last.tpe)))
error(meth.pos, "a parameter section with a `*'-parameter is not allowed to have default arguments")
+ if (phase.id <= currentRun.typerPhase.id) {
+ val allParams = meth.paramss.flatten
+ for (p <- allParams) {
+ deprecatedName(p).foreach(n => {
+ if (allParams.exists(p1 => p1.name == n || (p != p1 && deprecatedName(p1) == Some(n))))
+ error(p.pos, "deprecated parameter name "+ n +" has to be distinct from any other parameter name (deprecated or not).")
+ })
+ }
+ }
+
checkMethodStructuralCompatible(meth)
treeCopy.DefDef(ddef, typedMods, ddef.name, tparams1, vparamss1, tpt1, rhs1) setType NoType
diff --git a/src/library/scala/deprecatedName.scala b/src/library/scala/deprecatedName.scala
new file mode 100644
index 0000000000..30d0ae457a
--- /dev/null
+++ b/src/library/scala/deprecatedName.scala
@@ -0,0 +1,13 @@
+package scala
+
+import annotation.target._
+
+/**
+ * An annotation that designates the name of the parameter to which it is
+ * applied as deprecated. Using that name in a named argument generates
+ * a deprecation warning.
+ *
+ * @since 2.8.1
+ */
+@param
+class deprecatedName(name: Symbol) extends StaticAnnotation