Repository: groovy
Updated Branches:
  refs/heads/master 668c62858 -> a0aee5b31


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/ce246e65
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/ce246e65
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/ce246e65

Branch: refs/heads/master
Commit: ce246e651d95473b9a31a0c68a5f249c794663d5
Parents: 668c628
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:12:11 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/ce246e65/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 d4915e8..23e67b3 100644
--- a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -9498,7 +9498,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();
@@ -9514,7 +9514,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/ce246e65/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()
+        '''
+    }
+}

Reply via email to