Skip to content

Commit

Permalink
Support deprecatedName in arguments to ConstantAnnotations
Browse files Browse the repository at this point in the history
  • Loading branch information
lrytz committed Dec 3, 2020
1 parent 28d9c99 commit 1102f53
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/compiler/scala/tools/nsc/typechecker/Implicits.scala
Expand Up @@ -1823,8 +1823,9 @@ trait Implicits {
// check the message's syntax: should be a string literal that may contain occurrences of the string "${X}",
// where `X` refers to a type parameter of `sym`
def check(sym: Symbol): Option[String] =
sym.getAnnotation(clazz).flatMap(_.stringArg(0) match {
case Some(m) => new Message(sym, m, annotationName).validate
sym.getAnnotation(clazz).flatMap(a => a.stringArg(0).orElse(a.assocs.headOption) match {
case Some(m: String) => new Message(sym, m, annotationName).validate // temporary, until we make implicitAmbiguous also a ConstantAnnotation
case Some((_, LiteralAnnotArg(Constant(m: String)))) => new Message(sym, m, annotationName).validate
case None => Some(s"Missing argument `msg` on $annotationName annotation.")
})
}
Expand Down
9 changes: 7 additions & 2 deletions src/compiler/scala/tools/nsc/typechecker/Typers.scala
Expand Up @@ -4043,8 +4043,13 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper

val nvPairs = namedArgs map {
case arg @ NamedArg(Ident(name), rhs) =>
val sym = if (isJava) annScopeJava.lookup(name)
else findSymbol(typedFun.tpe.params)(_.name == name)
val sym =
if (isJava) annScopeJava.lookup(name)
else findSymbol(typedFun.tpe.params)(p => p.name == name || p.deprecatedParamName.contains(name).tap(r => if (r) {
val sinceVersion = p.deprecatedParamVersion.getOrElse("")
val since = if (sinceVersion.isEmpty) "" else s" since $sinceVersion"
context.deprecationWarning(arg.pos, p, s"the parameter name $name is deprecated$since: use `${p.name}` instead", sinceVersion)
}))
if (sym == NoSymbol) {
reportAnnotationError(UnknownAnnotationNameError(arg, name))
(nme.ERROR, None)
Expand Down
2 changes: 1 addition & 1 deletion src/library/scala/annotation/implicitNotFound.scala
Expand Up @@ -55,4 +55,4 @@ package scala.annotation
*
* @param msg The custom error message, must be a literal value.
*/
final class implicitNotFound(value: String) extends scala.annotation.ConstantAnnotation
final class implicitNotFound(@deprecatedName(name = "msg", since = "2.13.5") value: String) extends scala.annotation.ConstantAnnotation
11 changes: 7 additions & 4 deletions test/files/neg/names-defaults-neg-warn.check
@@ -1,14 +1,14 @@
names-defaults-neg-warn.scala:25: error: unknown parameter name: x
names-defaults-neg-warn.scala:28: error: unknown parameter name: x
Note that assignments in argument position are no longer allowed since Scala 2.13.
To express the assignment expression, wrap it in brackets, e.g., `{ x = ... }`.
f2(x = 1) // 2.12: deprecation warning, compiles. 2.13: error, no parameter named x
^
names-defaults-neg-warn.scala:36: error: unknown parameter name: x
names-defaults-neg-warn.scala:39: error: unknown parameter name: x
Note that assignments in argument position are no longer allowed since Scala 2.13.
To express the assignment expression, wrap it in brackets, e.g., `{ x = ... }`.
synchronized(x = 1) // deprecation warning in 2.12, error in 2.13
^
names-defaults-neg-warn.scala:45: error: unknown parameter name: x
names-defaults-neg-warn.scala:48: error: unknown parameter name: x
f2(x = 1) // 2.12, 2.13: error (no such parameter). no deprecation warning in 2.12, x is not a variable.
^
names-defaults-neg-warn.scala:13: warning: the parameter name s is deprecated: use x instead
Expand All @@ -17,5 +17,8 @@ names-defaults-neg-warn.scala:13: warning: the parameter name s is deprecated: u
names-defaults-neg-warn.scala:14: warning: the parameter name x is deprecated: use s instead
deprNam2.g(x = "dlkjf")
^
2 warnings
names-defaults-neg-warn.scala:17: warning: the parameter name x is deprecated since 1.2.3: use `y` instead
@annot(x = 1) def ann1 = 0
^
3 warnings
3 errors
7 changes: 6 additions & 1 deletion test/files/neg/names-defaults-neg-warn.scala
@@ -1,4 +1,4 @@
// scalac: -deprecation -Xfatal-warnings
// scalac: -deprecation -Werror
//
object Test extends App {
object deprNam2 {
Expand All @@ -13,6 +13,9 @@ object Test extends App {
deprNam2.f(s = "dlfkj")
deprNam2.g(x = "dlkjf")
deprNam2.g(s = new Object)

@annot(x = 1) def ann1 = 0
@annot(y = 1) def ann2 = 0
}

class C {
Expand Down Expand Up @@ -45,3 +48,5 @@ class C {
f2(x = 1) // 2.12, 2.13: error (no such parameter). no deprecation warning in 2.12, x is not a variable.
}
}

class annot(@deprecatedName("x", "1.2.3") y: Int) extends annotation.ConstantAnnotation

0 comments on commit 1102f53

Please sign in to comment.