Skip to content

Commit

Permalink
GROOVY-10367
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Mar 15, 2022
1 parent 76286cf commit d4fac05
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5138,6 +5138,26 @@ public void testTypeChecked10357() {
runConformTest(sources, "1");
}

@Test
public void testTypeChecked10367() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.TupleConstructor(defaults=false)\n" +
"class C<X, Y extends X> {\n" + // works without Y
" X x\n" +
"}\n" +
"@groovy.transform.TypeChecked\n" +
"def <Z extends Number> void test(Z z) {\n" +
" z = new C<>(z).x\n" + // Cannot assign value of type Object to variable of type Z
"}\n" +
"test(null)\n",
};
//@formatter:on

runConformTest(sources);
}

@Test
public void testTypeChecked10368() {
for (String bounds : new String[] {"T", "T extends Number", "T extends Object"}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ protected void inferDiamondType(final ConstructorCallExpression cce, final Class
// check if constructor call expression makes use of the diamond operator
if (cceType.getGenericsTypes() != null && cceType.getGenericsTypes().length == 0) {
ArgumentListExpression argumentListExpression = InvocationWriter.makeArgumentList(cce.getArguments());
/* GRECLIPSE edit -- GROOVY-9948, GROOVY-9983, GROOVY-10291, GROOVY-10368, et al.
/* GRECLIPSE edit -- GROOVY-9948, GROOVY-9983, GROOVY-10291, GROOVY-10367, GROOVY-10368, et al.
if (argumentListExpression.getExpressions().isEmpty()) {
adjustGenerics(lType, cceType);
} else {
Expand All @@ -1221,9 +1221,10 @@ protected void inferDiamondType(final ConstructorCallExpression cce, final Class
ClassNode type = GenericsUtils.parameterizeType(cceType, cceType);
type = inferReturnTypeGenerics(type, constructor, argumentListExpression);
// process target as additional type witness, if ...
if (type.toString(false).indexOf('#') > 0 // unresolved generics
// GROOVY-6232, GROOVY-9956: cce not assignment compatible
|| checkCompatibleAssignmentTypes(lType, type, cce) && !GenericsUtils.buildWildcardType(lType).isCompatibleWith(type)) {
if (lType.getGenericsTypes() != null // has generics
&& (type.toString(false).indexOf('#') > 0 // unresolved generics
// GROOVY-6232, GROOVY-9956, etc.: cce not assignment compatible
|| checkCompatibleAssignmentTypes(lType, type, cce) && !GenericsUtils.buildWildcardType(lType).isCompatibleWith(type))) {
// allow covariance of each type parameter, but maintain semantics for nested generics

ClassNode pType = GenericsUtils.parameterizeType(lType, type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ protected void inferDiamondType(final ConstructorCallExpression cce, final Class
ClassNode cceType = cce.getType(), inferredType = lType;
// check if constructor call expression makes use of the diamond operator
if (cceType.getGenericsTypes() != null && cceType.getGenericsTypes().length == 0) {
/* GRECLIPSE edit -- GROOVY-9948, GROOVY-9983, GROOVY-10291, GROOVY-10368, et al.
/* GRECLIPSE edit -- GROOVY-9948, GROOVY-9983, GROOVY-10291, GROOVY-10367, GROOVY-10368, et al.
ArgumentListExpression argumentListExpression = InvocationWriter.makeArgumentList(cce.getArguments());
if (argumentListExpression.getExpressions().isEmpty()) {
adjustGenerics(lType, cceType);
Expand All @@ -1146,9 +1146,10 @@ protected void inferDiamondType(final ConstructorCallExpression cce, final Class
ClassNode type = GenericsUtils.parameterizeType(cceType, cceType);
type = inferReturnTypeGenerics(type, constructor, argumentList);
// process target as additional type witness, if ...
if (type.toString(false).indexOf('#') > 0 // unresolved generics
// GROOVY-6232, GROOVY-9956: cce not assignment compatible
|| checkCompatibleAssignmentTypes(lType, type, cce) && !GenericsUtils.buildWildcardType(lType).isCompatibleWith(type)) {
if (lType.getGenericsTypes() != null // has generics
&& (type.toString(false).indexOf('#') > 0 // unresolved generics
// GROOVY-6232, GROOVY-9956, etc.: cce not assignment compatible
|| checkCompatibleAssignmentTypes(lType, type, cce) && !GenericsUtils.buildWildcardType(lType).isCompatibleWith(type))) {
// allow covariance of each type parameter, but maintain semantics for nested generics

ClassNode pType = GenericsUtils.parameterizeType(lType, type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1111,9 +1111,10 @@ protected void inferDiamondType(final ConstructorCallExpression cce, final Class
if (!argumentList.getExpressions().isEmpty() && constructor != null) {
ClassNode type = GenericsUtils.parameterizeType(cceType, cceType);
type = inferReturnTypeGenerics(type, constructor, argumentList);
if (type.toString(false).indexOf('#') > 0 // GROOVY-9983, GROOVY-10291, GROOVY-10368
if (lType.getGenericsTypes() != null // GROOVY-10367: nothing to inspect
&& (type.toString(false).indexOf('#') > 0 // GROOVY-9983, GROOVY-10291, GROOVY-10368: unresolved generic
// GROOVY-6232, GROOVY-9956: if cce not assignment compatible, process target as additional type witness
|| checkCompatibleAssignmentTypes(lType, type, cce) && !GenericsUtils.buildWildcardType(lType).isCompatibleWith(type)) {
|| checkCompatibleAssignmentTypes(lType, type, cce) && !GenericsUtils.buildWildcardType(lType).isCompatibleWith(type))) {
// allow covariance of each type parameter, but maintain semantics for nested generics

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

0 comments on commit d4fac05

Please sign in to comment.