groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: GROOVY-8609: Fails to compile when upper bound has generics(closes #730)
Date Mon, 28 May 2018 10:55:38 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X b2b1bdf5d -> 43b7c04d1


GROOVY-8609: Fails to compile when upper bound has generics(closes #730)

(cherry picked from commit a577b37)


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

Branch: refs/heads/GROOVY_2_6_X
Commit: 43b7c04d133fbd2140fa74595eb1e938032bb5cb
Parents: b2b1bdf
Author: sunlan <sunlan@apache.org>
Authored: Mon May 28 18:54:05 2018 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Mon May 28 18:55:33 2018 +0800

----------------------------------------------------------------------
 .../groovy/ast/tools/GenericsUtils.java         |   5 +-
 src/test/groovy/bugs/Groovy8609Bug.groovy       | 145 +++++++++++++++++++
 2 files changed, 149 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/43b7c04d/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java b/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
index 3ddbabe..cf3de8c 100644
--- a/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
+++ b/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
@@ -163,7 +163,10 @@ public class GenericsUtils {
         GenericsType[] parameterized = node.getGenericsTypes();
         if (parameterized == null || parameterized.length == 0) return;
         GenericsType[] redirectGenericsTypes = node.redirect().getGenericsTypes();
-        if (redirectGenericsTypes == null) redirectGenericsTypes = parameterized;
+        if (redirectGenericsTypes == null ||
+                (node.isGenericsPlaceHolder() && redirectGenericsTypes.length !=
parameterized.length) /* GROOVY-8609 */ ) {
+            redirectGenericsTypes = parameterized;
+        }
         if (redirectGenericsTypes.length != parameterized.length) {
             throw new GroovyBugError("Expected earlier checking to detect generics parameter
arity mismatch" +
                     "\nExpected: " + node.getName() + toGenericTypesString(redirectGenericsTypes)
+

http://git-wip-us.apache.org/repos/asf/groovy/blob/43b7c04d/src/test/groovy/bugs/Groovy8609Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy8609Bug.groovy b/src/test/groovy/bugs/Groovy8609Bug.groovy
new file mode 100644
index 0000000..bfc021f
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy8609Bug.groovy
@@ -0,0 +1,145 @@
+/*
+ *  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 gls.CompilableTestSupport
+
+class Groovy8609Bug extends CompilableTestSupport {
+    void testUpperBoundWithGenerics() {
+        assertScript '''
+        @groovy.transform.CompileStatic
+        public class A<T extends List<E>, E extends Map<String, Integer>>
{
+            E getFirstRecord(T recordList) {
+                return recordList.get(0)
+            }
+            
+            static void main(args) {
+                def list = new ArrayList<HashMap<String, Integer>>()
+                def record = new HashMap<String, Integer>()
+                list.add(record)
+                def a = new A<ArrayList<HashMap<String, Integer>>, HashMap<String,
Integer>>()
+                assert record.is(a.getFirstRecord(list))
+            }
+        }
+        '''
+    }
+
+    void testUpperBoundWithoutGenerics() {
+        assertScript '''
+        @groovy.transform.CompileStatic
+        public class A<T extends List<E>, E extends Map> {
+            E getFirstRecord(T recordList) {
+                return recordList.get(0);
+            }
+            
+            static void main(args) {
+                def list = new ArrayList<HashMap<String, Integer>>()
+                def record = new HashMap<String, Integer>()
+                list.add(record)
+                def a = new A<ArrayList<HashMap<String, Integer>>, HashMap<String,
Integer>>()
+                assert record.is(a.getFirstRecord(list))
+            }
+        }
+        '''
+    }
+
+    void testNoUpperBound() {
+        assertScript '''
+        @groovy.transform.CompileStatic
+        public class A<T extends List<E>, E> {
+            E getFirstRecord(T recordList) {
+                return recordList.get(0);
+            }
+            
+            static void main(args) {
+                def list = new ArrayList<HashMap<String, Integer>>()
+                def record = new HashMap<String, Integer>()
+                list.add(record)
+                def a = new A<ArrayList<HashMap<String, Integer>>, HashMap<String,
Integer>>()
+                assert record.is(a.getFirstRecord(list))
+            }
+        }
+        '''
+    }
+
+    void testUpperBoundWithGenericsThroughWrongType() {
+        def errMsg = shouldFail '''
+        @groovy.transform.CompileStatic
+        public class A<T extends List<E>, E extends Map<String, Integer>>
{
+            E getFirstRecord(T recordList) {
+                return recordList.get(0)
+            }
+            
+            static void main(args) {
+                def list = new ArrayList<TreeMap<String, Integer>>()
+                def record = new TreeMap<String, Integer>()
+                list.add(record)
+                def a = new A<ArrayList<HashMap<String, Integer>>, HashMap<String,
Integer>>()
+                assert record.is(a.getFirstRecord(list))
+            }
+        }
+        '''
+
+        assert errMsg.contains('[Static type checking] - Cannot call A <ArrayList, HashMap>#getFirstRecord(T)
with arguments [java.util.ArrayList <TreeMap>]')
+    }
+
+    void testUpperBoundWithGenericsThroughWrongType2() {
+        def errMsg = shouldFail '''
+        @groovy.transform.CompileStatic
+        public class A<T extends List<E>, E extends Map<String, Integer>>
{
+            E getFirstRecord(T recordList) {
+                return recordList.get(0)
+            }
+            
+            static void main(args) {
+                def list = new ArrayList<HashMap<String, Long>>()
+                def record = new HashMap<String, Long>()
+                list.add(record)
+                def a = new A<ArrayList<HashMap<String, Integer>>, HashMap<String,
Integer>>()
+                assert record.is(a.getFirstRecord(list))
+            }
+        }
+        '''
+
+        // TODO we should print generics details, e.g. [Static type checking] - Cannot call
A <ArrayList, HashMap<String, Integer>>#getFirstRecord(T) with arguments [java.util.ArrayList
<HashMap<String, Long>>]
+        assert errMsg.contains('[Static type checking] - Cannot call A <ArrayList, HashMap>#getFirstRecord(T)
with arguments [java.util.ArrayList <HashMap>]')
+    }
+
+    void testUpperBoundWithGenericsThroughWrongType3() {
+        def errMsg = shouldFail '''
+        @groovy.transform.CompileStatic
+        public class A<T extends List<E>, E extends Map<String, Integer>>
{
+            E getFirstRecord(T recordList) {
+                return recordList.get(0)
+            }
+            
+            static void main(args) {
+                def list = new ArrayList<HashMap<StringBuffer, Integer>>()
+                def record = new HashMap<StringBuffer, Integer>()
+                list.add(record)
+                def a = new A<ArrayList<HashMap<String, Integer>>, HashMap<String,
Integer>>()
+                assert record.is(a.getFirstRecord(list))
+            }
+        }
+        '''
+
+        // TODO we should print generics details, e.g. [Static type checking] - Cannot call
A <ArrayList, HashMap<String, Integer>>#getFirstRecord(T) with arguments [java.util.ArrayList
<HashMap<StringBuffer, Integer>>]
+        assert errMsg.contains('[Static type checking] - Cannot call A <ArrayList, HashMap>#getFirstRecord(T)
with arguments [java.util.ArrayList <HashMap>]')
+    }
+}


Mime
View raw message