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 2023/06/06 22:15:32 UTC

[groovy] branch GROOVY_4_0_X updated: GROOVY-11083: STC: propagate inferred closure param type

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 a10303bc6f GROOVY-11083: STC: propagate inferred closure param type
a10303bc6f is described below

commit a10303bc6fe51b7fc47a05ee531397db794f792b
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Jun 6 15:16:02 2023 -0500

    GROOVY-11083: STC: propagate inferred closure param type
---
 .../transform/stc/StaticTypeCheckingVisitor.java   |   3 +-
 .../groovy/transform/stc/ClosuresSTCTest.groovy    | 101 ++++++++++++---------
 .../groovy/transform/stc/CoercionSTCTest.groovy    |  11 +++
 3 files changed, 73 insertions(+), 42 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 fc29f686d7..173c09516f 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -3017,7 +3017,8 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
                     paramTypes = samParamTypes;
                 } else { // TODO: error for length mismatch
                     paramTypes = Arrays.copyOf(samParamTypes, n);
-                    for (int i = 0; i < Math.min(n, samParamTypes.length); i += 1) {
+                    for (int i = 0, j = Math.min(n, samParamTypes.length); i < j; i += 1) {
+                        if (p[i].isDynamicTyped()) { p[i].setType(samParamTypes[i]); } else // GROOVY-11083
                         checkParamType(p[i], paramTypes[i], i == n-1, expression instanceof LambdaExpression);
                     }
                 }
diff --git a/src/test/groovy/transform/stc/ClosuresSTCTest.groovy b/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
index b8a36d8541..5aa1e8adfa 100644
--- a/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
@@ -23,50 +23,35 @@ package groovy.transform.stc
  */
 class ClosuresSTCTest extends StaticTypeCheckingTestCase {
 
-    void testClosureWithoutArguments1() {
+    void testCallClosure1() {
         assertScript '''
             def c = { return 'foo' }
             assert c() == 'foo'
         '''
     }
 
-    void testClosureWithoutArguments2() {
+    void testCallClosure2() {
         assertScript '''
             def c = { -> return 'foo' }
             assert c() == 'foo'
         '''
     }
 
-    // GROOVY-10071
-    void testClosureWithoutArguments3() {
-        assertScript '''
-            def c = { ... zeroOrMore -> return 'foo' + zeroOrMore }
-            assert c('bar', 'baz') == 'foo[bar, baz]'
-            assert c('bar') == 'foo[bar]'
-            assert c() == 'foo[]'
-        '''
-    }
-
-    // GROOVY-10072, GROOVY-11023
-    void testClosureWithoutArguments4() {
-        assertScript '''
-            def c = { p = 'foo' -> return p }
-            assert c('bar') == 'bar'
-            assert c() == 'foo'
-        '''
-        assertScript '''
-            def c = { p, q = p.toString() -> '' + p + q }
-            assert c('foo', 'bar') == 'foobar'
-            assert c('foo') == 'foofoo'
+    void testCallClosure3() {
+        shouldFail MissingMethodException, '''
+            def c = { -> }
+            c('')
         '''
     }
 
-    void testClosureWithArguments1() {
+    void testCallClosure4() {
         assertScript '''
             def c = { int a, int b -> a + b }
             assert c(5, 7) == 12
         '''
+    }
 
+    void testCallClosure5() {
         shouldFailWithMessages '''
             def c = { int a, int b -> a + b }
             c('5', '7')
@@ -74,12 +59,14 @@ class ClosuresSTCTest extends StaticTypeCheckingTestCase {
         'Cannot call closure that accepts [int, int] with [java.lang.String, java.lang.String]'
     }
 
-    void testClosureWithArguments2() {
+    void testCallClosure6() {
         assertScript '''
             def result = { int a, int b -> a + b }(5, 7)
             assert result == 12
         '''
+    }
 
+    void testCallClosure7() {
         shouldFailWithMessages '''
             { int a, int b -> a + b }('5', 7)
         ''',
@@ -87,13 +74,59 @@ class ClosuresSTCTest extends StaticTypeCheckingTestCase {
     }
 
     // GROOVY-6365
-    void testClosureWithArguments3() {
+    void testCallClosure8() {
         assertScript '''
             def c = { Object[] args -> args.length }
             assert c('one', 'two') == 2
         '''
     }
 
+    // GROOVY-10071
+    void testCallClosure9() {
+        assertScript '''
+            def c = { ... zeroOrMore -> return 'foo' + zeroOrMore }
+            assert c('bar', 'baz') == 'foo[bar, baz]'
+            assert c('bar') == 'foo[bar]'
+            assert c() == 'foo[]'
+        '''
+    }
+
+    // GROOVY-10072
+    void testCallClosure10() {
+        assertScript '''
+            def c = { p = 'foo' -> return p }
+            assert c('bar') == 'bar'
+            assert c() == 'foo'
+        '''
+    }
+
+    // GROOVY-10072
+    void testCallClosure11() {
+        assertScript '''
+            def c = { Number n, Number total = 41 -> total += n }
+            assert c(1) == 42
+        '''
+    }
+
+    // GROOVY-10072
+    void testCallClosure12() {
+        shouldFailWithMessages '''
+            def c = { Number n, Number total = new Date() -> total += n }
+        ''',
+        'Cannot assign value of type java.util.Date to variable of type java.lang.Number'
+    }
+
+    // GROOVY-11023
+    void testCallClosure13() {
+        assertScript '''
+            def c = { p, q = p.toString() -> '' + p + q }
+            assert c('foo', 'bar') == 'foobar'
+            assert c('foo') == 'foofoo'
+        '''
+    }
+
+    //
+
     void testClosureReturnTypeInference1() {
         assertScript '''
             def c = { int a, int b -> return a + b }
@@ -385,24 +418,10 @@ class ClosuresSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
-    void testClosureCallAsAMethod() {
-        assertScript '''
-            Closure cl = { 'foo' }
-            assert cl() == 'foo'
-        '''
-    }
-
-    void testClosureCallWithOneArgAsAMethod() {
-        assertScript '''
-            Closure cl = { int x -> "foo$x" }
-            assert cl(1) == 'foo1'
-        '''
-    }
-
     void testRecurseClosureCallAsAMethod() {
         assertScript '''
             Closure<Integer> cl
-            cl = { int x-> x==0?x:1+cl(x-1) }
+            cl = { int x -> x == 0 ? x : 1+cl(x-1) }
         '''
     }
 
diff --git a/src/test/groovy/transform/stc/CoercionSTCTest.groovy b/src/test/groovy/transform/stc/CoercionSTCTest.groovy
index 212ebf11f0..1d57e73728 100644
--- a/src/test/groovy/transform/stc/CoercionSTCTest.groovy
+++ b/src/test/groovy/transform/stc/CoercionSTCTest.groovy
@@ -496,4 +496,15 @@ class CoercionSTCTest extends StaticTypeCheckingTestCase {
             test( [null] )
         '''
     }
+
+    // GROOVY-11083
+    void testCoerceToFunctionalInterface18() {
+        shouldFailWithMessages '''
+            void setFoo(Consumer<Number> c) {}
+            void test(Date d) {
+                foo = { n = d -> }
+            }
+        ''',
+        'Cannot assign value of type java.util.Date to variable of type java.lang.Number'
+    }
 }