changeset 1022:4caf17560ae0

7039931: Project Coin: diamond inference fail with generic constructor explicit type-arguments Summary: diamond should be disallowed in cases where explicit generic constructor parameters are specified Reviewed-by: jjg
author mcimadamore
date Sat, 30 Apr 2011 11:57:46 +0100
parents dc3d9ef880a1
children 459854f564ed
files src/share/classes/com/sun/tools/javac/comp/Check.java src/share/classes/com/sun/tools/javac/resources/compiler.properties test/tools/javac/diags/examples/DiamondAndExplicitParams.java test/tools/javac/generics/diamond/7030150/GenericConstructorAndDiamondTest.java test/tools/javac/generics/diamond/7030150/Neg01.java test/tools/javac/generics/diamond/7030150/Neg01.out test/tools/javac/generics/diamond/7030150/Neg02.java test/tools/javac/generics/diamond/7030150/Neg02.out test/tools/javac/generics/diamond/7030150/Neg03.java test/tools/javac/generics/diamond/7030150/Neg03.out test/tools/javac/generics/diamond/7030150/Pos01.java test/tools/javac/generics/diamond/7030150/Pos02.java
diffstat 12 files changed, 71 insertions(+), 152 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/comp/Check.java	Fri Apr 29 16:06:28 2011 +0100
+++ b/src/share/classes/com/sun/tools/javac/comp/Check.java	Sat Apr 30 11:57:46 2011 +0100
@@ -681,6 +681,12 @@
                 "cant.apply.diamond.1",
                 t, diags.fragment("diamond.non.generic", t));
             return types.createErrorType(t);
+        } else if (tree.typeargs != null &&
+                tree.typeargs.nonEmpty()) {
+            log.error(tree.clazz.pos(),
+                "cant.apply.diamond.1",
+                t, diags.fragment("diamond.and.explicit.params", t));
+            return types.createErrorType(t);
         } else {
             return t;
         }
--- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Fri Apr 29 16:06:28 2011 +0100
+++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Sat Apr 30 11:57:46 2011 +0100
@@ -1624,6 +1624,9 @@
 compiler.misc.diamond.non.generic=\
     cannot use ''<>'' with non-generic class {0}
 
+compiler.misc.diamond.and.explicit.params=\
+    cannot use ''<>'' with explicit type parameters for constructor
+
 # 0: type, 1: list of type
 compiler.misc.explicit.param.do.not.conform.to.bounds=\
     explicit type argument {0} does not conform to declared bound(s) {1}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/diags/examples/DiamondAndExplicitParams.java	Sat Apr 30 11:57:46 2011 +0100
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+// key: compiler.misc.diamond.and.explicit.params
+// key: compiler.err.cant.apply.diamond.1
+
+class DiamondAndAnonClass {
+    static class Foo<X> {
+        <Z> Foo() {}
+    }
+    void m() {
+        Foo<String> foo = new <Integer> Foo<>();
+    }
+}
--- a/test/tools/javac/generics/diamond/7030150/GenericConstructorAndDiamondTest.java	Fri Apr 29 16:06:28 2011 +0100
+++ b/test/tools/javac/generics/diamond/7030150/GenericConstructorAndDiamondTest.java	Sat Apr 30 11:57:46 2011 +0100
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 7030150
+ * @bug 7030150 7039931
  * @summary Type inference for generic instance creation failed for formal type parameter
  */
 
@@ -125,6 +125,14 @@
                 default: return false;
             }
         }
