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 2019/12/19 03:04:05 UTC
[groovy] branch master updated: GROOVY-6095,
GROOVY-9338: check a wildcard's bounds instead of base type
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new a553168 GROOVY-6095, GROOVY-9338: check a wildcard's bounds instead of base type
a553168 is described below
commit a553168a1937254e5904e2eb50d39df808bbc85a
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() {