-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix parsing Java annotation values #11809
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
Conversation
@smarter It there a way to test the annotation values read by the compiler? |
You could write a macro that calls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes to the parser look OK to me, but it would be nice to have a test.
|
7342196
to
029ec83
Compare
@noti0na1 What was the code where you saw that? Which annotation was it? |
@nicolasstucki Some of my code break the typing of annotations in Java. During debugging, I noticed the arguments from all annotations (in Java file) are not parsed correctly. If you print out the tree, you may see the wrong arguments? |
@noti0na1 could you provide a code example showing what happened and what you expected. That would form the basis for writing a test case for this - it's a little hard to understand by reverse engineering your patch here. |
Does this affect runtime annotations as well as compile-time-only annotations? A runtime annotation might be easier to construct a test case around. |
@SethTisue this PR changes the JavaParser so it affects how the scala compiler (and therefore macros) sees Java annotations coming from .java file, but it has no impact on what annotations are kept at runtime. |
An example would be any Java class with some annotations: class Test {
@SuppressWarnings("unchecked")
void f() {}
} I found this bug because I was trying to print some trees in the compiler, and I noticed the parsed arguments were wrong. The compiler doesn't use the annotation arguments in Java files currently, so it is difficult to write a test. |
@noti0na1 here's a macro that will give you annotations of the first method of mcr.scala package stuff
import scala.quoted.*
inline def mcr: Any = ${ mcrImpl }
def mcrImpl(using Quotes): Expr[Any] =
import quotes.reflect.*
println(Symbol.requiredClass("stuff.Bar").declaredMethods.head.annotations)
val tree = '{()}
tree Test.scala package stuff
trait Bar:
@SuppressWarnings(Array("unchecked"))
def f(x: Int) = ???
@main def Test = mcr It successfully obtains annotations for types from the Scala sources. However, for a class coming from a Java source, it doesn't find any annotations. Maybe you can come up with something based on the example above. |
029ec83
to
df97004
Compare
I add a test using the macro created by @anatoliykmetyuk , we can see from the output the arguments are correctly parsed now ("a", "b", "c" and "d"). @smarter Could you review the PR again? Thanks. |
The annotation arguments were not parsed correctly; since the literal
LiteralT
returns was always the annotaion id string.