changeset 57917:a6b0ad4c98ab records-and-sealed

dont allow sealed or non-sealed local classes
author vromero
date Thu, 10 Oct 2019 22:08:39 -0400
parents 6b5311b6bf73
children 5fbd6bd20a5b
files 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/sealed/SealedCompilationTests.java
diffstat 3 files changed, 36 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Thu Oct 10 18:54:34 2019 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Thu Oct 10 22:08:39 2019 -0400
@@ -2637,6 +2637,23 @@
                 }
 
                 //else intentional fall-through
+            } else if (allowSealedTypes) {
+                if (token.name() == names.non && peekToken(0, TokenKind.SUB, TokenKind.IDENTIFIER)) {
+                    Token tokenSub = S.token(1);
+                    Token tokenSealed = S.token(2);
+                    if (token.endPos == tokenSub.pos &&
+                            tokenSub.endPos == tokenSealed.pos &&
+                            tokenSealed.name() == names.sealed &&
+                            S.token(3).kind == TokenKind.CLASS) {
+                        log.error(token.pos, Errors.SealedOrNonSealedLocalClassesNotAllowed);
+                        nextToken();
+                        nextToken();
+                        nextToken();
+                    }
+                } else if (token.name() == names.sealed && S.token(1).kind == TokenKind.CLASS) {
+                    log.error(token.pos, Errors.SealedOrNonSealedLocalClassesNotAllowed);
+                    nextToken();
+                }
             }
         }
         if (isRecordToken() &&
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Oct 10 18:54:34 2019 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Oct 10 22:08:39 2019 -0400
@@ -3426,6 +3426,9 @@
 compiler.err.sealed.interface.or.abstract.must.have.subtypes=\
     sealed interfaces or abstract classes with an empty permits clause must have subtypes
 
+compiler.err.sealed.or.non.sealed.local.classes.not.allowed=\
+    sealed or non-sealed local classes are not allowed
+
 ###
 # errors related to records
 
--- a/test/langtools/tools/javac/sealed/SealedCompilationTests.java	Thu Oct 10 18:54:34 2019 -0700
+++ b/test/langtools/tools/javac/sealed/SealedCompilationTests.java	Thu Oct 10 22:08:39 2019 -0400
@@ -216,23 +216,22 @@
     }
 
     public void testNoLocalSealedClasses() {
-        // @@@ Currently failing; probably wrong diag key as well
-//        for (String s : List.of(
-//                """
-//                sealed class C {
-//                    void m() {
-//                        sealed class C { }
-//                    }
-//                }
-//                """,
-//                """
-//                sealed class C {
-//                    void m() {
-//                        non-sealed class D { }
-//                    }
-//                }
-//                """))
-//            assertFail("compiler.err.cant.inherit.from.sealed", s);
+        for (String s : List.of(
+                """
+                sealed class C {
+                    void m() {
+                        sealed class D { }
+                    }
+                }
+                """,
+                """
+                sealed class C {
+                    void m() {
+                        non-sealed class D { }
+                    }
+                }
+                """))
+            assertFail("compiler.err.sealed.or.non.sealed.local.classes.not.allowed", s);
     }
 
     public void testLocalCantExtendSealed() {