You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2016/08/01 23:14:11 UTC

[1/2] groovy git commit: GROOVY-7774: Collection addAll fails CompileStatic type checking when adding a collection of subtypes

Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X 6778128eb -> ca97826de


GROOVY-7774: Collection addAll fails CompileStatic type checking when adding a collection of subtypes


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/002f85e9
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/002f85e9
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/002f85e9

Branch: refs/heads/GROOVY_2_4_X
Commit: 002f85e920594aef9f60bc4de44a29d00329125b
Parents: 6778128
Author: paulk <pa...@asert.com.au>
Authored: Sat Jul 30 15:31:50 2016 +1000
Committer: paulk <pa...@asert.com.au>
Committed: Tue Aug 2 09:13:56 2016 +1000

----------------------------------------------------------------------
 .../groovy/runtime/DefaultGroovyMethods.java    |  4 +-
 .../groovy/transform/stc/Groovy7774Bug.groovy   | 42 ++++++++++++++++++++
 2 files changed, 44 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/002f85e9/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 53b406d..f5582d5 100644
--- a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -9368,7 +9368,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
      * @param items the items to add
      * @return true if the collection changed
      */
-    public static <T> boolean addAll(Collection<T> self, Iterator<T> items) {
+    public static <T> boolean addAll(Collection<T> self, Iterator<? extends T> items) {
         boolean changed = false;
         while (items.hasNext()) {
             T next =  items.next();
@@ -9384,7 +9384,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
      * @param items the items to add
      * @return true if the collection changed
      */
-    public static <T> boolean addAll(Collection<T> self, Iterable<T> items) {
+    public static <T> boolean addAll(Collection<T> self, Iterable<? extends T> items) {
         boolean changed = false;
         for (T next : items) {
             if (self.add(next)) changed = true;

http://git-wip-us.apache.org/repos/asf/groovy/blob/002f85e9/src/test/groovy/transform/stc/Groovy7774Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/transform/stc/Groovy7774Bug.groovy b/src/test/groovy/transform/stc/Groovy7774Bug.groovy
new file mode 100644
index 0000000..ca9b1ab
--- /dev/null
+++ b/src/test/groovy/transform/stc/Groovy7774Bug.groovy
@@ -0,0 +1,42 @@
+/*
+ *  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.transform.stc
+
+class Groovy7774Bug extends StaticTypeCheckingTestCase {
+    void testCollectionAddAllShouldHonorInheritance() {
+        assertScript '''
+            class X{}
+            class Y extends X{}
+
+            def create() {
+              Set<X> set = new HashSet<X>()
+              List<Y> addIterable = [new Y()]
+              Iterator<Y> addIterator = [new Y()].iterator()
+              Y[] addArray = [new Y()]
+              set.addAll(addIterable)
+              set.addAll(addIterator)
+              set.addAll(addArray)
+              assert set.size() == 3
+            }
+
+            create()
+        '''
+    }
+}


[2/2] groovy git commit: GROOVY-7774: Collection addAll fails CompileStatic type checking when adding a collection of subtypes (same treatment for some Map methods) (closes #376)

Posted by pa...@apache.org.
GROOVY-7774: Collection addAll fails CompileStatic type checking when adding a collection of subtypes (same treatment for some Map methods) (closes #376)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/ca97826d
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/ca97826d
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/ca97826d

Branch: refs/heads/GROOVY_2_4_X
Commit: ca97826de749480c2568fc542dca2806de196b26
Parents: 002f85e
Author: paulk <pa...@asert.com.au>
Authored: Sun Jul 31 17:29:59 2016 +1000
Committer: paulk <pa...@asert.com.au>
Committed: Tue Aug 2 09:13:57 2016 +1000

----------------------------------------------------------------------
 .../groovy/runtime/DefaultGroovyMethods.java    | 10 ++++---
 .../groovy/transform/stc/Groovy7774Bug.groovy   | 30 ++++++++++++++------
 2 files changed, 28 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/ca97826d/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index f5582d5..78db4c1 100644
--- a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -5093,7 +5093,9 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
             G key = outer.getKey();
             List<Map.Entry<K, V>> entries = outer.getValue();
             Map<K, V> target = createSimilarMap(self);
-            putAll(target, entries);
+            for (Map.Entry<K, V> entry : entries) {
+                target.put(entry.getKey(), entry.getValue());
+            }
             answer.put(key, target);
         }
         return answer;
@@ -8801,8 +8803,8 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
      * @return the same map, after the items have been added to it.
      * @since 1.6.1
      */
-    public static <K, V> Map<K, V> putAll(Map<K, V> self, Collection<Map.Entry<K, V>> entries) {
-        for (Map.Entry<K, V> entry : entries) {
+    public static <K, V> Map<K, V> putAll(Map<K, V> self, Collection<Map.Entry<? extends K, ? extends V>> entries) {
+        for (Map.Entry<? extends K, ? extends V> entry : entries) {
             self.put(entry.getKey(), entry.getValue());
         }
         return self;
@@ -8821,7 +8823,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
      * @return a new Map containing all key, value pairs from self and entries
      * @since 1.6.1
      */
-    public static <K, V> Map<K, V> plus(Map<K, V> self, Collection<Map.Entry<K, V>> entries) {
+    public static <K, V> Map<K, V> plus(Map<K, V> self, Collection<Map.Entry<? extends K, ? extends V>> entries) {
         Map<K, V> map = cloneSimilarMap(self);
         putAll(map, entries);
         return map;

http://git-wip-us.apache.org/repos/asf/groovy/blob/ca97826d/src/test/groovy/transform/stc/Groovy7774Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/transform/stc/Groovy7774Bug.groovy b/src/test/groovy/transform/stc/Groovy7774Bug.groovy
index ca9b1ab..96b3c7f 100644
--- a/src/test/groovy/transform/stc/Groovy7774Bug.groovy
+++ b/src/test/groovy/transform/stc/Groovy7774Bug.groovy
@@ -26,17 +26,31 @@ class Groovy7774Bug extends StaticTypeCheckingTestCase {
             class Y extends X{}
 
             def create() {
-              Set<X> set = new HashSet<X>()
-              List<Y> addIterable = [new Y()]
-              Iterator<Y> addIterator = [new Y()].iterator()
-              Y[] addArray = [new Y()]
-              set.addAll(addIterable)
-              set.addAll(addIterator)
-              set.addAll(addArray)
-              assert set.size() == 3
+                Set<X> set = new HashSet<X>()
+                List<Y> addIterable = [new Y()]
+                Iterator<Y> addIterator = [new Y()].iterator()
+                Y[] addArray = [new Y()]
+                set.addAll(addIterable)
+                set.addAll(addIterator)
+                set.addAll(addArray)
+                assert set.size() == 3
             }
 
             create()
         '''
     }
+
+    void testMapPutAllShouldHonorInheritance() {
+        assertScript '''
+            class X{}
+            class Y extends X{}
+
+            def create() {
+                Map<X, X> items = new HashMap<X, X>()
+                List<Map.Entry<Y, Y>> addEntries = new ArrayList<Map.Entry<Y, Y>>()
+                items.putAll(addEntries)
+            }
+            create()
+        '''
+    }
 }