提交 15f5d760 编写于 作者: M mcimadamore

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
上级 9fa2df8e
...@@ -681,6 +681,12 @@ public class Check { ...@@ -681,6 +681,12 @@ public class Check {
"cant.apply.diamond.1", "cant.apply.diamond.1",
t, diags.fragment("diamond.non.generic", t)); t, diags.fragment("diamond.non.generic", t));
return types.createErrorType(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 { } else {
return t; return t;
} }
......
...@@ -1624,6 +1624,9 @@ compiler.misc.diamond=\ ...@@ -1624,6 +1624,9 @@ compiler.misc.diamond=\
compiler.misc.diamond.non.generic=\ compiler.misc.diamond.non.generic=\
cannot use ''<>'' with non-generic class {0} 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 # 0: type, 1: list of type
compiler.misc.explicit.param.do.not.conform.to.bounds=\ compiler.misc.explicit.param.do.not.conform.to.bounds=\
explicit type argument {0} does not conform to declared bound(s) {1} explicit type argument {0} does not conform to declared bound(s) {1}
......
...@@ -21,20 +21,14 @@ ...@@ -21,20 +21,14 @@
* questions. * questions.
*/ */
/* // key: compiler.misc.diamond.and.explicit.params
* @test // key: compiler.err.cant.apply.diamond.1
* @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) {} class DiamondAndAnonClass {
static class Foo<X> {
<Z> Foo() {}
Pos02<X> test(X x) { }
return new Pos02<>(x); void m() {
Foo<String> foo = new <Integer> Foo<>();
} }
} }
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
/* /*
* @test * @test
* @bug 7030150 * @bug 7030150 7039931
* @summary Type inference for generic instance creation failed for formal type parameter * @summary Type inference for generic instance creation failed for formal type parameter
*/ */
...@@ -125,6 +125,14 @@ public class GenericConstructorAndDiamondTest { ...@@ -125,6 +125,14 @@ public class GenericConstructorAndDiamondTest {
default: return false; default: return false;
} }
} }
boolean matches(TypeArgumentKind other) {
switch (other) {
case STRING: return this != INTEGER;
case INTEGER: return this != STRING;
default: return true;
}
}
} }
enum ArgumentKind { enum ArgumentKind {
...@@ -149,9 +157,11 @@ public class GenericConstructorAndDiamondTest { ...@@ -149,9 +157,11 @@ public class GenericConstructorAndDiamondTest {
for (TypeArgumentKind declArgKind : TypeArgumentKind.values()) { for (TypeArgumentKind declArgKind : TypeArgumentKind.values()) {
for (TypeArgArity arity : TypeArgArity.values()) { for (TypeArgArity arity : TypeArgArity.values()) {
for (TypeArgumentKind useArgKind : TypeArgumentKind.values()) { for (TypeArgumentKind useArgKind : TypeArgumentKind.values()) {
for (ArgumentKind argKind : ArgumentKind.values()) { for (TypeArgumentKind diamondArgKind : TypeArgumentKind.values()) {
new GenericConstructorAndDiamondTest(boundKind, constructorKind, for (ArgumentKind argKind : ArgumentKind.values()) {
declArgKind, arity, useArgKind, argKind).run(comp, fm); new GenericConstructorAndDiamondTest(boundKind, constructorKind,
declArgKind, arity, useArgKind, diamondArgKind, argKind).run(comp, fm);
}
} }
} }
} }
...@@ -165,18 +175,21 @@ public class GenericConstructorAndDiamondTest { ...@@ -165,18 +175,21 @@ public class GenericConstructorAndDiamondTest {
TypeArgumentKind declTypeArgumentKind; TypeArgumentKind declTypeArgumentKind;
TypeArgArity useTypeArgArity; TypeArgArity useTypeArgArity;
TypeArgumentKind useTypeArgumentKind; TypeArgumentKind useTypeArgumentKind;
TypeArgumentKind diamondTypeArgumentKind;
ArgumentKind argumentKind; ArgumentKind argumentKind;
JavaSource source; JavaSource source;
DiagnosticChecker diagChecker; DiagnosticChecker diagChecker;
GenericConstructorAndDiamondTest(BoundKind boundKind, ConstructorKind constructorKind, GenericConstructorAndDiamondTest(BoundKind boundKind, ConstructorKind constructorKind,
TypeArgumentKind declTypeArgumentKind, TypeArgArity useTypeArgArity, TypeArgumentKind declTypeArgumentKind, TypeArgArity useTypeArgArity,
TypeArgumentKind useTypeArgumentKind, ArgumentKind argumentKind) { TypeArgumentKind useTypeArgumentKind, TypeArgumentKind diamondTypeArgumentKind,
ArgumentKind argumentKind) {
this.boundKind = boundKind; this.boundKind = boundKind;
this.constructorKind = constructorKind; this.constructorKind = constructorKind;
this.declTypeArgumentKind = declTypeArgumentKind; this.declTypeArgumentKind = declTypeArgumentKind;
this.useTypeArgArity = useTypeArgArity; this.useTypeArgArity = useTypeArgArity;
this.useTypeArgumentKind = useTypeArgumentKind; this.useTypeArgumentKind = useTypeArgumentKind;
this.diamondTypeArgumentKind = diamondTypeArgumentKind;
this.argumentKind = argumentKind; this.argumentKind = argumentKind;
this.source = new JavaSource(); this.source = new JavaSource();
this.diagChecker = new DiagnosticChecker(); this.diagChecker = new DiagnosticChecker();
...@@ -189,7 +202,7 @@ public class GenericConstructorAndDiamondTest { ...@@ -189,7 +202,7 @@ public class GenericConstructorAndDiamondTest {
"}\n" + "}\n" +
"class Test {\n" + "class Test {\n" +
"void test() {\n" + "void test() {\n" +
"Foo#TA1 f = new #TA2 Foo<>(#A);\n" + "Foo#TA1 f = new #TA2 Foo<#TA3>(#A);\n" +
"}\n" + "}\n" +
"}\n"; "}\n";
...@@ -201,6 +214,7 @@ public class GenericConstructorAndDiamondTest { ...@@ -201,6 +214,7 @@ public class GenericConstructorAndDiamondTest {
replace("#CK", constructorKind.constrStr) replace("#CK", constructorKind.constrStr)
.replace("#TA1", declTypeArgumentKind.getArgs(TypeArgArity.ONE)) .replace("#TA1", declTypeArgumentKind.getArgs(TypeArgArity.ONE))
.replace("#TA2", useTypeArgumentKind.getArgs(useTypeArgArity)) .replace("#TA2", useTypeArgumentKind.getArgs(useTypeArgArity))
.replace("#TA3", diamondTypeArgumentKind.typeargStr)
.replace("#A", argumentKind.argStr); .replace("#A", argumentKind.argStr);
} }
...@@ -227,9 +241,15 @@ public class GenericConstructorAndDiamondTest { ...@@ -227,9 +241,15 @@ public class GenericConstructorAndDiamondTest {
boolean badMethodTypeArg = constructorKind != ConstructorKind.NON_GENERIC && boolean badMethodTypeArg = constructorKind != ConstructorKind.NON_GENERIC &&
!useTypeArgumentKind.matches(argumentKind); !useTypeArgumentKind.matches(argumentKind);
boolean badGenericType = !boundKind.matches(declTypeArgumentKind); boolean badExplicitParams = (useTypeArgumentKind != TypeArgumentKind.NONE &&
diamondTypeArgumentKind == TypeArgumentKind.NONE) ||
!boundKind.matches(diamondTypeArgumentKind);
boolean badGenericType = !boundKind.matches(declTypeArgumentKind) ||
!diamondTypeArgumentKind.matches(declTypeArgumentKind);
boolean shouldFail = badActual || badArity || badMethodTypeArg || badGenericType; boolean shouldFail = badActual || badArity ||
badMethodTypeArg || badExplicitParams || badGenericType;
if (shouldFail != diagChecker.errorFound) { if (shouldFail != diagChecker.errorFound) {
throw new Error("invalid diagnostics for source:\n" + throw new Error("invalid diagnostics for source:\n" +
......
/*
* @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);
}
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
/*
* @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>("");
}
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
/*
* @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);
}
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
/*
* 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);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册