changeset 760:e9e41c88b03e

6714835: Safe cast is rejected (with warning) by javac Summary: Rules for unchecked cast conversion do not take into account type-containment Reviewed-by: jjg
author mcimadamore
date Thu, 04 Nov 2010 12:58:29 +0000
parents f2048d9c666e
children e406f0645b7e
files src/share/classes/com/sun/tools/javac/code/Types.java test/tools/javac/cast/6467183/T6467183a.out test/tools/javac/cast/6714835/T6714835.java test/tools/javac/cast/6714835/T6714835.out
diffstat 4 files changed, 28 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/code/Types.java	Thu Nov 04 12:57:48 2010 +0000
+++ b/src/share/classes/com/sun/tools/javac/code/Types.java	Thu Nov 04 12:58:29 2010 +0000
@@ -3151,7 +3151,7 @@
         return to.isParameterized() &&
                 (!(isUnbounded(to) ||
                 isSubtype(from, to) ||
-                ((subFrom != null) && isSameType(subFrom, to))));
+                ((subFrom != null) && containsType(to.allparams(), subFrom.allparams()))));
     }
 
     private List<Type> superClosure(Type t, Type s) {
--- a/test/tools/javac/cast/6467183/T6467183a.out	Thu Nov 04 12:57:48 2010 +0000
+++ b/test/tools/javac/cast/6467183/T6467183a.out	Thu Nov 04 12:58:29 2010 +0000
@@ -1,6 +1,4 @@
 T6467183a.java:16:26: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), T6467183a<T>.B, T6467183a<T>.A<T>
-T6467183a.java:24: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:28: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
+1 warning
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/cast/6714835/T6714835.java	Thu Nov 04 12:58:29 2010 +0000
@@ -0,0 +1,21 @@
+/*
+ * @test /nodynamiccopyright/
+ * @author mcimadamore
+ * @bug     6714835
+ * @summary Safe cast is rejected (with warning) by javac
+ * @compile/fail/ref=T6714835.out -Xlint:unchecked -Werror -XDrawDiagnostics T6714835.java
+ */
+
+import java.util.*;
+
+class T6714835 {
+    void cast1(Iterable<? extends Integer> x) {
+        Collection<? extends Number> x1 = (Collection<? extends Number>)x; //ok
+        Collection<? super Integer> x2 = (Collection<? super Integer>)x; //warn
+    }
+
+    void cast2(Iterable<? super Number> x) {
+        Collection<? super Integer> x1 = (Collection<? super Integer>)x; //ok
+        Collection<? extends Number> x2 = (Collection<? extends Number>)x; //warn
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/cast/6714835/T6714835.out	Thu Nov 04 12:58:29 2010 +0000
@@ -0,0 +1,5 @@
+T6714835.java:14:71: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), java.lang.Iterable<compiler.misc.type.captureof: 1, ? extends java.lang.Integer>, java.util.Collection<? super java.lang.Integer>
+T6714835.java:19:73: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), java.lang.Iterable<compiler.misc.type.captureof: 1, ? super java.lang.Number>, java.util.Collection<? extends java.lang.Number>
+- compiler.err.warnings.and.werror
+1 error
+2 warnings