Skip to content

Commit

Permalink
GROOVY-10698: STC: apply explicit/implicit type arguments to parameters
Browse files Browse the repository at this point in the history
3_0_X backport
  • Loading branch information
eric-milles committed Dec 11, 2023
1 parent a4e87e4 commit b909c50
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2344,9 +2344,10 @@ && findMethod(receiver, "<init>", init(argumentTypes)).size() == 1) {
&& parameters.length == argumentTypes.length - 1) {
ctor = typeCheckMapConstructor(call, receiver, arguments);
} else {
if (asBoolean(receiver.getGenericsTypes())) { // GROOVY-10283, GROOVY-10316, GROOVY-10482, GROOVY-10624, et al.
Map<GenericsTypeName, GenericsType> context = extractPlaceHolders(receiver, ctor.getDeclaringClass());
parameters = Arrays.stream(parameters).map(p -> new Parameter(applyGenericsContext(context, p.getType()), p.getName())).toArray(Parameter[]::new);
GenericsType[] typeParameters = ctor.getDeclaringClass().getGenericsTypes();
if (typeParameters != null) { // GROOVY-10283, GROOVY-10316, GROOVY-10482, GROOVY-10624, GROOVY-10698, et al.
Map<GenericsTypeName, GenericsType> context = extractGenericsConnectionsFromArguments(typeParameters, parameters, argumentList, receiver.getGenericsTypes());
if (!context.isEmpty()) parameters = Arrays.stream(parameters).map(p -> new Parameter(applyGenericsContext(context, p.getType()), p.getName())).toArray(Parameter[]::new);
}
resolvePlaceholdersFromImplicitTypeHints(argumentTypes, argumentList, parameters);
typeCheckMethodsWithGenericsOrFail(receiver, argumentTypes, ctor, call);
Expand Down Expand Up @@ -4347,7 +4348,7 @@ && isTypeSource(expr, enclosingMethod)) {
inferDiamondType((ConstructorCallExpression) expr, targetType);
}

return adjustForTargetType(type, targetType); // GROOVY-10688
return adjustForTargetType(type, targetType);
}

private static ClassNode adjustForTargetType(final ClassNode resultType, final ClassNode targetType) {
Expand Down Expand Up @@ -5466,7 +5467,7 @@ protected ClassNode inferReturnTypeGenerics(final ClassNode receiver, final Meth
private Map<GenericsTypeName, GenericsType> extractGenericsConnectionsFromArguments(final GenericsType[] methodGenericTypes, final Parameter[] parameters, final Expression arguments, final GenericsType[] explicitTypeHints) {
Map<GenericsTypeName, GenericsType> resolvedPlaceholders = new HashMap<>();

if (explicitTypeHints != null) { // resolve type parameters from type arguments
if (asBoolean(explicitTypeHints)) { // resolve type parameters from type arguments
int n = methodGenericTypes.length;
if (n == explicitTypeHints.length) {
for (int i = 0; i < n; i += 1) {
Expand Down
3 changes: 1 addition & 2 deletions src/test/groovy/transform/stc/GenericsSTCTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -1698,7 +1698,6 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
}

// GROOVY-10698
@NotYetImplemented
void testDiamondInferenceFromConstructor34() {
assertScript '''
class A<T> {
Expand All @@ -1710,7 +1709,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
@ASTTest(phase=INSTRUCTION_SELECTION, value={
def type = node.getNodeMetaData(INFERRED_TYPE)
assert type.toString(false) == 'A<java.lang.String>'
assert type.toString(false) == 'A <java.lang.String>'
})
def x = new A<>('witness', new B<>()) // Cannot call A#<init>(Object,B<Object>) with arguments [String, B<T>]
'''
Expand Down

0 comments on commit b909c50

Please sign in to comment.