6714835: Safe cast is rejected (with warning) by javac

Thu, 04 Nov 2010 12:58:29 +0000

author
mcimadamore
date
Thu, 04 Nov 2010 12:58:29 +0000
changeset 736
e9e41c88b03e
parent 735
f2048d9c666e
child 737
e406f0645b7e

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

src/share/classes/com/sun/tools/javac/code/Types.java file | annotate | diff | comparison | revisions
test/tools/javac/cast/6467183/T6467183a.out file | annotate | diff | comparison | revisions
test/tools/javac/cast/6714835/T6714835.java file | annotate | diff | comparison | revisions
test/tools/javac/cast/6714835/T6714835.out file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java	Thu Nov 04 12:57:48 2010 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java	Thu Nov 04 12:58:29 2010 +0000
     1.3 @@ -3151,7 +3151,7 @@
     1.4          return to.isParameterized() &&
     1.5                  (!(isUnbounded(to) ||
     1.6                  isSubtype(from, to) ||
     1.7 -                ((subFrom != null) && isSameType(subFrom, to))));
     1.8 +                ((subFrom != null) && containsType(to.allparams(), subFrom.allparams()))));
     1.9      }
    1.10  
    1.11      private List<Type> superClosure(Type t, Type s) {
     2.1 --- a/test/tools/javac/cast/6467183/T6467183a.out	Thu Nov 04 12:57:48 2010 +0000
     2.2 +++ b/test/tools/javac/cast/6467183/T6467183a.out	Thu Nov 04 12:58:29 2010 +0000
     2.3 @@ -1,6 +1,4 @@
     2.4  T6467183a.java:16:26: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), T6467183a<T>.B, T6467183a<T>.A<T>
     2.5 -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>
     2.6 -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>
     2.7  - compiler.err.warnings.and.werror
     2.8  1 error
     2.9 -3 warnings
    2.10 +1 warning
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javac/cast/6714835/T6714835.java	Thu Nov 04 12:58:29 2010 +0000
     3.3 @@ -0,0 +1,21 @@
     3.4 +/*
     3.5 + * @test /nodynamiccopyright/
     3.6 + * @author mcimadamore
     3.7 + * @bug     6714835
     3.8 + * @summary Safe cast is rejected (with warning) by javac
     3.9 + * @compile/fail/ref=T6714835.out -Xlint:unchecked -Werror -XDrawDiagnostics T6714835.java
    3.10 + */
    3.11 +
    3.12 +import java.util.*;
    3.13 +
    3.14 +class T6714835 {
    3.15 +    void cast1(Iterable<? extends Integer> x) {
    3.16 +        Collection<? extends Number> x1 = (Collection<? extends Number>)x; //ok
    3.17 +        Collection<? super Integer> x2 = (Collection<? super Integer>)x; //warn
    3.18 +    }
    3.19 +
    3.20 +    void cast2(Iterable<? super Number> x) {
    3.21 +        Collection<? super Integer> x1 = (Collection<? super Integer>)x; //ok
    3.22 +        Collection<? extends Number> x2 = (Collection<? extends Number>)x; //warn
    3.23 +    }
    3.24 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/tools/javac/cast/6714835/T6714835.out	Thu Nov 04 12:58:29 2010 +0000
     4.3 @@ -0,0 +1,5 @@
     4.4 +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>
     4.5 +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>
     4.6 +- compiler.err.warnings.and.werror
     4.7 +1 error
     4.8 +2 warnings

mercurial