+
+        boolean matches(TypeArgumentKind other) {
+            switch (other) {
+                case STRING: return this != INTEGER;
+                case INTEGER: return this != STRING;
+                default: return true;
+            }
+        }
     }
 
     enum ArgumentKind {
@@ -149,9 +157,11 @@
                 for (TypeArgumentKind declArgKind : TypeArgumentKind.values()) {
                     for (TypeArgArity arity : TypeArgArity.values()) {
                         for (TypeArgumentKind useArgKind : TypeArgumentKind.values()) {
-                            for (ArgumentKind argKind : ArgumentKind.values()) {
-                                new GenericConstructorAndDiamondTest(boundKind, constructorKind,
-                                        declArgKind, arity, useArgKind, argKind).run(comp, fm);
+                            for (TypeArgumentKind diamondArgKind : TypeArgumentKind.values()) {
+                                for (ArgumentKind argKind : ArgumentKind.values()) {
+                                    new GenericConstructorAndDiamondTest(boundKind, constructorKind,
+                                            declArgKind, arity, useArgKind, diamondArgKind, argKind).run(comp, fm);
+                                }
                             }
                         }
                     }
@@ -165,18 +175,21 @@
     TypeArgumentKind declTypeArgumentKind;
     TypeArgArity useTypeArgArity;
     TypeArgumentKind useTypeArgumentKind;
+    TypeArgumentKind diamondTypeArgumentKind;
     ArgumentKind argumentKind;
     JavaSource source;
     DiagnosticChecker diagChecker;
 
     GenericConstructorAndDiamondTest(BoundKind boundKind, ConstructorKind constructorKind,
             TypeArgumentKind declTypeArgumentKind, TypeArgArity useTypeArgArity,
-            TypeArgumentKind useTypeArgumentKind, ArgumentKind argumentKind) {
+            TypeArgumentKind useTypeArgumentKind, TypeArgumentKind diamondTypeArgumentKind,
+            ArgumentKind argumentKind) {
         this.boundKind = boundKind;
         this.constructorKind = constructorKind;
         this.declTypeArgumentKind = declTypeArgumentKind;
         this.useTypeArgArity = useTypeArgArity;
         this.useTypeArgumentKind = useTypeArgumentKind;
+        this.diamondTypeArgumentKind = diamondTypeArgumentKind;
         this.argumentKind = argumentKind;
         this.source = new JavaSource();
         this.diagChecker = new DiagnosticChecker();
@@ -189,7 +202,7 @@
                           "}\n" +
                           "class Test {\n" +
                               "void test() {\n" +
-                                 "Foo#TA1 f = new #TA2 Foo<>(#A);\n" +
+                                 "Foo#TA1 f = new #TA2 Foo<#TA3>(#A);\n" +
                               "}\n" +
                           "}\n";
 
@@ -201,6 +214,7 @@
                     replace("#CK", constructorKind.constrStr)
                     .replace("#TA1", declTypeArgumentKind.getArgs(TypeArgArity.ONE))
                     .replace("#TA2", useTypeArgumentKind.getArgs(useTypeArgArity))
+                    .replace("#TA3", diamondTypeArgumentKind.typeargStr)
                     .replace("#A", argumentKind.argStr);
         }
 
@@ -227,9 +241,15 @@
         boolean badMethodTypeArg = constructorKind != ConstructorKind.NON_GENERIC &&
                 !useTypeArgumentKind.matches(argumentKind);
 
-        boolean badGenericType = !boundKind.matches(declTypeArgumentKind);
+        boolean badExplicitParams = (useTypeArgumentKind != TypeArgumentKind.NONE &&
+                diamondTypeArgumentKind == TypeArgumentKind.NONE) ||
+                !boundKind.matches(diamondTypeArgumentKind);
 
-        boolean shouldFail = badActual || badArity || badMethodTypeArg || badGenericType;
+        boolean badGenericType = !boundKind.matches(declTypeArgumentKind) ||
+                !diamondTypeArgumentKind.matches(declTypeArgumentKind);
+
+        boolean shouldFail = badActual || badArity ||
+                badMethodTypeArg || badExplicitParams || badGenericType;
 
         if (shouldFail != diagChecker.errorFound) {
             throw new Error("invalid diagnostics for source:\n" +
--- a/test/tools/javac/generics/diamond/7030150/Neg01.java	Fri Apr 29 16:06:28 2011 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,17 +0,0 @@
-/*
- * @test /nodynamiccopyright/
- * @bug 7030150
- * @summary Type inference for generic instance creation failed for formal type parameter
- *          check that explicit type-argument that causes resolution failure is rejected
- * @compile/fail/ref=Neg01.out -XDrawDiagnostics Neg01.java
- */
-
-class Neg01 {
-
-    static class Foo<X> {
-        <T> Foo(T t) {}
-    }
-
-    Foo<Integer> fi1 = new <String> Foo<>(1);
-    Foo<Integer> fi2 = new <String> Foo<Integer>(1);
-}
--- a/test/tools/javac/generics/diamond/7030150/Neg01.out	Fri Apr 29 16:06:28 2011 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-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)
-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)
-2 errors
--- a/test/tools/javac/generics/diamond/7030150/Neg02.java	Fri Apr 29 16:06:28 2011 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,17 +0,0 @@
-/*
- * @test /nodynamiccopyright/
- * @bug 7030150
- * @summary Type inference for generic instance creation failed for formal type parameter
- *          check that compiler rejects bad number of explicit type-arguments
- * @compile/fail/ref=Neg02.out -XDrawDiagnostics Neg02.java
- */
-
-class Neg02 {
-
-    static class Foo<X> {
-        <T> Foo(T t) {}
-    }
-
-    Foo<Integer> fi1 = new <String, Integer> Foo<>("");
-    Foo<Integer> fi2 = new <String, Integer> Foo<Integer>("");
-}
--- a/test/tools/javac/generics/diamond/7030150/Neg02.out	Fri Apr 29 16:06:28 2011 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-Neg02.java:15:24: compiler.err.cant.apply.diamond.1: (compiler.misc.diamond: Neg02.Foo), (compiler.misc.arg.length.mismatch)
-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)
-2 errors
--- a/test/tools/javac/generics/diamond/7030150/Neg03.java	Fri Apr 29 16:06:28 2011 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,17 +0,0 @@
-/*
- * @test /nodynamiccopyright/
- * @bug 7030150
- * @summary Type inference for generic instance creation failed for formal type parameter
- *          check that explicit type-argument that does not conform to bound is rejected
- * @compile/fail/ref=Neg03.out -XDrawDiagnostics Neg03.java
- */
-
-class Neg03 {
-
-    static class Foo<X> {
-        <T extends Integer> Foo(T t) {}
-    }
-
-    Foo<Integer> fi1 = new <String> Foo<>(1);
-    Foo<Integer> fi2 = new <String> Foo<Integer>(1);
-}
--- a/test/tools/javac/generics/diamond/7030150/Neg03.out	Fri Apr 29 16:06:28 2011 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-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)
-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)
-2 errors
--- a/test/tools/javac/generics/diamond/7030150/Pos01.java	Fri Apr 29 16:06:28 2011 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,44 +0,0 @@
-/*
- * Copyright (c) 2011, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-/*
- * @test
- * @bug 7030150
- * @summary Type inference for generic instance creation failed for formal type parameter
- *          check that redundant type-arguments on non-generic constructor are accepted
- * @compile Pos01.java
- */
-
-class Pos01 {
-
-    static class Foo<X> {
-        Foo(X t) {}
-    }
-
-    Foo<Integer> fi1 = new Foo<>(1);
-    Foo<Integer> fi2 = new Foo<Integer>(1);
-    Foo<Integer> fi3 = new <String> Foo<>(1);
-    Foo<Integer> fi4 = new <String> Foo<Integer>(1);
-    Foo<Integer> fi5 = new <String, String> Foo<>(1);
-    Foo<Integer> fi6 = new <String, String> Foo<Integer>(1);
-}
--- a/test/tools/javac/generics/diamond/7030150/Pos02.java	Fri Apr 29 16:06:28 2011 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,40 +0,0 @@
-/*
- * Copyright (c) 2011, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-/*
- * @test
- * @bug 7030150
- * @summary Type inference for generic instance creation failed for formal type parameter
- *          check that diamond in return context works w/o problems
- * @compile Pos02.java
- */
-
-class Pos02<X> {
-
-    Pos02(X x) {}
-
-
-    Pos02<X> test(X x) {
-        return new Pos02<>(x);
-    }
-}