提交 bfa11307 编写于 作者: M mcimadamore

6467183: javac fails to raise unchecked warning on cast of parameterized generic subclass

Summary: cleanup code for generating unchecked cast warnings
Reviewed-by: jjg
上级 e8ce578c
......@@ -1010,8 +1010,8 @@ public class Types {
&& !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 @@ public class Types {
}
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) {
......
/*
* 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;
}
}
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
/*
* 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;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册