changeset 49054:44a050354dfc lworld

8199452: [lworld] Check#checkRefType is too strict and forbids various legitimate operations on value classes
author sadayapalam
date Mon, 12 Mar 2018 15:44:20 +0530
parents 7c1c51c340e6
children 8eb17cb8094a
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java test/langtools/tools/javac/valhalla/lworld-values/ValuesAsRefs.java
diffstat 3 files changed, 73 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Mar 12 11:46:54 2018 +0530
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Mar 12 15:44:20 2018 +0530
@@ -1514,7 +1514,7 @@
     }
 
     public void visitSynchronized(JCSynchronized tree) {
-        chk.checkRefType(tree.pos(), attribExpr(tree.lock, env));
+        chk.checkRefType(tree.pos(), attribExpr(tree.lock, env), false);
         attribStat(tree.body, env);
         result = null;
     }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Mon Mar 12 11:46:54 2018 +0530
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Mon Mar 12 15:44:20 2018 +0530
@@ -731,9 +731,10 @@
      *  or a type variable.
      *  @param pos           Position to be used for error reporting.
      *  @param t             The type to be checked.
+     *  @param valueOK       If false, a value class does not qualify
      */
-    Type checkRefType(DiagnosticPosition pos, Type t) {
-        if (t.isReference() && !types.isValue(t))
+    Type checkRefType(DiagnosticPosition pos, Type t, boolean valueOK) {
+        if (t.isReference() && (valueOK || !types.isValue(t)))
             return t;
         else
             return typeTagError(pos,
@@ -741,6 +742,15 @@
                                 t);
     }
 
+    /** Check that type is a reference type, i.e. a class, interface or array type
+     *  or a type variable.
+     *  @param pos           Position to be used for error reporting.
+     *  @param t             The type to be checked.
+     */
+    Type checkRefType(DiagnosticPosition pos, Type t) {
+        return checkRefType(pos, t, true);
+    }
+
     /** Check that each type is a reference type, i.e. a class, interface or array type
      *  or a type variable.
      *  @param trees         Original trees, used for error reporting.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/valhalla/lworld-values/ValuesAsRefs.java	Mon Mar 12 15:44:20 2018 +0530
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @summary Test that values code like a class - i.e are accepted in some places where only references used be
+ * @run main ValuesAsRefs
+ */
+
+import java.util.ArrayList;
+
+public final __ByValue class ValuesAsRefs {
+
+    final ArrayList<? extends ValuesAsRefs> ao = null; // values can be wildcard bounds.
+
+    final __ByValue class I implements java.io.Serializable {
+        final int y = 42;
+    }
+
+    void foo() {
+        I i = this.__MakeDefault I();  // values can be enclosing instances.
+        Object o = (I & java.io.Serializable) i; // values can be used in intersection casts
+    }
+    <T> void goo() {
+        this.<ValuesAsRefs>goo(); // values can be type arguments to generic method calls
+    }
+
+    public static void main(String [] args) {
+        Object o = null;
+        ArrayList<ValuesAsRefs.I> aloi = new ArrayList<>(); // values can be type arguments.
+        boolean OK = false;
+        try {
+            aloi.add((ValuesAsRefs.I) o);
+        } catch (NullPointerException npe) {
+            OK = true;
+        }
+        if (!OK)
+            throw new AssertionError("Missing NPE");
+    }
+}
\ No newline at end of file