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 2021/05/09 15:47:36 UTC
[groovy] branch GROOVY_2_5_X updated: GROOVY-8202: STC: void
doesn't factor into closure return type inference
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_2_5_X by this push:
new 2ab55b8 GROOVY-8202: STC: void doesn't factor into closure return type inference
2ab55b8 is described below
commit 2ab55b8ac25d74cb31b6a6fef6d0fcebbcd289e3
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sun May 9 09:55:14 2021 -0500
GROOVY-8202: STC: void doesn't factor into closure return type inference
Conflicts:
src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
---
.../transform/stc/StaticTypeCheckingVisitor.java | 1 +
.../groovy/transform/stc/ClosuresSTCTest.groovy | 75 ++++++++++++++++++++++
2 files changed, 76 insertions(+)
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 f6f5406..34f8b9e 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -2185,6 +2185,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
}
protected void addClosureReturnType(ClassNode returnType) {
+ if (VOID_TYPE.equals(returnType)) return; // GROOVY-8202
typeCheckingContext.getEnclosingClosure().addReturnType(returnType);
}
diff --git a/src/test/groovy/transform/stc/ClosuresSTCTest.groovy b/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
index 742aa1e..085b5a0 100644
--- a/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
@@ -109,6 +109,81 @@ class ClosuresSTCTest extends StaticTypeCheckingTestCase {
'''
}
+ // GROOVY-8427
+ void testClosureReturnTypeInference6() {
+ assertScript '''
+ import java.util.function.Consumer
+
+ class C {
+ static <T> void m(T a, Consumer<T> c) {
+ c.accept(a)
+ }
+ static void main(args) {
+ def c = { ->
+ int x = 0
+ m('') {
+ print 'void return'
+ }
+ }
+ c.call()
+ }
+ }
+ '''
+ }
+
+ // GROOVY-8202
+ void testClosureReturnTypeInference7() {
+ assertScript '''
+ void proc() {
+ }
+ String test0(flag) {
+ if (flag) {
+ 'foo'
+ } else {
+ proc()
+ }
+ }
+ String test1(flag) {
+ Closure<String> c = { ->
+ if (flag) {
+ 'bar'
+ } else {
+ proc()
+ null
+ }
+ }
+ c.call()
+ }
+ String test2(flag) {
+ Closure<String> c = { -> // Cannot assign Closure<Object> to Closure<String>
+ if (flag) {
+ 'baz'
+ } else {
+ proc()
+ }
+ }
+ c.call()
+ }
+
+ assert test0(true) == 'foo'
+ assert test1(true) == 'bar'
+ assert test2(true) == 'baz'
+ assert test0(false) == null
+ assert test1(false) == null
+ assert test2(false) == null
+ '''
+
+ assertScript '''
+ Closure<Void> c = { flag ->
+ if (flag) {
+ print 'true'
+ } else {
+ print 'false'
+ }
+ }
+ '''
+ }
+
// GROOVY-5145
void testCollect() {
assertScript '''