You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2021/03/23 03:18:40 UTC
[groovy] branch master updated: GROOVY-9991: closure without arrow
works for 0 and 1 parameter methods
This is an automated email from the ASF dual-hosted git repository.
sunlan 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 98ac980 GROOVY-9991: closure without arrow works for 0 and 1 parameter methods
98ac980 is described below
commit 98ac980d18c0566a4061e19cdc65103fefa3fc42
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Mar 19 20:03:32 2021 -0500
GROOVY-9991: closure without arrow works for 0 and 1 parameter methods
---
.../transform/stc/StaticTypeCheckingVisitor.java | 14 ++++++-----
.../stc/FieldsAndPropertiesSTCTest.groovy | 27 ++++++++++++++++++----
2 files changed, 30 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 591c2bc..ed54498 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -182,6 +182,7 @@ import static org.codehaus.groovy.ast.ClassHelper.short_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.void_WRAPPER_TYPE;
import static org.codehaus.groovy.ast.tools.ClosureUtils.getParametersSafe;
import static org.codehaus.groovy.ast.tools.ClosureUtils.getResolveStrategyName;
+import static org.codehaus.groovy.ast.tools.ClosureUtils.hasImplicitParameter;
import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
import static org.codehaus.groovy.ast.tools.GeneralUtils.binX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.block;
@@ -916,19 +917,20 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
private void inferParameterAndReturnTypesOfClosureOnRHS(final ClassNode lhsType, final ClosureExpression rhsExpression) {
Tuple2<ClassNode[], ClassNode> typeInfo = GenericsUtils.parameterizeSAM(lhsType);
Parameter[] closureParameters = getParametersSafe(rhsExpression);
- ClassNode[] parameterTypes = typeInfo.getV1();
+ ClassNode[] samParameterTypes = typeInfo.getV1();
- int n = closureParameters.length;
- if (n == parameterTypes.length) {
+ int n = closureParameters.length, m = samParameterTypes.length;
+ if (n == m || (1 == m && hasImplicitParameter(rhsExpression))) {
for (int i = 0; i < n; i += 1) {
Parameter parameter = closureParameters[i];
if (parameter.isDynamicTyped()) {
- parameter.setType(parameterTypes[i]);
- parameter.setOriginType(parameterTypes[i]);
+ parameter.setType(samParameterTypes[i]);
+ parameter.setOriginType(samParameterTypes[i]);
}
}
} else {
- addStaticTypeError("Wrong number of parameters: ", rhsExpression);
+ String descriptor = toMethodParametersString(findSAM(lhsType).getName(), samParameterTypes);
+ addStaticTypeError("Wrong number of parameters for method target " + descriptor, rhsExpression);
}
storeInferredReturnType(rhsExpression, typeInfo.getV2());
diff --git a/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy b/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
index 42dc99c..4ff9ccf 100644
--- a/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
+++ b/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
@@ -342,7 +342,7 @@ class FieldsAndPropertiesSTCTest extends StaticTypeCheckingTestCase {
}
// GROOVY-9882
- void testFieldInitShouldPassForCcompatibleTypesWithClosure() {
+ void testFieldInitShouldPassForCompatibleTypesWithClosure() {
assertScript '''
class Foo {
java.util.function.Supplier<String> bar = { 'abc' }
@@ -351,13 +351,30 @@ class FieldsAndPropertiesSTCTest extends StaticTypeCheckingTestCase {
'''
}
- void testFieldInitClosureParameterMismatch() {
+ void testClosureParameterMismatch() {
shouldFailWithMessages '''
class Foo {
- java.util.function.Supplier<String> bar = { a -> '' }
+ java.util.function.Supplier<String> bar = { baz -> '' }
}
- new Foo()
- ''', 'Wrong number of parameters'
+ ''', 'Wrong number of parameters for method target get()'
+ shouldFailWithMessages '''
+ class Foo {
+ java.util.function.Consumer<String> bar = { -> null }
+ }
+ ''', 'Wrong number of parameters for method target accept(java.lang.String)'
+ }
+
+ // GROOVY-9991
+ void testClosureParameterMatch() {
+ assertScript '''
+ java.util.function.Consumer<String> s = { print it }
+ '''
+ assertScript '''
+ java.util.function.Predicate p = { x -> false }
+ '''
+ assertScript '''
+ java.util.function.Predicate p = { false }
+ '''
}
// GROOVY-5517