Skip to content

Commit 97c2515

Browse files
authored
Merge pull request scala#10517 from kapunga/12845-use-java-deprecation
Always respect java.lang.Deprecated annotation
2 parents 85fa13d + 6280a5b commit 97c2515

File tree

11 files changed

+36
-8
lines changed

11 files changed

+36
-8
lines changed

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4180,7 +4180,11 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
41804180

41814181
if (hasError) ErroneousAnnotation
41824182
else if (unmappable) UnmappableAnnotation
4183-
else AnnotationInfo(annType, Nil, nvPairs.map(p => (p._1, p._2.get))).setOriginal(Apply(typedFun, namedArgs).setPos(ann.pos))
4183+
else {
4184+
if (annTypeSym == JavaDeprecatedAttr && !context.unit.isJava && settings.lintDeprecation)
4185+
context.warning(ann.pos, """Prefer the Scala annotation over Java's `@Deprecated` to provide a message and version: @deprecated("message", since = "MyLib 1.0")""", WarningCategory.LintDeprecation)
4186+
AnnotationInfo(annType, Nil, nvPairs.map(p => (p._1, p._2.get))).setOriginal(Apply(typedFun, namedArgs).setPos(ann.pos))
4187+
}
41844188
}
41854189
}
41864190
@inline def statically = {
@@ -4230,7 +4234,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
42304234
info.args.exists(treeInfo.isDefaultGetter)
42314235
}
42324236
if (annTypeSym == DeprecatedAttr && settings.lintDeprecation && argss.head.lengthIs < 2 && usesDefault)
4233-
context.warning(ann.pos, """Specify both message and version: @deprecated("message", since = "1.0")""", WarningCategory.LintDeprecation)
4237+
context.warning(ann.pos, """Specify both message and version: @deprecated("message", since = "MyLib 1.0")""", WarningCategory.LintDeprecation)
42344238
info
42354239
}
42364240
}

src/library/scala/deprecated.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ import scala.annotation.meta._
4040
* // warning: there were three deprecation warnings in total; re-run with -deprecation for details
4141
* }}}
4242
*
43+
* The Scala compiler also warns about using definitions annotated with [[java.lang.Deprecated]]. However it is
44+
* recommended to use the Scala `@deprecated` annotation in Scala code because it allows providing a deprecation message.
45+
*
4346
* '''`@deprecated` in the Scala language and its standard library'''<br/>
4447
*
4548
* A deprecated element of the Scala language or a definition in the Scala standard library will

