You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2021/03/20 01:16:07 UTC

[groovy] branch GROOVY-9991 created (now edb564b)

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

emilles pushed a change to branch GROOVY-9991
in repository https://gitbox.apache.org/repos/asf/groovy.git.


      at edb564b  GROOVY-9991: closure without arrow works for 0 and 1 parameter methods

This branch includes the following new commits:

     new edb564b  GROOVY-9991: closure without arrow works for 0 and 1 parameter methods

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[groovy] 01/01: GROOVY-9991: closure without arrow works for 0 and 1 parameter methods

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit edb564b4519bb6c4dcef30ab78bb019274c9bc63
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Mar 19 20:03:32 2021 -0500

    GROOVY-9991: closure without arrow works for 0 and 1 parameter methods
---
 .../transform/stc/StaticTypeCheckingVisitor.java   | 16 +++++++------
 .../stc/FieldsAndPropertiesSTCTest.groovy          | 27 ++++++++++++++++++----
 2 files changed, 31 insertions(+), 12 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 3d914a8..2c0acf0 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -182,6 +182,7 @@ import static org.codehaus.groovy.ast.ClassHelper.short_TYPE;
 import static org.codehaus.groovy.ast.ClassHelper.void_WRAPPER_TYPE;
 import static org.codehaus.groovy.ast.tools.ClosureUtils.getParametersSafe;
 import static org.codehaus.groovy.ast.tools.ClosureUtils.getResolveStrategyName;
+import static org.codehaus.groovy.ast.tools.ClosureUtils.hasImplicitParameter;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.binX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.block;
@@ -914,19 +915,20 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
     private void inferParameterAndReturnTypesOfClosureOnRHS(final ClassNode lhsType, final ClosureExpression rhsExpression) {
         Tuple2<ClassNode[], ClassNode> typeInfo = GenericsUtils.parameterizeSAM(lhsType);
         Parameter[] closureParameters = getParametersSafe(rhsExpression);
-        ClassNode[] parameterTypes = typeInfo.getV1();
+        ClassNode[] samParameterTypes = typeInfo.getV1();
 
-        int n = closureParameters.length;
-        if (n == parameterTypes.length) {
-            for (int i = 0; i < n; i += 1) {
+        if (samParameterTypes.length == closureParameters.length
+                || (samParameterTypes.length == 1 && hasImplicitParameter(rhsExpression))) {
+            for (int i = 0; i < closureParameters.length; i += 1) {
                 Parameter parameter = closureParameters[i];
                 if (parameter.isDynamicTyped()) {
-                    parameter.setType(parameterTypes[i]);
-                    parameter.setOriginType(parameterTypes[i]);
+                    parameter.setType(samParameterTypes[i]);
+                    parameter.setOriginType(samParameterTypes[i]);
                 }
             }
         } else {
-            addStaticTypeError("Wrong number of parameters: ", rhsExpression);
+            String descriptor = toMethodParametersString(findSAM(lhsType).getName(), samParameterTypes);
+            addStaticTypeError("Wrong number of parameters for method target " + descriptor, rhsExpression);
         }
 
         storeInferredReturnType(rhsExpression, typeInfo.getV2());
diff --git a/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy b/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
index 42dc99c..4ff9ccf 100644
--- a/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
+++ b/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
@@ -342,7 +342,7 @@ class FieldsAndPropertiesSTCTest extends StaticTypeCheckingTestCase {
     }
 
     // GROOVY-9882
-    void testFieldInitShouldPassForCcompatibleTypesWithClosure() {
+    void testFieldInitShouldPassForCompatibleTypesWithClosure() {
         assertScript '''
             class Foo {
                 java.util.function.Supplier<String> bar = { 'abc' }
@@ -351,13 +351,30 @@ class FieldsAndPropertiesSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
-    void testFieldInitClosureParameterMismatch() {
+    void testClosureParameterMismatch() {
         shouldFailWithMessages '''
             class Foo {
-                java.util.function.Supplier<String> bar = { a -> '' }
+                java.util.function.Supplier<String> bar = { baz -> '' }
             }
-            new Foo()
-        ''', 'Wrong number of parameters'
+        ''', 'Wrong number of parameters for method target get()'
+        shouldFailWithMessages '''
+            class Foo {
+                java.util.function.Consumer<String> bar = { -> null }
+            }
+        ''', 'Wrong number of parameters for method target accept(java.lang.String)'
+    }
+
+    // GROOVY-9991
+    void testClosureParameterMatch() {
+        assertScript '''
+            java.util.function.Consumer<String> s = { print it }
+        '''
+        assertScript '''
+            java.util.function.Predicate p = { x -> false }
+        '''
+        assertScript '''
+            java.util.function.Predicate p = { false }
+        '''
     }
 
     // GROOVY-5517