changeset 60086:0988d6372138 sealed-types

issue an error if a type in the permits list refers to declaring class or a supertype
author vromero
date Wed, 26 Feb 2020 23:48:13 -0500
parents 335223833edc
children 749031d7910f 02506a5c9735
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 src/jdk.compiler/share/classes/com/sun/tools/javac/tree/Pretty.java src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java
diffstat 4 files changed, 33 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Feb 25 19:23:17 2020 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Wed Feb 26 23:48:13 2020 -0500
@@ -5044,12 +5044,18 @@
                     } else if (subType.tsym.packge().modle != c.packge().modle) {
                         log.error(TreeInfo.declarationFor(subType.tsym, env.tree), Errors.CantInheritFromSealed(c));
                     }
-                    if (c.isInterface()) {
+                    if (subType.tsym == c.type.tsym || types.isSuperType(subType, c.type)) {
+                        log.error(TreeInfo.declarationFor(subType.tsym, ((JCClassDecl)env.tree).permitting),
+                                Errors.TypeListedInPermitsIsSameClassOrSupertype(subType.tsym == c.type.tsym ?
+                                        Fragments.SameClass : Fragments.Supertype));
+                    } else if (c.isInterface()) {
                         if (!types.interfaces(subType).map(t -> t.tsym).contains(c.type.tsym)) {
-                            log.error(TreeInfo.declarationFor(subType.tsym, env.tree), Errors.SubtypeListedInPermitsDoesntExtendSealed(subType, c.type));
+                            log.error(TreeInfo.declarationFor(subType.tsym, env.tree),
+                                    Errors.SubtypeListedInPermitsDoesntExtendSealed(subType, c.type));
                         }
                     } else if (((ClassType)subType).supertype_field.tsym != c.type.tsym) {
-                        log.error(TreeInfo.declarationFor(subType.tsym, env.tree), Errors.SubtypeListedInPermitsDoesntExtendSealed(subType, c.type));
+                        log.error(TreeInfo.declarationFor(subType.tsym, env.tree),
+                                Errors.SubtypeListedInPermitsDoesntExtendSealed(subType, c.type));
                     }
                 }
             }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Feb 25 19:23:17 2020 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Feb 26 23:48:13 2020 -0500
@@ -3460,6 +3460,18 @@
 compiler.err.subtype.listed.in.permits.doesnt.extend.sealed=\
     type {0} does not extend sealed type {1}
 
+# 0: fragment
+compiler.err.type.listed.in.permits.is.same.class.or.supertype=\
+    permitted type can not {0}
+
+compiler.misc.same.class=\
+    be the same as the declaring class
+
+compiler.misc.supertype=\
+    be a supertype
+
+# other sealed types related errors
+
 compiler.err.sealed.type.must.have.subtypes=\
     sealed type must have subtypes
 
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/Pretty.java	Tue Feb 25 19:23:17 2020 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/Pretty.java	Wed Feb 26 23:48:13 2020 -0500
@@ -564,6 +564,10 @@
                     print(" extends ");
                     printExprs(tree.implementing);
                 }
+                if (tree.permitting.nonEmpty()) {
+                    print(" permits ");
+                    printExprs(tree.permitting);
+                }
             } else {
                 if ((tree.mods.flags & ENUM) != 0)
                     print("enum " + tree.name);
@@ -578,6 +582,10 @@
                     print(" implements ");
                     printExprs(tree.implementing);
                 }
+                if (tree.permitting.nonEmpty()) {
+                    print(" permits ");
+                    printExprs(tree.permitting);
+                }
             }
             print(" ");
             if ((tree.mods.flags & ENUM) != 0) {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java	Tue Feb 25 19:23:17 2020 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java	Wed Feb 26 23:48:13 2020 -0500
@@ -747,6 +747,10 @@
         return s.result;
     }
 
+    public static JCTree declarationFor(final Symbol sym, final List<? extends JCTree> trees) {
+        return trees.stream().map(t -> TreeInfo.declarationFor(sym, t)).filter(t -> t != null).findFirst().get();
+    }
+
     public static Env<AttrContext> scopeFor(JCTree node, JCCompilationUnit unit) {
         return scopeFor(pathFor(node, unit));
     }