changeset 59647:5b433fded322

8246199: 'permits' is a restricted identifier Reviewed-by: mcimadamore
author vromero
date Mon, 08 Jun 2020 11:08:28 -0400
parents 58e5f682e8d4
children b9459ed1c062
files src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java test/langtools/tools/javac/sealed/SealedCompilationTests.java
diffstat 2 files changed, 37 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Mon Jun 08 16:59:43 2020 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Mon Jun 08 11:08:28 2020 -0400
@@ -3360,6 +3360,13 @@
                 log.warning(pos, Warnings.RestrictedTypeNotAllowedPreview(name, Source.JDK15));
             }
         }
+        if (name == names.permits) {
+            if (allowSealedTypes) {
+                return Source.JDK15;
+            } else if (shouldWarn) {
+                log.warning(pos, Warnings.RestrictedTypeNotAllowedPreview(name, Source.JDK15));
+            }
+        }
         return null;
     }
 
--- a/test/langtools/tools/javac/sealed/SealedCompilationTests.java	Mon Jun 08 16:59:43 2020 +0200
+++ b/test/langtools/tools/javac/sealed/SealedCompilationTests.java	Mon Jun 08 11:08:28 2020 -0400
@@ -267,6 +267,8 @@
 
         for (String s : List.of(
                 "class sealed {}",
+                "enum sealed {}",
+                "record sealed() {}",
                 "interface sealed {}",
                 "@interface sealed {}"
         )) {
@@ -281,6 +283,34 @@
             assertFail("compiler.err.restricted.type.not.allowed.here", s);
         }
 
+        for (String s : List.of(
+                "class SealedTest { String permits; }",
+                "class SealedTest { int permits = 0; }",
+                "class SealedTest { void test(String permits) { } }",
+                "class SealedTest { void permits(String permits) { } }",
+                "class SealedTest { void test() { String permits = null; } }"
+        )) {
+            assertOK(s);
+        }
+
+        for (String s : List.of(
+                "class permits {}",
+                "enum permits {}",
+                "record permits() {}",
+                "interface permits {}",
+                "@interface permits {}"
+        )) {
+            assertFail("compiler.err.restricted.type.not.allowed", s);
+        }
+
+        for (String s : List.of(
+                "class Foo { permits m() {} }",
+                "class Foo { permits i; }",
+                "class Foo { void m(permits i) {} }"
+        )) {
+            assertFail("compiler.err.restricted.type.not.allowed.here", s);
+        }
+
         String[] testOptions = {/* no options */};
         String[] previousCompOptions = getCompileOptions();
         setCompileOptions(testOptions);