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:36:21 UTC
[groovy] branch master 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 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 9d0f47a002 GROOVY-7789: STC: apply method call type arguments to `@ClosureParams`
9d0f47a002 is described below
commit 9d0f47a002617c05e1d0139bae4811d9cebfe502
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 88610a43bd..e4eb401715 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -3209,7 +3209,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 2537b14226..4f9e8bb146 100644
--- a/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy
@@ -261,21 +261,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']) }
@@ -285,7 +304,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']) }
@@ -295,7 +314,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']) }
@@ -305,7 +324,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']) }
@@ -315,7 +334,7 @@ class ClosureParamTypeInferenceSTCTest extends StaticTypeCheckingTestCase {
'''
}
- void testFromStringWithGenericPlaceholderFromClassWithTwoGenericsAndNoExplicitSignatureAndNoFQNAndReferenceToSameUnitClass() {
+ void testFromStringWithTypeParameterFromClassWithTwoGenericsAndNoExplicitSignatureAndNoFQNAndReferenceToSameUnitClass() {
assertScript '''import groovy.transform.stc.FromString
class Foo {
void bar() {
@@ -330,7 +349,7 @@ class ClosureParamTypeInferenceSTCTest extends StaticTypeCheckingTestCase {
'''
}
- void testFromStringWithGenericPlaceholderFromClassWithTwoGenericsAndNoExplicitSignatureAndNoFQNAndReferenceToSameUnitClassAndTwoArgs() {
+ void testFromStringWithTypeParameterFromClassWithTwoGenericsAndNoExplicitSignatureAndNoFQNAndReferenceToSameUnitClassAndTwoArgs() {
assertScript '''import groovy.transform.stc.FromString
class Foo {
void bar() {
@@ -345,7 +364,7 @@ class ClosureParamTypeInferenceSTCTest extends StaticTypeCheckingTestCase {
'''
}
- void testFromStringWithGenericPlaceholderFromClassWithTwoGenericsAndPolymorphicSignature() {
+ void testFromStringWithTypeParameterFromClassWithTwoGenericsAndPolymorphicSignature() {
assertScript '''import groovy.transform.stc.FromString
class Foo {
void bar() {
@@ -397,7 +416,7 @@ class ClosureParamTypeInferenceSTCTest extends StaticTypeCheckingTestCase {
void testStringGroovyMethodsFindMethodWithList() {
assertScript '''
"75001 Paris".find(/(\\d{5}\\s(\\w+))/) { List<String> all -> println all*.toUpperCase() }
-'''
+ '''
}
void testInferenceForDGM_countIterableOrIterator() {