changeset 58682:124c43cccb99 records

fixes to record parsing
author vromero
date Fri, 08 Nov 2019 10:59:42 -0500
parents 48dc4292588c
children 9f2e60579175
files src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java test/langtools/tools/javac/records/BadRecord.java test/langtools/tools/javac/records/BadRecord.out test/langtools/tools/javac/records/RecordCompilationTests.java
diffstat 4 files changed, 15 insertions(+), 119 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Thu Nov 07 18:43:37 2019 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Fri Nov 08 10:59:42 2019 -0500
@@ -2558,7 +2558,7 @@
             if (token.kind == INTERFACE ||
                 token.kind == CLASS ||
                 token.kind == ENUM ||
-                (token.kind == IDENTIFIER && token.name() == names.record) ) {
+                isRecordStart()) {
                 return List.of(classOrRecordOrInterfaceOrEnumDeclaration(mods, dc));
             } else {
                 JCExpression t = parseType(true);
@@ -2625,9 +2625,7 @@
                 //else intentional fall-through
             }
         }
-        if (isRecordToken() &&
-            (peekToken(TokenKind.IDENTIFIER, TokenKind.LPAREN) ||
-             peekToken(TokenKind.IDENTIFIER, TokenKind.LT))) {
+        if (isRecordStart() && allowRecords) {
             dc = token.comment(CommentStyle.JAVADOC);
             return List.of(recordDeclaration(F.at(pos).Modifiers(0), dc));
         } else {
@@ -3661,7 +3659,7 @@
     protected JCStatement classOrRecordOrInterfaceOrEnumDeclaration(JCModifiers mods, Comment dc) {
         if (token.kind == CLASS) {
             return classDeclaration(mods, dc);
-        } if (isRecordToken()) {
+        } if (isRecordStart()) {
             return recordDeclaration(mods, dc);
         } else if (token.kind == INTERFACE) {
             return interfaceDeclaration(mods, dc);
@@ -4022,7 +4020,7 @@
             int pos = token.pos;
             JCModifiers mods = modifiersOpt();
             if (token.kind == CLASS ||
-                isRecordToken() ||
+                isRecordStart() ||
                 token.kind == INTERFACE ||
                 token.kind == ENUM) {
                 return List.of(classOrRecordOrInterfaceOrEnumDeclaration(mods, dc));
@@ -4117,9 +4115,16 @@
         }
     }
 
-    boolean isRecordToken() {
-        return allowRecords && token.kind == IDENTIFIER && token.name() == names.record;
-    }
+    boolean isRecordStart() {
+     if (token.kind == IDENTIFIER && token.name() == names.record &&
+            (peekToken(TokenKind.IDENTIFIER, TokenKind.LPAREN) ||
+             peekToken(TokenKind.IDENTIFIER, TokenKind.LT))) {
+          checkSourceLevel(Feature.RECORDS);
+          return true;
+    } else {
+       return false;
+   }
+}
 
     /** MethodDeclaratorRest =
      *      FormalParameters BracketsOpt [THROWS TypeList] ( MethodBody | [DEFAULT AnnotationValue] ";")
--- a/test/langtools/tools/javac/records/BadRecord.java	Thu Nov 07 18:43:37 2019 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,72 +0,0 @@
-/*
- * @test /nodynamiccopyright/
- * @summary Verifying error recovery for broken record classes
- * @compile/fail/ref=BadRecord.out --enable-preview -source ${jdk.version} -XDrawDiagnostics BadRecord.java
- */
-record BadRecord001 {}
-
-record BadRecord0022( {}
-
-record BadRecord0033(int {}
-
-record BadRecord0044(int) {}
-
-record BadRecord005(int i {}
-
-record BadRecord006(int i, {}
-
-record BadRecord007(int i,) {}
-
-record BadRecord008(int i, int {}
-
-record BadRecord009(int i, int) {}
-
-record BadRecord010(int i, int j {}
-
-record BadRecord011(int i, int j, {}
-
-record BadRecord012(int i, int j,) {}
-
-record BadRecord013;
-
-record BadRecord014(;
-
-record BadRecord015(int;
-
-record BadRecord016(int);
-
-record BadRecord017(int i;
-
-record BadRecord018(int i,;
-
-record BadRecord019(int i,);
-
-record BadRecord020(int i, int;
-
-record BadRecord021(int i, int);
-
-record BadRecord022(int i, int j;
-
-record BadRecord023(int i, int j,;
-
-record BadRecord024(int i, int j,);
-
-record BadRecord025(int x)
-
-record BadRecord026 {}
-
-record BadRecord027(final int x) { }
-
-record BadRecord028(private int x) { }
-
-record BadRecord029(public int x) { }
-
-record BadRecord030(volatile int x) { }
-
-record BadRecord030(int x) {
-    private int x() { return x; }
-}
-
-record R(int x) {
-    public int x;
-}
--- a/test/langtools/tools/javac/records/BadRecord.out	Thu Nov 07 18:43:37 2019 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,37 +0,0 @@
-BadRecord.java:6:20: compiler.err.expected: '('
-BadRecord.java:6:21: compiler.err.illegal.start.of.type
-BadRecord.java:8:23: compiler.err.illegal.start.of.type
-BadRecord.java:10:25: compiler.err.expected: token.identifier
-BadRecord.java:12:25: compiler.err.expected: token.identifier
-BadRecord.java:14:26: compiler.err.expected3: ',', ')', '['
-BadRecord.java:16:28: compiler.err.illegal.start.of.type
-BadRecord.java:18:27: compiler.err.illegal.start.of.type
-BadRecord.java:20:31: compiler.err.expected: token.identifier
-BadRecord.java:22:31: compiler.err.expected: token.identifier
-BadRecord.java:24:33: compiler.err.expected3: ',', ')', '['
-BadRecord.java:26:35: compiler.err.illegal.start.of.type
-BadRecord.java:28:34: compiler.err.illegal.start.of.type
-BadRecord.java:30:20: compiler.err.expected: '('
-BadRecord.java:32:21: compiler.err.illegal.start.of.type
-BadRecord.java:34:24: compiler.err.expected: token.identifier
-BadRecord.java:36:24: compiler.err.expected: token.identifier
-BadRecord.java:36:25: compiler.err.expected: '{'
-BadRecord.java:38:26: compiler.err.expected3: ',', ')', '['
-BadRecord.java:40:27: compiler.err.illegal.start.of.type
-BadRecord.java:42:27: compiler.err.illegal.start.of.type
-BadRecord.java:42:28: compiler.err.expected: '{'
-BadRecord.java:44:31: compiler.err.expected: token.identifier
-BadRecord.java:46:31: compiler.err.expected: token.identifier
-BadRecord.java:46:32: compiler.err.expected: '{'
-BadRecord.java:48:33: compiler.err.expected3: ',', ')', '['
-BadRecord.java:50:34: compiler.err.illegal.start.of.type
-BadRecord.java:52:34: compiler.err.illegal.start.of.type
-BadRecord.java:52:35: compiler.err.expected: '{'
-BadRecord.java:54:27: compiler.err.expected: '{'
-BadRecord.java:58:21: compiler.err.record.cant.declare.field.modifiers
-BadRecord.java:60:21: compiler.err.record.cant.declare.field.modifiers
-BadRecord.java:62:21: compiler.err.record.cant.declare.field.modifiers
-BadRecord.java:64:21: compiler.err.record.cant.declare.field.modifiers
-BadRecord.java:71:16: compiler.err.record.cannot.declare.instance.fields
-BadRecord.java:72:2: compiler.err.premature.eof
-36 errors
--- a/test/langtools/tools/javac/records/RecordCompilationTests.java	Thu Nov 07 18:43:37 2019 -0500
+++ b/test/langtools/tools/javac/records/RecordCompilationTests.java	Fri Nov 08 10:59:42 2019 -0500
@@ -70,7 +70,7 @@
         assertFail("compiler.err.premature.eof", "record R();");
         assertFail("compiler.err.illegal.start.of.type", "record R(,) { }");
         assertFail("compiler.err.illegal.start.of.type", "record R((int x)) { }");
-        assertFail("compiler.err.expected", "record R { }");
+        assertFail("compiler.err.expected4", "record R { }");
         assertFail("compiler.err.expected", "record R(foo) { }");
         assertFail("compiler.err.expected", "record R(int int) { }");
         assertFail("compiler.err.mod.not.allowed.here", "abstract record R(String foo) { }");