changeset 48447:e20c366ff2b0 datum

record cant be used as a type name anymore
author vromero
date Wed, 13 Dec 2017 14:12:24 -0500
parents 8b7339ec0b5e
children 599a2974e628
files src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties test/langtools/tools/javac/datum/BadUseOfRecordTest.java test/langtools/tools/javac/datum/BadUseOfRecordTest.out
diffstat 5 files changed, 47 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java	Wed Dec 13 11:17:28 2017 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java	Wed Dec 13 14:12:24 2017 -0500
@@ -176,7 +176,8 @@
         DIAMOND_WITH_ANONYMOUS_CLASS_CREATION(JDK9, Fragments.FeatureDiamondAndAnonClass, DiagKind.NORMAL),
         UNDERSCORE_IDENTIFIER(MIN, JDK8),
         PRIVATE_INTERFACE_METHODS(JDK9, Fragments.FeaturePrivateIntfMethods, DiagKind.PLURAL),
-        LOCAL_VARIABLE_TYPE_INFERENCE(JDK10);
+        LOCAL_VARIABLE_TYPE_INFERENCE(JDK10),
+        DATA_CLASSES(JDK10);
 
         enum DiagKind {
             NORMAL,
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Wed Dec 13 11:17:28 2017 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Wed Dec 13 14:12:24 2017 -0500
@@ -2398,11 +2398,12 @@
                 // we have to check if this is a record declaration
                 Token t1 = S.token(1);
                 Token t2 = S.token(2);
-                if (t1.kind == IDENTIFIER && t1.name() == names.record && t2.kind == IDENTIFIER) {
+                Token t3 = S.token(3);
+                if (t1.kind == IDENTIFIER && t1.name() == names.record && t2.kind == IDENTIFIER && t3.kind == LPAREN) {
                     nextToken();
                 }
             }
-            if (token.kind == IDENTIFIER && token.name() == names.record && peekToken(IDENTIFIER)) {
+            if (isRecordDeclaration()) {
                 JCModifiers mods = modifiersOpt();
                 dc = token.comment(CommentStyle.JAVADOC);
                 return List.of(recordDeclaration(mods, dc));
@@ -3063,6 +3064,10 @@
         return Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source) && name == names.var;
     }
 
+    boolean isRestrictedRecordTypeName(Name name) {
+        return Feature.DATA_CLASSES.allowedInSource(source) && name == names.record;
+    }
+
     /** VariableDeclaratorId = Ident BracketsOpt
      */
     JCVariableDecl variableDeclaratorId(JCModifiers mods, JCExpression type) {
@@ -3380,7 +3385,7 @@
     protected JCStatement classOrRecordOrInterfaceOrEnumDeclaration(JCModifiers mods, Comment dc) {
         if (token.kind == CLASS) {
             return classDeclaration(mods, dc);
-        } if (token.kind == IDENTIFIER && token.name() == names.record) {
+        } if (isRecordDeclaration()) {
             return recordDeclaration(mods, dc);
         } else if (token.kind == INTERFACE) {
             return interfaceDeclaration(mods, dc);
@@ -3509,6 +3514,10 @@
         if (isRestrictedLocalVarTypeName(name)) {
             reportSyntaxError(pos, "var.not.allowed", name);
         }
+
+        if (isRestrictedRecordTypeName(name)) {
+            reportSyntaxError(pos, "record.not.allowed", name);
+        }
         return name;
     }
 
@@ -3737,7 +3746,7 @@
             int pos = token.pos;
             JCModifiers mods = modifiersOpt();
             if (token.kind == CLASS ||
-                token.kind == IDENTIFIER && token.name() == names.record ||
+                isRecordDeclaration() ||
                 token.kind == INTERFACE ||
                 token.kind == ENUM) {
                 return List.of(classOrRecordOrInterfaceOrEnumDeclaration(mods, dc));
@@ -3754,6 +3763,10 @@
         }
     }
 
+    boolean isRecordDeclaration() {
+        return token.kind == IDENTIFIER && token.name() == names.record && peekToken(TokenKind.IDENTIFIER, TokenKind.LPAREN);
+    }
+
     private List<JCTree> methodOrFieldMemberDecl(Name className, JCModifiers mods, boolean isInterface, Comment dc, boolean isRecord) {
         int pos = token.pos;
         List<JCTypeParameter> typarams = typeParametersOpt();
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Dec 13 11:17:28 2017 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Dec 13 14:12:24 2017 -0500
@@ -3209,3 +3209,8 @@
 # 0: name
 compiler.err.duplicate.argument.to.super=\
     duplicate argument {0}, arguments passed to the super of a record must be unique
+
+# 0: token
+compiler.err.record.not.allowed=\
+    ''{0}'' not allowed here\n\
+    as of release 10, ''{0}'' is a restricted type name and cannot be used for type declarations
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/datum/BadUseOfRecordTest.java	Wed Dec 13 14:12:24 2017 -0500
@@ -0,0 +1,18 @@
+/*
+ * @test /nodynamiccopyright/
+ * @summary check that record is not used as a type name
+ * @compile/fail/ref=BadUseOfRecordTest.out -XDrawDiagnostics BadUseOfRecordTest.java
+ */
+
+class BadUseOfRecordTest {
+
+    interface record {
+        void m();
+    }
+
+    class record {}
+
+    enum record {A, B}
+
+    record record(int x);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/datum/BadUseOfRecordTest.out	Wed Dec 13 14:12:24 2017 -0500
@@ -0,0 +1,5 @@
+BadUseOfRecordTest.java:9:15: compiler.err.record.not.allowed: record
+BadUseOfRecordTest.java:13:11: compiler.err.record.not.allowed: record
+BadUseOfRecordTest.java:15:10: compiler.err.record.not.allowed: record
+BadUseOfRecordTest.java:17:12: compiler.err.record.not.allowed: record
+4 errors