This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
     new 5a51d5f449 GROOVY-11414: STC: resolve or stub closure / lambda return 
type generics
5a51d5f449 is described below

commit 5a51d5f44911cce95fc722593fd7ae25dbd3df08
Author: Eric Milles <eric.mil...@thomsonreuters.com>
AuthorDate: Tue Jun 25 11:34:44 2024 -0500

    GROOVY-11414: STC: resolve or stub closure / lambda return type generics
---
 .../groovy/transform/stc/StaticTypeCheckingVisitor.java    | 14 +++++---------
 src/test/groovy/transform/stc/LambdaTest.groovy            | 10 ++++++++++
 2 files changed, 15 insertions(+), 9 deletions(-)

diff --git 
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
 
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index acd1a7af9f..36b082ffd1 100644
--- 
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ 
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -279,7 +279,6 @@ import static 
org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.filter
 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;
@@ -2945,10 +2944,9 @@ public class StaticTypeCheckingVisitor extends 
ClassCodeVisitorSupport {
                     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);
@@ -3132,10 +3130,8 @@ public class StaticTypeCheckingVisitor extends 
ClassCodeVisitorSupport {
                         });
                     }
 
-                    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);
                 }
             }
 
diff --git a/src/test/groovy/transform/stc/LambdaTest.groovy 
b/src/test/groovy/transform/stc/LambdaTest.groovy
index 909928391b..f99c6f9045 100644
--- a/src/test/groovy/transform/stc/LambdaTest.groovy
+++ b/src/test/groovy/transform/stc/LambdaTest.groovy
@@ -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, '''

Reply via email to