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/09/04 19:13:22 UTC

[groovy] branch master updated: 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 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 8217c29  GROOVY-9460: fix matching for T<?>, T<? super LB> and T<? extends UB>
8217c29 is described below

commit 8217c29c25a29a9047b7a97601e9d457c903d764
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Sep 4 14:13:13 2020 -0500

    GROOVY-9460: fix matching for T<?>, T<? super LB> and T<? extends UB>
    
    closes #1355
---
 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 {}