6938454: Unable to determine generic type in program that compiles under Java 6
authormcimadamore
Thu, 29 Jul 2010 15:56:25 +0100
changeset 6154 c1b0c6641b92
parent 6153 277c719ff46e
child 6155 214f78bb8006
6938454: Unable to determine generic type in program that compiles under Java 6 Summary: a redundant dubtyping check causes spurious inference failure Reviewed-by: jjg
langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java
langtools/src/share/classes/com/sun/tools/javac/comp/Infer.java
langtools/test/tools/javac/generics/inference/6938454/T6938454a.java
langtools/test/tools/javac/generics/inference/6938454/T6938454b.java
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Jul 27 11:52:11 2010 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Jul 29 15:56:25 2010 +0100
@@ -1694,8 +1694,22 @@
             //if the type of the instance creation expression is an interface
             //skip the method resolution step (JLS 15.12.2.7). The type to be
             //inferred is of the kind <X1,X2, ... Xn>C<X1,X2, ... Xn>
-            clazztype = new ForAll(clazztype.tsym.type.allparams(),
-                    clazztype.tsym.type);
+            clazztype = new ForAll(clazztype.tsym.type.allparams(), clazztype.tsym.type) {
+                @Override
+                public List<Type> getConstraints(TypeVar tv, ConstraintKind ck) {
+                    switch (ck) {
+                        case EXTENDS: return types.getBounds(tv);
+                        default: return List.nil();
+                    }
+                }
+                @Override
+                public Type inst(List<Type> inferred, Types types) throws Infer.NoInstanceException {
+                    // check that inferred bounds conform to their bounds
+                    infer.checkWithinBounds(tvars,
+                           types.subst(tvars, tvars, inferred), Warner.noWarnings);
+                    return super.inst(inferred, types);
+                }
+            };
         } else {
             //if the type of the instance creation expression is a class type
             //apply method resolution inference (JLS 15.12.2.7). The return type
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Infer.java	Tue Jul 27 11:52:11 2010 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Infer.java	Thu Jul 29 15:56:25 2010 +0100
@@ -256,7 +256,7 @@
             UndetVar uv = (UndetVar) l.head;
             TypeVar tv = (TypeVar)uv.qtype;
             ListBuffer<Type> hibounds = new ListBuffer<Type>();
-            for (Type t : that.getConstraints(tv, ConstraintKind.EXTENDS).prependList(types.getBounds(tv))) {
+            for (Type t : that.getConstraints(tv, ConstraintKind.EXTENDS)) {
                 if (!t.containsSome(that.tvars) && t.tag != BOT) {
                     hibounds.append(t);
                 }
@@ -280,7 +280,6 @@
         // check bounds
         List<Type> targs = Type.map(undetvars, getInstFun);
         targs = types.subst(targs, that.tvars, targs);
-        checkWithinBounds(that.tvars, targs, warn);
         return chk.checkType(warn.pos(), that.inst(targs, types), to);
     }
 
@@ -398,7 +397,7 @@
                         UndetVar uv = (UndetVar)t;
                         if (uv.qtype == tv) {
                             switch (ck) {
-                                case EXTENDS: return uv.hibounds;
+                                case EXTENDS: return uv.hibounds.appendList(types.subst(types.getBounds(tv), all_tvars, inferredTypes));
                                 case SUPER: return uv.lobounds;
                                 case EQUAL: return uv.inst != null ? List.of(uv.inst) : List.<Type>nil();
                             }
@@ -458,7 +457,7 @@
 
     /** check that type parameters are within their bounds.
      */
-    private void checkWithinBounds(List<Type> tvars,
+    void checkWithinBounds(List<Type> tvars,
                                    List<Type> arguments,
                                    Warner warn)
         throws InvalidInstanceException {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/inference/6938454/T6938454a.java	Thu Jul 29 15:56:25 2010 +0100
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2010 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
+ * @bug 6938454
+ *
+ * @summary Unable to determine generic type in program that compiles under Java 6
+ * @author mcimadamore
+ * @compile T6938454a.java
+ *
+ */
+
+class T6938454a {
+
+    static abstract class A { }
+
+    static class B extends A { }
+
+    B getB(B b) {
+        return makeA(b);
+    }
+
+    <X extends A, Y extends X> Y makeA(X x) {
+        return (Y)new B();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/inference/6938454/T6938454b.java	Thu Jul 29 15:56:25 2010 +0100
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2010 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.
+ */
+
+import java.util.List;
+
+/*
+ * @test
+ * @bug 6938454
+ *
+ * @summary Unable to determine generic type in program that compiles under Java 6
+ * @author mcimadamore
+ * @compile T6938454b.java
+ *
+ */
+
+class T6938454b {
+
+    static interface A {}
+    static interface B extends A {}
+    static class C implements B {}
+
+    <T, R extends T, S extends R> List<R> m(List<T> l, S s) {
+        return null;
+    }
+
+    List<B> test(List<A> la) {
+        return m(la, new C());
+    }
+}