Mon, 16 Aug 2010 14:58:10 +0100
6369605: Unconstrained type variables fails to include bounds
Summary: unconstrained type-variables with recursive bounds are not inferred properly
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/code/Type.java Mon Aug 16 14:56:23 2010 +0100 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Type.java Mon Aug 16 14:58:10 2010 +0100 1.3 @@ -347,11 +347,17 @@ 1.4 return false; 1.5 } 1.6 1.7 - /** Does this type contain an occurrence of some type in `elems'? 1.8 + /** Does this type contain an occurrence of some type in 'ts'? 1.9 */ 1.10 - public boolean containsSome(List<Type> ts) { 1.11 - for (List<Type> l = ts; l.nonEmpty(); l = l.tail) 1.12 - if (this.contains(ts.head)) return true; 1.13 + public boolean containsAny(List<Type> ts) { 1.14 + for (Type t : ts) 1.15 + if (this.contains(t)) return true; 1.16 + return false; 1.17 + } 1.18 + 1.19 + public static boolean containsAny(List<Type> ts1, List<Type> ts2) { 1.20 + for (Type t : ts1) 1.21 + if (t.containsAny(ts2)) return true; 1.22 return false; 1.23 } 1.24 1.25 @@ -431,6 +437,10 @@ 1.26 this.bound = bound; 1.27 } 1.28 1.29 + public boolean contains(Type t) { 1.30 + return kind != UNBOUND && type.contains(t); 1.31 + } 1.32 + 1.33 public boolean isSuperBound() { 1.34 return kind == SUPER || 1.35 kind == UNBOUND; 1.36 @@ -681,7 +691,9 @@ 1.37 return 1.38 elem == this 1.39 || (isParameterized() 1.40 - && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem))); 1.41 + && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem))) 1.42 + || (isCompound() 1.43 + && (supertype_field.contains(elem) || contains(interfaces_field, elem))); 1.44 } 1.45 1.46 public void complete() {
2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Infer.java Mon Aug 16 14:56:23 2010 +0100 2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Infer.java Mon Aug 16 14:58:10 2010 +0100 2.3 @@ -138,24 +138,73 @@ 2.4 /** A mapping that returns its type argument with every UndetVar replaced 2.5 * by its `inst' field. Throws a NoInstanceException 2.6 * if this not possible because an `inst' field is null. 2.7 + * Note: mutually referring undertvars will be left uninstantiated 2.8 + * (that is, they will be replaced by the underlying type-variable). 2.9 */ 2.10 + 2.11 Mapping getInstFun = new Mapping("getInstFun") { 2.12 public Type apply(Type t) { 2.13 switch (t.tag) { 2.14 - case UNKNOWN: 2.15 - throw ambiguousNoInstanceException 2.16 - .setMessage("undetermined.type"); 2.17 - case UNDETVAR: 2.18 - UndetVar that = (UndetVar) t; 2.19 - if (that.inst == null) 2.20 + case UNKNOWN: 2.21 throw ambiguousNoInstanceException 2.22 - .setMessage("type.variable.has.undetermined.type", 2.23 - that.qtype); 2.24 - return apply(that.inst); 2.25 - default: 2.26 - return t.map(this); 2.27 + .setMessage("undetermined.type"); 2.28 + case UNDETVAR: 2.29 + UndetVar that = (UndetVar) t; 2.30 + if (that.inst == null) 2.31 + throw ambiguousNoInstanceException 2.32 + .setMessage("type.variable.has.undetermined.type", 2.33 + that.qtype); 2.34 + return isConstraintCyclic(that) ? 2.35 + that.qtype : 2.36 + apply(that.inst); 2.37 + default: 2.38 + return t.map(this); 2.39 } 2.40 } 2.41 + 2.42 + private boolean isConstraintCyclic(UndetVar uv) { 2.43 + Types.UnaryVisitor<Boolean> constraintScanner = 2.44 + new Types.UnaryVisitor<Boolean>() { 2.45 + 2.46 + List<Type> seen = List.nil(); 2.47 + 2.48 + Boolean visit(List<Type> ts) { 2.49 + for (Type t : ts) { 2.50 + if (visit(t)) return true; 2.51 + } 2.52 + return false; 2.53 + } 2.54 + 2.55 + public Boolean visitType(Type t, Void ignored) { 2.56 + return false; 2.57 + } 2.58 + 2.59 + @Override 2.60 + public Boolean visitClassType(ClassType t, Void ignored) { 2.61 + if (t.isCompound()) { 2.62 + return visit(types.supertype(t)) || 2.63 + visit(types.interfaces(t)); 2.64 + } else { 2.65 + return visit(t.getTypeArguments()); 2.66 + } 2.67 + } 2.68 + @Override 2.69 + public Boolean visitWildcardType(WildcardType t, Void ignored) { 2.70 + return visit(t.type); 2.71 + } 2.72 + 2.73 + @Override 2.74 + public Boolean visitUndetVar(UndetVar t, Void ignored) { 2.75 + if (seen.contains(t)) { 2.76 + return true; 2.77 + } else { 2.78 + seen = seen.prepend(t); 2.79 + return visit(t.inst); 2.80 + } 2.81 + } 2.82 + }; 2.83 + return constraintScanner.visit(uv); 2.84 + } 2.85 }; 2.86 2.87 /*************************************************************************** 2.88 @@ -257,10 +306,9 @@ 2.89 TypeVar tv = (TypeVar)uv.qtype; 2.90 ListBuffer<Type> hibounds = new ListBuffer<Type>(); 2.91 for (Type t : that.getConstraints(tv, ConstraintKind.EXTENDS)) { 2.92 - if (!t.containsSome(that.tvars) && t.tag != BOT) { 2.93 - hibounds.append(t); 2.94 - } 2.95 + hibounds.append(types.subst(t, that.tvars, undetvars)); 2.96 } 2.97 + 2.98 List<Type> inst = that.getConstraints(tv, ConstraintKind.EQUAL); 2.99 if (inst.nonEmpty() && inst.head.tag != BOT) { 2.100 uv.inst = inst.head; 2.101 @@ -279,9 +327,32 @@ 2.102 2.103 // check bounds 2.104 List<Type> targs = Type.map(undetvars, getInstFun); 2.105 - targs = types.subst(targs, that.tvars, targs); 2.106 + if (Type.containsAny(targs, that.tvars)) { 2.107 + //replace uninferred type-vars 2.108 + targs = types.subst(targs, 2.109 + that.tvars, 2.110 + instaniateAsUninferredVars(undetvars, that.tvars)); 2.111 + } 2.112 return chk.checkType(warn.pos(), that.inst(targs, types), to); 2.113 } 2.114 + //where 2.115 + private List<Type> instaniateAsUninferredVars(List<Type> undetvars, List<Type> tvars) { 2.116 + ListBuffer<Type> new_targs = ListBuffer.lb(); 2.117 + //step 1 - create syntethic captured vars 2.118 + for (Type t : undetvars) { 2.119 + UndetVar uv = (UndetVar)t; 2.120 + Type newArg = new CapturedType(t.tsym.name, t.tsym, uv.inst, syms.botType, null); 2.121 + new_targs = new_targs.append(newArg); 2.122 + } 2.123 + //step 2 - replace synthetic vars in their bounds 2.124 + for (Type t : new_targs.toList()) { 2.125 + CapturedType ct = (CapturedType)t; 2.126 + ct.bound = types.subst(ct.bound, tvars, new_targs.toList()); 2.127 + WildcardType wt = new WildcardType(ct.bound, BoundKind.EXTENDS, syms.boundClass); 2.128 + ct.wildcard = wt; 2.129 + } 2.130 + return new_targs.toList(); 2.131 + } 2.132 2.133 /** Instantiate method type `mt' by finding instantiations of 2.134 * `tvars' so that method can be applied to `argtypes'.
3.1 --- a/test/tools/javac/Diagnostics/6862608/T6862608a.out Mon Aug 16 14:56:23 2010 +0100 3.2 +++ b/test/tools/javac/Diagnostics/6862608/T6862608a.out Mon Aug 16 14:58:10 2010 +0100 3.3 @@ -1,3 +1,3 @@ 3.4 -T6862608a.java:19:41: compiler.err.invalid.inferred.types: T, (compiler.misc.inferred.do.not.conform.to.params: java.lang.Iterable<? extends java.util.Comparator<? super java.lang.String>>, java.util.List<java.util.Comparator<?>>) 3.5 +T6862608a.java:19:41: compiler.err.prob.found.req: (compiler.misc.incompatible.types.1: (compiler.misc.no.conforming.instance.exists: T, java.util.Comparator<T>, java.util.Comparator<java.lang.String>)), <T>java.util.Comparator<T>, java.util.Comparator<java.lang.String> 3.6 - compiler.misc.where.description.typevar: T,{(compiler.misc.where.typevar: T, java.lang.Object, kindname.method, <T>compound(java.lang.Iterable<? extends java.util.Comparator<? super T>>))} 3.7 1 error
4.1 --- a/test/tools/javac/diags/examples.not-yet.txt Mon Aug 16 14:56:23 2010 +0100 4.2 +++ b/test/tools/javac/diags/examples.not-yet.txt Mon Aug 16 14:58:10 2010 +0100 4.3 @@ -64,6 +64,7 @@ 4.4 compiler.misc.fatal.err.cant.locate.meth # Resolve, from Lower 4.5 compiler.misc.file.does.not.contain.package 4.6 compiler.misc.illegal.start.of.class.file 4.7 +compiler.misc.inferred.do.not.conform.to.params # UNUSED (hard to see if very complex inference scenario might require this though, so leaving it in, as per JLS3) 4.8 compiler.misc.kindname.annotation 4.9 compiler.misc.kindname.enum 4.10 compiler.misc.kindname.package
5.1 --- a/test/tools/javac/diags/examples/InvalidInferredTypes.java Mon Aug 16 14:56:23 2010 +0100 5.2 +++ b/test/tools/javac/diags/examples/InvalidInferredTypes.java Mon Aug 16 14:58:10 2010 +0100 5.3 @@ -22,17 +22,17 @@ 5.4 */ 5.5 5.6 // key: compiler.err.invalid.inferred.types 5.7 -// key: compiler.misc.inferred.do.not.conform.to.params 5.8 +// key: compiler.misc.inferred.do.not.conform.to.bounds 5.9 5.10 import java.util.*; 5.11 5.12 class InvalidInferredTypes { 5.13 5.14 - <T> Comparator<T> compound(Iterable<? extends Comparator<? super T>> it) { 5.15 + <T extends List<? super T>> T makeList() { 5.16 return null; 5.17 } 5.18 5.19 - public void test(List<Comparator<?>> x) { 5.20 - Comparator<String> c3 = compound(x); 5.21 + public void test() { 5.22 + List<? super String> l = makeList(); 5.23 } 5.24 }
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/test/tools/javac/generics/inference/6369605/T6369605a.java Mon Aug 16 14:58:10 2010 +0100 6.3 @@ -0,0 +1,50 @@ 6.4 +/* 6.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6.7 + * 6.8 + * This code is free software; you can redistribute it and/or modify it 6.9 + * under the terms of the GNU General Public License version 2 only, as 6.10 + * published by the Free Software Foundation. 6.11 + * 6.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 6.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 6.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 6.15 + * version 2 for more details (a copy is included in the LICENSE file that 6.16 + * accompanied this code). 6.17 + * 6.18 + * You should have received a copy of the GNU General Public License version 6.19 + * 2 along with this work; if not, write to the Free Software Foundation, 6.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 6.21 + * 6.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 6.23 + * or visit www.oracle.com if you need additional information or have any 6.24 + * questions. 6.25 + */ 6.26 + 6.27 +/** 6.28 + * @test 6.29 + * @bug 6369605 6.30 + * @summary Unconstrained type variables fails to include bounds 6.31 + * @author mcimadamore 6.32 + * @compile T6369605a.java 6.33 + */ 6.34 +import java.util.List; 6.35 + 6.36 +class T6369605a { 6.37 + static <T extends List<T>> T m1() { 6.38 + return null; 6.39 + } 6.40 + 6.41 + static <T extends List<U>, U extends List<T>> T m2() { 6.42 + return null; 6.43 + } 6.44 + 6.45 + static <T extends List<U>, U extends List<V>, V extends List<T>> T m3() { 6.46 + return null; 6.47 + } 6.48 + 6.49 + List<?> l1 = m1(); 6.50 + List<?> l2 = m2(); 6.51 + List<?> l3 = m3(); 6.52 +} 6.53 +
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/test/tools/javac/generics/inference/6369605/T6369605b.java Mon Aug 16 14:58:10 2010 +0100 7.3 @@ -0,0 +1,49 @@ 7.4 +/* 7.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 7.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7.7 + * 7.8 + * This code is free software; you can redistribute it and/or modify it 7.9 + * under the terms of the GNU General Public License version 2 only, as 7.10 + * published by the Free Software Foundation. 7.11 + * 7.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 7.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 7.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 7.15 + * version 2 for more details (a copy is included in the LICENSE file that 7.16 + * accompanied this code). 7.17 + * 7.18 + * You should have received a copy of the GNU General Public License version 7.19 + * 2 along with this work; if not, write to the Free Software Foundation, 7.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 7.21 + * 7.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 7.23 + * or visit www.oracle.com if you need additional information or have any 7.24 + * questions. 7.25 + */ 7.26 + 7.27 +/** 7.28 + * @test 7.29 + * @bug 6369605 7.30 + * @summary Unconstrained type variables fails to include bounds 7.31 + * @author mcimadamore 7.32 + * @compile T6369605b.java 7.33 + */ 7.34 +import java.util.List; 7.35 + 7.36 +class T6369605b { 7.37 + static <T extends List<X>, X> List<T> m1() { 7.38 + return null; 7.39 + } 7.40 + 7.41 + static <T extends List<U>, U extends List<X>, X> List<T> m2() { 7.42 + return null; 7.43 + } 7.44 + 7.45 + static <T extends List<U>, U extends List<V>, V extends List<X>, X> List<T> m3() { 7.46 + return null; 7.47 + } 7.48 + 7.49 + List<?> l1 = m1(); 7.50 + List<?> l2 = m2(); 7.51 + List<?> l3 = m3(); 7.52 +}
8.1 --- a/test/tools/javac/generics/inference/6638712/T6638712a.out Mon Aug 16 14:56:23 2010 +0100 8.2 +++ b/test/tools/javac/generics/inference/6638712/T6638712a.out Mon Aug 16 14:58:10 2010 +0100 8.3 @@ -1,2 +1,2 @@ 8.4 -T6638712a.java:16:41: compiler.err.invalid.inferred.types: T, (compiler.misc.inferred.do.not.conform.to.params: java.lang.Iterable<? extends java.util.Comparator<? super java.lang.String>>, java.util.List<java.util.Comparator<?>>) 8.5 +T6638712a.java:16:41: compiler.err.prob.found.req: (compiler.misc.incompatible.types.1: (compiler.misc.no.conforming.instance.exists: T, java.util.Comparator<T>, java.util.Comparator<java.lang.String>)), <T>java.util.Comparator<T>, java.util.Comparator<java.lang.String> 8.6 1 error