mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-20 11:04:34 +02:00
7039931: Project Coin: diamond inference fail with generic constructor explicit type-arguments
Diamond should be disallowed in cases where explicit generic constructor parameters are specified Reviewed-by: jjg
This commit is contained in:
parent
ea2c9e0af0
commit
fcbd7a77c4
11 changed files with 45 additions and 126 deletions
|
@ -681,6 +681,12 @@ public class Check {
|
|||
"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;
|
||||
}
|
||||
|
|
|
@ -1624,6 +1624,9 @@ compiler.misc.diamond=\
|
|||
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}
|
||||
|
|
|
@ -21,20 +21,14 @@
|
|||
* 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
|
||||
*/
|
||||
// key: compiler.misc.diamond.and.explicit.params
|
||||
// key: compiler.err.cant.apply.diamond.1
|
||||
|
||||
class Pos02<X> {
|
||||
|
||||
Pos02(X x) {}
|
||||
|
||||
|
||||
Pos02<X> test(X x) {
|
||||
return new Pos02<>(x);
|
||||
class DiamondAndAnonClass {
|
||||
static class Foo<X> {
|
||||
<Z> Foo() {}
|
||||
}
|
||||
void m() {
|
||||
Foo<String> foo = new <Integer> Foo<>();
|
||||
}
|
||||
}
|
|
@ -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 @@ public class GenericConstructorAndDiamondTest {
|
|||
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 @@ public class GenericConstructorAndDiamondTest {
|
|||
for (TypeArgumentKind declArgKind : TypeArgumentKind.values()) {
|
||||
for (TypeArgArity arity : TypeArgArity.values()) {
|
||||
for (TypeArgumentKind useArgKind : TypeArgumentKind.values()) {
|
||||
for (TypeArgumentKind diamondArgKind : TypeArgumentKind.values()) {
|
||||
for (ArgumentKind argKind : ArgumentKind.values()) {
|
||||
new GenericConstructorAndDiamondTest(boundKind, constructorKind,
|
||||
declArgKind, arity, useArgKind, argKind).run(comp, fm);
|
||||
declArgKind, arity, useArgKind, diamondArgKind, argKind).run(comp, fm);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -165,18 +175,21 @@ public class GenericConstructorAndDiamondTest {
|
|||
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 @@ public class GenericConstructorAndDiamondTest {
|
|||
"}\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 @@ public class GenericConstructorAndDiamondTest {
|
|||
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 @@ public class GenericConstructorAndDiamondTest {
|
|||
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" +
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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
|
|
@ -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>("");
|
||||
}
|
|
@ -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
|
|
@ -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);
|
||||
}
|
|
@ -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
|
|
@ -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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue