# HG changeset patch # User mcimadamore # Date 1304161066 -3600 # Node ID 4caf17560ae0c448b40c6500d220b9dc7e685a29 # Parent dc3d9ef880a1d0d5b5c587d34a13d10ac787ff3b 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 diff -r dc3d9ef880a1 -r 4caf17560ae0 src/share/classes/com/sun/tools/javac/comp/Check.java --- 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; } diff -r dc3d9ef880a1 -r 4caf17560ae0 src/share/classes/com/sun/tools/javac/resources/compiler.properties --- 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} diff -r dc3d9ef880a1 -r 4caf17560ae0 test/tools/javac/diags/examples/DiamondAndExplicitParams.java --- /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 { + Foo() {} + } + void m() { + Foo foo = new Foo<>(); + } +} diff -r dc3d9ef880a1 -r 4caf17560ae0 test/tools/javac/generics/diamond/7030150/GenericConstructorAndDiamondTest.java --- 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" + diff -r dc3d9ef880a1 -r 4caf17560ae0 test/tools/javac/generics/diamond/7030150/Neg01.java --- 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 { - Foo(T t) {} - } - - Foo fi1 = new Foo<>(1); - Foo fi2 = new Foo(1); -} diff -r dc3d9ef880a1 -r 4caf17560ae0 test/tools/javac/generics/diamond/7030150/Neg01.out --- 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, (compiler.misc.no.conforming.assignment.exists: int, java.lang.String) -2 errors diff -r dc3d9ef880a1 -r 4caf17560ae0 test/tools/javac/generics/diamond/7030150/Neg02.java --- 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 { - Foo(T t) {} - } - - Foo fi1 = new Foo<>(""); - Foo fi2 = new Foo(""); -} diff -r dc3d9ef880a1 -r 4caf17560ae0 test/tools/javac/generics/diamond/7030150/Neg02.out --- 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, (compiler.misc.arg.length.mismatch) -2 errors diff -r dc3d9ef880a1 -r 4caf17560ae0 test/tools/javac/generics/diamond/7030150/Neg03.java --- 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 { - Foo(T t) {} - } - - Foo fi1 = new Foo<>(1); - Foo fi2 = new Foo(1); -} diff -r dc3d9ef880a1 -r 4caf17560ae0 test/tools/javac/generics/diamond/7030150/Neg03.out --- 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, (compiler.misc.explicit.param.do.not.conform.to.bounds: java.lang.String, java.lang.Integer) -2 errors diff -r dc3d9ef880a1 -r 4caf17560ae0 test/tools/javac/generics/diamond/7030150/Pos01.java --- 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 { - Foo(X t) {} - } - - Foo fi1 = new Foo<>(1); - Foo fi2 = new Foo(1); - Foo fi3 = new Foo<>(1); - Foo fi4 = new Foo(1); - Foo fi5 = new Foo<>(1); - Foo fi6 = new Foo(1); -} diff -r dc3d9ef880a1 -r 4caf17560ae0 test/tools/javac/generics/diamond/7030150/Pos02.java --- 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 { - - Pos02(X x) {} - - - Pos02 test(X x) { - return new Pos02<>(x); - } -}