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/03/23 15:28:34 UTC

[groovy] branch GROOVY-9996 created (now 3e6855d)

This is an automated email from the ASF dual-hosted git repository.

emilles pushed a change to branch GROOVY-9996
in repository https://gitbox.apache.org/repos/asf/groovy.git.


      at 3e6855d  GROOVY-9996: infer return type generics using declared type of variables

This branch includes the following new commits:

     new 3e6855d  GROOVY-9996: infer return type generics using declared type of variables

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[groovy] 01/01: GROOVY-9996: infer return type generics using declared type of variables

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY-9996
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 3e6855da4c9517c41b124789a5a35e5ba00944e6
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Mar 23 10:20:45 2021 -0500

    GROOVY-9996: infer return type generics using declared type of variables
---
 .../transform/stc/StaticTypeCheckingVisitor.java      | 10 +++++++++-
 src/test/groovy/transform/stc/GenericsSTCTest.groovy  | 19 ++++++++++++++++++-
 2 files changed, 27 insertions(+), 2 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 ed54498..d35fb1c 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -5176,7 +5176,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
                     continue; // GROOVY-9984: skip null
                 boolean lastArg = (i == paramLength - 1);
                 ClassNode paramType = parameters[i].getType();
-                ClassNode argumentType = getType(expressions.get(i));
+                ClassNode argumentType = getDeclaredOrInferredType(expressions.get(i));
                 while (paramType.isArray() && argumentType.isArray()) {
                     paramType = paramType.getComponentType();
                     argumentType = argumentType.getComponentType();
@@ -5441,6 +5441,14 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
         return resolveClassNodeGenerics(resolvedPlaceholders, placeholdersFromContext, currentType);
     }
 
+    private ClassNode getDeclaredOrInferredType(final Expression expression) {
+        // in case of "T t = new ExtendsOrImplementsT()", return T for the expression type
+        if (expression instanceof Variable && !((Variable) expression).isDynamicTyped()) {
+            return getOriginalDeclarationType(expression); // GROOVY-9996
+        }
+        return getInferredTypeFromTempInfo(expression, getType(expression));
+    }
+
     private static ClassNode getDeclaringClass(final MethodNode method, final Expression arguments) {
         ClassNode declaringClass = method.getDeclaringClass();
 
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index 087eaab..c68377b 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -307,7 +307,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
     // GROOVY-9956
     void testDiamondInferrenceFromConstructor8() {
         assertScript '''
-            @groovy.transform.TupleConstructor
+            @groovy.transform.TupleConstructor(defaults=false)
             class C<T> {
                 T p
             }
@@ -319,6 +319,23 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    // GROOVY-9996
+    void testDiamondInferrenceFromConstructor8a() {
+        assertScript '''
+            @groovy.transform.TupleConstructor(defaults=false)
+            class C<T> {
+                T p
+            }
+            interface I { }
+            class D implements I { }
+            void test(C<I> c) { assert c.p instanceof D }
+
+            I i = new D() // infers D for "i"
+            def ci = new C<>(i) // infers C<D> for "ci"
+            test(ci)
+        '''
+    }
+
     void testDiamondInferrenceFromConstructor9() {
         assertScript '''
             abstract class A<X> { }