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 23:20:15 UTC

[groovy] branch GROOVY-9338 updated (a184021 -> b54d8d0)

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

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


 discard a184021  GROOVY-6095, GROOVY-9338: check a wildcard's bounds instead of base type
     new b54d8d0  GROOVY-6095, GROOVY-9338: check a wildcard's bounds instead of base type

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (a184021)
            \
             N -- N -- N   refs/heads/GROOVY-9338 (b54d8d0)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/test/groovy/transform/stc/BugsSTCTest.groovy | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)


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

Posted by em...@apache.org.
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 b54d8d009de789ddfad0a8ca9ce198a50059f36d
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 ++++++++++++++++++++++
 src/test/groovy/transform/stc/BugsSTCTest.groovy   | 23 ++++++---
 3 files changed, 100 insertions(+), 11 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 <\?>\]/
+    }
+}
diff --git a/src/test/groovy/transform/stc/BugsSTCTest.groovy b/src/test/groovy/transform/stc/BugsSTCTest.groovy
index 37e0a99..28b1d77 100644
--- a/src/test/groovy/transform/stc/BugsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/BugsSTCTest.groovy
@@ -151,13 +151,24 @@ class BugsSTCTest extends StaticTypeCheckingTestCase {
 
     void testGroovy7477NullGenericsType() {
         assertScript '''
-        class L<E> extends ArrayList<E> {
-            boolean removeIf(Comparator<? super E> filter) { }
-        }
-        L<String> items = ['foo', 'bar'] as L<String>
-        items.removeIf({a, b -> 1} as Comparator<?>)
-        assert items
+            class L<E> extends ArrayList<E> {
+                boolean removeIf(Comparator<? super E> filter) {
+                }
+            }
+            def items = ['foo', 'bar'] as L<String>
+            items.removeIf({a, b -> 1} as Comparator<String>)
+            assert items
         '''
+
+        shouldFailWithMessages '''
+            class L<E> extends ArrayList<E> {
+                boolean removeIf(Comparator<? super E> filter) {
+                }
+            }
+            L<String> items = ['foo', 'bar'] as L<String>
+            items.removeIf({a, b -> 1} as Comparator<?>)
+            assert items
+        ''', 'Cannot call L <String>#removeIf(java.util.Comparator <? super java.lang.String>) with arguments [java.util.Comparator <?>]'
     }
 
     void testGroovy5482ListsAndFlowTyping() {