Skip to content

Commit

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

@Test
public void testTypeChecked10368() {
for (String bounds : new String[] {"T", "T extends Number", "T extends Object"}) {
//@formatter:off
String[] sources = {
"Main.groovy",
"class C<" + bounds + "> {\n" +
" C(p) {\n" +
" }\n" +
"}\n" +
"void m(C<Integer> c) {\n" +
"}\n" +
"@groovy.transform.TypeChecked\n" +
"void test() {\n" +
" m(new C<>(null))\n" + // Cannot call m(C<java.lang.Integer>) with arguments [C<# extends java.lang.Number>]
"}\n" +
"test()\n",
};
//@formatter:on

runConformTest(sources);
}
}

@Test
public void testTypeChecked10414() {
//@formatter:off
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, et al.
/* GRECLIPSE edit -- GROOVY-9948, GROOVY-9983, GROOVY-10291, GROOVY-10368, et al.
if (argumentListExpression.getExpressions().isEmpty()) {
adjustGenerics(lType, cceType);
} else {
Expand All @@ -1231,8 +1231,8 @@ protected void inferDiamondType(final ConstructorCallExpression cce, final Class
if (lhs == null || rhs == null || lhs.length != rhs.length) throw new GroovyBugError(
"Parameterization failed: " + prettyPrintType(pType) + " ~ " + prettyPrintType(type));

if (java.util.stream.IntStream.range(0, lhs.length).allMatch(i ->
GenericsUtils.buildWildcardType(getCombinedBoundType(lhs[i])).isCompatibleWith(getCombinedBoundType(rhs[i])))) {
if (IntStream.range(0, lhs.length).allMatch(i ->
GenericsUtils.buildWildcardType(getCombinedBoundType(lhs[i])).isCompatibleWith(rhs[i].getType()))) {
type = pType; // lType proved to be a viable type witness
}
}
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, et al.
/* GRECLIPSE edit -- GROOVY-9948, GROOVY-9983, GROOVY-10291, GROOVY-10368, et al.
ArgumentListExpression argumentListExpression = InvocationWriter.makeArgumentList(cce.getArguments());
if (argumentListExpression.getExpressions().isEmpty()) {
adjustGenerics(lType, cceType);
Expand Down Expand Up @@ -1156,8 +1156,8 @@ protected void inferDiamondType(final ConstructorCallExpression cce, final Class
if (lhs == null || rhs == null || lhs.length != rhs.length) throw new GroovyBugError(
"Parameterization failed: " + prettyPrintType(pType) + " ~ " + prettyPrintType(type));

if (java.util.stream.IntStream.range(0, lhs.length).allMatch(i ->
GenericsUtils.buildWildcardType(getCombinedBoundType(lhs[i])).isCompatibleWith(getCombinedBoundType(rhs[i])))) {
if (IntStream.range(0, lhs.length).allMatch(i ->
GenericsUtils.buildWildcardType(getCombinedBoundType(lhs[i])).isCompatibleWith(rhs[i].getType()))) {
type = pType; // lType proved to be a viable type witness
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ 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
if (type.toString(false).indexOf('#') > 0 // GROOVY-9983, GROOVY-10291, GROOVY-10368
// GROOVY-6232, GROOVY-9956: if cce not assignment compatible, process target as additional type witness
|| checkCompatibleAssignmentTypes(lType, type, cce) && !GenericsUtils.buildWildcardType(lType).isCompatibleWith(type)) {
// allow covariance of each type parameter, but maintain semantics for nested generics
Expand All @@ -1121,8 +1121,8 @@ protected void inferDiamondType(final ConstructorCallExpression cce, final Class
if (lhs == null || rhs == null || lhs.length != rhs.length) throw new GroovyBugError(
"Parameterization failed: " + prettyPrintType(pType) + " ~ " + prettyPrintType(type));

if (java.util.stream.IntStream.range(0, lhs.length).allMatch(i ->
GenericsUtils.buildWildcardType(getCombinedBoundType(lhs[i])).isCompatibleWith(getCombinedBoundType(rhs[i])))) {
if (IntStream.range(0, lhs.length).allMatch(i ->
GenericsUtils.buildWildcardType(getCombinedBoundType(lhs[i])).isCompatibleWith(rhs[i].getType()))) {
type = pType; // lType proved to be a viable type witness
}
}
Expand Down

0 comments on commit 76286cf

Please sign in to comment.