Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ConstantAnnotation when extracting literal annotation arguments #9463

Merged
merged 1 commit into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/reflect/scala/reflect/internal/AnnotationInfos.scala
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ trait AnnotationInfos extends api.Annotations { self: SymbolTable =>
def stringArg(index: Int) = constantAtIndex(index) map (_.stringValue)
def intArg(index: Int) = constantAtIndex(index) map (_.intValue)
def booleanArg(index: Int) = constantAtIndex(index) map (_.booleanValue)
def symbolArg(index: Int) = argAtIndex(index) collect {
def symbolArg(index: Int) = argAtIndex(args, index) collect {
case Apply(fun, Literal(str) :: Nil) if fun.symbol == definitions.Symbol_apply =>
newTermName(str.stringValue)
}
Expand All @@ -283,10 +283,14 @@ trait AnnotationInfos extends api.Annotations { self: SymbolTable =>
// expression, there is a fair chance they will turn up here not as
// Literal(const) but some arbitrary AST.
def constantAtIndex(index: Int): Option[Constant] =
argAtIndex(index) collect { case Literal(x) => x }

def argAtIndex(index: Int): Option[Tree] =
if (index < args.size) Some(args(index)) else None
if (args.nonEmpty) argAtIndex(args, index) collect {
case Literal(x) => x
} else if (assocs.nonEmpty) argAtIndex(assocs, index) collect {
case (_, LiteralAnnotArg(const)) => const
} else None

def argAtIndex[T](l: List[T], index: Int): Option[T] =
if (index < l.size) Some(l(index)) else None

def transformArgs(f: List[Tree] => List[Tree]): AnnotationInfo =
new CompleteAnnotationInfo(atp, f(args), assocs)
Expand Down
23 changes: 23 additions & 0 deletions test/files/run/constAnnArgs.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

scala> @deprecated(message = "x", since = "y") def f = 1; f
^
warning: method f is deprecated (since y): x
def f: Int
val res0: Int = 1

scala> :pa -raw << JUMP!
// Entering paste mode (JUMP! to finish)

package scala { class deprecated(message: String = "", since: String = "") extends scala.annotation.ConstantAnnotation }
JUMP!

// Exiting paste mode, now interpreting.


scala> @deprecated(message = "x", since = "y") def g = 1; g
^
warning: method g is deprecated (since y): x
def g: Int
val res1: Int = 1

scala> :quit
12 changes: 12 additions & 0 deletions test/files/run/constAnnArgs.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import scala.tools.partest.ReplTest

object Test extends ReplTest {
override def extraSettings = "-deprecation"
def code =
"""@deprecated(message = "x", since = "y") def f = 1; f
|:pa -raw << JUMP!
|package scala { class deprecated(message: String = "", since: String = "") extends scala.annotation.ConstantAnnotation }
|JUMP!
|@deprecated(message = "x", since = "y") def g = 1; g
|""".stripMargin
}