changeset 53971:e8e2b6a83723 lworld

8217958: [lworld] Trouble assigning/casting to a value array type with parameterized element types
author sadayapalam
date Tue, 29 Jan 2019 13:44:36 +0530
parents c9bd516ddc27
children 4bd0c15e9a63
files src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java test/langtools/tools/javac/valhalla/lworld-values/GenericArrayRegression.java test/langtools/tools/javac/valhalla/lworld-values/GenericArrayTest.java test/langtools/tools/javac/valhalla/lworld-values/GenericArrayTest.out
diffstat 4 files changed, 103 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java	Mon Jan 28 17:29:40 2019 +0530
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java	Tue Jan 29 13:44:36 2019 +0530
@@ -1070,10 +1070,15 @@
             if (t.hasTag(ARRAY) && s.hasTag(ARRAY)) {
                 if (((ArrayType)t).elemtype.isPrimitive()) {
                     return isSameType(elemtype(t), elemtype(s));
-                } else if (isValue(((ArrayType)t).elemtype) || isValue(((ArrayType)s).elemtype)) {
-                    return isSameType(elemtype(t), elemtype(s));
                 } else {
-                    return isSubtypeUncheckedInternal(elemtype(t), elemtype(s), false, warn);
+                    Type et = elemtype(t);
+                    Type es = elemtype(s);
+                    if (!isSubtypeUncheckedInternal(et, es, false, warn))
+                        return false;
+                    if (isValue(et) || isValue(es)) {
+                        return isSameType(erasure(et), erasure(es));
+                    }
+                    return true;
                 }
             } else if (isSubtype(t, s, capture)) {
                 return true;
@@ -1870,10 +1875,15 @@
                 case ARRAY:
                     if (elemtype(t).isPrimitive() || elemtype(s).isPrimitive()) {
                         return elemtype(t).hasTag(elemtype(s).getTag());
-                    } else if (isValue(t.elemtype) || isValue(((ArrayType)s).elemtype)) {
-                        return isSameType(elemtype(t), elemtype(s));
                     } else {
-                        return visit(elemtype(t), elemtype(s));
+                        Type et = elemtype(t);
+                        Type es = elemtype(s);
+                        if (!visit(et, es))
+                            return false;
+                        if (isValue(et) || isValue(es)) {
+                            return isSameType(erasure(et), erasure(es));
+                        }
+                        return true;
                     }
                 default:
                     return false;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/valhalla/lworld-values/GenericArrayRegression.java	Tue Jan 29 13:44:36 2019 +0530
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2019, 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
+ * @bug 8217958
+ * @summary Trouble assigning/casting to a value array type with parameterized element type
+ * @compile GenericArrayRegression.java
+ */
+
+public class GenericArrayRegression {
+
+   static value class Entry<E> {
+     private final int value;
+
+     public Entry(int value) {
+       this.value = value;
+     }
+
+     public static <E> Entry<E>[] of() {
+       return (Entry<E>[])new Entry<?>[0];
+     }
+   }
+
+   public static void main(String[] args) {
+     var entry = Entry.of();
+   }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/valhalla/lworld-values/GenericArrayTest.java	Tue Jan 29 13:44:36 2019 +0530
@@ -0,0 +1,24 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8217958
+ * @summary Trouble assigning/casting to a value array type with parameterized element type
+ * @compile/fail/ref=GenericArrayTest.out -Xlint:all -Werror -XDrawDiagnostics -XDdev GenericArrayTest.java
+ */
+
+public class GenericArrayTest {
+
+    public value class Value<T> {
+
+        T t = null;
+
+        void foo() {
+            Value<T>[] v = new Value[1024];
+            Value<GenericArrayTest>[] vx = new Value[1024];
+            Value<String>[] vs = new Value[1024];
+            v = (Value<T> []) new Value[1024];
+            vx = (Value <GenericArrayTest> [])new Value[1024];
+            vs = (Value <String> []) new Value[1024];
+            vx = vs;
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/valhalla/lworld-values/GenericArrayTest.out	Tue Jan 29 13:44:36 2019 +0530
@@ -0,0 +1,15 @@
+GenericArrayTest.java:15:32: compiler.warn.raw.class.use: GenericArrayTest.Value, GenericArrayTest.Value<T>
+GenericArrayTest.java:15:28: compiler.warn.prob.found.req: (compiler.misc.unchecked.assign), GenericArrayTest.Value[], GenericArrayTest.Value<T>[]
+GenericArrayTest.java:16:48: compiler.warn.raw.class.use: GenericArrayTest.Value, GenericArrayTest.Value<T>
+GenericArrayTest.java:16:44: compiler.warn.prob.found.req: (compiler.misc.unchecked.assign), GenericArrayTest.Value[], GenericArrayTest.Value<GenericArrayTest>[]
+GenericArrayTest.java:17:38: compiler.warn.raw.class.use: GenericArrayTest.Value, GenericArrayTest.Value<T>
+GenericArrayTest.java:17:34: compiler.warn.prob.found.req: (compiler.misc.unchecked.assign), GenericArrayTest.Value[], GenericArrayTest.Value<java.lang.String>[]
+GenericArrayTest.java:18:35: compiler.warn.raw.class.use: GenericArrayTest.Value, GenericArrayTest.Value<T>
+GenericArrayTest.java:18:31: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), GenericArrayTest.Value[], GenericArrayTest.Value<T>[]
+GenericArrayTest.java:19:51: compiler.warn.raw.class.use: GenericArrayTest.Value, GenericArrayTest.Value<T>
+GenericArrayTest.java:19:47: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), GenericArrayTest.Value[], GenericArrayTest.Value<GenericArrayTest>[]
+GenericArrayTest.java:20:42: compiler.warn.raw.class.use: GenericArrayTest.Value, GenericArrayTest.Value<T>
+GenericArrayTest.java:20:38: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), GenericArrayTest.Value[], GenericArrayTest.Value<java.lang.String>[]
+GenericArrayTest.java:21:18: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: GenericArrayTest.Value<java.lang.String>[], GenericArrayTest.Value<GenericArrayTest>[])
+1 error
+12 warnings