Skip to content

Commit d4fac05

Browse files
committed
GROOVY-10367
1 parent 76286cf commit d4fac05

4 files changed

Lines changed: 33 additions & 10 deletions

File tree

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/TypeCheckedTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5138,6 +5138,26 @@ public void testTypeChecked10357() {
51385138
runConformTest(sources, "1");
51395139
}
51405140

5141+
@Test
5142+
public void testTypeChecked10367() {
5143+
//@formatter:off
5144+
String[] sources = {
5145+
"Main.groovy",
5146+
"@groovy.transform.TupleConstructor(defaults=false)\n" +
5147+
"class C<X, Y extends X> {\n" + // works without Y
5148+
" X x\n" +
5149+
"}\n" +
5150+
"@groovy.transform.TypeChecked\n" +
5151+
"def <Z extends Number> void test(Z z) {\n" +
5152+
" z = new C<>(z).x\n" + // Cannot assign value of type Object to variable of type Z
5153+
"}\n" +
5154+
"test(null)\n",
5155+
};
5156+
//@formatter:on
5157+
5158+
runConformTest(sources);
5159+
}
5160+
51415161
@Test
51425162
public void testTypeChecked10368() {
51435163
for (String bounds : new String[] {"T", "T extends Number", "T extends Object"}) {

base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ protected void inferDiamondType(final ConstructorCallExpression cce, final Class
12041204
// check if constructor call expression makes use of the diamond operator
12051205
if (cceType.getGenericsTypes() != null && cceType.getGenericsTypes().length == 0) {
12061206
ArgumentListExpression argumentListExpression = InvocationWriter.makeArgumentList(cce.getArguments());
1207-
/* GRECLIPSE edit -- GROOVY-9948, GROOVY-9983, GROOVY-10291, GROOVY-10368, et al.
1207+
/* GRECLIPSE edit -- GROOVY-9948, GROOVY-9983, GROOVY-10291, GROOVY-10367, GROOVY-10368, et al.
12081208
if (argumentListExpression.getExpressions().isEmpty()) {
12091209
adjustGenerics(lType, cceType);
12101210
} else {
@@ -1221,9 +1221,10 @@ protected void inferDiamondType(final ConstructorCallExpression cce, final Class
12211221
ClassNode type = GenericsUtils.parameterizeType(cceType, cceType);
12221222
type = inferReturnTypeGenerics(type, constructor, argumentListExpression);
12231223
// process target as additional type witness, if ...
1224-
if (type.toString(false).indexOf('#') > 0 // unresolved generics
1225-
// GROOVY-6232, GROOVY-9956: cce not assignment compatible
1226-
|| checkCompatibleAssignmentTypes(lType, type, cce) && !GenericsUtils.buildWildcardType(lType).isCompatibleWith(type)) {
1224+
if (lType.getGenericsTypes() != null // has generics
1225+
&& (type.toString(false).indexOf('#') > 0 // unresolved generics
1226+
// GROOVY-6232, GROOVY-9956, etc.: cce not assignment compatible
1227+
|| checkCompatibleAssignmentTypes(lType, type, cce) && !GenericsUtils.buildWildcardType(lType).isCompatibleWith(type))) {
12271228
// allow covariance of each type parameter, but maintain semantics for nested generics
12281229

12291230
ClassNode pType = GenericsUtils.parameterizeType(lType, type);

base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ protected void inferDiamondType(final ConstructorCallExpression cce, final Class
11271127
ClassNode cceType = cce.getType(), inferredType = lType;
11281128
// check if constructor call expression makes use of the diamond operator
11291129
if (cceType.getGenericsTypes() != null && cceType.getGenericsTypes().length == 0) {
1130-
/* GRECLIPSE edit -- GROOVY-9948, GROOVY-9983, GROOVY-10291, GROOVY-10368, et al.
1130+
/* GRECLIPSE edit -- GROOVY-9948, GROOVY-9983, GROOVY-10291, GROOVY-10367, GROOVY-10368, et al.
11311131
ArgumentListExpression argumentListExpression = InvocationWriter.makeArgumentList(cce.getArguments());
11321132
if (argumentListExpression.getExpressions().isEmpty()) {
11331133
adjustGenerics(lType, cceType);
@@ -1146,9 +1146,10 @@ protected void inferDiamondType(final ConstructorCallExpression cce, final Class
11461146
ClassNode type = GenericsUtils.parameterizeType(cceType, cceType);
11471147
type = inferReturnTypeGenerics(type, constructor, argumentList);
11481148
// process target as additional type witness, if ...
1149-
if (type.toString(false).indexOf('#') > 0 // unresolved generics
1150-
// GROOVY-6232, GROOVY-9956: cce not assignment compatible
1151-
|| checkCompatibleAssignmentTypes(lType, type, cce) && !GenericsUtils.buildWildcardType(lType).isCompatibleWith(type)) {
1149+
if (lType.getGenericsTypes() != null // has generics
1150+
&& (type.toString(false).indexOf('#') > 0 // unresolved generics
1151+
// GROOVY-6232, GROOVY-9956, etc.: cce not assignment compatible
1152+
|| checkCompatibleAssignmentTypes(lType, type, cce) && !GenericsUtils.buildWildcardType(lType).isCompatibleWith(type))) {
11521153
// allow covariance of each type parameter, but maintain semantics for nested generics
11531154

11541155
ClassNode pType = GenericsUtils.parameterizeType(lType, type);

base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,9 +1111,10 @@ protected void inferDiamondType(final ConstructorCallExpression cce, final Class
11111111
if (!argumentList.getExpressions().isEmpty() && constructor != null) {
11121112
ClassNode type = GenericsUtils.parameterizeType(cceType, cceType);
11131113
type = inferReturnTypeGenerics(type, constructor, argumentList);
1114-
if (type.toString(false).indexOf('#') > 0 // GROOVY-9983, GROOVY-10291, GROOVY-10368
1114+
if (lType.getGenericsTypes() != null // GROOVY-10367: nothing to inspect
1115+
&& (type.toString(false).indexOf('#') > 0 // GROOVY-9983, GROOVY-10291, GROOVY-10368: unresolved generic
11151116
// GROOVY-6232, GROOVY-9956: if cce not assignment compatible, process target as additional type witness
1116-
|| checkCompatibleAssignmentTypes(lType, type, cce) && !GenericsUtils.buildWildcardType(lType).isCompatibleWith(type)) {
1117+
|| checkCompatibleAssignmentTypes(lType, type, cce) && !GenericsUtils.buildWildcardType(lType).isCompatibleWith(type))) {
11171118
// allow covariance of each type parameter, but maintain semantics for nested generics
11181119

11191120
ClassNode pType = GenericsUtils.parameterizeType(lType, type);

0 commit comments

Comments
 (0)