src/reflect/scala/reflect/internal/Symbols.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
900900
@tailrec
901901
final def isStrictFP: Boolean = this != NoSymbol && !isDeferred && (hasAnnotation(ScalaStrictFPAttr) || originalOwner.isStrictFP)
902902
def isSerializable = info.baseClasses.exists(_ == SerializableClass)
903-
def isDeprecated = hasAnnotation(DeprecatedAttr) || (isJava && hasAnnotation(JavaDeprecatedAttr))
903+
def isDeprecated = hasAnnotation(DeprecatedAttr) || hasAnnotation(JavaDeprecatedAttr)
904904
def deprecationMessage = getAnnotation(DeprecatedAttr) flatMap (_ stringArg 0)
905905
def deprecationVersion = getAnnotation(DeprecatedAttr).flatMap(_.stringArg(1)) match {
906906
case v @ Some(_) => v

test/files/neg/deprecated.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
deprecated.scala:5: warning: Specify both message and version: @deprecated("message", since = "1.0")
1+
deprecated.scala:5: warning: Specify both message and version: @deprecated("message", since = "MyLib 1.0")
22
@deprecated def f = ???
33
^
4-
deprecated.scala:9: warning: Specify both message and version: @deprecated("message", since = "1.0")
4+
deprecated.scala:9: warning: Specify both message and version: @deprecated("message", since = "MyLib 1.0")
55
@deprecated("Don't use it."/*, forRemoval=true*/) def stale = ???
66
^
77
deprecated.scala:21: warning: method f in trait T is deprecated

test/files/neg/t12648.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
LogConfig_2.scala:7: warning: value MESSAGE_FORMAT_VERSION_CONFIG in class TopicConfig_1 is deprecated
22
val MessageFormatVersionPropX = TopicConfig_1.MESSAGE_FORMAT_VERSION_CONFIG
33
^
4-
LogConfig_2.scala:10: warning: Specify both message and version: @deprecated("message", since = "1.0")
4+
LogConfig_2.scala:10: warning: Specify both message and version: @deprecated("message", since = "MyLib 1.0")
55
@deprecated("3.0")
66
^
7-
LogConfig_2.scala:12: warning: Specify both message and version: @deprecated("message", since = "1.0")
7+
LogConfig_2.scala:12: warning: Specify both message and version: @deprecated("message", since = "MyLib 1.0")
88
@deprecated("3.0") @nowarn("cat=deprecation")
99
^
1010
LogConfig_2.scala:12: warning: @nowarn annotation does not suppress any warnings

test/files/neg/t12845.check

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
t12845.scala:4: warning: Prefer the Scala annotation over Java's `@Deprecated` to provide a message and version: @deprecated("message", since = "MyLib 1.0")
2+
@Deprecated def f = 0
3+
^
4+
t12845.scala:6: warning: method f in object O is deprecated
5+
class C { def g = O.f }
6+
^
7+
error: No warnings can be incurred under -Werror.
8+
2 warnings
9+
1 error

test/files/neg/t12845.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//> using options -Werror -deprecation -Xlint
2+
3+
object O {
4+
@Deprecated def f = 0
5+
}
6+
class C { def g = O.f }

test/files/run/t9529.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#partest java8
2+
warning: 1 deprecation; re-run with -deprecation for details
23
A: List()
34
B: List(@java.lang.Deprecated())
45
C: List(@anns.Ann_0(name=C, value=see))
@@ -16,6 +17,7 @@ u: List(@anns.Ann_0$Container(value=[@anns.Ann_0(name=u, value=you), @anns.Ann_0
1617
List(@anns.Ann_0$Container(value=[@anns.Ann_0(name=<init>, value=constructor), @anns.Ann_0(name=<init>, value=initializer)]))
1718

1819
#partest java11
20+
warning: 1 deprecation; re-run with -deprecation for details
1921
A: List()
2022
B: List(@java.lang.Deprecated(forRemoval=false, since=""))
2123
C: List(@anns.Ann_0(name="C", value="see"))
@@ -33,6 +35,7 @@ u: List(@anns.Ann_0$Container(value={@anns.Ann_0(name="u", value="you"), @anns.A
3335
List(@anns.Ann_0$Container(value={@anns.Ann_0(name="<init>", value="constructor"), @anns.Ann_0(name="<init>", value="initializer")}))
3436

3537
#partest java17
38+
warning: 1 deprecation; re-run with -deprecation for details
3639
A: List()
3740
B: List(@java.lang.Deprecated(forRemoval=false, since=""))
3841
C: List(@anns.Ann_0(name="C", value="see"))
@@ -50,6 +53,7 @@ u: List(@anns.Ann_0$Container({@anns.Ann_0(name="u", value="you"), @anns.Ann_0(n
5053
List(@anns.Ann_0$Container({@anns.Ann_0(name="<init>", value="constructor"), @anns.Ann_0(name="<init>", value="initializer")}))
5154

5255
#partest java20+
56+
warning: 1 deprecation; re-run with -deprecation for details
5357
A: List()
5458
B: List(@java.lang.Deprecated(forRemoval=false, since=""))
5559
C: List(@anns.Ann_0(name="C", value="see"))

test/files/run/t9644.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
warning: 3 deprecations; re-run with -deprecation for details

test/files/run/t9644b.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
warning: 3 deprecations; re-run with -deprecation for details

0 commit comments

Comments
 (0)