diff --git a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java index f74e3e15cbf..76f6e9456fe 100644 --- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java +++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java @@ -1694,8 +1694,22 @@ public class Attr extends JCTree.Visitor { //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 C - clazztype = new ForAll(clazztype.tsym.type.allparams(), - clazztype.tsym.type); + clazztype = new ForAll(clazztype.tsym.type.allparams(), clazztype.tsym.type) { + @Override + public List getConstraints(TypeVar tv, ConstraintKind ck) { + switch (ck) { + case EXTENDS: return types.getBounds(tv); + default: return List.nil(); + } + } + @Override + public Type inst(List 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 diff --git a/langtools/src/share/classes/com/sun/tools/javac/comp/Infer.java b/langtools/src/share/classes/com/sun/tools/javac/comp/Infer.java index 6951385dfd3..eb53fbcb319 100644 --- a/langtools/src/share/classes/com/sun/tools/javac/comp/Infer.java +++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Infer.java @@ -256,7 +256,7 @@ public class Infer { UndetVar uv = (UndetVar) l.head; TypeVar tv = (TypeVar)uv.qtype; ListBuffer hibounds = new ListBuffer(); - 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 @@ public class Infer { // check bounds List 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 @@ public class Infer { 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.nil(); } @@ -458,7 +457,7 @@ public class Infer { /** check that type parameters are within their bounds. */ - private void checkWithinBounds(List tvars, + void checkWithinBounds(List tvars, List arguments, Warner warn) throws InvalidInstanceException { diff --git a/langtools/test/tools/javac/generics/inference/6938454/T6938454a.java b/langtools/test/tools/javac/generics/inference/6938454/T6938454a.java new file mode 100644 index 00000000000..be8ac4088a9 --- /dev/null +++ b/langtools/test/tools/javac/generics/inference/6938454/T6938454a.java @@ -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); + } + + Y makeA(X x) { + return (Y)new B(); + } +} diff --git a/langtools/test/tools/javac/generics/inference/6938454/T6938454b.java b/langtools/test/tools/javac/generics/inference/6938454/T6938454b.java new file mode 100644 index 00000000000..e9d390ca55d --- /dev/null +++ b/langtools/test/tools/javac/generics/inference/6938454/T6938454b.java @@ -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 {} + + List m(List l, S s) { + return null; + } + + List test(List la) { + return m(la, new C()); + } +}