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

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


The following commit(s) were added to refs/heads/master by this push:
     new cac9179176 GROOVY-11092: STC (MCE): coerced closure argument 
destructuring for list
cac9179176 is described below

commit cac91791764b8f0b3338318248474412f89e3456
Author: Eric Milles <[email protected]>
AuthorDate: Tue Jun 13 09:02:35 2023 -0500

    GROOVY-11092: STC (MCE): coerced closure argument destructuring for list
---
 .../transform/stc/StaticTypeCheckingVisitor.java   | 31 ++++------------------
 .../stc/ClosureParamTypeInferenceSTCTest.groovy    |  2 +-
 .../groovy/transform/stc/CoercionSTCTest.groovy    |  9 +++++++
 src/test/groovy/transform/stc/LambdaTest.groovy    |  6 +++++
 4 files changed, 21 insertions(+), 27 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 cfa5e53578..2c96a568e4 100644
--- 
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ 
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -997,9 +997,6 @@ public class StaticTypeCheckingVisitor extends 
ClassCodeVisitorSupport {
         }
     }
 
-    /**
-     * @see #inferClosureParameterTypes
-     */
     private void inferParameterAndReturnTypesOfClosureOnRHS(final ClassNode 
lhsType, final ClosureExpression rhsExpression) {
         ClassNode[] samParameterTypes;
         {
@@ -1035,11 +1032,12 @@ out:    if ((samParameterTypes.length == 1 && 
isOrImplements(samParameterTypes[0
             for (int i = 0; i < n; i += 1) {
                 Parameter parameter = closureParameters[i];
                 if (parameter.isDynamicTyped()) {
-                    parameter.setType(expectedTypes[i]);
+                    parameter.setType(expectedTypes[i]); // GROOVY-11083, 
GROOVY-11085, et al.
                 } else {
                     checkParamType(parameter, expectedTypes[i], i == n-1, 
rhsExpression instanceof LambdaExpression);
                 }
             }
+            rhsExpression.putNodeMetaData(CLOSURE_ARGUMENTS, expectedTypes);
         } else {
             addStaticTypeError("Wrong number of parameters for method target: 
" + toMethodParametersString(findSAM(lhsType).getName(), samParameterTypes), 
rhsExpression);
         }
@@ -1048,7 +1046,7 @@ out:    if ((samParameterTypes.length == 1 && 
isOrImplements(samParameterTypes[0
     }
 
     private void checkParamType(final Parameter source, final ClassNode 
target, final boolean isLast, final boolean lambda) {
-        if (/*lambda ? !source.getOriginType().equals(target) 
:*/!typeCheckMethodArgumentWithGenerics(source.getOriginType(), target, isLast))
+        if (/*lambda ? !source.getOriginType().isDerivedFrom(target) 
:*/!typeCheckMethodArgumentWithGenerics(source.getOriginType(), target, isLast))
             addStaticTypeError("Expected type " + prettyPrintType(target) + " 
for " + (lambda ? "lambda" : "closure") + " parameter: " + source.getName(), 
source);
     }
 
@@ -3143,27 +3141,8 @@ out:    if ((samParameterTypes.length == 1 && 
isOrImplements(samParameterTypes[0
                 ClassNode targetType = target.getType();
                 if (targetType != null && targetType.isGenericsPlaceHolder())
                     targetType = 
getCombinedBoundType(targetType.asGenericsType());
-                targetType = applyGenericsContext(context, targetType); // 
fill place-holders
-                ClassNode[] samParamTypes = 
GenericsUtils.parameterizeSAM(targetType).getV1();
-
-                int n; Parameter[] p = expression.getParameters();
-                if (p == null) {
-                    // zero parameters
-                    paramTypes = ClassNode.EMPTY_ARRAY;
-                } else if ((n = p.length) == 0) {
-                    // implicit parameter(s)
-                    paramTypes = samParamTypes;
-                } else { // TODO: error for length mismatch
-                    paramTypes = Arrays.copyOf(samParamTypes, n);
-                    for (int i = 0, j = Math.min(n, samParamTypes.length); i < 
j; i += 1) {
-                        if (p[i].isDynamicTyped()) { 
p[i].setType(samParamTypes[i]); } else // GROOVY-11083
-                        checkParamType(p[i], paramTypes[i], i == n-1, 
expression instanceof LambdaExpression);
-                    }
-                }
-                if (paramTypes.length != samParamTypes.length) { // GROOVY-8499
-                    addError("Incorrect number of parameters. Expected " + 
samParamTypes.length + " but found " + paramTypes.length, expression);
-                }
-                expression.putNodeMetaData(CLOSURE_ARGUMENTS, paramTypes);
+                targetType = applyGenericsContext(context, targetType); // 
fill "T"
+                inferParameterAndReturnTypesOfClosureOnRHS(targetType, 
expression);
             }
         }
     }
diff --git 
a/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy 
b/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy
index 68220b735f..f8909ba0c7 100644
--- a/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy
@@ -389,7 +389,7 @@ class ClosureParamTypeInferenceSTCTest extends 
StaticTypeCheckingTestCase {
     // GROOVY-8499
     void testParamCountCheck6() {
         shouldFailWithMessages '''
-            ['ab'.chars,'12'.chars].combinations().stream().map((x, y) -> 
"$x$y")
+            ['ab'.chars,'12'.chars].combinations().collect((l,n) -> "$l$n")
         ''',
         'Incorrect number of parameters. Expected 1 but found 2'
     }
diff --git a/src/test/groovy/transform/stc/CoercionSTCTest.groovy 
b/src/test/groovy/transform/stc/CoercionSTCTest.groovy
index 51f65dfab0..77fa5eeaef 100644
--- a/src/test/groovy/transform/stc/CoercionSTCTest.groovy
+++ b/src/test/groovy/transform/stc/CoercionSTCTest.groovy
@@ -558,4 +558,13 @@ class CoercionSTCTest extends StaticTypeCheckingTestCase {
         ''',
         'Wrong number of parameters for method target'
     }
+
+    // GROOVY-11092, GROOVY-8499
+    void testCoerceToFunctionalInterface21() {
+        // TODO: if extension combinations indicates List<List<?>> this can 
work
+        shouldFailWithMessages '''
+            ['ab'.chars,'12'.chars].combinations().stream().map((l,n) -> 
"$l$n")
+        ''',
+        'Wrong number of parameters for method target: apply(java.lang.Object)'
+    }
 }
diff --git a/src/test/groovy/transform/stc/LambdaTest.groovy 
b/src/test/groovy/transform/stc/LambdaTest.groovy
index 5d3d7a2b2e..9999f92cac 100644
--- a/src/test/groovy/transform/stc/LambdaTest.groovy
+++ b/src/test/groovy/transform/stc/LambdaTest.groovy
@@ -822,6 +822,12 @@ final class LambdaTest {
             Function<List<String>,String> f = (one,two) -> { one + two }
             assert f.apply(['foo','bar']) == 'foobar'
         '''
+        assertScript shell, '''
+            void setFun(Function<List<String>,String> f) {
+                assert f.apply(['foo','bar']) == 'foobar'
+            }
+            fun = (one,two) -> { one + two }
+        '''
     }
 
     @Test

Reply via email to