7030150: Type inference for generic instance creation failed for formal type parameter

Tue, 29 Mar 2011 16:40:51 +0100

author
mcimadamore
date
Tue, 29 Mar 2011 16:40:51 +0100
changeset 950
f5b5112ee1cc
parent 949
ddec8c712e85
child 951
de1c65ecfec2

7030150: Type inference for generic instance creation failed for formal type parameter
Summary: Problem when explicit generic constructor type-arguments are used in conjunction with diamond
Reviewed-by: jjg

src/share/classes/com/sun/tools/javac/code/Types.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/comp/Attr.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/comp/Resolve.java file | annotate | diff | comparison | revisions
test/tools/javac/generics/diamond/7030150/GenericConstructorAndDiamondTest.java file | annotate | diff | comparison | revisions
test/tools/javac/generics/diamond/7030150/Neg01.java file | annotate | diff | comparison | revisions
test/tools/javac/generics/diamond/7030150/Neg01.out file | annotate | diff | comparison | revisions
test/tools/javac/generics/diamond/7030150/Neg02.java file | annotate | diff | comparison | revisions
test/tools/javac/generics/diamond/7030150/Neg02.out file | annotate | diff | comparison | revisions
test/tools/javac/generics/diamond/7030150/Neg03.java file | annotate | diff | comparison | revisions
test/tools/javac/generics/diamond/7030150/Neg03.out file | annotate | diff | comparison | revisions
test/tools/javac/generics/diamond/7030150/Pos01.java file | annotate | diff | comparison | revisions
test/tools/javac/generics/diamond/7030150/Pos02.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java	Tue Mar 29 16:40:31 2011 +0100
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java	Tue Mar 29 16:40:51 2011 +0100
     1.3 @@ -2461,6 +2461,22 @@
     1.4              }
     1.5          };
     1.6  
     1.7 +    public Type createMethodTypeWithReturn(Type original, Type newReturn) {
     1.8 +        return original.accept(methodWithReturn, newReturn);
     1.9 +    }
    1.10 +    // where
    1.11 +        private final MapVisitor<Type> methodWithReturn = new MapVisitor<Type>() {
    1.12 +            public Type visitType(Type t, Type newReturn) {
    1.13 +                throw new IllegalArgumentException("Not a method type: " + t);
    1.14 +            }
    1.15 +            public Type visitMethodType(MethodType t, Type newReturn) {
    1.16 +                return new MethodType(t.argtypes, newReturn, t.thrown, t.tsym);
    1.17 +            }
    1.18 +            public Type visitForAll(ForAll t, Type newReturn) {
    1.19 +                return new ForAll(t.tvars, t.qtype.accept(this, newReturn));
    1.20 +            }
    1.21 +        };
    1.22 +
    1.23      // <editor-fold defaultstate="collapsed" desc="createErrorType">
    1.24      public Type createErrorType(Type originalType) {
    1.25          return new ErrorType(originalType, syms.errSymbol);
     2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Mar 29 16:40:31 2011 +0100
     2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Mar 29 16:40:51 2011 +0100
     2.3 @@ -1580,7 +1580,7 @@
     2.4          // Attribute clazz expression and store
     2.5          // symbol + type back into the attributed tree.
     2.6          Type clazztype = attribType(clazz, env);
     2.7 -        Pair<Scope,Scope> mapping = getSyntheticScopeMapping(clazztype, cdef != null);
     2.8 +        Pair<Scope,Scope> mapping = getSyntheticScopeMapping(clazztype);
     2.9          clazztype = chk.checkDiamond(tree, clazztype);
    2.10          chk.validate(clazz, localEnv);
    2.11          if (tree.encl != null) {
    2.12 @@ -1778,62 +1778,48 @@
    2.13                          Pair<Scope, Scope> mapping,
    2.14                          List<Type> argtypes,
    2.15                          List<Type> typeargtypes) {
    2.16 -        if (clazztype.isErroneous() || mapping == erroneousMapping) {
    2.17 +        if (clazztype.isErroneous() ||
    2.18 +                clazztype.isInterface() ||
    2.19 +                mapping == erroneousMapping) {
    2.20              //if the type of the instance creation expression is erroneous,
    2.21 -            //or something prevented us to form a valid mapping, return the
    2.22 -            //(possibly erroneous) type unchanged
    2.23 +            //or if it's an interface, or if something prevented us to form a valid
    2.24 +            //mapping, return the (possibly erroneous) type unchanged
    2.25              return clazztype;
    2.26          }
    2.27 -        else if (clazztype.isInterface()) {
    2.28 -            //if the type of the instance creation expression is an interface
    2.29 -            //skip the method resolution step (JLS 15.12.2.7). The type to be
    2.30 -            //inferred is of the kind <X1,X2, ... Xn>C<X1,X2, ... Xn>
    2.31 -            clazztype = new ForAll(clazztype.tsym.type.allparams(), clazztype.tsym.type) {
    2.32 -                @Override
    2.33 -                public List<Type> getConstraints(TypeVar tv, ConstraintKind ck) {
    2.34 -                    switch (ck) {
    2.35 -                        case EXTENDS: return types.getBounds(tv);
    2.36 -                        default: return List.nil();
    2.37 -                    }
    2.38 -                }
    2.39 -                @Override
    2.40 -                public Type inst(List<Type> inferred, Types types) throws Infer.NoInstanceException {
    2.41 -                    // check that inferred bounds conform to their bounds
    2.42 -                    infer.checkWithinBounds(tvars,
    2.43 -                           types.subst(tvars, tvars, inferred), Warner.noWarnings);
    2.44 -                    return super.inst(inferred, types);
    2.45 -                }
    2.46 -            };
    2.47 +
    2.48 +        //dup attribution environment and augment the set of inference variables
    2.49 +        Env<AttrContext> localEnv = env.dup(tree);
    2.50 +        localEnv.info.tvars = clazztype.tsym.type.getTypeArguments();
    2.51 +
    2.52 +        //if the type of the instance creation expression is a class type
    2.53 +        //apply method resolution inference (JLS 15.12.2.7). The return type
    2.54 +        //of the resolved constructor will be a partially instantiated type
    2.55 +        ((ClassSymbol) clazztype.tsym).members_field = mapping.snd;
    2.56 +        Symbol constructor;
    2.57 +        try {
    2.58 +            constructor = rs.resolveDiamond(tree.pos(),
    2.59 +                    localEnv,
    2.60 +                    clazztype.tsym.type,
    2.61 +                    argtypes,
    2.62 +                    typeargtypes);
    2.63 +        } finally {
    2.64 +            ((ClassSymbol) clazztype.tsym).members_field = mapping.fst;
    2.65 +        }
    2.66 +        if (constructor.kind == MTH) {
    2.67 +            ClassType ct = new ClassType(clazztype.getEnclosingType(),
    2.68 +                    clazztype.tsym.type.getTypeArguments(),
    2.69 +                    clazztype.tsym);
    2.70 +            clazztype = checkMethod(ct,
    2.71 +                    constructor,
    2.72 +                    localEnv,
    2.73 +                    tree.args,
    2.74 +                    argtypes,
    2.75 +                    typeargtypes,
    2.76 +                    localEnv.info.varArgs).getReturnType();
    2.77          } else {
    2.78 -            //if the type of the instance creation expression is a class type
    2.79 -            //apply method resolution inference (JLS 15.12.2.7). The return type
    2.80 -            //of the resolved constructor will be a partially instantiated type
    2.81 -            ((ClassSymbol) clazztype.tsym).members_field = mapping.snd;
    2.82 -            Symbol constructor;
    2.83 -            try {
    2.84 -                constructor = rs.resolveDiamond(tree.pos(),
    2.85 -                        env,
    2.86 -                        clazztype.tsym.type,
    2.87 -                        argtypes,
    2.88 -                        typeargtypes);
    2.89 -            } finally {
    2.90 -                ((ClassSymbol) clazztype.tsym).members_field = mapping.fst;
    2.91 -            }
    2.92 -            if (constructor.kind == MTH) {
    2.93 -                ClassType ct = new ClassType(clazztype.getEnclosingType(),
    2.94 -                        clazztype.tsym.type.getTypeArguments(),
    2.95 -                        clazztype.tsym);
    2.96 -                clazztype = checkMethod(ct,
    2.97 -                        constructor,
    2.98 -                        env,
    2.99 -                        tree.args,
   2.100 -                        argtypes,
   2.101 -                        typeargtypes,
   2.102 -                        env.info.varArgs).getReturnType();
   2.103 -            } else {
   2.104 -                clazztype = syms.errType;
   2.105 -            }
   2.106 +            clazztype = syms.errType;
   2.107          }
   2.108 +
   2.109          if (clazztype.tag == FORALL && !pt.isErroneous()) {
   2.110              //if the resolved constructor's return type has some uninferred
   2.111              //type-variables, infer them using the expected type and declared
   2.112 @@ -1863,34 +1849,28 @@
   2.113       *  inference. The inferred return type of the synthetic constructor IS
   2.114       *  the inferred type for the diamond operator.
   2.115       */
   2.116 -    private Pair<Scope, Scope> getSyntheticScopeMapping(Type ctype, boolean overrideProtectedAccess) {
   2.117 +    private Pair<Scope, Scope> getSyntheticScopeMapping(Type ctype) {
   2.118          if (ctype.tag != CLASS) {
   2.119              return erroneousMapping;
   2.120          }
   2.121 +
   2.122          Pair<Scope, Scope> mapping =
   2.123                  new Pair<Scope, Scope>(ctype.tsym.members(), new Scope(ctype.tsym));
   2.124 -        List<Type> typevars = ctype.tsym.type.getTypeArguments();
   2.125 +
   2.126 +        //for each constructor in the original scope, create a synthetic constructor
   2.127 +        //whose return type is the type of the class in which the constructor is
   2.128 +        //declared, and insert it into the new scope.
   2.129          for (Scope.Entry e = mapping.fst.lookup(names.init);
   2.130                  e.scope != null;
   2.131                  e = e.next()) {
   2.132 -            MethodSymbol newConstr = (MethodSymbol) e.sym.clone(ctype.tsym);
   2.133 -            if (overrideProtectedAccess && (newConstr.flags() & PROTECTED) != 0) {
   2.134 -                //make protected constructor public (this is required for
   2.135 -                //anonymous inner class creation expressions using diamond)
   2.136 -                newConstr.flags_field |= PUBLIC;
   2.137 -                newConstr.flags_field &= ~PROTECTED;
   2.138 -            }
   2.139 -            newConstr.name = names.init;
   2.140 -            List<Type> oldTypeargs = List.nil();
   2.141 -            if (newConstr.type.tag == FORALL) {
   2.142 -                oldTypeargs = ((ForAll) newConstr.type).tvars;
   2.143 -            }
   2.144 -            newConstr.type = new MethodType(newConstr.type.getParameterTypes(),
   2.145 -                    new ClassType(ctype.getEnclosingType(), ctype.tsym.type.getTypeArguments(), ctype.tsym),
   2.146 -                    newConstr.type.getThrownTypes(),
   2.147 -                    syms.methodClass);
   2.148 -            newConstr.type = new ForAll(typevars.prependList(oldTypeargs), newConstr.type);
   2.149 -            mapping.snd.enter(newConstr);
   2.150 +            Type synthRestype = new ClassType(ctype.getEnclosingType(),
   2.151 +                        ctype.tsym.type.getTypeArguments(),
   2.152 +                        ctype.tsym);
   2.153 +            MethodSymbol synhConstr = new MethodSymbol(e.sym.flags(),
   2.154 +                    names.init,
   2.155 +                    types.createMethodTypeWithReturn(e.sym.type, synthRestype),
   2.156 +                    e.sym.owner);
   2.157 +            mapping.snd.enter(synhConstr);
   2.158          }
   2.159          return mapping;
   2.160      }
     3.1 --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Tue Mar 29 16:40:31 2011 +0100
     3.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Tue Mar 29 16:40:51 2011 +0100
     3.3 @@ -338,7 +338,11 @@
     3.4  
     3.5          // tvars is the list of formal type variables for which type arguments
     3.6          // need to inferred.
     3.7 -        List<Type> tvars = env.info.tvars;
     3.8 +        List<Type> tvars = null;
     3.9 +        if (env.info.tvars != null) {
    3.10 +            tvars = types.newInstances(env.info.tvars);
    3.11 +            mt = types.subst(mt, env.info.tvars, tvars);
    3.12 +        }
    3.13          if (typeargtypes == null) typeargtypes = List.nil();
    3.14          if (mt.tag != FORALL && typeargtypes.nonEmpty()) {
    3.15              // This is not a polymorphic method, but typeargs are supplied
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/tools/javac/generics/diamond/7030150/GenericConstructorAndDiamondTest.java	Tue Mar 29 16:40:51 2011 +0100
     4.3 @@ -0,0 +1,252 @@
     4.4 +/*
     4.5 + * Copyright (c) 2011, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    4.23 + * or visit www.oracle.com if you need additional information or have any
    4.24 + * questions.
    4.25 + */
    4.26 +
    4.27 +/*
    4.28 + * @test
    4.29 + * @bug 7030150
    4.30 + * @summary Type inference for generic instance creation failed for formal type parameter
    4.31 + */
    4.32 +
    4.33 +import com.sun.source.util.JavacTask;
    4.34 +import java.net.URI;
    4.35 +import java.util.Arrays;
    4.36 +import javax.tools.Diagnostic;
    4.37 +import javax.tools.JavaCompiler;
    4.38 +import javax.tools.JavaFileObject;
    4.39 +import javax.tools.SimpleJavaFileObject;
    4.40 +import javax.tools.StandardJavaFileManager;
    4.41 +import javax.tools.ToolProvider;
    4.42 +
    4.43 +public class GenericConstructorAndDiamondTest {
    4.44 +
    4.45 +    enum BoundKind {
    4.46 +        NO_BOUND(""),
    4.47 +        STRING_BOUND("extends String"),
    4.48 +        INTEGER_BOUND("extends Integer");
    4.49 +
    4.50 +        String boundStr;
    4.51 +
    4.52 +        private BoundKind(String boundStr) {
    4.53 +            this.boundStr = boundStr;
    4.54 +        }
    4.55 +
    4.56 +        boolean matches(TypeArgumentKind tak) {
    4.57 +            switch (tak) {
    4.58 +                case NONE: return true;
    4.59 +                case STRING: return this != INTEGER_BOUND;
    4.60 +                case INTEGER: return this != STRING_BOUND;
    4.61 +                default: return false;
    4.62 +            }
    4.63 +        }
    4.64 +    }
    4.65 +
    4.66 +    enum ConstructorKind {
    4.67 +        NON_GENERIC("Foo(Object o) {}"),
    4.68 +        GENERIC_NO_BOUND("<T> Foo(T t) {}"),
    4.69 +        GENERIC_STRING_BOUND("<T extends String> Foo(T t) {}"),
    4.70 +        GENERIC_INTEGER_BOUND("<T extends Integer> Foo(T t) {}");
    4.71 +
    4.72 +        String constrStr;
    4.73 +
    4.74 +        private ConstructorKind(String constrStr) {
    4.75 +            this.constrStr = constrStr;
    4.76 +        }
    4.77 +
    4.78 +        boolean matches(ArgumentKind ak) {
    4.79 +            switch (ak) {
    4.80 +                case STRING: return this != GENERIC_INTEGER_BOUND;
    4.81 +                case INTEGER: return this != GENERIC_STRING_BOUND;
    4.82 +                default: return false;
    4.83 +            }
    4.84 +        }
    4.85 +    }
    4.86 +
    4.87 +    enum TypeArgArity {
    4.88 +        ONE(1),
    4.89 +        TWO(2),
    4.90 +        THREE(3);
    4.91 +
    4.92 +        int n;
    4.93 +
    4.94 +        private TypeArgArity(int n) {
    4.95 +            this.n = n;
    4.96 +        }
    4.97 +    }
    4.98 +
    4.99 +    enum TypeArgumentKind {
   4.100 +        NONE(""),
   4.101 +        STRING("String"),
   4.102 +        INTEGER("Integer");
   4.103 +
   4.104 +        String typeargStr;
   4.105 +
   4.106 +        private TypeArgumentKind(String typeargStr) {
   4.107 +            this.typeargStr = typeargStr;
   4.108 +        }
   4.109 +
   4.110 +        String getArgs(TypeArgArity arity) {
   4.111 +            if (this == NONE) return "";
   4.112 +            else {
   4.113 +                StringBuilder buf = new StringBuilder();
   4.114 +                String sep = "";
   4.115 +                for (int i = 0 ; i < arity.n ; i++) {
   4.116 +                    buf.append(sep);
   4.117 +                    buf.append(typeargStr);
   4.118 +                    sep = ",";
   4.119 +                }
   4.120 +                return "<" + buf.toString() + ">";
   4.121 +            }
   4.122 +        }
   4.123 +
   4.124 +        boolean matches(ArgumentKind ak) {
   4.125 +            switch (ak) {
   4.126 +                case STRING: return this != INTEGER;
   4.127 +                case INTEGER: return this != STRING;
   4.128 +                default: return false;
   4.129 +            }
   4.130 +        }
   4.131 +    }
   4.132 +
   4.133 +    enum ArgumentKind {
   4.134 +        STRING("\"\""),
   4.135 +        INTEGER("1");
   4.136 +
   4.137 +        String argStr;
   4.138 +
   4.139 +        private ArgumentKind(String argStr) {
   4.140 +            this.argStr = argStr;
   4.141 +        }
   4.142 +    }
   4.143 +
   4.144 +    public static void main(String... args) throws Exception {
   4.145 +
   4.146 +        //create default shared JavaCompiler - reused across multiple compilations
   4.147 +        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   4.148 +        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
   4.149 +
   4.150 +        for (BoundKind boundKind : BoundKind.values()) {
   4.151 +            for (ConstructorKind constructorKind : ConstructorKind.values()) {
   4.152 +                for (TypeArgumentKind declArgKind : TypeArgumentKind.values()) {
   4.153 +                    for (TypeArgArity arity : TypeArgArity.values()) {
   4.154 +                        for (TypeArgumentKind useArgKind : TypeArgumentKind.values()) {
   4.155 +                            for (ArgumentKind argKind : ArgumentKind.values()) {
   4.156 +                                new GenericConstructorAndDiamondTest(boundKind, constructorKind,
   4.157 +                                        declArgKind, arity, useArgKind, argKind).run(comp, fm);
   4.158 +                            }
   4.159 +                        }
   4.160 +                    }
   4.161 +                }
   4.162 +            }
   4.163 +        }
   4.164 +    }
   4.165 +
   4.166 +    BoundKind boundKind;
   4.167 +    ConstructorKind constructorKind;
   4.168 +    TypeArgumentKind declTypeArgumentKind;
   4.169 +    TypeArgArity useTypeArgArity;
   4.170 +    TypeArgumentKind useTypeArgumentKind;
   4.171 +    ArgumentKind argumentKind;
   4.172 +    JavaSource source;
   4.173 +    DiagnosticChecker diagChecker;
   4.174 +
   4.175 +    GenericConstructorAndDiamondTest(BoundKind boundKind, ConstructorKind constructorKind,
   4.176 +            TypeArgumentKind declTypeArgumentKind, TypeArgArity useTypeArgArity,
   4.177 +            TypeArgumentKind useTypeArgumentKind, ArgumentKind argumentKind) {
   4.178 +        this.boundKind = boundKind;
   4.179 +        this.constructorKind = constructorKind;
   4.180 +        this.declTypeArgumentKind = declTypeArgumentKind;
   4.181 +        this.useTypeArgArity = useTypeArgArity;
   4.182 +        this.useTypeArgumentKind = useTypeArgumentKind;
   4.183 +        this.argumentKind = argumentKind;
   4.184 +        this.source = new JavaSource();
   4.185 +        this.diagChecker = new DiagnosticChecker();
   4.186 +    }
   4.187 +
   4.188 +    class JavaSource extends SimpleJavaFileObject {
   4.189 +
   4.190 +        String template = "class Foo<X #BK> {\n" +
   4.191 +                              "#CK\n" +
   4.192 +                          "}\n" +
   4.193 +                          "class Test {\n" +
   4.194 +                              "void test() {\n" +
   4.195 +                                 "Foo#TA1 f = new #TA2 Foo<>(#A);\n" +
   4.196 +                              "}\n" +
   4.197 +                          "}\n";
   4.198 +
   4.199 +        String source;
   4.200 +
   4.201 +        public JavaSource() {
   4.202 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   4.203 +            source = template.replace("#BK", boundKind.boundStr).
   4.204 +                    replace("#CK", constructorKind.constrStr)
   4.205 +                    .replace("#TA1", declTypeArgumentKind.getArgs(TypeArgArity.ONE))
   4.206 +                    .replace("#TA2", useTypeArgumentKind.getArgs(useTypeArgArity))
   4.207 +                    .replace("#A", argumentKind.argStr);
   4.208 +        }
   4.209 +
   4.210 +        @Override
   4.211 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   4.212 +            return source;
   4.213 +        }
   4.214 +    }
   4.215 +
   4.216 +    void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
   4.217 +        JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
   4.218 +                null, null, Arrays.asList(source));
   4.219 +        ct.analyze();
   4.220 +        check();
   4.221 +    }
   4.222 +
   4.223 +    void check() {
   4.224 +        boolean badActual = !constructorKind.matches(argumentKind);
   4.225 +
   4.226 +        boolean badArity = constructorKind != ConstructorKind.NON_GENERIC &&
   4.227 +                useTypeArgumentKind != TypeArgumentKind.NONE &&
   4.228 +                useTypeArgArity != TypeArgArity.ONE;
   4.229 +
   4.230 +        boolean badMethodTypeArg = constructorKind != ConstructorKind.NON_GENERIC &&
   4.231 +                !useTypeArgumentKind.matches(argumentKind);
   4.232 +
   4.233 +        boolean badGenericType = !boundKind.matches(declTypeArgumentKind);
   4.234 +
   4.235 +        boolean shouldFail = badActual || badArity || badMethodTypeArg || badGenericType;
   4.236 +
   4.237 +        if (shouldFail != diagChecker.errorFound) {
   4.238 +            throw new Error("invalid diagnostics for source:\n" +
   4.239 +                source.getCharContent(true) +
   4.240 +                "\nFound error: " + diagChecker.errorFound +
   4.241 +                "\nExpected error: " + shouldFail);
   4.242 +        }
   4.243 +    }
   4.244 +
   4.245 +    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   4.246 +
   4.247 +        boolean errorFound;
   4.248 +
   4.249 +        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   4.250 +            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   4.251 +                errorFound = true;
   4.252 +            }
   4.253 +        }
   4.254 +    }
   4.255 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/tools/javac/generics/diamond/7030150/Neg01.java	Tue Mar 29 16:40:51 2011 +0100
     5.3 @@ -0,0 +1,17 @@
     5.4 +/*
     5.5 + * @test /nodynamiccopyright/
     5.6 + * @bug 7030150
     5.7 + * @summary Type inference for generic instance creation failed for formal type parameter
     5.8 + *          check that explicit type-argument that causes resolution failure is rejected
     5.9 + * @compile/fail/ref=Neg01.out -XDrawDiagnostics Neg01.java
    5.10 + */
    5.11 +
    5.12 +class Neg01 {
    5.13 +
    5.14 +    static class Foo<X> {
    5.15 +        <T> Foo(T t) {}
    5.16 +    }
    5.17 +
    5.18 +    Foo<Integer> fi1 = new <String> Foo<>(1);
    5.19 +    Foo<Integer> fi2 = new <String> Foo<Integer>(1);
    5.20 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/test/tools/javac/generics/diamond/7030150/Neg01.out	Tue Mar 29 16:40:51 2011 +0100
     6.3 @@ -0,0 +1,3 @@
     6.4 +Neg01.java:15:24: compiler.err.cant.apply.diamond.1: (compiler.misc.diamond: Neg01.Foo), (compiler.misc.infer.no.conforming.assignment.exists: X, int, java.lang.String)
     6.5 +Neg01.java:16:24: compiler.err.cant.apply.symbol.1: kindname.constructor, Foo, T, int, kindname.class, Neg01.Foo<X>, (compiler.misc.no.conforming.assignment.exists: int, java.lang.String)
     6.6 +2 errors
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/test/tools/javac/generics/diamond/7030150/Neg02.java	Tue Mar 29 16:40:51 2011 +0100
     7.3 @@ -0,0 +1,17 @@
     7.4 +/*
     7.5 + * @test /nodynamiccopyright/
     7.6 + * @bug 7030150
     7.7 + * @summary Type inference for generic instance creation failed for formal type parameter
     7.8 + *          check that compiler rejects bad number of explicit type-arguments
     7.9 + * @compile/fail/ref=Neg02.out -XDrawDiagnostics Neg02.java
    7.10 + */
    7.11 +
    7.12 +class Neg02 {
    7.13 +
    7.14 +    static class Foo<X> {
    7.15 +        <T> Foo(T t) {}
    7.16 +    }
    7.17 +
    7.18 +    Foo<Integer> fi1 = new <String, Integer> Foo<>("");
    7.19 +    Foo<Integer> fi2 = new <String, Integer> Foo<Integer>("");
    7.20 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/test/tools/javac/generics/diamond/7030150/Neg02.out	Tue Mar 29 16:40:51 2011 +0100
     8.3 @@ -0,0 +1,3 @@
     8.4 +Neg02.java:15:24: compiler.err.cant.apply.diamond.1: (compiler.misc.diamond: Neg02.Foo), (compiler.misc.arg.length.mismatch)
     8.5 +Neg02.java:16:24: compiler.err.cant.apply.symbol.1: kindname.constructor, Foo, T, java.lang.String, kindname.class, Neg02.Foo<X>, (compiler.misc.arg.length.mismatch)
     8.6 +2 errors
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/test/tools/javac/generics/diamond/7030150/Neg03.java	Tue Mar 29 16:40:51 2011 +0100
     9.3 @@ -0,0 +1,17 @@
     9.4 +/*
     9.5 + * @test /nodynamiccopyright/
     9.6 + * @bug 7030150
     9.7 + * @summary Type inference for generic instance creation failed for formal type parameter
     9.8 + *          check that explicit type-argument that does not conform to bound is rejected
     9.9 + * @compile/fail/ref=Neg03.out -XDrawDiagnostics Neg03.java
    9.10 + */
    9.11 +
    9.12 +class Neg03 {
    9.13 +
    9.14 +    static class Foo<X> {
    9.15 +        <T extends Integer> Foo(T t) {}
    9.16 +    }
    9.17 +
    9.18 +    Foo<Integer> fi1 = new <String> Foo<>(1);
    9.19 +    Foo<Integer> fi2 = new <String> Foo<Integer>(1);
    9.20 +}
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/test/tools/javac/generics/diamond/7030150/Neg03.out	Tue Mar 29 16:40:51 2011 +0100
    10.3 @@ -0,0 +1,3 @@
    10.4 +Neg03.java:15:24: compiler.err.cant.apply.diamond.1: (compiler.misc.diamond: Neg03.Foo), (compiler.misc.explicit.param.do.not.conform.to.bounds: java.lang.String, java.lang.Integer)
    10.5 +Neg03.java:16:24: compiler.err.cant.apply.symbol.1: kindname.constructor, Foo, T, int, kindname.class, Neg03.Foo<X>, (compiler.misc.explicit.param.do.not.conform.to.bounds: java.lang.String, java.lang.Integer)
    10.6 +2 errors
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/test/tools/javac/generics/diamond/7030150/Pos01.java	Tue Mar 29 16:40:51 2011 +0100
    11.3 @@ -0,0 +1,44 @@
    11.4 +/*
    11.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
    11.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    11.7 + *
    11.8 + * This code is free software; you can redistribute it and/or modify it
    11.9 + * under the terms of the GNU General Public License version 2 only, as
   11.10 + * published by the Free Software Foundation.
   11.11 + *
   11.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
   11.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   11.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   11.15 + * version 2 for more details (a copy is included in the LICENSE file that
   11.16 + * accompanied this code).
   11.17 + *
   11.18 + * You should have received a copy of the GNU General Public License version
   11.19 + * 2 along with this work; if not, write to the Free Software Foundation,
   11.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   11.21 + *
   11.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   11.23 + * or visit www.oracle.com if you need additional information or have any
   11.24 + * questions.
   11.25 + */
   11.26 +
   11.27 +/*
   11.28 + * @test
   11.29 + * @bug 7030150
   11.30 + * @summary Type inference for generic instance creation failed for formal type parameter
   11.31 + *          check that redundant type-arguments on non-generic constructor are accepted
   11.32 + * @compile Pos01.java
   11.33 + */
   11.34 +
   11.35 +class Pos01 {
   11.36 +
   11.37 +    static class Foo<X> {
   11.38 +        Foo(X t) {}
   11.39 +    }
   11.40 +
   11.41 +    Foo<Integer> fi1 = new Foo<>(1);
   11.42 +    Foo<Integer> fi2 = new Foo<Integer>(1);
   11.43 +    Foo<Integer> fi3 = new <String> Foo<>(1);
   11.44 +    Foo<Integer> fi4 = new <String> Foo<Integer>(1);
   11.45 +    Foo<Integer> fi5 = new <String, String> Foo<>(1);
   11.46 +    Foo<Integer> fi6 = new <String, String> Foo<Integer>(1);
   11.47 +}
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/test/tools/javac/generics/diamond/7030150/Pos02.java	Tue Mar 29 16:40:51 2011 +0100
    12.3 @@ -0,0 +1,40 @@
    12.4 +/*
    12.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
    12.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    12.7 + *
    12.8 + * This code is free software; you can redistribute it and/or modify it
    12.9 + * under the terms of the GNU General Public License version 2 only, as
   12.10 + * published by the Free Software Foundation.
   12.11 + *
   12.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
   12.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   12.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   12.15 + * version 2 for more details (a copy is included in the LICENSE file that
   12.16 + * accompanied this code).
   12.17 + *
   12.18 + * You should have received a copy of the GNU General Public License version
   12.19 + * 2 along with this work; if not, write to the Free Software Foundation,
   12.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   12.21 + *
   12.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   12.23 + * or visit www.oracle.com if you need additional information or have any
   12.24 + * questions.
   12.25 + */
   12.26 +
   12.27 +/*
   12.28 + * @test
   12.29 + * @bug 7030150
   12.30 + * @summary Type inference for generic instance creation failed for formal type parameter
   12.31 + *          check that diamond in return context works w/o problems
   12.32 + * @compile Pos02.java
   12.33 + */
   12.34 +
   12.35 +class Pos02<X> {
   12.36 +
   12.37 +    Pos02(X x) {}
   12.38 +
   12.39 +
   12.40 +    Pos02<X> test(X x) {
   12.41 +        return new Pos02<>(x);
   12.42 +    }
   12.43 +}

mercurial