Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 802a7b0

Browse files
authored
Merge pull request #11066 from lrytz/b12878
Skip over JEP 445 compact compilation units
2 parents dd497fb + 1f84425 commit 802a7b0

File tree

9 files changed

+63
-14
lines changed

9 files changed

+63
-14
lines changed

src/compiler/scala/tools/nsc/javac/JavaParsers.scala

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -770,14 +770,9 @@ trait JavaParsers extends ast.parser.ParsersCommon with JavaScanners {
770770
}
771771
}
772772

773-
def memberDecl(mods: Modifiers, parentToken: Int): List[Tree] = {
774-
in.token match {
775-
case CLASS | ENUM | RECORD | INTERFACE | AT =>
776-
typeDecl(mods)
777-
case _ =>
778-
termDecl(mods, parentToken)
779-
}
780-
}
773+
def memberDecl(mods: Modifiers, parentToken: Int): List[Tree] =
774+
if (isTypeDeclStart()) typeDecl(mods)
775+
else termDecl(mods, parentToken)
781776

782777
def makeCompanionObject(cdef: ClassDef, statics: List[Tree]): Tree =
783778
atPos(cdef.pos) {
@@ -1061,6 +1056,13 @@ trait JavaParsers extends ast.parser.ParsersCommon with JavaScanners {
10611056
(res, hasClassBody)
10621057
}
10631058

1059+
def isTypeDeclStart(): Boolean = {
1060+
adaptRecordIdentifier()
1061+
in.token match {
1062+
case ENUM | INTERFACE | AT | CLASS | RECORD => true
1063+
case _ => false
1064+
}
1065+
}
10641066
def typeDecl(mods: Modifiers): List[Tree] = {
10651067
adaptRecordIdentifier()
10661068
in.token match {
@@ -1092,6 +1094,14 @@ trait JavaParsers extends ast.parser.ParsersCommon with JavaScanners {
10921094
/** CompilationUnit ::= [[Annotation] package QualId semi] {Import} {TypeDecl} //TopStatSeq
10931095
*/
10941096
def compilationUnit(): Tree = {
1097+
var compact = false
1098+
def typeDeclOrCompact(mods: Modifiers): List[Tree] =
1099+
if (isTypeDeclStart()) typeDecl(mods)
1100+
else {
1101+
val ts = termDecl(mods, CLASS)
1102+
if (ts.nonEmpty) compact = true
1103+
Nil
1104+
}
10951105
val buf = ListBuffer.empty[Tree]
10961106
var pos = in.currentPos
10971107
val leadingAnnots = if (in.token == AT) annotations() else Nil
@@ -1107,7 +1117,7 @@ trait JavaParsers extends ast.parser.ParsersCommon with JavaScanners {
11071117
}
11081118
else {
11091119
if (!leadingAnnots.isEmpty)
1110-
buf ++= typeDecl(modifiers(inInterface = false, annots0 = leadingAnnots))
1120+
buf ++= typeDeclOrCompact(modifiers(inInterface = false, annots0 = leadingAnnots))
11111121
Ident(nme.EMPTY_PACKAGE_NAME)
11121122
}
11131123
thisPackageName = gen.convertToTypeName(pkg) match {
@@ -1120,10 +1130,11 @@ trait JavaParsers extends ast.parser.ParsersCommon with JavaScanners {
11201130
while (in.token != EOF && in.token != RBRACE) {
11211131
while (in.token == SEMI) in.nextToken()
11221132
if (in.token != EOF)
1123-
buf ++= typeDecl(modifiers(inInterface = false))
1133+
buf ++= typeDeclOrCompact(modifiers(inInterface = false))
11241134
}
11251135
accept(EOF)
1126-
atPos(pos) {
1136+
if (compact) EmptyTree
1137+
else atPos(pos) {
11271138
makePackaging(pkg, buf.toList)
11281139
}
11291140
}

test/files/neg/i20026.check

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
JTest.java:3: error: illegal start of type declaration
1+
JTest.java:3: error: illegal start of type
22
import java.util.*;
3-
^
4-
1 error
3+
^
4+
JTest.java:3: error: identifier expected but `;` found.
5+
import java.util.*;
6+
^
7+
2 errors

test/files/neg/t12878.check

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Test.scala:3: error: not found: type H
2+
new H()
3+
^
4+
Test.scala:4: error: not found: value T
5+
new T.H()
6+
^
7+
2 errors

test/files/neg/t12878/T.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//> using 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; }

test/files/neg/t12878/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()
4+
new T.H()
5+
}

test/files/neg/t12878/U.java

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

test/files/pos/t12878/T.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//> using 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; }

test/files/pos/t12878/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+
}

test/files/pos/t12878/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)