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/11/05 19:37:47 UTC

[groovy] branch GROOVY_4_0_X updated: GROOVY-7789: STC: apply method call type arguments to `@ClosureParams`

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 f241c15265 GROOVY-7789: STC: apply method call type arguments to `@ClosureParams`
f241c15265 is described below

commit f241c152658aaa22ca272d2c08af5ec2f8502fb5
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sat Nov 5 14:09:11 2022 -0500

    GROOVY-7789: STC: apply method call type arguments to `@ClosureParams`
---
 .../transform/stc/StaticTypeCheckingVisitor.java   |  9 ++++-
 .../stc/ClosureParamTypeInferenceSTCTest.groovy    | 39 ++++++++++++++++------
 2 files changed, 37 insertions(+), 11 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 6534213809..97d8ad0bfa 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -3186,7 +3186,14 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
             methodNode.setGenericsTypes(selectedMethod.getGenericsTypes());
         }
 
-        returnType = inferReturnTypeGenerics(receiver, methodNode, arguments);
+        GenericsType[] typeArguments = null; // GROOVY-7789
+        Expression emc = typeCheckingContext.getEnclosingMethodCall();
+        if (emc instanceof MethodCallExpression) {
+            MethodCallExpression call = (MethodCallExpression) emc;
+            if (arguments == call.getArguments()) typeArguments = call.getGenericsTypes();
+        }
+
+        returnType = inferReturnTypeGenerics(receiver, methodNode, arguments, typeArguments);
         GenericsType[] returnTypeGenerics = returnType.getGenericsTypes();
         for (int i = 0, n = returnTypeGenerics.length; i < n; i += 1) {
             GenericsType gt = returnTypeGenerics[i];
diff --git a/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy b/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy
index ed5344d30d..bb90f34451 100644
--- a/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy
@@ -263,21 +263,40 @@ class ClosureParamTypeInferenceSTCTest extends StaticTypeCheckingTestCase {
         'Expected type java.util.List<java.lang.String> for closure parameter: d'
     }
 
-    void testFromStringWithDirectGenericPlaceholder() {
+    void testFromStringWithTypeParameter1() {
         assertScript '''import groovy.transform.stc.FromString
             def <T> void foo(T t, @ClosureParams(value=FromString,options="T") Closure cl) { cl.call(t) }
             foo('hey') { println it.toUpperCase() }
         '''
     }
 
-    void testFromStringWithGenericPlaceholder() {
+    void testFromStringWithTypeParameter2() {
         assertScript '''import groovy.transform.stc.FromString
             def <T> void foo(T t, @ClosureParams(value=FromString,options="java.util.List<T>") Closure cl) { cl.call([t,t]) }
             foo('hey') { List<String> str -> str.each { println it.toUpperCase() } }
         '''
     }
 
-    void testFromStringWithGenericPlaceholderFromClass() {
+    // GROOVY-7789
+    void testFromStringWithTypeParameter3() {
+        assertScript '''import groovy.transform.stc.FromString
+            class Monad<T> {  private final Closure c
+                Monad(@ClosureParams(value=FromString, options='T') Closure c) {
+                    this.c = c
+                }
+                def call(T t) {
+                    c.call(t)
+                }
+            }
+            def <U> Monad<U> wrap(@ClosureParams(value=FromString, options='U') Closure c) {
+                new Monad<>(c)
+            }
+            def list_size = this.<List>wrap({ list -> list.size() })
+            assert list_size([]) == 0
+        '''
+    }
+
+    void testFromStringWithTypeParameterFromClass() {
         assertScript '''import groovy.transform.stc.FromString
             class Foo<T> {
                 void foo(@ClosureParams(value=FromString,options="java.util.List<T>") Closure cl) { cl.call(['hey','ya']) }
@@ -287,7 +306,7 @@ class ClosureParamTypeInferenceSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
-    void testFromStringWithGenericPlaceholderFromClassWithTwoGenerics() {
+    void testFromStringWithTypeParameterFromClassWithTwoGenerics() {
         assertScript '''import groovy.transform.stc.FromString
             class Foo<T,U> {
                 void foo(@ClosureParams(value=FromString,options="java.util.List<U>") Closure cl) { cl.call(['hey','ya']) }
@@ -297,7 +316,7 @@ class ClosureParamTypeInferenceSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
-    void testFromStringWithGenericPlaceholderFromClassWithTwoGenericsAndNoExplicitSignature() {
+    void testFromStringWithTypeParameterFromClassWithTwoGenericsAndNoExplicitSignature() {
         assertScript '''import groovy.transform.stc.FromString
             class Foo<T,U> {
                 public void foo(@ClosureParams(value=FromString,options="java.util.List<U>") Closure cl) { cl.call(['hey','ya']) }
@@ -307,7 +326,7 @@ class ClosureParamTypeInferenceSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
-    void testFromStringWithGenericPlaceholderFromClassWithTwoGenericsAndNoExplicitSignatureAndNoFQN() {
+    void testFromStringWithTypeParameterFromClassWithTwoGenericsAndNoExplicitSignatureAndNoFQN() {
         assertScript '''import groovy.transform.stc.FromString
             class Foo<T,U> {
                 public void foo(@ClosureParams(value=FromString,options="List<U>") Closure cl) { cl.call(['hey','ya']) }
@@ -317,7 +336,7 @@ class ClosureParamTypeInferenceSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
-    void testFromStringWithGenericPlaceholderFromClassWithTwoGenericsAndNoExplicitSignatureAndNoFQNAndReferenceToSameUnitClass() {
+    void testFromStringWithTypeParameterFromClassWithTwoGenericsAndNoExplicitSignatureAndNoFQNAndReferenceToSameUnitClass() {
         assertScript '''import groovy.transform.stc.FromString
             class Foo {
                 void bar() {
@@ -332,7 +351,7 @@ class ClosureParamTypeInferenceSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
-    void testFromStringWithGenericPlaceholderFromClassWithTwoGenericsAndNoExplicitSignatureAndNoFQNAndReferenceToSameUnitClassAndTwoArgs() {
+    void testFromStringWithTypeParameterFromClassWithTwoGenericsAndNoExplicitSignatureAndNoFQNAndReferenceToSameUnitClassAndTwoArgs() {
         assertScript '''import groovy.transform.stc.FromString
             class Foo {
                 void bar() {
@@ -347,7 +366,7 @@ class ClosureParamTypeInferenceSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
-    void testFromStringWithGenericPlaceholderFromClassWithTwoGenericsAndPolymorphicSignature() {
+    void testFromStringWithTypeParameterFromClassWithTwoGenericsAndPolymorphicSignature() {
         assertScript '''import groovy.transform.stc.FromString
             class Foo {
                 void bar() {
@@ -399,7 +418,7 @@ class ClosureParamTypeInferenceSTCTest extends StaticTypeCheckingTestCase {
     void testStringGroovyMethodsFindMethodWithList() {
         assertScript '''
             "75001 Paris".find(/(\\d{5}\\s(\\w+))/) { List<String> all -> println all*.toUpperCase() }
-'''
+        '''
     }
 
     void testInferenceForDGM_countIterableOrIterator() {