Skip to content

Commit

Permalink
GROOVY-11414: STC: resolve or stub closure / lambda return type generics
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jun 25, 2024
1 parent 601b16d commit 5a51d5f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.findDGMMethodsForClassNode;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.findSetters;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.findTargetVariable;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.fullyResolve;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.fullyResolveType;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.getCombinedBoundType;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.getCombinedGenericsType;
Expand Down Expand Up @@ -2945,10 +2944,9 @@ protected void visitMethodCallArguments(final ClassNode receiver, final Argument
if (i > 0 || !(selectedMethod instanceof ExtensionMethodNode)) {
inferClosureParameterTypes(receiver, arguments, source, target, selectedMethod);
}
if (isFunctionalInterface(targetType)) {
storeInferredReturnType(source, GenericsUtils.parameterizeSAM(targetType).getV2());
} else if (isClosureWithType(targetType)) {
storeInferredReturnType(source, getCombinedBoundType(targetType.getGenericsTypes()[0]));
if (isClosureWithType(targetType)) {
ClassNode returnType = getCombinedBoundType(targetType.getGenericsTypes()[0]);
storeInferredReturnType(source, returnType);
}
}
expression.visit(this);
Expand Down Expand Up @@ -3132,10 +3130,8 @@ protected void inferClosureParameterTypes(final ClassNode receiver, final Expres
});
}

for (GenericsType tp : typeParameters) {
GenericsTypeName name = new GenericsTypeName(tp.getName());
context.computeIfAbsent(name, x -> fullyResolve(tp, context));
}
// GROOVY-8917, GROOVY-10049, GROOVY-11414, et al.
stubMissingTypeVariables(typeParameters, context);
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/test/groovy/transform/stc/LambdaTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ final class LambdaTest {
'''
}

// GROOVY-11414
@Test
void testFunction5() {
def err = shouldFail shell, '''
def map = (Map<String, Long>) [:]
map.computeIfAbsent('key', (k) -> 1)
'''
assert err =~ /Cannot return value of type int for lambda expecting java.lang.Long/
}

@Test
void testBinaryOperator() {
assertScript shell, '''
Expand Down

0 comments on commit 5a51d5f

Please sign in to comment.