changeset 49055:8eb17cb8094a lworld

8197909: [lworld] Change Javac's diagnostic emission strategy for cyclic.value.type.membership
author sadayapalam
date Mon, 12 Mar 2018 17:14:07 +0530
parents 44a050354dfc
children f438b78bc361
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java test/langtools/tools/javac/valhalla/lworld-values/CheckCyclicMembership.java test/langtools/tools/javac/valhalla/lworld-values/CheckFlattenableCycles.java test/langtools/tools/javac/valhalla/lworld-values/CheckFlattenableCycles.out
diffstat 4 files changed, 38 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Mon Mar 12 15:44:20 2018 +0530
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Mon Mar 12 17:14:07 2018 +0530
@@ -93,6 +93,7 @@
     private final Source source;
     private final Profile profile;
     private final boolean warnOnAnyAccessToMembers;
+    private final boolean rejectValueMembershipCycles;
 
     // The set of lint options currently in effect. It is initialized
     // from the context, and then is set/reset as needed by Attr as it
@@ -132,6 +133,7 @@
 
         source = Source.instance(context);
         warnOnAnyAccessToMembers = options.isSet("warnOnAccessToMembers");
+        rejectValueMembershipCycles = options.isSet("rejectValueMembershipCycles");
 
         Target target = Target.instance(context);
         syntheticNameChar = target.syntheticNameChar();
@@ -2179,11 +2181,9 @@
             for (List<? extends JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
                 if (l.head.hasTag(VARDEF)) {
                     JCVariableDecl field = (JCVariableDecl) l.head;
-                    if (!field.sym.isStatic()) {
+                    if (cyclePossible(field.sym)) {
                         Type fieldType = field.sym.type;
-                        if (types.isValue(fieldType)) {
-                            checkNonCyclicMembership((ClassSymbol) fieldType.tsym, field.pos());
-                        }
+                        checkNonCyclicMembership((ClassSymbol) fieldType.tsym, field.pos());
                     }
                 }
             }
@@ -2200,16 +2200,21 @@
         }
         try {
             c.flags_field |= LOCKED;
-            for (Symbol fld : c.members().getSymbols(s -> s.kind == VAR &&
-                    !s.isStatic() &&
-                    (types.isValue(s.type)), NON_RECURSIVE)) {
+            for (Symbol fld : c.members().getSymbols(s -> s.kind == VAR && cyclePossible((VarSymbol) s), NON_RECURSIVE)) {
                 checkNonCyclicMembership((ClassSymbol) fld.type.tsym, pos);
             }
         } finally {
             c.flags_field &= ~LOCKED;
         }
     }
-
+        // where
+        private boolean cyclePossible(VarSymbol symbol) {
+            if (symbol.isStatic())
+                return false;
+            if (rejectValueMembershipCycles && types.isValue(symbol.type))
+                return true;
+            return (symbol.flags() & FLATTENABLE) != 0;
+        }
 
     void checkNonCyclicDecl(JCClassDecl tree) {
         CycleChecker cc = new CycleChecker();
--- a/test/langtools/tools/javac/valhalla/lworld-values/CheckCyclicMembership.java	Mon Mar 12 15:44:20 2018 +0530
+++ b/test/langtools/tools/javac/valhalla/lworld-values/CheckCyclicMembership.java	Mon Mar 12 17:14:07 2018 +0530
@@ -2,7 +2,7 @@
  * @test /nodynamiccopyright/
  * @summary Value types may not declare fields of its own type either directly or indirectly.
  *
- * @compile/fail/ref=CheckCyclicMembership.out -XDrawDiagnostics CheckCyclicMembership.java
+ * @compile/fail/ref=CheckCyclicMembership.out -XDrejectValueMembershipCycles -XDrawDiagnostics CheckCyclicMembership.java
  */
 
 final __ByValue class CheckCyclicMembership {
@@ -16,5 +16,5 @@
     final int i = 10;
     final String s = "blah";
     final InnerRef ir = new InnerRef(); // OK.
-    final InnerValue iv = __MakeDefault InnerValue(); // Error. Some Order dependancy hides this. FIXME.
+    final InnerValue iv = __MakeDefault InnerValue();
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/valhalla/lworld-values/CheckFlattenableCycles.java	Mon Mar 12 17:14:07 2018 +0530
@@ -0,0 +1,20 @@
+/*
+ * @test /nodynamiccopyright/
+ * @summary Check for cycles through fields declared flattenable.
+ *
+ * @compile/fail/ref=CheckFlattenableCycles.out -XDrawDiagnostics CheckFlattenableCycles.java
+ */
+
+final __ByValue class CheckFlattenableCycles {
+    class InnerRef {
+        CheckFlattenableCycles cfc;
+    }
+    __ByValue final class InnerValue {
+        final __Flattenable CheckFlattenableCycles cfc = __MakeDefault CheckFlattenableCycles(); // Error.
+    }
+    final __Flattenable CheckFlattenableCycles cfc = __MakeDefault CheckFlattenableCycles(); // Error.
+    final int i = 10;
+    final String s = "blah";
+    final InnerRef ir = new InnerRef(); // OK.
+    final __Flattenable InnerValue iv = __MakeDefault InnerValue();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/valhalla/lworld-values/CheckFlattenableCycles.out	Mon Mar 12 17:14:07 2018 +0530
@@ -0,0 +1,3 @@
+CheckFlattenableCycles.java:15:48: compiler.err.cyclic.value.type.membership: CheckFlattenableCycles
+CheckFlattenableCycles.java:13:52: compiler.err.cyclic.value.type.membership: CheckFlattenableCycles.InnerValue
+2 errors
\ No newline at end of file