Fri, 22 Mar 2013 12:39:34 +0000
8010101: Intersection type cast issues redundant unchecked warning
Summary: Code for checking intersection type cast is incorrectly swapping operands, leading to spurious warnings
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java Fri Mar 22 12:38:12 2013 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java Fri Mar 22 12:39:34 2013 +0000 1.3 @@ -1418,23 +1418,10 @@ 1.4 } 1.5 } 1.6 1.7 - if (t.isCompound()) { 1.8 - Warner oldWarner = warnStack.head; 1.9 - warnStack.head = noWarnings; 1.10 - if (!visit(supertype(t), s)) 1.11 - return false; 1.12 - for (Type intf : interfaces(t)) { 1.13 - if (!visit(intf, s)) 1.14 - return false; 1.15 - } 1.16 - if (warnStack.head.hasLint(LintCategory.UNCHECKED)) 1.17 - oldWarner.warn(LintCategory.UNCHECKED); 1.18 - return true; 1.19 - } 1.20 - 1.21 - if (s.isCompound()) { 1.22 - // call recursively to reuse the above code 1.23 - return visitClassType((ClassType)s, t); 1.24 + if (t.isCompound() || s.isCompound()) { 1.25 + return !t.isCompound() ? 1.26 + visitIntersectionType((IntersectionClassType)s, t, true) : 1.27 + visitIntersectionType((IntersectionClassType)t, s, false); 1.28 } 1.29 1.30 if (s.tag == CLASS || s.tag == ARRAY) { 1.31 @@ -1512,6 +1499,18 @@ 1.32 return false; 1.33 } 1.34 1.35 + boolean visitIntersectionType(IntersectionClassType ict, Type s, boolean reverse) { 1.36 + Warner warn = noWarnings; 1.37 + for (Type c : ict.getComponents()) { 1.38 + warn.clear(); 1.39 + if (reverse ? !isCastable(s, c, warn) : !isCastable(c, s, warn)) 1.40 + return false; 1.41 + } 1.42 + if (warn.hasLint(LintCategory.UNCHECKED)) 1.43 + warnStack.head.warn(LintCategory.UNCHECKED); 1.44 + return true; 1.45 + } 1.46 + 1.47 @Override 1.48 public Boolean visitArrayType(ArrayType t, Type s) { 1.49 switch (s.tag) { 1.50 @@ -3889,11 +3888,18 @@ 1.51 } 1.52 1.53 private boolean giveWarning(Type from, Type to) { 1.54 - Type subFrom = asSub(from, to.tsym); 1.55 - return to.isParameterized() && 1.56 - (!(isUnbounded(to) || 1.57 - isSubtype(from, to) || 1.58 - ((subFrom != null) && containsType(to.allparams(), subFrom.allparams())))); 1.59 + List<Type> bounds = to.isCompound() ? 1.60 + ((IntersectionClassType)to).getComponents() : List.of(to); 1.61 + for (Type b : bounds) { 1.62 + Type subFrom = asSub(from, b.tsym); 1.63 + if (b.isParameterized() && 1.64 + (!(isUnbounded(b) || 1.65 + isSubtype(from, b) || 1.66 + ((subFrom != null) && containsType(b.allparams(), subFrom.allparams()))))) { 1.67 + return true; 1.68 + } 1.69 + } 1.70 + return false; 1.71 } 1.72 1.73 private List<Type> superClosure(Type t, Type s) {
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/lambda/Intersection02.java Fri Mar 22 12:39:34 2013 +0000 2.3 @@ -0,0 +1,41 @@ 2.4 +/* 2.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +/* 2.28 + * @test 2.29 + * @bug 8010101 2.30 + * @summary Intersection type cast issues redundant unchecked warning 2.31 + * @compile/fail/ref=Intersection02.out -Werror -Xlint:unchecked -XDrawDiagnostics Intersection02.java 2.32 + */ 2.33 +import java.io.Serializable; 2.34 +import java.util.List; 2.35 + 2.36 +class Intersection02 { 2.37 + 2.38 + interface P<X> { } 2.39 + 2.40 + void test(List<String> ls) { 2.41 + Object o1 = (List<String> & Serializable)ls; 2.42 + Object o2 = (List<String> & Serializable & P<String>)ls; 2.43 + } 2.44 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/lambda/Intersection02.out Fri Mar 22 12:39:34 2013 +0000 3.3 @@ -0,0 +1,4 @@ 3.4 +Intersection02.java:39:62: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), java.util.List<java.lang.String>, java.lang.Object&java.util.List<java.lang.String>&java.io.Serializable&Intersection02.P<java.lang.String> 3.5 +- compiler.err.warnings.and.werror 3.6 +1 error 3.7 +1 warning