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/02/01 23:33:45 UTC

[groovy] branch master updated: GROOVY-10911: STC: multiple-interface type witness for `? extends Type`

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 a6a9872c7f GROOVY-10911: STC: multiple-interface type witness for `? extends Type`
a6a9872c7f is described below

commit a6a9872c7f55ded020ed689575f3674bddc708e4
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Feb 1 17:33:27 2023 -0600

    GROOVY-10911: STC: multiple-interface type witness for `? extends Type`
---
 .../transform/stc/StaticTypeCheckingSupport.java   | 33 ++++++++--------------
 .../groovy/transform/stc/GenericsSTCTest.groovy    | 29 +++++++++++++++++++
 2 files changed, 40 insertions(+), 22 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 e113687537..48b1c86879 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -1749,17 +1749,20 @@ public abstract class StaticTypeCheckingSupport {
             GenericsType ui = usage[i], di = declaration[i];
             if (di.isPlaceholder()) {     // di like "T"
                 storeGenericsConnection(connections, di.getName(), ui);
-            } else if (di.isWildcard()) { // di like "?", "? super T", "? extends T", ...
-                ClassNode lowerBound = di.getLowerBound(), upperBounds[] = di.getUpperBounds();
-                if (ui.isWildcard()) {    // ui like "?", "? super Type" or "? extends Type"
-                    extractGenericsConnections(connections, ui.getLowerBound(), lowerBound);
-                    extractGenericsConnections(connections, ui.getUpperBounds(), upperBounds);
-                } else if (!isUnboundedWildcard(di)) {
-                    ClassNode boundType = lowerBound != null ? lowerBound : upperBounds[0];
+            } else if (di.isWildcard()) { // di like "?", "? super T", "? extends T"
+                if (isUnboundedWildcard(di)) continue;
+                boolean lowerBound = (di.getLowerBound() != null);
+                ClassNode boundType = lowerBound ? di.getLowerBound() : di.getUpperBounds()[0];
+                if (ui.isWildcard()) {    // ui like "?", "? super Type", "? extends One & Two"
+                    if (lowerBound)
+                        extractGenericsConnections(connections, ui.getLowerBound(), boundType);
+                    else if (ui.getUpperBounds()!=null) for (ClassNode ub: ui.getUpperBounds())
+                        extractGenericsConnections(connections, ub, boundType); // GROOVY-10911
+                } else {
                     if (boundType.isGenericsPlaceHolder()) { // di like "? extends/super T"
                         // 6731,7992,8983,9998,10047,10499,10749,10765,...
                         ui = new GenericsType(ui.getType()); // erase type
-                        if (lowerBound != null) ui.setWildcard(true); // weak
+                        if (lowerBound) ui.setWildcard(true); // binds weak
                         String placeholderName = boundType.getUnresolvedName();
                         storeGenericsConnection(connections, placeholderName, ui);
                     } else { // di like "? extends Iterable<T>" and ui like "List<Type>"
@@ -1772,20 +1775,6 @@ public abstract class StaticTypeCheckingSupport {
         }
     }
 
-    private static void extractGenericsConnections(final Map<GenericsTypeName, GenericsType> connections, final ClassNode[] usage, final ClassNode[] declaration) {
-        if (usage == null || declaration == null || declaration.length == 0) return;
-        // both have generics
-        for (int i = 0, n = usage.length; i < n; i += 1) {
-            ClassNode ui = usage[i];
-            ClassNode di = declaration[i];
-            if (di.isGenericsPlaceHolder()) {
-                storeGenericsConnection(connections, di.getUnresolvedName(), new GenericsType(ui));
-            } else if (di.isUsingGenerics()) {
-                extractGenericsConnections(connections, ui.getGenericsTypes(), di.getGenericsTypes());
-            }
-        }
-    }
-
     private static void storeGenericsConnection(final Map<GenericsTypeName, GenericsType> connections, final String placeholderName, final GenericsType gt) {
       //connections.merge(new GenericsTypeName(placeholderName), gt, (gt1, gt2) -> getCombinedGenericsType(gt1, gt2));
         connections.put(new GenericsTypeName(placeholderName), gt);
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index 7f0fb0978b..ea9b8215c4 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -1877,6 +1877,35 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
         }
     }
 
+    // GROOVY-10911
+    void testMethodCallShouldWorkForProperUpperBound() {
+        assertScript '''
+            abstract class A {
+                String name
+            }
+            class B extends A {
+                int count
+            }
+            class C extends A {
+                double avg
+            }
+            void proc(List<? extends Object> list) {
+            }
+            void test(A a) {
+                def list
+                if (a instanceof B) {
+                    list = [a.name, a.count, ""]
+                } else {
+                    def c = a as C
+                    list = [c.name, "  ", c.avg]
+                }
+                proc(list) // STC OOB exception!
+            }
+            test(new B())
+            test(new C())
+        '''
+    }
+
     void testGroovy5154() {
         assertScript '''
             class Foo {