groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: Refine parameter type inference
Date Fri, 18 May 2018 08:46:34 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X 580303793 -> 59f612d42


Refine parameter type inference

(cherry picked from commit 3ff3111)


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

Branch: refs/heads/GROOVY_2_5_X
Commit: 59f612d4200218f66669cf13ae794506db4a0b7b
Parents: 5803037
Author: sunlan <sunlan@apache.org>
Authored: Fri May 18 16:45:06 2018 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Fri May 18 16:46:29 2018 +0800

----------------------------------------------------------------------
 .../codehaus/groovy/control/ResolveVisitor.java |  2 +-
 .../stc/StaticTypeCheckingVisitor.java          | 23 ++++++++-
 src/test/groovy/bugs/Groovy6171Bug.groovy       | 52 ++++++++++++++++++--
 .../groovy/transform/stc/GenericsSTCTest.groovy |  4 +-
 4 files changed, 72 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/59f612d4/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
index f12f65a..1b3ec1a 100644
--- a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
+++ b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
@@ -374,7 +374,7 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
 
         if (genericParameterNames.get(typeName) != null) {
             GenericsType gt = genericParameterNames.get(typeName);
-            type.setRedirect(gt.getType()); // FIXME lost the generics type info
+            type.setRedirect(gt.getType());
             type.setGenericsTypes(new GenericsType[]{ gt });
             type.setGenericsPlaceHolder(true);
             return true;

http://git-wip-us.apache.org/repos/asf/groovy/blob/59f612d4/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 4926999..d13cca5 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -5358,7 +5358,9 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
             this.parameter = parameter;
             ClassNode inferred = parameter.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
             if (inferred == null) {
-                parameter.setNodeMetaData(StaticTypesMarker.INFERRED_TYPE, parameter.getOriginType());
+                inferred = getInferredTypeOfPlaceholder(parameter);
+
+                parameter.setNodeMetaData(StaticTypesMarker.INFERRED_TYPE, inferred);
             }
         }
 
@@ -5402,4 +5404,23 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
             return parameter.equals(other);
         }
     }
+
+    private static ClassNode getInferredTypeOfPlaceholder(Variable variable) {
+        ClassNode originType = variable.getOriginType();
+
+        if (originType.isGenericsPlaceHolder()) {
+            GenericsType[] genericsTypes = originType.getGenericsTypes();
+
+            if (null != genericsTypes && genericsTypes.length > 0) {
+                GenericsType gt = genericsTypes[0];
+                ClassNode[] upperBounds = gt.getUpperBounds();
+
+                if (null != upperBounds && upperBounds.length > 0) {
+                    return upperBounds[0];
+                }
+            }
+        }
+
+        return variable.getOriginType();
+    }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/59f612d4/src/test/groovy/bugs/Groovy6171Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy6171Bug.groovy b/src/test/groovy/bugs/Groovy6171Bug.groovy
index 574db5b..0d0d6d1 100644
--- a/src/test/groovy/bugs/Groovy6171Bug.groovy
+++ b/src/test/groovy/bugs/Groovy6171Bug.groovy
@@ -19,7 +19,6 @@
 package groovy.bugs
 
 import gls.CompilableTestSupport
-import groovy.transform.NotYetImplemented
 
 class Groovy6171Bug extends CompilableTestSupport {
     void testGroovy6171() {
@@ -54,8 +53,7 @@ class Groovy6171Bug extends CompilableTestSupport {
         assert errMsg.contains('The type HashSet is not a valid substitute for the bounded
parameter <T extends java.util.List<X>>')
     }
 
-    @NotYetImplemented
-    void test2() {
+    void testParameter() {
         assertScript '''
         @groovy.transform.CompileStatic
         public class Foo<T extends List<X>, X extends Number> {
@@ -74,8 +72,52 @@ class Groovy6171Bug extends CompilableTestSupport {
         '''
     }
 
+    void testVariable() {
+        assertScript '''
+        @groovy.transform.CompileStatic
+        public class Foo<T extends List<X>, X extends Number> {
+            X getFirstElement() {
+                def list = new ArrayList<Integer>()
+                list.add(123)
+                T t = list
+                X x = t.get(0)
+                return x
+            }
+            
+            static void main(String[] args) {
+                def f = new Foo<ArrayList<Integer>, Integer>()
+                assert 123 == f.getFirstElement()
+            }
+        }
+        '''
+    }
+
+    void testField() {
+        assertScript '''
+        @groovy.transform.CompileStatic
+        public class Foo<T extends List<X>, X extends Number> {
+            T t
+            
+            {
+                def list = new ArrayList<Integer>()
+                list.add(123)
+                t = list
+            }
+        
+            X getFirstElement() {
+                X x = t.get(0)
+                return x
+            }
+            
+            static void main(String[] args) {
+                def f = new Foo<ArrayList<Integer>, Integer>()
+                assert 123 == f.getFirstElement()
+            }
+        }
+        '''
+    }
 
-    void test3() {
+    void testParameter2() {
         assertScript '''
         @groovy.transform.CompileStatic
         public class Foo<T extends List<X>, X extends Number> {
@@ -101,7 +143,7 @@ class Groovy6171Bug extends CompilableTestSupport {
         '''
     }
 
-    void test4() {
+    void testParameterAndVariable() {
         assertScript '''
         @groovy.transform.CompileStatic
         public class Foo<T extends List<X>, X extends Number> {

http://git-wip-us.apache.org/repos/asf/groovy/blob/59f612d4/src/test/groovy/transform/stc/GenericsSTCTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index d402b60..7bfa8b5 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -1082,7 +1082,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
                 }
             }
             Baz.qux([new Object()])
-        ''', 'Cannot call <T extends java.util.List<? extends java.lang.CharSequence>>
Foo#bar(T) with arguments [T]'
+        ''', 'Cannot call <T extends java.util.List<? extends java.lang.CharSequence>>
Foo#bar(T) with arguments [java.util.List <Object>]'
     }
 
     void testOutOfBoundsBySuperPlaceholderParameterType() {
@@ -1096,7 +1096,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
                 }
             }
             Baz.qux(['abc'])
-        ''', 'Cannot call <T extends java.util.List<? super java.lang.CharSequence>>
Foo#bar(T) with arguments [T]'
+        ''', 'Cannot call <T extends java.util.List<? super java.lang.CharSequence>>
Foo#bar(T) with arguments [java.util.List <String>] '
     }
 
     // GROOVY-5721


Mime
View raw message