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 2019/12/14 21:01:07 UTC

[groovy] 01/01: GROOVY-6095, GROOVY-9338: check a wildcard's bounds instead of base type

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

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

commit a1840218d98978e6b2ef774ad1955bd0f73d8e6c
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sat Dec 14 15:00:45 2019 -0600

    GROOVY-6095, GROOVY-9338: check a wildcard's bounds instead of base type
---
 .../java/org/codehaus/groovy/ast/GenericsType.java | 28 ++++++++--
 src/test/groovy/bugs/Groovy9338.groovy             | 60 ++++++++++++++++++++++
 2 files changed, 83 insertions(+), 5 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/ast/GenericsType.java b/src/main/java/org/codehaus/groovy/ast/GenericsType.java
index d63e59f..585cf57 100644
--- a/src/main/java/org/codehaus/groovy/ast/GenericsType.java
+++ b/src/main/java/org/codehaus/groovy/ast/GenericsType.java
@@ -415,8 +415,17 @@ public class GenericsType extends ASTNode {
                                         // check for recursive generic typedef, like in <T extends Comparable<? super T>>
                                         gt = classNodePlaceholders.getOrDefault(new GenericsTypeName(gt.getName()), gt);
                                     }
-                                    match = implementsInterfaceOrIsSubclassOf(gt.getType(), classNodeType.getType());
-
+                                    // GROOVY-6095, GROOVY-9338
+                                    if (classNodeType.isWildcard()) {
+                                        if (classNodeType.getLowerBound() != null
+                                                || classNodeType.getUpperBounds() != null) {
+                                            match = classNodeType.checkGenerics(gt.getType());
+                                        } else {
+                                            match = false; // "?" (from Comparable<?>) does not satisfy anything
+                                        }
+                                    } else {
+                                        match = implementsInterfaceOrIsSubclassOf(gt.getType(), classNodeType.getType());
+                                    }
                                 } else if (redirectBoundType.getUpperBounds() != null) {
                                     // ex: class Comparable<Integer> <=> bound Comparable<? extends T & I>
                                     for (ClassNode upperBound : redirectBoundType.getUpperBounds()) {
@@ -425,9 +434,18 @@ public class GenericsType extends ASTNode {
                                             // check for recursive generic typedef, like in <T extends Comparable<? super T>>
                                             gt = classNodePlaceholders.getOrDefault(new GenericsTypeName(gt.getName()), gt);
                                         }
-                                        match = implementsInterfaceOrIsSubclassOf(classNodeType.getType(), gt.getType())
-                                                || classNodeType.isCompatibleWith(gt.getType()); // workaround for GROOVY-6095
-
+                                        // GROOVY-6095, GROOVY-9338
+                                        if (classNodeType.isWildcard()) {
+                                            if (classNodeType.getLowerBound() != null) {
+                                                match = gt.checkGenerics(classNodeType.getLowerBound());
+                                            } else if (classNodeType.getUpperBounds() != null) {
+                                                match = gt.checkGenerics(classNodeType.getUpperBounds()[0]);
+                                            } else {
+                                                match = false; // "?" (from Comparable<?>) does not satisfy anything
+                                            }
+                                        } else {
+                                            match = implementsInterfaceOrIsSubclassOf(classNodeType.getType(), gt.getType());
+                                        }
                                         if (!match) break;
                                     }
                                 }
diff --git a/src/test/groovy/bugs/Groovy9338.groovy b/src/test/groovy/bugs/Groovy9338.groovy
new file mode 100644
index 0000000..34813f8
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy9338.groovy
@@ -0,0 +1,60 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package groovy.bugs
+
+import groovy.transform.CompileStatic
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.shouldFail
+
+@CompileStatic
+final class Groovy9338 {
+
+    @Test
+    void testGenericsUnsatisfied1() {
+        def err = shouldFail '''
+            void meth(Class<? extends CharSequence> c) {
+                print c.simpleName
+            }
+            @groovy.transform.CompileStatic
+            void test() {
+                def c = (Class<?>) String.class\n
+                meth(c)
+            }
+            test()
+        '''
+        assert err =~ /Cannot call \w+#meth\(java.lang.Class <\? extends java.lang.CharSequence>\) with arguments \[java.lang.Class <\?>\]/
+    }
+
+    @Test
+    void testGenericsUnsatisfied2() {
+        def err = shouldFail '''
+            void meth(Class<? super CharSequence> c) {
+                print c.simpleName
+            }
+            @groovy.transform.CompileStatic
+            void test() {
+                def c = (Class<?>) String.class\n
+                meth(c)
+            }
+            test()
+        '''
+        assert err =~ /Cannot call \w+#meth\(java.lang.Class <\? super java.lang.CharSequence>\) with arguments \[java.lang.Class <\?>\]/
+    }
+}