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 d1182f6f07 GROOVY-11181: STC: include parameter type in error
d1182f6f07 is described below

commit d1182f6f078db7ee062bc6addad86bb2006c10f5
Author: Eric Milles <eric.mil...@thomsonreuters.com>
AuthorDate: Sat Oct 5 09:17:16 2024 -0500

    GROOVY-11181: STC: include parameter type in error
    
    4_0_X backport
---
 .../transform/stc/StaticTypeCheckingVisitor.java   |  8 ++++----
 .../transform/stc/MethodReferenceTest.groovy       | 24 ++++++++++++++++++++--
 2 files changed, 26 insertions(+), 6 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 14462f47b9..a62f93ba3e 100644
--- 
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ 
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -3800,12 +3800,12 @@ public class StaticTypeCheckingVisitor extends 
ClassCodeVisitorSupport {
                 if (i >= nthParameter && paramType.isArray())
                     paramType = paramType.getComponentType();
 
-                if (!isFunctionalInterface(paramType.redirect())) {
-                    addError("The argument is a method reference, but the 
parameter type is not a functional interface", argumentExpression);
-                    newArgumentExpressions.add(argumentExpression);
-                } else {
+                if (isFunctionalInterface(paramType)) {
                     methodReferencePositions.add(i);
                     
newArgumentExpressions.add(constructLambdaExpressionForMethodReference(paramType,
 (MethodReferenceExpression) argumentExpression));
+                } else {
+                    newArgumentExpressions.add(argumentExpression);
+                    addStaticTypeError("Argument is a method reference, but 
parameter type '" + prettyPrintTypeName(paramType) + "' is not a functional 
interface", argumentExpression);
                 }
             }
         }
diff --git a/src/test/groovy/transform/stc/MethodReferenceTest.groovy 
b/src/test/groovy/transform/stc/MethodReferenceTest.groovy
index 392af6827a..91f61ed26d 100644
--- a/src/test/groovy/transform/stc/MethodReferenceTest.groovy
+++ b/src/test/groovy/transform/stc/MethodReferenceTest.groovy
@@ -1489,7 +1489,7 @@ final class MethodReferenceTest {
                 baz(this::foo) // not yet supported!
             }
         '''
-        assert err =~ /The argument is a method reference, but the parameter 
type is not a functional interface/
+        assert err =~ /Argument is a method reference, but parameter type 
'java.lang.Object' is not a functional interface/
     }
 
     @Test // GROOVY-10336
@@ -1506,7 +1506,27 @@ final class MethodReferenceTest {
                 }
             }
         '''
-        assert err =~ /The argument is a method reference, but the parameter 
type is not a functional interface/
+        assert err =~ /Argument is a method reference, but parameter type 
'java.lang.Object' is not a functional interface/
+    }
+
+    @Test // GROOVY-10979
+    void testNotFunctionalInterface3() {
+        def err = shouldFail shell, '''
+            Integer m(String x) {
+                return 1
+            }
+            @CompileStatic
+            void test() {
+                java.util.stream.Stream<Number>                                
          x = null
+                BiFunction<Function<String, Integer>, Number, Function<String, 
Integer>> y = null
+                BinaryOperator<Function<String, Integer>>                      
          z = null
+                // reduce number(s) to string-to-integer functions
+                x.<Function<String, Integer>>reduce(this::m, y, z)
+                x.reduce(this::m, y, z)
+                x.reduce((s) -> 1, y, z)
+            }
+        '''
+        assert err =~ /Argument is a method reference, but parameter type 'U' 
is not a functional interface/
     }
 
     @Test // GROOVY-11254

Reply via email to