You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2018/05/28 10:54:20 UTC
groovy git commit: GROOVY-8609: Fails to compile when upper bound has
generics(closes #730)
Repository: groovy
Updated Branches:
refs/heads/master 72f60418a -> a577b37f8
GROOVY-8609: Fails to compile when upper bound has generics(closes #730)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/a577b37f
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/a577b37f
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/a577b37f
Branch: refs/heads/master
Commit: a577b37f8500734b84c2824237eca1e5888c1627
Parents: 72f6041
Author: sunlan <su...@apache.org>
Authored: Mon May 28 18:54:05 2018 +0800
Committer: sunlan <su...@apache.org>
Committed: Mon May 28 18:54:05 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/a577b37f/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 606cf63..3c94f82 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/a577b37f/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>]')
+ }
+}