# HG changeset patch # User mcimadamore # Date 1236273848 0 # Node ID a8ec0baae870c14d6f8618e37ded191a5fb3abe9 # Parent 120470d1e00d9398ae252038eacb6db164650255 6467183: javac fails to raise unchecked warning on cast of parameterized generic subclass Summary: cleanup code for generating unchecked cast warnings Reviewed-by: jjg diff -r 120470d1e00d -r a8ec0baae870 langtools/src/share/classes/com/sun/tools/javac/code/Types.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 superClosure(Type t, Type s) { diff -r 120470d1e00d -r a8ec0baae870 langtools/test/tools/javac/cast/6467183/T6467183a.java --- /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 { + + class A {} + class B extends A {} + class C extends A {} + + void cast1(B b) { + Object o = (A)b; + } + + void cast2(B b) { + Object o = (A)b; + } + + void cast3(A a) { + Object o = (C)a; + } + + void cast4(A a) { + Object o = (C)a; + } +} diff -r 120470d1e00d -r a8ec0baae870 langtools/test/tools/javac/cast/6467183/T6467183a.out --- /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.B, T6467183a.A +T6467183a.java:47:41: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), T6467183a.A, T6467183a.C +T6467183a.java:51:42: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), T6467183a.A, T6467183a.C +- compiler.err.warnings.and.werror +1 error +3 warnings diff -r 120470d1e00d -r a8ec0baae870 langtools/test/tools/javac/cast/6467183/T6467183b.java --- /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 { + + class A {} + class B extends A {} + + void cast(A a) { + Object o = (B)a; + } +}