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/11/30 17:42:37 UTC
(groovy) branch GROOVY_3_0_X updated: GROOVY-10660: STC: infer closure parameter types from method return type
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 fdbd736546 GROOVY-10660: STC: infer closure parameter types from method return type
fdbd736546 is described below
commit fdbd7365469f96f25cdd875b8957cc7542ef3693
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Thu Nov 30 11:36:52 2023 -0600
GROOVY-10660: STC: infer closure parameter types from method return type
3_0_X backport
---
.../transform/stc/StaticTypeCheckingVisitor.java | 12 +++++++-
.../stc/ClosureParamTypeInferenceSTCTest.groovy | 34 ++++++++++++++++++++++
2 files changed, 45 insertions(+), 1 deletion(-)
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 e302bb94fb..ac9722e169 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -2219,11 +2219,15 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
@Override
protected void visitConstructorOrMethod(final MethodNode node, final boolean isConstructor) {
typeCheckingContext.pushEnclosingMethod(node);
+ final ClassNode returnType = node.getReturnType(); // GROOVY-10660: implicit return case
+ if (!isConstructor && (isClosureWithType(returnType) || isFunctionalInterface(returnType))) {
+ new ReturnAdder(returnStmt -> applyTargetType(returnType, returnStmt.getExpression())).visitMethod(node);
+ }
if (!isSkipMode(node) && !shouldSkipMethodNode(node)) {
super.visitConstructorOrMethod(node, isConstructor);
}
if (!isConstructor) {
- returnAdder.visitMethod(node); // return statement added after visitConstructorOrMethod finished... we can not count these auto-generated return statements(GROOVY-7753), see `typeCheckingContext.pushEnclosingReturnStatement`
+ returnAdder.visitMethod(node); // GROOVY-7753: we cannot count these auto-generated return statements, see `typeCheckingContext.pushEnclosingReturnStatement`
}
typeCheckingContext.popEnclosingMethod();
}
@@ -2237,6 +2241,12 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
@Override
public void visitReturnStatement(final ReturnStatement statement) {
+ if (typeCheckingContext.getEnclosingClosure() == null) {
+ MethodNode method = typeCheckingContext.getEnclosingMethod();
+ if (method != null && !method.isVoidMethod() && !method.isDynamicReturnType()) {
+ applyTargetType(method.getReturnType(), statement.getExpression()); // GROOVY-10660
+ }
+ }
super.visitReturnStatement(statement);
returnListener.returnStatementAdded(statement);
}
diff --git a/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy b/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy
index 25742714bf..82fbc71ac0 100644
--- a/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ClosureParamTypeInferenceSTCTest.groovy
@@ -712,6 +712,40 @@ class ClosureParamTypeInferenceSTCTest extends StaticTypeCheckingTestCase {
'''
}
+ void testGroovy10660() {
+ assertScript '''
+ <T> BiConsumer<String, List<T>> m(BiConsumer<String, ? super T> proc) {
+ // batch processor:
+ return { text, list ->
+ for (item in list) proc.accept(text, item)
+ }
+ }
+
+ m { text, item -> }
+ '''
+
+ assertScript '''
+ <T> BiConsumer<String, List<T>> m(BiConsumer<String, ? super T> proc) {
+ // batch processor:
+ return (text, list) -> {
+ for (item in list) proc.accept(text, item)
+ }
+ }
+
+ m((text, item) -> { })
+ '''
+
+ assertScript '''
+ <T> BiConsumer<String, List<T>> m(BiConsumer<String, ? super T> proc) {
+ /*implicit return*/ (text, list) -> {
+ for (item in list) proc.accept(text, item)
+ }
+ }
+
+ m((text, item) -> { })
+ '''
+ }
+
void testGroovy10673() {
assertScript '''
void proc(Consumer<Number> action) {