6938454: Unable to determine generic type in program that compiles under Java 6

Thu, 29 Jul 2010 15:56:25 +0100

author
mcimadamore
date
Thu, 29 Jul 2010 15:56:25 +0100
changeset 615
36c4ec4525b4
parent 614
ed354a00f76b
child 616
e79e8efe1b3e

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

src/share/classes/com/sun/tools/javac/comp/Attr.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/comp/Infer.java file | annotate | diff | comparison | revisions
test/tools/javac/generics/inference/6938454/T6938454a.java file | annotate | diff | comparison | revisions
test/tools/javac/generics/inference/6938454/T6938454b.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Jul 27 11:52:11 2010 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Jul 29 15:56:25 2010 +0100
     1.3 @@ -1694,8 +1694,22 @@
     1.4              //if the type of the instance creation expression is an interface
     1.5              //skip the method resolution step (JLS 15.12.2.7). The type to be
     1.6              //inferred is of the kind <X1,X2, ... Xn>C<X1,X2, ... Xn>
     1.7 -            clazztype = new ForAll(clazztype.tsym.type.allparams(),
     1.8 -                    clazztype.tsym.type);
     1.9 +            clazztype = new ForAll(clazztype.tsym.type.allparams(), clazztype.tsym.type) {
    1.10 +                @Override
    1.11 +                public List<Type> getConstraints(TypeVar tv, ConstraintKind ck) {
    1.12 +                    switch (ck) {
    1.13 +                        case EXTENDS: return types.getBounds(tv);
    1.14 +                        default: return List.nil();
    1.15 +                    }
    1.16 +                }
    1.17 +                @Override
    1.18 +                public Type inst(List<Type> inferred, Types types) throws Infer.NoInstanceException {
    1.19 +                    // check that inferred bounds conform to their bounds
    1.20 +                    infer.checkWithinBounds(tvars,
    1.21 +                           types.subst(tvars, tvars, inferred), Warner.noWarnings);
    1.22 +                    return super.inst(inferred, types);
    1.23 +                }
    1.24 +            };
    1.25          } else {
    1.26              //if the type of the instance creation expression is a class type
    1.27              //apply method resolution inference (JLS 15.12.2.7). The return type
     2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Infer.java	Tue Jul 27 11:52:11 2010 -0700
     2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Infer.java	Thu Jul 29 15:56:25 2010 +0100
     2.3 @@ -256,7 +256,7 @@
     2.4              UndetVar uv = (UndetVar) l.head;
     2.5              TypeVar tv = (TypeVar)uv.qtype;
     2.6              ListBuffer<Type> hibounds = new ListBuffer<Type>();
     2.7 -            for (Type t : that.getConstraints(tv, ConstraintKind.EXTENDS).prependList(types.getBounds(tv))) {
     2.8 +            for (Type t : that.getConstraints(tv, ConstraintKind.EXTENDS)) {
     2.9                  if (!t.containsSome(that.tvars) && t.tag != BOT) {
    2.10                      hibounds.append(t);
    2.11                  }
    2.12 @@ -280,7 +280,6 @@
    2.13          // check bounds
    2.14          List<Type> targs = Type.map(undetvars, getInstFun);
    2.15          targs = types.subst(targs, that.tvars, targs);
    2.16 -        checkWithinBounds(that.tvars, targs, warn);
    2.17          return chk.checkType(warn.pos(), that.inst(targs, types), to);
    2.18      }
    2.19  
    2.20 @@ -398,7 +397,7 @@
    2.21                          UndetVar uv = (UndetVar)t;
    2.22                          if (uv.qtype == tv) {
    2.23                              switch (ck) {
    2.24 -                                case EXTENDS: return uv.hibounds;
    2.25 +                                case EXTENDS: return uv.hibounds.appendList(types.subst(types.getBounds(tv), all_tvars, inferredTypes));
    2.26                                  case SUPER: return uv.lobounds;
    2.27                                  case EQUAL: return uv.inst != null ? List.of(uv.inst) : List.<Type>nil();
    2.28                              }
    2.29 @@ -458,7 +457,7 @@
    2.30  
    2.31      /** check that type parameters are within their bounds.
    2.32       */
    2.33 -    private void checkWithinBounds(List<Type> tvars,
    2.34 +    void checkWithinBounds(List<Type> tvars,
    2.35                                     List<Type> arguments,
    2.36                                     Warner warn)
    2.37          throws InvalidInstanceException {
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javac/generics/inference/6938454/T6938454a.java	Thu Jul 29 15:56:25 2010 +0100
     3.3 @@ -0,0 +1,47 @@
     3.4 +/*
     3.5 + * Copyright 2010 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 6938454
    3.30 + *
    3.31 + * @summary Unable to determine generic type in program that compiles under Java 6
    3.32 + * @author mcimadamore
    3.33 + * @compile T6938454a.java
    3.34 + *
    3.35 + */
    3.36 +
    3.37 +class T6938454a {
    3.38 +
    3.39 +    static abstract class A { }
    3.40 +
    3.41 +    static class B extends A { }
    3.42 +
    3.43 +    B getB(B b) {
    3.44 +        return makeA(b);
    3.45 +    }
    3.46 +
    3.47 +    <X extends A, Y extends X> Y makeA(X x) {
    3.48 +        return (Y)new B();
    3.49 +    }
    3.50 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/tools/javac/generics/inference/6938454/T6938454b.java	Thu Jul 29 15:56:25 2010 +0100
     4.3 @@ -0,0 +1,49 @@
     4.4 +/*
     4.5 + * Copyright 2010 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 +import java.util.List;
    4.28 +
    4.29 +/*
    4.30 + * @test
    4.31 + * @bug 6938454
    4.32 + *
    4.33 + * @summary Unable to determine generic type in program that compiles under Java 6
    4.34 + * @author mcimadamore
    4.35 + * @compile T6938454b.java
    4.36 + *
    4.37 + */
    4.38 +
    4.39 +class T6938454b {
    4.40 +
    4.41 +    static interface A {}
    4.42 +    static interface B extends A {}
    4.43 +    static class C implements B {}
    4.44 +
    4.45 +    <T, R extends T, S extends R> List<R> m(List<T> l, S s) {
    4.46 +        return null;
    4.47 +    }
    4.48 +
    4.49 +    List<B> test(List<A> la) {
    4.50 +        return m(la, new C());
    4.51 +    }
    4.52 +}

mercurial