Skip to content

Commit

Permalink
Use deprecated since
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Jun 11, 2023
1 parent 5c32b00 commit 334a256
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1507,22 +1507,17 @@ abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {
protected def getScope(flags: JavaAccFlags): Scope =
if (flags.isStatic) staticScope else instanceScope

// Retain first matching AnnotationInfo, preserving order.
// Preserve order, but discard a previous matching attibute and keep the last, since runtime attrs include args.
private def addUniqueAnnotation(symbol: Symbol, annot: AnnotationInfo): symbol.type = {
def addIt(annots: List[AnnotationInfo], rest: List[AnnotationInfo]): List[AnnotationInfo] =
rest match {
case h :: t =>
if (h.atp.typeSymbol.initialize.isJavaDefined && h.matches(annot.atp.typeSymbol.initialize)) Nil
else addIt(annots, t) match {
case Nil => Nil
case loopy => h :: loopy
}
case Nil => annot :: Nil
val loopy = addIt(annots, t)
if (h.atp.typeSymbol.initialize.isJavaDefined && h.matches(annot.atp.typeSymbol.initialize)) loopy
else h :: loopy
case Nil => annots
}
addIt(annots = Nil, symbol.annotations) match {
case Nil => symbol
case augmented => symbol.setAnnotations(augmented)
}
symbol.setAnnotations(addIt(annot :: Nil, symbol.annotations))
}
}
object ClassfileParser {
Expand Down
5 changes: 4 additions & 1 deletion src/reflect/scala/reflect/internal/Symbols.scala
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,10 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
def isSerializable = info.baseClasses.exists(_ == SerializableClass)
def isDeprecated = hasAnnotation(DeprecatedAttr) || (isJava && hasAnnotation(JavaDeprecatedAttr))
def deprecationMessage = getAnnotation(DeprecatedAttr) flatMap (_ stringArg 0)
def deprecationVersion = getAnnotation(DeprecatedAttr) flatMap (_ stringArg 1)
def deprecationVersion = getAnnotation(DeprecatedAttr).flatMap(_.stringArg(1)) match {
case v @ Some(_) => v
case _ => getAnnotation(JavaDeprecatedAttr).flatMap(_.stringArg(0))
}
def deprecatedParamName = getAnnotation(DeprecatedNameAttr) flatMap (ann => ann.symbolArg(0).orElse(ann.stringArg(0).map(newTermName)).orElse(Some(nme.NO_NAME)))
def deprecatedParamVersion = getAnnotation(DeprecatedNameAttr) flatMap (_ stringArg 1)
def hasDeprecatedInheritanceAnnotation
Expand Down
12 changes: 12 additions & 0 deletions test/files/neg/t12799.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Test_2.scala:7: warning: class B in package example is deprecated (since 1.0): beeless
val deprecatedMethod = classOf[B].getMethod("a")
^
Test_2.scala:9: warning: method a in trait A is deprecated (since 0.42)
new B().a
^
Test_2.scala:9: warning: class B in package example is deprecated (since 1.0): beeless
new B().a
^
error: No warnings can be incurred under -Werror.
3 warnings
1 error
10 changes: 10 additions & 0 deletions test/files/neg/t12799/A.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// javaVersion: 9+

package example;

interface A {
@Deprecated(since="0.42")
default String a() {
return "a";
}
}
5 changes: 5 additions & 0 deletions test/files/neg/t12799/B_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

package example

@deprecated("beeless", since="1.0")
class B extends A
11 changes: 11 additions & 0 deletions test/files/neg/t12799/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

// scalac: -Werror -Xlint

import example._

object Test extends App {
val deprecatedMethod = classOf[B].getMethod("a")
assert(deprecatedMethod.isAnnotationPresent(classOf[Deprecated]))
new B().a
}
//java.lang.annotation.AnnotationFormatError: Duplicate annotation for class: interface java.lang.Deprecated: @java.lang.Deprecated(forRemoval=false, since="")
3 changes: 3 additions & 0 deletions test/files/run/t12799.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Test_2.scala:9: warning: method a in trait A is deprecated
new B().a
^
3 changes: 3 additions & 0 deletions test/files/run/t12799/Test_2.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@

// scalac: -Xlint

import example._

object Test extends App {
val deprecatedMethod = classOf[B].getMethod("a")
assert(deprecatedMethod.isAnnotationPresent(classOf[Deprecated]))
new B().a
}
//java.lang.annotation.AnnotationFormatError: Duplicate annotation for class: interface java.lang.Deprecated: @java.lang.Deprecated(forRemoval=false, since="")

0 comments on commit 334a256

Please sign in to comment.