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 1ee6f66  GROOVY-10254: STC: support SAM-type closure coerce on return
1ee6f66 is described below

commit 1ee6f66a56da9b21b43ddc584a7be7edcc8379ba
Author: Eric Milles <[email protected]>
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 5e06288..18817e9 100644
--- 
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ 
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -749,6 +749,10 @@ public abstract class StaticTypeCheckingSupport {
             return true;
         }
 
+        if (right.isDerivedFrom(CLOSURE_TYPE) && isSAMType(left)) {
+            return true;
+        }
+
         // GROOVY-7316, GROOVY-10256: "Type x = m()" given "def <T> T m()"; T 
adapts to target
         return right.isGenericsPlaceHolder() && 
right.asGenericsType().isCompatibleWith(left);
     }
diff --git a/src/test/groovy/transform/stc/ClosuresSTCTest.groovy 
b/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
index 437c63e..798c291 100644
--- a/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
@@ -669,6 +669,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(); }
@@ -684,6 +702,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() }

Reply via email to