Skip to content
Permalink
Browse files
GROOVY-10368: STC: accept target type witness for bounded diamond
  • Loading branch information
eric-milles committed Mar 15, 2022
1 parent a80121d commit 01bb08e069fca08af00442978c95cd4a39c36c5a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
@@ -1097,7 +1097,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
@@ -1107,8 +1107,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
}
}
@@ -1167,6 +1167,21 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
'''
}

// GROOVY-10368
void testDiamondInferrenceFromConstructor25() {
['T', 'T extends Number', 'T extends Object'].each {
assertScript """
class C<$it> {
C(p) {
}
}
void m(C<Integer> c) {
}
m(new C<>(null)) // Cannot call m(C<Integer>) with arguments [C<# extends Number>]
"""
}
}

// GROOVY-10280
void testTypeArgumentPropagation() {
assertScript '''

0 comments on commit 01bb08e

Please sign in to comment.