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 for parsing annotation arguments from java #11117

Merged
merged 4 commits into from Feb 3, 2021
Merged

Support for parsing annotation arguments from java #11117

merged 4 commits into from Feb 3, 2021

Conversation

Kordyjan
Copy link
Contributor

No description provided.

@smarter
Copy link
Member

smarter commented Jan 18, 2021

I won't have time to review this PR this week, @liufengyun : do you think you could have a look?

@liufengyun
Copy link
Contributor

I won't have time to review this PR this week, @liufengyun : do you think you could have a look?

No problem, I'll do it.

@smarter
Copy link
Member

smarter commented Jan 18, 2021

Some of the code in this PR appears to be adapted from Scala 2 code in https://github.com/scala/scala/blob/2.13.x/src/compiler/scala/tools/nsc/javac/JavaParsers.scala (the relevant scala 2 PRs are scala/scala#8781 and scala/scala#8982). This should be indicated in the corresponding commit messages for proper attribution.

@Kordyjan
Copy link
Contributor Author

Attribution added and ported work squashed into a single commit.

@@ -81,6 +81,12 @@ object Typer {
*/
private val DroppedEmptyArgs = new Property.Key[Unit]


/** Marker context property that indicates that typer is resolving types for arguments of
* an annotation defined in Java. This means that T can appear in positions where array[T] is expected.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: array[T] -> Array[T]?

The text is not easy to understand. Maybe add a small code example?

val arg1 = fallback match {
case Some(f) => tryAlternatively { typed(tree.arg, pt) } { f }
case _ => typed(tree.arg, pt)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe merge this pattern match with the one above, to make the logic more clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done it that way because I wanted to avoid repeating typed(tree.arg, pt) three times, as it may be a source of errors when the logic would be changed. On the other hand, merging those two patterns is much more readable.

}
if (c.isJava) c.fresh.setProperty(JavaAnnotationArg, ())
else c
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If c.isJava implies JavaAnnotationArg, do we still need the property JavaAnnotationArg?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My logic here was that JavaAnnotationArg implies that c.isJava is true and that we are currently typechecking annotation arguments.
If it is guaranteed that isJava being true inside of typedNamedArg implies that we are typechecking annotation arguments, then indeed it may be redundant. But I'm not sure if it is guaranteed now and it stays guaranteed in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I'm not sure if it is guaranteed now and it stays guaranteed in the future.

I think it's safe to assume this if you leave a comment explaining the assumption in the code, we can always adapt the code if Java somehow changes, but I wouldn't worry too much about it.

* A port of fixes from scala/scala#8781 with improvements.
* Some of improvements come from scala/scala#8982.
* There are small changes to typer to allow for single elements of any
  type T be used where array of T is expected inside of java annotations
  arguments as it is correct in java and is used in 2 files in scala
  standard library.
val arg1 = typed(tree.arg, pt)
val arg1 = if (ctx.property(JavaAnnotationArg).isDefined) {
pt match {
case AppliedType(a, typ :: Nil) if (a.isRef(defn.ArrayClass)) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about reducing the outer if/else by moving the condition to the guard here?

Copy link
Contributor

@liufengyun liufengyun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

compiler/src/dotty/tools/dotc/typer/Typer.scala Outdated Show resolved Hide resolved
Co-authored-by: Fengyun Liu <liu@fengy.me>
@Kordyjan Kordyjan merged commit a113884 into scala:master Feb 3, 2021
@Kordyjan Kordyjan deleted the java-annotations branch February 3, 2021 17:05
@Kordyjan Kordyjan added this to the 3.0.0 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JavaParser is skipping some annotations that ClassfileParser is picking up
3 participants