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 2022/03/11 18:35:51 UTC

[groovy] branch GROOVY_2_5_X updated: GROOVY-10280: STC: do not mix type param contexts when resolving

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 9f0de36  GROOVY-10280: STC: do not mix type param contexts when resolving
9f0de36 is described below

commit 9f0de36864d05227ddfabc5caa00a81794bb188e
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Mar 11 11:07:57 2022 -0600

    GROOVY-10280: STC: do not mix type param contexts when resolving
---
 .../transform/stc/StaticTypeCheckingSupport.java   | 15 +++-----
 .../groovy/transform/stc/GenericsSTCTest.groovy    | 43 ++++++++++++++++++++++
 2 files changed, 48 insertions(+), 10 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
index 1feb1b7..95af881 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -1642,16 +1642,11 @@ public abstract class StaticTypeCheckingSupport {
         }
     }
 
-    public static ClassNode resolveClassNodeGenerics(Map<GenericsTypeName, GenericsType> resolvedPlaceholders, final Map<GenericsTypeName, GenericsType> placeholdersFromContext, ClassNode currentType) {
-        ClassNode target = currentType.redirect();
-        resolvedPlaceholders = new HashMap<GenericsTypeName, GenericsType>(resolvedPlaceholders);
-        applyContextGenerics(resolvedPlaceholders, placeholdersFromContext);
-
-        Map<GenericsTypeName, GenericsType> connections = new HashMap<GenericsTypeName, GenericsType>();
-        extractGenericsConnections(connections, currentType, target);
-        applyGenericsConnections(connections, resolvedPlaceholders);
-        currentType = applyGenericsContext(resolvedPlaceholders, currentType);
-        return currentType;
+    public static ClassNode resolveClassNodeGenerics(final Map<GenericsTypeName, GenericsType> resolvedPlaceholders, final Map<GenericsTypeName, GenericsType> placeholdersFromContext, final ClassNode currentType) {
+        ClassNode type = currentType; // GROOVY-10280, et al.
+        type = applyGenericsContext(resolvedPlaceholders, type);
+        type = applyGenericsContext(placeholdersFromContext, type);
+        return type;
     }
 
     static void applyGenericsConnections(
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index 09b3fd6..213c65f 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -256,6 +256,49 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    // GROOVY-10280
+    void testTypeArgumentPropagation() {
+        assertScript '''
+            class Test<T> {
+                T test() {
+                    @ASTTest(phase=INSTRUCTION_SELECTION, value={
+                        def type = node.getNodeMetaData(INFERRED_TYPE)
+                        assert type.toString(false) == 'T'
+                    })
+                    def t = new Foo<T>().x.y.z
+                }
+            }
+            class Foo<X> {
+                Bar<X> x = new Bar<>()
+            }
+            class Bar<T> {
+                Baz<T> y = new Baz<>()
+            }
+            class Baz<Z> {
+                Z z
+            }
+
+            new Test<String>().test()
+        '''
+    }
+
+    void testTypeArgumentPropagation2() {
+        assertScript '''
+            class Foo<T extends Bar> {
+                public List<T> bars
+                void test() {
+                    bars[0].baz()
+                }
+            }
+            class Bar {
+                def baz() {}
+            }
+
+            def obj = new Foo(bars: [new Bar()])
+            obj.test()
+        '''
+    }
+
     void testLinkedListWithListArgument() {
         assertScript '''
             List<String> list = new LinkedList<String>(['1','2','3'])