6730476: invalid "unchecked generic array" warning
authormcimadamore
Wed, 21 Apr 2010 12:24:56 +0100
changeset 5489 e7af65bf7577
parent 5322 ac6d66f658cb
child 5490 a237b108549d
6730476: invalid "unchecked generic array" warning Summary: Reifiable-ness of varargs element type should be checked after JLS3 15.12.2.8 Reviewed-by: jjg
langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java
langtools/src/share/classes/com/sun/tools/javac/comp/Check.java
langtools/src/share/classes/com/sun/tools/javac/comp/Infer.java
langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java
langtools/test/tools/javac/varargs/6730476/T6730476a.java
langtools/test/tools/javac/varargs/6730476/T6730476b.java
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Sat Apr 17 08:12:45 2010 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Wed Apr 21 12:24:56 2010 +0100
@@ -2621,12 +2621,10 @@
             }
             if (useVarargs) {
                 JCTree tree = env.tree;
-                Type argtype = owntype.getParameterTypes().last();
-                if (!types.isReifiable(argtype))
-                    chk.warnUnchecked(env.tree.pos(),
-                                      "unchecked.generic.array.creation",
-                                      argtype);
-                Type elemtype = types.elemtype(argtype);
+                if (owntype.getReturnType().tag != FORALL || warned) {
+                    chk.checkVararg(env.tree.pos(), owntype.getParameterTypes());
+                }
+                Type elemtype = types.elemtype(owntype.getParameterTypes().last());
                 switch (tree.getTag()) {
                 case JCTree.APPLY:
                     ((JCMethodInvocation) tree).varargsElement = elemtype;
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java	Sat Apr 17 08:12:45 2010 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java	Wed Apr 21 12:24:56 2010 +0100
@@ -677,6 +677,19 @@
         }
     }
 
+    /**
+     * Check that vararg method call is sound
+     * @param pos Position to be used for error reporting.
+     * @param argtypes Actual arguments supplied to vararg method.
+     */
+    void checkVararg(DiagnosticPosition pos, List<Type> argtypes) {
+        Type argtype = argtypes.last();
+        if (!types.isReifiable(argtype))
+            warnUnchecked(pos,
+                              "unchecked.generic.array.creation",
+                              argtype);
+    }
+
     /** Check that given modifiers are legal for given symbol and
      *  return modifiers together with any implicit modififiers for that symbol.
      *  Warning: we can't use flags() here since this method
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Infer.java	Sat Apr 17 08:12:45 2010 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Infer.java	Wed Apr 21 12:24:56 2010 +0100
@@ -287,7 +287,8 @@
     /** Instantiate method type `mt' by finding instantiations of
      *  `tvars' so that method can be applied to `argtypes'.
      */
-    public Type instantiateMethod(List<Type> tvars,
+    public Type instantiateMethod(final Env<AttrContext> env,
+                                  List<Type> tvars,
                                   MethodType mt,
                                   final List<Type> argtypes,
                                   final boolean allowBoxing,
@@ -416,6 +417,9 @@
                     // check that inferred bounds conform to their bounds
                     checkWithinBounds(all_tvars,
                            types.subst(inferredTypes, tvars, inferred), warn);
+                    if (useVarargs) {
+                        chk.checkVararg(env.tree.pos(), formals);
+                    }
                     return super.inst(inferred, types);
             }};
             return mt2;
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Sat Apr 17 08:12:45 2010 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Wed Apr 21 12:24:56 2010 +0100
@@ -345,7 +345,8 @@
 
         if (instNeeded)
             return
-            infer.instantiateMethod(tvars,
+            infer.instantiateMethod(env,
+                                    tvars,
                                     (MethodType)mt,
                                     argtypes,
                                     allowBoxing,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/varargs/6730476/T6730476a.java	Wed Apr 21 12:24:56 2010 +0100
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2010 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6730476
+ *
+ * @summary invalid "unchecked generic array" warning
+ * @author mcimadamore
+ * @compile T6730476a.java -Xlint -Werror
+ *
+ */
+
+class T6730476a {
+    <T> void f(int i, T ... x) {}
+    void g() {
+      f(1);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/varargs/6730476/T6730476b.java	Wed Apr 21 12:24:56 2010 +0100
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2010 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6730476
+ *
+ * @summary invalid "unchecked generic array" warning
+ * @author mcimadamore
+ * @compile T6730476b.java -Xlint -Werror
+ *
+ */
+
+class T6730476b {
+    java.util.List<Integer> ints = java.util.Arrays.asList();
+}