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 2022/09/10 22:45:10 UTC

[groovy] 01/02: GROOVY-10254: STC: support SAM-type closure coerce on return

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

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

commit 81bc2c9baaee2d69908c7de033b1ee6ad94dacff
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sun Sep 26 16:48:19 2021 -0500

    GROOVY-10254: STC: support SAM-type closure coerce on return
---
 .../transform/stc/StaticTypeCheckingSupport.java   |  4 +++
 .../groovy/transform/stc/ClosuresSTCTest.groovy    | 31 ++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
index 9611851b68..55848f4403 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -718,6 +718,10 @@ public abstract class StaticTypeCheckingSupport {
             return true;
         }
 
+        if (right.isDerivedFrom(CLOSURE_TYPE) && isSAMType(left)) {
+            return true;
+        }
+
         if (left.isGenericsPlaceHolder()) {
             // GROOVY-7307
             GenericsType[] genericsTypes = left.getGenericsTypes();
diff --git a/src/test/groovy/transform/stc/ClosuresSTCTest.groovy b/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
index a910068280..0cdefd461b 100644
--- a/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
@@ -600,6 +600,24 @@ class ClosuresSTCTest extends StaticTypeCheckingTestCase {
         """
     }
 
+    // GROOVY-7003
+    void testSAMProperty2() {
+        assertScript '''
+            import java.beans.*
+
+            class C {
+                static PropertyChangeListener listener = { PropertyChangeEvent event ->
+                    result = "${event.oldValue} -> ${event.newValue}"
+                }
+                public static result
+            }
+
+            def event = new PropertyChangeEvent(new Object(), 'foo', 'bar', 'baz')
+            C.getListener().propertyChange(event)
+            assert C.result == 'bar -> baz'
+        '''
+    }
+
     void testSAMAttribute() {
         assertScript """
             interface SAM { def foo(); }
@@ -615,6 +633,19 @@ class ClosuresSTCTest extends StaticTypeCheckingTestCase {
         """
     }
 
+    // GROOVY-10254
+    void testSAMReturnType() {
+        assertScript '''
+            interface SAM<T> { T get() }
+            SAM<Integer> foo() {
+                return { -> 42 }
+            }
+
+            def result = foo().get()
+            assert result == 42
+        '''
+    }
+
     void testMultipleSAMSignature() {
         assertScript '''
             interface SAM { def foo() }