6467183: javac fails to raise unchecked warning on cast of parameterized generic subclass
authormcimadamore
Thu, 05 Mar 2009 17:24:08 +0000
changeset 2218 a8ec0baae870
parent 2217 120470d1e00d
child 2219 03b1b17c4b2e
6467183: javac fails to raise unchecked warning on cast of parameterized generic subclass Summary: cleanup code for generating unchecked cast warnings Reviewed-by: jjg
langtools/src/share/classes/com/sun/tools/javac/code/Types.java
langtools/test/tools/javac/cast/6467183/T6467183a.java
langtools/test/tools/javac/cast/6467183/T6467183a.out
langtools/test/tools/javac/cast/6467183/T6467183b.java
--- a/langtools/src/share/classes/com/sun/tools/javac/code/Types.java	Mon Mar 02 15:11:29 2009 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/code/Types.java	Thu Mar 05 17:24:08 2009 +0000
@@ -1010,8 +1010,8 @@
                                 && !disjointTypes(aHigh.allparams(), lowSub.allparams())
                                 && !disjointTypes(aLow.allparams(), highSub.allparams())
                                 && !disjointTypes(aLow.allparams(), lowSub.allparams())) {
-                                if (upcast ? giveWarning(a, highSub) || giveWarning(a, lowSub)
-                                           : giveWarning(highSub, a) || giveWarning(lowSub, a))
+                                if (upcast ? giveWarning(a, b) :
+                                    giveWarning(b, a))
                                     warnStack.head.warnUnchecked();
                                 return true;
                             }
@@ -3224,9 +3224,11 @@
     }
 
     private boolean giveWarning(Type from, Type to) {
-        // To and from are (possibly different) parameterizations
-        // of the same class or interface
-        return to.isParameterized() && !containsType(to.allparams(), from.allparams());
+        Type subFrom = asSub(from, to.tsym);
+        return to.isParameterized() &&
+                (!(isUnbounded(to) ||
+                isSubtype(from, to) ||
+                ((subFrom != null) && isSameType(subFrom, to))));
     }
 
     private List<Type> superClosure(Type t, Type s) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/cast/6467183/T6467183a.java	Thu Mar 05 17:24:08 2009 +0000
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2009 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
+ * @author mcimadamore
+ * @bug     6467183
+ * @summary
+ * @compile/fail/ref=T6467183a.out -Xlint:unchecked -Werror -XDrawDiagnostics T6467183a.java
+ */
+
+class T6467183a<T> {
+
+    class A<S> {}
+    class B extends A<Integer> {}
+    class C<X> extends A<X> {}
+
+    void cast1(B b) {
+        Object o = (A<T>)b;
+    }
+
+    void cast2(B b) {
+        Object o = (A<? extends Number>)b;
+    }
+
+    void cast3(A<Integer> a) {
+        Object o = (C<? extends Number>)a;
+    }
+
+    void cast4(A<Integer> a) {
+        Object o = (C<? extends Integer>)a;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/cast/6467183/T6467183a.out	Thu Mar 05 17:24:08 2009 +0000
@@ -0,0 +1,6 @@
+T6467183a.java:39:26: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), T6467183a<T>.B, T6467183a<T>.A<T>
+T6467183a.java:47:41: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), T6467183a<T>.A<java.lang.Integer>, T6467183a<T>.C<? extends java.lang.Number>
+T6467183a.java:51:42: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), T6467183a<T>.A<java.lang.Integer>, T6467183a<T>.C<? extends java.lang.Integer>
+- compiler.err.warnings.and.werror
+1 error
+3 warnings
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/cast/6467183/T6467183b.java	Thu Mar 05 17:24:08 2009 +0000
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2009 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
+ * @author mcimadamore
+ * @bug     6467183
+ * @summary
+ * @compile/fail -Xlint:unchecked -Werror -XDrawDiagnostics T6467183b.java
+ */
+
+class T6665356b<T> {
+
+    class A<S> {}
+    class B<X> extends A<X> {}
+
+    void cast(A<? extends Number> a) {
+        Object o = (B<? extends Integer>)a;
+    }
+}