Skip to content

Java parses leading annotation #20064

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,11 @@ object JavaParsers {
}
}

def modifiers(inInterface: Boolean): Modifiers = {
def modifiers(inInterface: Boolean, annots0: List[Tree] = Nil): Modifiers = {
var flags: FlagSet = Flags.JavaDefined
// assumed true unless we see public/private/protected
var isPackageAccess = true
var annots = new ListBuffer[Tree]
var annots = ListBuffer.from[Tree](annots0)
def addAnnot(tpt: Tree) =
annots += atSpan(in.offset) {
in.nextToken()
Expand All @@ -453,7 +453,7 @@ object JavaParsers {

while (true)
in.token match {
case AT if (in.lookaheadToken != INTERFACE) =>
case AT if in.lookaheadToken != INTERFACE =>
in.nextToken()
annotation() match {
case Some(anno) => annots += anno
Expand Down Expand Up @@ -1080,24 +1080,30 @@ object JavaParsers {
/** CompilationUnit ::= [package QualId semi] TopStatSeq
*/
def compilationUnit(): Tree = {
val start = in.offset
val buf = ListBuffer.empty[Tree]
var start = in.offset
val leadingAnnots = if (in.token == AT) annotations() else Nil
val pkg: RefTree =
if (in.token == AT || in.token == PACKAGE) {
annotations()
if in.token == PACKAGE then
if !leadingAnnots.isEmpty then
//if (unit.source.file.name != "package-info.java")
// syntaxError(pos, "package annotations must be in file package-info.java")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

not sure scalac needs to enforce this javac rule if the annotations are not used

Copy link
Member

Choose a reason for hiding this comment

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

I think generally we don't add code to enforce Java rules that don't affect us in any way.

start = in.offset
accept(PACKAGE)
val pkg = qualId()
accept(SEMI)
pkg
}
else
if !leadingAnnots.isEmpty then
buf ++= typeDecl(start, modifiers(inInterface = false, annots0 = leadingAnnots))
Ident(nme.EMPTY_PACKAGE)
thisPackageName = convertToTypeName(pkg) match {
case Some(t) => t.name.toTypeName
case _ => tpnme.EMPTY
}
val buf = new ListBuffer[Tree]
while (in.token == IMPORT)
buf ++= importDecl()
if buf.isEmpty then
while in.token == IMPORT do
buf ++= importDecl()
while (in.token != EOF && in.token != RBRACE) {
while (in.token == SEMI) in.nextToken()
if (in.token != EOF) {
Expand Down
Empty file added tests/pos/i20026/Empty.java
Empty file.
4 changes: 4 additions & 0 deletions tests/pos/i20026/JFun.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@FunctionalInterface
public interface JFun {
String f(String s);
}
4 changes: 4 additions & 0 deletions tests/pos/i20026/JTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

@api.TestInstance(api.TestInstance.Lifecycle.PER_CLASS)
public class JTest {
}
6 changes: 6 additions & 0 deletions tests/pos/i20026/KTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

import api.*;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class KTest {
}
20 changes: 20 additions & 0 deletions tests/pos/i20026/TestInstance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

package api;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface TestInstance {
enum Lifecycle {
PER_CLASS;
}
Lifecycle value();
}
6 changes: 6 additions & 0 deletions tests/pos/i20026/test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

object Test extends App {
println {
new JTest
Copy link
Member

Choose a reason for hiding this comment

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

(same remark as in scala 2) add reference to JFun and KTest?

}
}