You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2021/04/25 22:05:14 UTC

[groovy] branch master updated: GROOVY-10053: accept type parameter from context as method type argument

This is an automated email from the ASF dual-hosted git repository.

emilles 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 be6c9d7  GROOVY-10053: accept type parameter from context as method type argument
be6c9d7 is described below

commit be6c9d72778af6b001c4feb678836e70bed9ebea
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sun Apr 25 16:50:49 2021 -0500

    GROOVY-10053: accept type parameter from context as method type argument
---
 .../groovy/transform/stc/StaticTypeCheckingSupport.java        | 10 +++++++---
 .../groovy/transform/stc/StaticTypeCheckingVisitor.java        |  5 ++---
 src/test/groovy/transform/stc/GenericsSTCTest.groovy           |  2 +-
 3 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
index a2ccba2..20965df 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -1617,12 +1617,16 @@ public abstract class StaticTypeCheckingSupport {
                         }
                     } else if (!newValue.isPlaceholder() || newValue != resolvedPlaceholders.get(name)) {
                         // GROOVY-6787: Don't override the original if the replacement doesn't respect the bounds otherwise
-                        // the original bounds are lost, which can result in accepting an incompatible type as an argument.
+                        // the original bounds are lost, which can result in accepting an incompatible type as an argument!
                         ClassNode replacementType = extractType(newValue);
-                        if (oldValue.isCompatibleWith(replacementType)) {
+                        ClassNode suitabilityType = !replacementType.isGenericsPlaceHolder()
+                                ? replacementType : Optional.ofNullable(replacementType.getGenericsTypes())
+                                        .map(gts -> extractType(gts[0])).orElse(replacementType.redirect());
+
+                        if (oldValue.isCompatibleWith(suitabilityType)) {
                             if (newValue.isWildcard() && newValue.getLowerBound() == null && newValue.getUpperBounds() == null) {
                                 // GROOVY-9998: apply upper/lower bound for unknown
-                                entry.setValue(new GenericsType(replacementType));
+                                entry.setValue(replacementType.asGenericsType());
                             } else {
                                 entry.setValue(newValue);
                             }
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index b265462..e6b71ea 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -5313,10 +5313,9 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
                     if (isVargs && lastArg && paramType.isArray()) {
                         paramType = paramType.getComponentType();
                     }
-                    argumentType = wrapTypeIfNecessary(argumentType);
 
                     Map<GenericsTypeName, GenericsType> connections = new HashMap<>();
-                    extractGenericsConnections(connections, argumentType, paramType);
+                    extractGenericsConnections(connections, wrapTypeIfNecessary(argumentType), paramType);
                     extractGenericsConnectionsForSuperClassAndInterfaces(resolvedPlaceholders, connections);
 
                     applyGenericsConnections(connections, resolvedPlaceholders);
@@ -5396,7 +5395,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
     }
 
     private static void extractGenericsConnectionsForSuperClassAndInterfaces(final Map<GenericsTypeName, GenericsType> resolvedPlaceholders, final Map<GenericsTypeName, GenericsType> connections) {
-        for (GenericsType value : new HashSet<GenericsType>(connections.values())) {
+        for (GenericsType value : new HashSet<>(connections.values())) {
             if (!value.isPlaceholder() && !value.isWildcard()) {
                 ClassNode valueType = value.getType();
                 List<ClassNode> deepNodes = new LinkedList<>();
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index 788b2b1..386de4e 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -258,7 +258,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
                 def <N extends Number> Set<N> g(Class<N> t) {
                     Set<N> result = new HashSet<>()
                     f().stream().filter(t::isInstance)
-                        .<N>map($cast).forEach(n -> result.add(n))
+                        .map($cast).forEach(n -> result.add(n))
                     return result
                 }