diff --git a/compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala b/compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala index d1164d4742af..aa88433807d4 100644 --- a/compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala @@ -761,11 +761,9 @@ object JavaParsers { ValDef(name, tpt2, if (mods.is(Flags.Param)) EmptyTree else unimplementedExpr).withMods(mods1) } - def memberDecl(start: Offset, mods: Modifiers, parentToken: Int): List[Tree] = in.token match - case CLASS | ENUM | RECORD | INTERFACE | AT => - typeDecl(start, if definesInterface(parentToken) then mods | Flags.JavaStatic else mods) - case _ => - termDecl(start, mods, parentToken) + def memberDecl(start: Offset, mods: Modifiers, parentToken: Int): List[Tree] = + if (isTypeDeclStart()) typeDecl(start, if definesInterface(parentToken) then mods | Flags.JavaStatic else mods) + else termDecl(start, mods, parentToken) def makeCompanionObject(cdef: TypeDef, statics: List[Tree]): Tree = atSpan(cdef.span) { @@ -1049,6 +1047,10 @@ object JavaParsers { } } + def isTypeDeclStart() = in.token match { + case CLASS | ENUM | RECORD | INTERFACE | AT => true + case _ => false + } def typeDecl(start: Offset, mods: Modifiers): List[Tree] = in.token match case ENUM => enumDecl(start, mods) case INTERFACE => interfaceDecl(start, mods) @@ -1098,6 +1100,13 @@ object JavaParsers { case Some(t) => t.name.toTypeName case _ => tpnme.EMPTY } + var compact = false + def typeDeclOrCompact(start: Offset, mods: Modifiers): List[Tree] = + if (isTypeDeclStart()) typeDecl(start, mods) + else + val ts = termDecl(start, mods, CLASS) + if (ts.nonEmpty) compact = true + Nil val buf = new ListBuffer[Tree] while (in.token == IMPORT) buf ++= importDecl() @@ -1107,12 +1116,13 @@ object JavaParsers { val start = in.offset val mods = modifiers(inInterface = false) adaptRecordIdentifier() // needed for typeDecl - buf ++= typeDecl(start, mods) + buf ++= typeDeclOrCompact(start, mods) } } val unit = atSpan(start) { PackageDef(pkg, buf.toList) } accept(EOF) - unit match + if (compact) EmptyTree + else unit match case PackageDef(Ident(nme.EMPTY_PACKAGE), Nil) => EmptyTree case _ => unit } diff --git a/tests/neg/i18584/T.java b/tests/neg/i18584/T.java new file mode 100644 index 000000000000..ad05ca47aefe --- /dev/null +++ b/tests/neg/i18584/T.java @@ -0,0 +1,9 @@ +// test: -jvm 25+ + +int k = 0; +File f = null; +java.io.File g = f; +@Deprecated void main() { return; } +public record Person(String name, int age) { } +public class H { } +public static int k() { return 1; } diff --git a/tests/neg/i18584/Test.scala b/tests/neg/i18584/Test.scala new file mode 100644 index 000000000000..46977fea6d20 --- /dev/null +++ b/tests/neg/i18584/Test.scala @@ -0,0 +1,5 @@ +class Test { + new U + new H() // error + new T.H() // error +} diff --git a/tests/neg/i18584/U.java b/tests/neg/i18584/U.java new file mode 100644 index 000000000000..f9c640868358 --- /dev/null +++ b/tests/neg/i18584/U.java @@ -0,0 +1 @@ +public class U { } diff --git a/tests/pos/i18584/T.java b/tests/pos/i18584/T.java new file mode 100644 index 000000000000..ad05ca47aefe --- /dev/null +++ b/tests/pos/i18584/T.java @@ -0,0 +1,9 @@ +// test: -jvm 25+ + +int k = 0; +File f = null; +java.io.File g = f; +@Deprecated void main() { return; } +public record Person(String name, int age) { } +public class H { } +public static int k() { return 1; } diff --git a/tests/pos/i18584/Test.scala b/tests/pos/i18584/Test.scala new file mode 100644 index 000000000000..c4f42cf01183 --- /dev/null +++ b/tests/pos/i18584/Test.scala @@ -0,0 +1,3 @@ +class Test { + new U +} diff --git a/tests/pos/i18584/U.java b/tests/pos/i18584/U.java new file mode 100644 index 000000000000..f9c640868358 --- /dev/null +++ b/tests/pos/i18584/U.java @@ -0,0 +1 @@ +public class U { }