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/15 17:07:35 UTC

[groovy] branch master updated: GROOVY-10082: STC: allow simple covariace of closure return

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 0484437  GROOVY-10082: STC: allow simple covariace of closure return
0484437 is described below

commit 0484437c0cc808f62d87f4b40e76ab48223eba55
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sat May 15 12:07:21 2021 -0500

    GROOVY-10082: STC: allow simple covariace of closure return
---
 .../transform/stc/StaticTypeCheckingVisitor.java   | 24 +++++++++++-----------
 .../groovy/transform/stc/ClosuresSTCTest.groovy    | 17 +++++++++++++--
 2 files changed, 27 insertions(+), 14 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 0f6ccaa..003b199 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -850,7 +850,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
                             && !getOutermost(enclosingType).equals(getOutermost(resultType))
                             && (Modifier.isPrivate(modifiers) || !Objects.equals(enclosingType.getPackageName(), resultType.getPackageName()))) {
                         resultType = originType; // TODO: Find accesible type in hierarchy of resultType?
-                    } else if (GenericsUtils.hasUnresolvedGenerics(resultType)) {// GROOVY-9033, GROOVY-10089, et al.
+                    } else if (GenericsUtils.hasUnresolvedGenerics(resultType)) { // GROOVY-9033, GROOVY-10089, et al.
                         Map<GenericsTypeName, GenericsType> enclosing = extractGenericsParameterMapOfThis(typeCheckingContext);
                         resultType = fullyResolveType(resultType, Optional.ofNullable(enclosing).orElseGet(Collections::emptyMap));
                     }
@@ -2174,11 +2174,17 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
             type = getType(expression);
         }
         if (typeCheckingContext.getEnclosingClosure() != null) {
+            ClassNode inferredReturnType = getInferredReturnType(typeCheckingContext.getEnclosingClosure().getClosureExpression());
             // GROOVY-9995: return ctor call with diamond operator
             if (expression instanceof ConstructorCallExpression) {
-                ClassNode inferredClosureReturnType = getInferredReturnType(typeCheckingContext.getEnclosingClosure().getClosureExpression());
-                if (inferredClosureReturnType == null) inferredClosureReturnType = DYNAMIC_TYPE; // GROOVY-10080
-                inferDiamondType((ConstructorCallExpression) expression, inferredClosureReturnType);
+                inferDiamondType((ConstructorCallExpression) expression, inferredReturnType != null ? inferredReturnType : /*GROOVY-10080:*/DYNAMIC_TYPE);
+            }
+            if (STRING_TYPE.equals(inferredReturnType) && isGStringOrGStringStringLUB(type)) {
+                type = STRING_TYPE; // GROOVY-9971: convert GString to String at point of return
+            } else if (inferredReturnType != null && !inferredReturnType.isGenericsPlaceHolder()
+                    && !type.isUsingGenerics() && !type.equals(inferredReturnType) && (inferredReturnType.isInterface()
+                            ? type.implementsInterface(inferredReturnType) : type.isDerivedFrom(inferredReturnType))) {
+                type = inferredReturnType; // GROOVY-10082: allow simple covariance
             }
             return type;
         }
@@ -2212,14 +2218,8 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
     }
 
     protected void addClosureReturnType(final ClassNode returnType) {
-        TypeCheckingContext.EnclosingClosure enclosingClosure = typeCheckingContext.getEnclosingClosure();
-        if (isGStringOrGStringStringLUB(returnType)
-                && STRING_TYPE.equals(getInferredReturnType(enclosingClosure.getClosureExpression()))) {
-            // GROOVY-9971: convert GString to String at the point of return
-            enclosingClosure.addReturnType(STRING_TYPE);
-        } else if (!VOID_TYPE.equals(returnType)) {
-            enclosingClosure.addReturnType(returnType);
-        }
+        if (returnType != null && !returnType.equals(VOID_TYPE)) // GROOVY-8202
+            typeCheckingContext.getEnclosingClosure().addReturnType(returnType);
     }
 
     @Override
diff --git a/src/test/groovy/transform/stc/ClosuresSTCTest.groovy b/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
index 2f76ca7..4024fc3 100644
--- a/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
@@ -160,9 +160,22 @@ class ClosuresSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
-    // GROOVY-8427
+    // GROOVY-10082
     void testClosureReturnTypeInference6() {
         assertScript '''
+            class A {}
+            class B extends A {}
+            Closure<A> c = { -> new B() } // Cannot assign Closure<B> to Closure<A>
+
+            def result = c()
+            assert result instanceof A
+            assert result instanceof B
+        '''
+    }
+
+    // GROOVY-8427
+    void testClosureReturnTypeInference7() {
+        assertScript '''
             import java.util.function.Consumer
 
             class C {
@@ -183,7 +196,7 @@ class ClosuresSTCTest extends StaticTypeCheckingTestCase {
     }
 
     // GROOVY-8202
-    void testClosureReturnTypeInference7() {
+    void testClosureReturnTypeInference8() {
         assertScript '''
             void proc() {
             }