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 2020/08/27 20:56:45 UTC

[groovy] 01/01: GROOVY-9460: fix matching for T, T and T

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

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

commit cb68f81036581a6a3bb69dce18862c2432949a9e
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Thu Aug 27 15:51:24 2020 -0500

    GROOVY-9460: fix matching for T<?>, T<? super LB> and T<? extends UB>
---
 src/main/java/org/codehaus/groovy/ast/GenericsType.java | 14 ++++++--------
 src/test/groovy/transform/stc/GenericsSTCTest.groovy    | 16 ++++++++++++++++
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/ast/GenericsType.java b/src/main/java/org/codehaus/groovy/ast/GenericsType.java
index 98f7b83..84f28f7 100644
--- a/src/main/java/org/codehaus/groovy/ast/GenericsType.java
+++ b/src/main/java/org/codehaus/groovy/ast/GenericsType.java
@@ -375,18 +375,16 @@ public class GenericsType extends ASTNode {
                     match = name.equals(gtn);
                     if (!match) {
                         GenericsType genericsType = boundPlaceHolders.get(gtn);
-                        match = false;
                         if (genericsType != null) {
                             if (genericsType.isPlaceholder()) {
                                 match = true;
                             } else if (genericsType.isWildcard()) {
-                                if (genericsType.getUpperBounds() != null) {
-                                    for (ClassNode ub : genericsType.getUpperBounds()) {
-                                        match |= redirectBoundType.isCompatibleWith(ub);
-                                    }
-                                    if (genericsType.getLowerBound() != null) {
-                                        match |= redirectBoundType.isCompatibleWith(genericsType.getLowerBound());
-                                    }
+                                if (genericsType.getUpperBounds() != null) { // multiple bounds not allowed for ?
+                                    match = redirectBoundType.isCompatibleWith(genericsType.getUpperBounds()[0]);
+                                } else if (genericsType.getLowerBound() != null) {
+                                    match = redirectBoundType.isCompatibleWith(genericsType.getLowerBound());
+                                } else {
+                                    match = true;
                                 }
                             }
                         }
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index 1fb352c..0f3e2e9 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -392,6 +392,22 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    // GROOVY-9460
+    void testMethodCallWithClassParameterUnbounded() {
+        assertScript '''
+            class Bar {
+                static void baz(Class<?> target) {
+                }
+            }
+            class Foo<D> { // cannot be "T" because that matches type parameter in Class
+                void test(Class<D> param) {
+                    Bar.baz(param) // Cannot call Bar#baz(java.lang.Class<?>) with arguments [java.lang.Class<D>]
+                }
+            }
+            new Foo<String>().test(String.class)
+        '''
+    }
+
     void testConstructorCallWithClassParameterUsingClassLiteralArg() {
         assertScript '''
             class A {}