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/05/09 15:25:08 UTC

[groovy] branch GROOVY_3_0_X updated: GROOVY-8202: STC: void doesn't factor into closure return type inference

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


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new e59a897  GROOVY-8202: STC: void doesn't factor into closure return type inference
e59a897 is described below

commit e59a897b4d20bed478acf8df5eebcbf317bd103c
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sun May 9 09:55:14 2021 -0500

    GROOVY-8202: STC: void doesn't factor into closure return type inference
    
    Conflicts:
    	src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
    	src/test/groovy/transform/stc/ClosuresSTCTest.groovy
---
 .../transform/stc/StaticTypeCheckingVisitor.java   |  1 +
 .../groovy/transform/stc/ClosuresSTCTest.groovy    | 75 ++++++++++++++++++++++
 2 files changed, 76 insertions(+)

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 49f5353..8e73f86 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -2178,6 +2178,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
     }
 
     protected void addClosureReturnType(final ClassNode returnType) {
+        if (VOID_TYPE.equals(returnType)) return; // GROOVY-8202
         typeCheckingContext.getEnclosingClosure().addReturnType(returnType);
     }
 
diff --git a/src/test/groovy/transform/stc/ClosuresSTCTest.groovy b/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
index 34af553..29e9e2e 100644
--- a/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
@@ -117,6 +117,81 @@ class ClosuresSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    // GROOVY-8427
+    void testClosureReturnTypeInference6() {
+        assertScript '''
+            import java.util.function.Consumer
+
+            class C {
+                static <T> void m(T a, Consumer<T> c) {
+                    c.accept(a)
+                }
+                static void main(args) {
+                    def c = { ->
+                        int x = 0
+                        m('') {
+                            print 'void return'
+                        }
+                    }
+                    c.call()
+                }
+            }
+        '''
+    }
+
+    // GROOVY-8202
+    void testClosureReturnTypeInference7() {
+        assertScript '''
+            void proc() {
+            }
+            String test0(flag) {
+              if (flag) {
+                'foo'
+              } else {
+                proc()
+              }
+            }
+            String test1(flag) {
+              Closure<String> c = { ->
+                if (flag) {
+                  'bar'
+                } else {
+                  proc()
+                  null
+                }
+              }
+              c.call()
+            }
+            String test2(flag) {
+              Closure<String> c = { -> // Cannot assign Closure<Object> to Closure<String>
+                if (flag) {
+                  'baz'
+                } else {
+                  proc()
+                }
+              }
+              c.call()
+            }
+
+            assert test0(true) == 'foo'
+            assert test1(true) == 'bar'
+            assert test2(true) == 'baz'
+            assert test0(false) == null
+            assert test1(false) == null
+            assert test2(false) == null
+        '''
+
+        assertScript '''
+            Closure<Void> c = { flag ->
+                if (flag) {
+                    print 'true'
+                } else {
+                    print 'false'
+                }
+            }
+        '''
+    }
+
     // GROOVY-5145
     void testCollect() {
         assertScript '''