Skip to content

Commit fb44ac8

Browse files
committed
Skip over JEP 445 compact compilation units
Compact compilation units are not referrible, so we can skip over them.
1 parent d5ca220 commit fb44ac8

File tree

7 files changed

+45
-7
lines changed

7 files changed

+45
-7
lines changed

compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -761,11 +761,9 @@ object JavaParsers {
761761
ValDef(name, tpt2, if (mods.is(Flags.Param)) EmptyTree else unimplementedExpr).withMods(mods1)
762762
}
763763

764-
def memberDecl(start: Offset, mods: Modifiers, parentToken: Int): List[Tree] = in.token match
765-
case CLASS | ENUM | RECORD | INTERFACE | AT =>
766-
typeDecl(start, if definesInterface(parentToken) then mods | Flags.JavaStatic else mods)
767-
case _ =>
768-
termDecl(start, mods, parentToken)
764+
def memberDecl(start: Offset, mods: Modifiers, parentToken: Int): List[Tree] =
765+
if (isTypeDeclStart()) typeDecl(start, if definesInterface(parentToken) then mods | Flags.JavaStatic else mods)
766+
else termDecl(start, mods, parentToken)
769767

770768
def makeCompanionObject(cdef: TypeDef, statics: List[Tree]): Tree =
771769
atSpan(cdef.span) {
@@ -1049,6 +1047,10 @@ object JavaParsers {
10491047
}
10501048
}
10511049

1050+
def isTypeDeclStart() = in.token match {
1051+
case CLASS | ENUM | RECORD | INTERFACE | AT => true
1052+
case _ => false
1053+
}
10521054
def typeDecl(start: Offset, mods: Modifiers): List[Tree] = in.token match
10531055
case ENUM => enumDecl(start, mods)
10541056
case INTERFACE => interfaceDecl(start, mods)
@@ -1098,6 +1100,13 @@ object JavaParsers {
10981100
case Some(t) => t.name.toTypeName
10991101
case _ => tpnme.EMPTY
11001102
}
1103+
var compact = false
1104+
def typeDeclOrCompact(start: Offset, mods: Modifiers): List[Tree] =
1105+
if (isTypeDeclStart()) typeDecl(start, mods)
1106+
else
1107+
val ts = termDecl(start, mods, CLASS)
1108+
if (ts.nonEmpty) compact = true
1109+
Nil
11011110
val buf = new ListBuffer[Tree]
11021111
while (in.token == IMPORT)
11031112
buf ++= importDecl()
@@ -1107,12 +1116,13 @@ object JavaParsers {
11071116
val start = in.offset
11081117
val mods = modifiers(inInterface = false)
11091118
adaptRecordIdentifier() // needed for typeDecl
1110-
buf ++= typeDecl(start, mods)
1119+
buf ++= typeDeclOrCompact(start, mods)
11111120
}
11121121
}
11131122
val unit = atSpan(start) { PackageDef(pkg, buf.toList) }
11141123
accept(EOF)
1115-
unit match
1124+
if (compact) EmptyTree
1125+
else unit match
11161126
case PackageDef(Ident(nme.EMPTY_PACKAGE), Nil) => EmptyTree
11171127
case _ => unit
11181128
}

tests/neg/i18584/T.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// test: -jvm 25+
2+
3+
int k = 0;
4+
File f = null;
5+
java.io.File g = f;
6+
@Deprecated void main() { return; }
7+
public record Person(String name, int age) { }
8+
public class H { }
9+
public static int k() { return 1; }

tests/neg/i18584/Test.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Test {
2+
new U
3+
new H() // error
4+
new T.H() // error
5+
}

tests/neg/i18584/U.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public class U { }

tests/pos/i18584/T.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// test: -jvm 25+
2+
3+
int k = 0;
4+
File f = null;
5+
java.io.File g = f;
6+
@Deprecated void main() { return; }
7+
public record Person(String name, int age) { }
8+
public class H { }
9+
public static int k() { return 1; }

tests/pos/i18584/Test.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Test {
2+
new U
3+
}

tests/pos/i18584/U.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public class U { }

0 commit comments

Comments
 (0)