groovy git commit: GROOVY-8439: @CompileStatic cannot compile generic class's static method

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

groovy git commit: GROOVY-8439: @CompileStatic cannot compile generic class's static method

Daniel.Sun
Repository: groovy
Updated Branches:
  refs/heads/master e154c3e56 -> 3eb491cde


GROOVY-8439: @CompileStatic cannot compile generic class's static method


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

Branch: refs/heads/master
Commit: 3eb491cded846c352952cdb3a6fffed817e14741
Parents: e154c3e
Author: danielsun1106 <[hidden email]>
Authored: Sat Mar 10 22:16:06 2018 +0800
Committer: danielsun1106 <[hidden email]>
Committed: Sat Mar 10 22:16:06 2018 +0800

----------------------------------------------------------------------
 .../transform/stc/StaticTypeCheckingSupport.java      | 14 ++++++++++----
 .../transform/stc/StaticTypeCheckingVisitor.java      |  6 +-----
 src/test/groovy/bugs/Groovy8439Bug.groovy             | 14 ++++++++++++++
 3 files changed, 25 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/3eb491cd/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
----------------------------------------------------------------------
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 cd5d977..c13a163 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -2021,10 +2021,16 @@ public abstract class StaticTypeCheckingSupport {
     }
 
     static Map<String, GenericsType> extractGenericsParameterMapOfThis(MethodNode mn) {
-        if (mn==null) return null;
-        Map<String, GenericsType> map = getGenericsParameterMapOfThis(mn.getDeclaringClass());
-        map = mergeGenerics(map, mn.getGenericsTypes());
-        return map;
+        if (mn == null) return null;
+
+        Map<String, GenericsType> map;
+        if (mn.isStatic()) {
+            map = new HashMap<>();
+        } else {
+            map = getGenericsParameterMapOfThis(mn.getDeclaringClass());
+        }
+
+        return mergeGenerics(map, mn.getGenericsTypes());
     }
 
     private static Map<String, GenericsType> mergeGenerics(Map<String, GenericsType> current, GenericsType[] newGenerics) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/3eb491cd/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
----------------------------------------------------------------------
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 7881176..800453f 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -2744,11 +2744,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
                             inferredType = originType;
                         }
                     } else if (!typeCheckMethodArgumentWithGenerics(originType, inferredType, lastArg)) {
-                        if (!isGenericsPlaceHolderOrArrayOf(originType)) { // the original type is not generics placeholder, use original type directly. See GROOVY-8439
-                            inferredType = originType;
-                        } else {
-                            addError("Expected parameter of type "+ inferredType.toString(false)+" but got "+originType.toString(false), closureParam.getType());
-                        }
+                        addError("Expected parameter of type "+ inferredType.toString(false)+" but got "+originType.toString(false), closureParam.getType());
                     }
 
                     typeCheckingContext.controlStructureVariables.put(closureParam, inferredType);

http://git-wip-us.apache.org/repos/asf/groovy/blob/3eb491cd/src/test/groovy/bugs/Groovy8439Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy8439Bug.groovy b/src/test/groovy/bugs/Groovy8439Bug.groovy
index ccca137..13af477 100644
--- a/src/test/groovy/bugs/Groovy8439Bug.groovy
+++ b/src/test/groovy/bugs/Groovy8439Bug.groovy
@@ -46,4 +46,18 @@ class Groovy8439Bug extends GroovyTestCase {
         Test.something(null, [])
         '''
     }
+
+    void testSTCGenerics2() {
+        assertScript '''
+        import groovy.transform.CompileStatic
+        @CompileStatic
+        class Test<T extends Map> {
+            static def something(List<String> strList) {
+                strList.collect { "hello,$it" }
+            }
+        }
+        
+        assert ["hello,Jochen", "hello,Paul", "hello,Daniel"] == Test.something(["Jochen", "Paul", "Daniel"])
+        '''
+    }
 }

Daniel Sun
Apache Groovy committer

Blog: http://blog.sunlan.me
Twitter: @daniel_sun