Thu, 23 Oct 2008 18:10:23 +0100
6548436: Incorrect inconvertible types error
Summary: Types.rewrite quantifiers should cope with captured type-variables properly
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java Thu Oct 23 18:00:05 2008 +0100 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java Thu Oct 23 18:10:23 2008 +0100 1.3 @@ -3367,33 +3367,67 @@ 1.4 * quantifiers) only 1.5 */ 1.6 private Type rewriteQuantifiers(Type t, boolean high, boolean rewriteTypeVars) { 1.7 - ListBuffer<Type> from = new ListBuffer<Type>(); 1.8 - ListBuffer<Type> to = new ListBuffer<Type>(); 1.9 - adaptSelf(t, from, to); 1.10 - ListBuffer<Type> rewritten = new ListBuffer<Type>(); 1.11 - List<Type> formals = from.toList(); 1.12 - boolean changed = false; 1.13 - for (Type arg : to.toList()) { 1.14 - Type bound; 1.15 - if (rewriteTypeVars && arg.tag == TYPEVAR) { 1.16 - TypeVar tv = (TypeVar)arg; 1.17 - bound = high ? tv.bound : syms.botType; 1.18 - } else { 1.19 - bound = high ? upperBound(arg) : lowerBound(arg); 1.20 + return new Rewriter(high, rewriteTypeVars).rewrite(t); 1.21 + } 1.22 + 1.23 + class Rewriter extends UnaryVisitor<Type> { 1.24 + 1.25 + boolean high; 1.26 + boolean rewriteTypeVars; 1.27 + 1.28 + Rewriter(boolean high, boolean rewriteTypeVars) { 1.29 + this.high = high; 1.30 + this.rewriteTypeVars = rewriteTypeVars; 1.31 + } 1.32 + 1.33 + Type rewrite(Type t) { 1.34 + ListBuffer<Type> from = new ListBuffer<Type>(); 1.35 + ListBuffer<Type> to = new ListBuffer<Type>(); 1.36 + adaptSelf(t, from, to); 1.37 + ListBuffer<Type> rewritten = new ListBuffer<Type>(); 1.38 + List<Type> formals = from.toList(); 1.39 + boolean changed = false; 1.40 + for (Type arg : to.toList()) { 1.41 + Type bound = visit(arg); 1.42 + if (arg != bound) { 1.43 + changed = true; 1.44 + bound = high ? makeExtendsWildcard(bound, (TypeVar)formals.head) 1.45 + : makeSuperWildcard(bound, (TypeVar)formals.head); 1.46 + } 1.47 + rewritten.append(bound); 1.48 + formals = formals.tail; 1.49 } 1.50 - Type newarg = bound; 1.51 - if (arg != bound) { 1.52 - changed = true; 1.53 - newarg = high ? makeExtendsWildcard(bound, (TypeVar)formals.head) 1.54 - : makeSuperWildcard(bound, (TypeVar)formals.head); 1.55 - } 1.56 - rewritten.append(newarg); 1.57 - formals = formals.tail; 1.58 + if (changed) 1.59 + return subst(t.tsym.type, from.toList(), rewritten.toList()); 1.60 + else 1.61 + return t; 1.62 } 1.63 - if (changed) 1.64 - return subst(t.tsym.type, from.toList(), rewritten.toList()); 1.65 - else 1.66 - return t; 1.67 + 1.68 + public Type visitType(Type t, Void s) { 1.69 + return high ? upperBound(t) : lowerBound(t); 1.70 + } 1.71 + 1.72 + @Override 1.73 + public Type visitCapturedType(CapturedType t, Void s) { 1.74 + return visitWildcardType(t.wildcard, null); 1.75 + } 1.76 + 1.77 + @Override 1.78 + public Type visitTypeVar(TypeVar t, Void s) { 1.79 + if (rewriteTypeVars) 1.80 + return high ? t.bound : syms.botType; 1.81 + else 1.82 + return t; 1.83 + } 1.84 + 1.85 + @Override 1.86 + public Type visitWildcardType(WildcardType t, Void s) { 1.87 + Type bound = high ? t.getExtendsBound() : 1.88 + t.getSuperBound(); 1.89 + if (bound == null) 1.90 + bound = high ? syms.objectType : syms.botType; 1.91 + return bound; 1.92 + } 1.93 } 1.94 1.95 /**
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/cast/6548436/T6548436a.java Thu Oct 23 18:10:23 2008 +0100 2.3 @@ -0,0 +1,40 @@ 2.4 +/* 2.5 + * Copyright 2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 2.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 2.24 + * have any questions. 2.25 + */ 2.26 + 2.27 +/* 2.28 + * @test 2.29 + * @bug 6548436 2.30 + * @summary Incorrect inconvertible types error 2.31 + * @author Maurizio Cimadamore 2.32 + * 2.33 + * @compile T6548436a.java 2.34 + */ 2.35 + 2.36 +public class T6548436a { 2.37 + 2.38 + static class Base<E extends Comparable<E>> {} 2.39 + 2.40 + static void test(Base<?> je) { 2.41 + Object o = (Base<Integer>)je; 2.42 + } 2.43 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/cast/6548436/T6548436b.java Thu Oct 23 18:10:23 2008 +0100 3.3 @@ -0,0 +1,40 @@ 3.4 +/* 3.5 + * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 3.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 3.24 + * have any questions. 3.25 + */ 3.26 + 3.27 +/* 3.28 + * @test 3.29 + * @bug 6548436 3.30 + * @summary Incorrect inconvertible types error 3.31 + * @author Maurizio Cimadamore 3.32 + * 3.33 + * @compile T6548436b.java 3.34 + */ 3.35 + 3.36 +public class T6548436b { 3.37 + 3.38 + enum E { } 3.39 + 3.40 + static void test(Enum<?> o) { 3.41 + Object e = (E)o; 3.42 + } 3.43 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/cast/6548436/T6548436c.java Thu Oct 23 18:10:23 2008 +0100 4.3 @@ -0,0 +1,42 @@ 4.4 +/* 4.5 + * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. 4.11 + * 4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 + * version 2 for more details (a copy is included in the LICENSE file that 4.16 + * accompanied this code). 4.17 + * 4.18 + * You should have received a copy of the GNU General Public License version 4.19 + * 2 along with this work; if not, write to the Free Software Foundation, 4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 + * 4.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 4.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 4.24 + * have any questions. 4.25 + */ 4.26 + 4.27 +/* 4.28 + * @test 4.29 + * @bug 6548436 4.30 + * @summary Incorrect inconvertible types error 4.31 + * @author Maurizio Cimadamore 4.32 + * 4.33 + * @compile T6548436c.java 4.34 + */ 4.35 + 4.36 +public class T6548436c { 4.37 + 4.38 + interface A<T extends A<? super T>> { } 4.39 + 4.40 + interface B extends A<B> { } 4.41 + 4.42 + static void test(A<?> a) { 4.43 + Object o = (B)a; 4.44 + } 4.45 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/test/tools/javac/cast/6548436/T6548436d.java Thu Oct 23 18:10:23 2008 +0100 5.3 @@ -0,0 +1,40 @@ 5.4 +/* 5.5 + * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. 5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.7 + * 5.8 + * This code is free software; you can redistribute it and/or modify it 5.9 + * under the terms of the GNU General Public License version 2 only, as 5.10 + * published by the Free Software Foundation. 5.11 + * 5.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 5.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 5.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 5.15 + * version 2 for more details (a copy is included in the LICENSE file that 5.16 + * accompanied this code). 5.17 + * 5.18 + * You should have received a copy of the GNU General Public License version 5.19 + * 2 along with this work; if not, write to the Free Software Foundation, 5.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 5.21 + * 5.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 5.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 5.24 + * have any questions. 5.25 + */ 5.26 + 5.27 +/* 5.28 + * @test 5.29 + * @bug 6548436 5.30 + * @summary Incorrect inconvertible types error 5.31 + * @author Maurizio Cimadamore 5.32 + * 5.33 + * @compile/fail T6548436d.java 5.34 + */ 5.35 + 5.36 +public class T6548436d { 5.37 + 5.38 + static class Base<E extends Comparable<E>> {} 5.39 + 5.40 + static void test(Base<? extends Double> je) { 5.41 + Object o = (Base<Integer>)je; 5.42 + } 5.43 +} 5.44 \ No newline at end of file