changeset 57906:543585aa050d records-and-sealed

sealed interfaces or abstract classes with an empty permits clause must have subtypes
author vromero
date Thu, 10 Oct 2019 14:29:38 -0400
parents c78c8c9fcda5
children 13f970ffb7d6
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties test/langtools/tools/javac/sealed/SealedCompilationTests.java
diffstat 3 files changed, 17 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Oct 10 13:52:06 2019 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Oct 10 14:29:38 2019 -0400
@@ -4712,6 +4712,13 @@
             // Get environment current at the point of class definition.
             Env<AttrContext> env = typeEnvs.get(c);
 
+            if (c.isSealed() &&
+                    !((ClassType)c.type).isPermittedExplicit &&
+                    ((ClassType)c.type).permitted.isEmpty() &&
+                    (c.isInterface() || c.isAbstract())) {
+                log.error(env.tree, Errors.SealedInterfaceOrAbstractMustHaveSubtypes);
+            }
+
             // The info.lint field in the envs stored in typeEnvs is deliberately uninitialized,
             // because the annotations were not available at the time the env was created. Therefore,
             // we look up the environment chain for the first enclosing environment for which the
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Oct 10 13:52:06 2019 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Oct 10 14:29:38 2019 -0400
@@ -3423,6 +3423,9 @@
 compiler.err.class.cant.be.leaf.in.sealed.hierarchy=\
     {0} cannot be a leaf in a sealed hierarchy
 
+compiler.err.sealed.interface.or.abstract.must.have.subtypes=\
+    sealed interfaces or abstract classes with an empty permits clause must have subtypes
+
 ###
 # errors related to records
 
--- a/test/langtools/tools/javac/sealed/SealedCompilationTests.java	Thu Oct 10 13:52:06 2019 -0400
+++ b/test/langtools/tools/javac/sealed/SealedCompilationTests.java	Thu Oct 10 14:29:38 2019 -0400
@@ -166,4 +166,11 @@
                         "    public static void foo() { new I2() { public void run() { } }; }\n" +
                         "}");
     }
+
+    public void testSealedInterfaceAndAbstracClasses() {
+        assertFail("compiler.err.sealed.interface.or.abstract.must.have.subtypes",
+                "sealed interface I1 {}");
+        assertFail("compiler.err.sealed.interface.or.abstract.must.have.subtypes",
+                "sealed abstract class AC {}");
+    }
 }