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 23:19:18 UTC

[groovy] branch GROOVY_2_5_X updated (7d17238646 -> 359dfe71f8)

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

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


    from 7d17238646 GROOVY-10291, GROOVY-10367: STC: diamond inference for object expression
     new 9d0e33a577 GROOVY-10254: STC: support SAM-type closure coerce on return
     new 359dfe71f8 GROOVY-10256: STC: "Type x = m()" given "def <T extends NotType> T m();"

The 2 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.


Summary of changes:
 .../transform/stc/StaticTypeCheckingSupport.java   |  9 ++++---
 .../groovy/transform/stc/ClosuresSTCTest.groovy    | 31 ++++++++++++++++++++++
 .../groovy/transform/stc/GenericsSTCTest.groovy    | 12 +++++++++
 3 files changed, 49 insertions(+), 3 deletions(-)


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

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

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

commit 9d0e33a57765193077f6e10a1034e246de57b734
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 26a59aa92b..727df0dd29 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -765,6 +765,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 a98c07c12a..4ddd49edf9 100644
--- a/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
@@ -644,6 +644,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(); }
@@ -659,6 +677,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() }


[groovy] 02/02: GROOVY-10256: STC: "Type x = m()" given "def T m();"

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

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

commit 359dfe71f8750e5127580912229e7de34721dc62
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sat Sep 10 17:17:19 2022 -0500

    GROOVY-10256: STC: "Type x = m()" given "def <T extends NotType> T m();"
---
 .../groovy/transform/stc/StaticTypeCheckingSupport.java      |  5 ++---
 src/test/groovy/transform/stc/GenericsSTCTest.groovy         | 12 ++++++++++++
 2 files changed, 14 insertions(+), 3 deletions(-)

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 727df0dd29..a1abf7fc8e 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -778,9 +778,8 @@ public abstract class StaticTypeCheckingSupport {
             }
         }
 
-        // GROOVY-7316 : it is an apparently legal thing to allow this. It's not type safe,
-        // but it is allowed...
-        return right.isGenericsPlaceHolder();
+        // GROOVY-7316, GROOVY-10256: "Type x = m()" given "def <T> T m()"; T adapts to target
+        return right.isGenericsPlaceHolder() && right.asGenericsType().isCompatibleWith(left);
     }
 
     private static boolean isGroovyConstructorCompatible(final Expression rightExpression) {
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index 0086a47d55..119e4a0c9b 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -285,6 +285,18 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    // GROOVY-7316, GROOVY-10256
+    void testReturnTypeInferenceWithMethodGenerics16() {
+        shouldFailWithMessages '''
+            def <T extends CharSequence> T chars() {
+            }
+            List test() {
+                chars()
+            }
+        ''',
+        'Cannot return value of type java.lang.CharSequence on method returning type java.util.List'
+    }
+
     // GROOVY-10098
     void testReturnTypeInferenceWithMethodGenerics17() {
         assertScript '''